abstract class Shape {
public String color;
//constractor default, constraktor yang tidak memiliki argumen dan isi
public Shape() {
}
//sama halnya dengan kelas biasa abstract class juga bisa berisi method
//seperti yang ada pada kelas biasa
public void setColor(String c)
{ color = c; }
public String getColor(){
return color;
}
//sebuah abstract method yang tidak memiliki body
//method ini akan diinisialisasi nantinya pada subclass yang mewarisi kelass abstract Shape ini
//semua abstract method yang ada pada class abstract harus diimplementasikan
//semua oleh subclass
abstract public double area();
}
//keyword extends adalah keyword yang digunakan oleh subclass
//ketika melakukan pewarisan terhada super class
public class Point extends Shape {
static int x, y;
public Point(){
x = 0;
y = 0;
}
public double area() {
return 0;
}
public double perimeter() {
return 0;
}
public static void print() {
System.out.println("point: " + x + "," + y);
}
public static void main(String args[]) {
Point p = new Point();
p.print(); }
}
Output
point: 0, 0
Showing posts with label java. Show all posts
Showing posts with label java. Show all posts
Saturday, July 6, 2013
example abstrack class of java netbeans
abstract class Shape {
public String color;
//constractor default, constraktor yang tidak memiliki argumen dan isi
public Shape() {
}
//sama halnya dengan kelas biasa abstract class juga bisa berisi method
//seperti yang ada pada kelas biasa
public void setColor(String c)
{ color = c; }
public String getColor(){
return color;
}
//sebuah abstract method yang tidak memiliki body
//method ini akan diinisialisasi nantinya pada subclass yang mewarisi kelass abstract Shape ini
//semua abstract method yang ada pada class abstract harus diimplementasikan
//semua oleh subclass
abstract public double area();
}
//keyword extends adalah keyword yang digunakan oleh subclass
//ketika melakukan pewarisan terhada super class
public class Point extends Shape {
static int x, y;
public Point(){
x = 0;
y = 0;
}
public double area() {
return 0;
}
public double perimeter() {
return 0;
}
public static void print() {
System.out.println("point: " + x + "," + y);
}
public static void main(String args[]) {
Point p = new Point();
p.print(); }
}
Output
point: 0, 0
public String color;
//constractor default, constraktor yang tidak memiliki argumen dan isi
public Shape() {
}
//sama halnya dengan kelas biasa abstract class juga bisa berisi method
//seperti yang ada pada kelas biasa
public void setColor(String c)
{ color = c; }
public String getColor(){
return color;
}
//sebuah abstract method yang tidak memiliki body
//method ini akan diinisialisasi nantinya pada subclass yang mewarisi kelass abstract Shape ini
//semua abstract method yang ada pada class abstract harus diimplementasikan
//semua oleh subclass
abstract public double area();
}
//keyword extends adalah keyword yang digunakan oleh subclass
//ketika melakukan pewarisan terhada super class
public class Point extends Shape {
static int x, y;
public Point(){
x = 0;
y = 0;
}
public double area() {
return 0;
}
public double perimeter() {
return 0;
}
public static void print() {
System.out.println("point: " + x + "," + y);
}
public static void main(String args[]) {
Point p = new Point();
p.print(); }
}
Output
point: 0, 0
Friday, July 5, 2013
exemple data abstrack coding java
abstrak public class Mobil {
public void injakPedalGas();
public void injakRem(){
System.out.println(“Mobil berhenti!”);
}
}
public class Kijang extends Mobil{
public void injakPedalGas(){
System.out.println("Mobil Melaju dengan kecepatan 80 Km/jam...");
}
}
public class BMW extends Mobil {
public void injakPedalGas(){
System.out.println("Mobil Melaju dengan kecepatan 100 Km/jam...");
}
}
>> class for running
public class TestMobil {
public static void main(String[] args){
Mobil mobil = new Mobil(){
public void injakPedalGas(){
System.out.println("Mobil berjalan...");
}
};
Kijang kijang = new Kijang();
Escudo escudo = new Escudo();
BMW bmw = new BMW();
mobil.injakPedalGas();
mobil = kijang;
mobil.injakPedalGas();
mobil = escudo;
mobil.injakPedalGas();
mobil = bmw;
mobil.injakPedalGas();
}
}
public void injakPedalGas();
public void injakRem(){
System.out.println(“Mobil berhenti!”);
}
}
public class Kijang extends Mobil{
public void injakPedalGas(){
System.out.println("Mobil Melaju dengan kecepatan 80 Km/jam...");
}
}
public class BMW extends Mobil {
public void injakPedalGas(){
System.out.println("Mobil Melaju dengan kecepatan 100 Km/jam...");
}
}
>> class for running
public class TestMobil {
public static void main(String[] args){
Mobil mobil = new Mobil(){
public void injakPedalGas(){
System.out.println("Mobil berjalan...");
}
};
Kijang kijang = new Kijang();
Escudo escudo = new Escudo();
BMW bmw = new BMW();
mobil.injakPedalGas();
mobil = kijang;
mobil.injakPedalGas();
mobil = escudo;
mobil.injakPedalGas();
mobil = bmw;
mobil.injakPedalGas();
}
}
Saturday, June 1, 2013
Deklarasi sebuah Method
Deklarasi sebuah Method
public, private, protected, package
static
abstract
final
native
synchronized
<return type> <nama_method>(parameter)
throws exception
Contoh :
public double luas(int panjang, int lebar);
public, private, protected, package
static
abstract
final
native
synchronized
<return type> <nama_method>(parameter)
throws exception
Contoh :
public double luas(int panjang, int lebar);
Thursday, May 23, 2013
Exemple AWT java Netbeans
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Insets;
/**
* Example from Chapter 3
*
* Simple object to prompt for user id/password.
*
* @author Jeff Heaton
* @version 1.0
*/
public class SecurePrompt extends javax.swing.JDialog {
public SecurePrompt(Frame parent) {
super(parent, true);
//{{INIT_CONTROLS
setTitle("Security");
getContentPane().setLayout(null);
setSize(403, 129);
setVisible(false);
JLabel1.setText("User ID:");
getContentPane().add(JLabel1);
JLabel1.setBounds(12, 12, 48, 24);
JLabel2.setText("Password:");
getContentPane().add(JLabel2);
JLabel2.setBounds(12, 48, 72, 24);
_uid.setText("jheaton");
getContentPane().add(_uid);
_uid.setBounds(72, 12, 324, 24);
_ok.setText("OK");
getContentPane().add(_ok);
_ok.setBounds(60, 84, 84, 24);
getContentPane().add(_pwd);
_pwd.setBounds(72, 48, 324, 24);
_cancel.setText("Cancel");
getContentPane().add(_cancel);
_cancel.setBounds(264, 84, 84, 24);
//}}
//{{REGISTER_LISTENERS
SymAction lSymAction = new SymAction();
_ok.addActionListener(lSymAction);
_cancel.addActionListener(lSymAction);
//}}
}
public void setVisible(boolean b) {
if (b)
setLocation(50, 50);
super.setVisible(b);
}
public void addNotify() {
// Record the size of the window prior to calling parents addNotify.
Dimension size = getSize();
super.addNotify();
if (frameSizeAdjusted)
return;
frameSizeAdjusted = true;
// Adjust size of frame according to the insets
Insets insets = getInsets();
setSize(insets.left + insets.right + size.width, insets.top
+ insets.bottom + size.height);
}
// Used by addNotify
boolean frameSizeAdjusted = false;
//{{DECLARE_CONTROLS
javax.swing.JLabel JLabel1 = new javax.swing.JLabel();
javax.swing.JLabel JLabel2 = new javax.swing.JLabel();
/**
* The user ID entered.
*/
javax.swing.JTextField _uid = new javax.swing.JTextField();
/**
*/
javax.swing.JButton _ok = new javax.swing.JButton();
/**
* The password is entered.
*/
javax.swing.JPasswordField _pwd = new javax.swing.JPasswordField();
javax.swing.JButton _cancel = new javax.swing.JButton();
//}}
class SymAction implements java.awt.event.ActionListener {
public void actionPerformed(java.awt.event.ActionEvent event) {
Object object = event.getSource();
if (object == _ok)
Ok_actionPerformed(event);
else if (object == _cancel)
Cancel_actionPerformed(event);
}
}
/**
* Called when ok is clicked.
*
* @param event
*/
void Ok_actionPerformed(java.awt.event.ActionEvent event) {
setVisible(false);
}
/**
* Called when cancel is clicked.
*
* @param event
*/
void Cancel_actionPerformed(java.awt.event.ActionEvent event) {
_uid.setText("");
_pwd.setText("");
setVisible(false);
}
}
Wednesday, May 15, 2013
user id and password
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Insets;
/**
* Simple object to prompt for user id/password.
*
*/
public class SecurePrompt extends javax.swing.JDialog {
public SecurePrompt(Frame parent) {
super(parent, true);
//{{INIT_CONTROLS
setTitle("Security");
getContentPane().setLayout(null);
setSize(403, 129);
setVisible(false);
JLabel1.setText("User ID:");
getContentPane().add(JLabel1);
JLabel1.setBounds(12, 12, 48, 24);
JLabel2.setText("Password:");
getContentPane().add(JLabel2);
JLabel2.setBounds(12, 48, 72, 24);
_uid.setText("jheaton");
getContentPane().add(_uid);
_uid.setBounds(72, 12, 324, 24);
_ok.setText("OK");
getContentPane().add(_ok);
_ok.setBounds(60, 84, 84, 24);
getContentPane().add(_pwd);
_pwd.setBounds(72, 48, 324, 24);
_cancel.setText("Cancel");
getContentPane().add(_cancel);
_cancel.setBounds(264, 84, 84, 24);
//}}
//{{REGISTER_LISTENERS
SymAction lSymAction = new SymAction();
_ok.addActionListener(lSymAction);
_cancel.addActionListener(lSymAction);
//}}
}
public void setVisible(boolean b) {
if (b)
setLocation(50, 50);
super.setVisible(b);
}
public void addNotify() {
// Record the size of the window prior to calling parents addNotify.
Dimension size = getSize();
super.addNotify();
if (frameSizeAdjusted)
return;
frameSizeAdjusted = true;
// Adjust size of frame according to the insets
Insets insets = getInsets();
setSize(insets.left + insets.right + size.width, insets.top
+ insets.bottom + size.height);
}
// Used by addNotify
boolean frameSizeAdjusted = false;
//{{DECLARE_CONTROLS
javax.swing.JLabel JLabel1 = new javax.swing.JLabel();
javax.swing.JLabel JLabel2 = new javax.swing.JLabel();
/**
* The user ID entered.
*/
javax.swing.JTextField _uid = new javax.swing.JTextField();
/**
*/
javax.swing.JButton _ok = new javax.swing.JButton();
/**
* The password is entered.
*/
javax.swing.JPasswordField _pwd = new javax.swing.JPasswordField();
javax.swing.JButton _cancel = new javax.swing.JButton();
//}}
class SymAction implements java.awt.event.ActionListener {
public void actionPerformed(java.awt.event.ActionEvent event) {
Object object = event.getSource();
if (object == _ok)
Ok_actionPerformed(event);
else if (object == _cancel)
Cancel_actionPerformed(event);
}
}
/**
* Called when ok is clicked.
*
* @param event
*/
void Ok_actionPerformed(java.awt.event.ActionEvent event) {
setVisible(false);
}
/**
* Called when cancel is clicked.
*
* @param event
*/
void Cancel_actionPerformed(java.awt.event.ActionEvent event) {
_uid.setText("");
_pwd.setText("");
setVisible(false);
}
}
Thursday, May 9, 2013
error massage
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class ErrorDialog {
public static void main(String argv[]) {
String message = "\"The Comedy of Errors\"\n"
+ "is considered by many scholars to be\n"
+ "the first play Shakespeare wrote";
JOptionPane.showMessageDialog(new JFrame(), message, "Dialog",
JOptionPane.ERROR_MESSAGE);
}
}
creating and using dialog
// : c14:Dialogs.java
// Creating and using Dialog Boxes.
// <applet code=Dialogs width=125 height=75></applet>
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
class MyDialog extends JDialog {
public MyDialog(JFrame parent) {
super(parent, "My dialog", true);
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(new JLabel("Here is my dialog"));
JButton ok = new JButton("OK");
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose(); // Closes the dialog
}
});
cp.add(ok);
setSize(150, 125);
}
}
public class Dialogs extends JApplet {
private JButton b1 = new JButton("Dialog Box");
private MyDialog dlg = new MyDialog(null);
public void init() {
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dlg.show();
}
});
getContentPane().add(b1);
}
public static void main(String[] args) {
run(new Dialogs(), 125, 75);
}
public static void run(JApplet applet, int width, int height) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(applet);
frame.setSize(width, height);
applet.init();
applet.start();
frame.setVisible(true);
}
} ///:~
confirmation dialog
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class OptionDialog {
public static void main(String argv[]) {
if (JOptionPane.showConfirmDialog(new JFrame(),
"Do you want to quit this application ?", "Title",
JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
System.exit(0);
}
}
Thursday, May 2, 2013
Contoh Polimorfisme
Abang-abang, mbak bro, mas bro,.....
disini ane akan mencontohkan pemrograman yang berkaitan dengan "POLIMORFISME" ,.......
tetetetetetettetetetetetet
hahahahahaha
tetetetetetettetetetetetet
hahahahahaha
langsung saja agan tinggal download source code nya di bawah sini,.......
Sunday, April 21, 2013
Iterasi Bilangan Pangkat
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bilanganpangkat;
import java.util.Scanner;
/**
*
* @author surur syifaus
*/
public class pangkat_iterasi {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner x=new Scanner(System.in);
int a,b;
System.out.println("angka dasar");
a=x.nextInt();
System.out.println("angka pangkat");
b=x.nextInt();
double c=( (Math.pow(a, b)));
if(b==0){
System.out.println("hasil = 1");
}
else{
System.out.println(""+c);
}
}
}
Iterasi Bilangan Fibonanci
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package new1;
import javax.swing.JOptionPane;
/**
*
* @author surur syifaus
*/
public class fibonanci_iterasi {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int deret=Integer.parseInt(JOptionPane.showInputDialog("Masukkan berapa deret Fibonacci: "));
int a=0;
int b=1;
System.out.print(deret+" deret Fibonacci: " );
for (int i=0;i<deret;i++){
System.out.print(a+" ");
a=a+b;
b=a-b;
}
}
}
Saturday, April 20, 2013
JOptionsPane java Netbeans
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package daftar;
import javax.swing.*;
/**
*
* @author surur
*/
public class prak1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String input;
int nim, ttl;
String nama, fakjur, alamat, cita2;
input=JOptionPane.showInputDialog("NAMA : ");
nama=(input);
input=JOptionPane.showInputDialog("NIM : ");
nim=Integer.parseInt(input);
input=JOptionPane.showInputDialog("Fakultas/Jurusan : ");
fakjur=(input);
input=JOptionPane.showInputDialog("TTL : ");
ttl=Integer.parseInt(input);
input=JOptionPane.showInputDialog("Alamat : ");
alamat=(input);
input=JOptionPane.showInputDialog("Cita-cita : ");
cita2=(input);
JOptionPane.showMessageDialog(null,("Nama : "+nama)+("\nNIM : "+nim)+("\nFakultas/Jurusan : "+fakjur)
+("\nTTL : "+ttl)+("\nAlamat : "+alamat)+("\nCita-cita : "+cita2));
System.exit(0);
}
}
Persegi kotak Angka
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package coba;
import java.util.Scanner;
/**
*
* @author surur
*/
public class yess {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner x=new Scanner(System.in);
int y=x.nextInt();
for (int i = 0; i < y; i++) {
System.out.println();
for (int j = 0; j < y; j++) {
System.out.print(i);
}
}
}
}
Segitiga Angka Biasa (gag kuwalek)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package coba;
import java.util.Scanner;
/**
*
* @author surur
*/
public class yess {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner x=new Scanner(System.in);
int y=x.nextInt();
for (int i = 0; i < y; i++) {
System.out.println();
for (int j = 0; j < i; j++) {
System.out.print(i);
}
}
}
}
Segitiga Angka Terbalik
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package coba;
import java.util.Scanner;
/**
*
* @author surur
*/
public class yess {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner x=new Scanner(System.in);
int y=x.nextInt();
for (int i = 0; i < y; i++) {
System.out.println();
for (int j = i; j < y; j++) {
System.out.print(i);
}
}
}
}
contoh program sederhana (Array)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package praktikum1;
import javax.swing.JOptionPane;
/**
*
* @author surur
*/
public class tugas {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("=================================================");
{System.out.println("NIM"+" "+"Nama"+" "+"MK"+" "+"N.UTS"+" "+"N.UAS"+" "+"N.TUGAS");}
System.out.println("=================================================");
String[][]A=new String[1][3];
String[][]B=new String[3][1];
String[][]C=new String[1][1];
String[][]D=new String[1][1];
String[][]E=new String[1][1];
String[][]F=new String[1][1];
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < B.length; j++) {
A[i][j]=JOptionPane.showInputDialog(null,"NIM : ");
for (int q = 0; q < C.length; q++) {
for (int w = 0; w < D.length; w++) {
C[q][w]=JOptionPane.showInputDialog(null,"Nama :");
for (int a = 0; a < C.length; a++) {
for (int s = 0; s < F.length; s++) {
E[a][s]=JOptionPane.showInputDialog(null,"MK : ");
for (int k = 0; k < E.length; k++) {
for (int l = 0; l < F.length; l++) {
E[k][l]=JOptionPane.showInputDialog(null,"N.UTS : ");
for (int m = 0; m < E.length; m++) {
for (int n = 0; n < F.length; n++) {
E[m][n]=JOptionPane.showInputDialog(null,"N.UAS : ");
for (int o = 0; o < E.length; o++) {
for (int p = 0; p < F.length; p++) {
E[o][p]=JOptionPane.showInputDialog(null,"N.TUGAS : ");
System.out.println( A[i][j]+" "+C[q][w]+" "+E[a][s]+" "+E[k][l]+" "+E[m][n]+" "+E[o][p]);
}}
} } } } }
}
}
}
}
}
}
}
Wednesday, April 3, 2013
contoh "IF"
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package tugas;
/**
*
* @author surur
*/
public class logicalIF {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int nilai=33;
if(nilai>=76)
System.out.println("A");
else if (nilai >= 66)
System.out.println("B");
else if (nilai>=56)
System.out.println("C");
else if (nilai>=46)
System.out.println("D");
else
System.out.println("E");
}
}
contoh "switch-case"
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package praktikum1;
/**
*
* @author surur
*/
public class test1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
/**dani mau mengambil mata kuliah
* yang dipilih adalah kalkulus
*/
int makul=2;
switch(makul){
case 1:
System.out.println("algo");break;
case 2:
System.out.println("kalkulus");break;
case 3:
System.out.println("teknik digital");break;
}
}
}
contoh "do-while"
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package tugas;
/**
*
* @author surur
*/
public class dowhile {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int a=1;
do{
System.out.println(a+"\tSyifaus Surur");
a++;
}while(a<=1000);
}
}
Subscribe to:
Posts (Atom)