Minggu, 08 Januari 2012

1 // Fig. 10.4: Employee.java
2 // Employee abstract superclass.
3
4 public abstract class Employee
5 {
6 private String firstName;
7 private String lastName;
8 private String socialSecurityNumber;
9
10 // three-argument constructor
11 public Employee( String first, String last, String ssn )
12 {
13 firstName = first;
14 lastName = last;
15 socialSecurityNumber = ssn;
16 } // end three-argument Employee constructor
17
18 // set first name
19 public void setFirstName( String first )
20 {
21 firstName = first;
22 } // end method setFirstName
23
24 // return first name
25 public String getFirstName()
26 {
27 return firstName;
28 } // end method getFirstName
29
30 // set last name
31 public void setLastName( String last )
32 {
33 lastName = last;
34 } // end method setLastName
35
36 // return last name
37 public String getLastName()
38 {
39 return lastName;
40 } // end method getLastName
41
42 // set social security number
43 public void setSocialSecurityNumber( String ssn )
44 {
45 socialSecurityNumber = ssn; // should validate
46 } // end method setSocialSecurityNumber
47
48 // return social security number
49 public String getSocialSecurityNumber()
50 {
51 return socialSecurityNumber;
52 } // end method getSocialSecurityNumber
53
54 // return String representation of Employee object
55 public String toString()
56 {
57 return String.format( "%s %s\nsocial security number: %s",
58 getFirstName(), getLastName(), getSocialSecurityNumber() );
59 } // end method toString
60
61 // abstract method overridden by subclasses
62 public abstract double earnings(); // no implementation here
63 } // end abstract class Employee


1 // Fig. 10.5: SalariedEmployee.java
2 // SalariedEmployee class extends Employee.
3
4 public class SalariedEmployee extends Employee
5 {
6 private double weeklySalary;
7
8 // four-argument constructor
9 public SalariedEmployee( String first, String last, String ssn,
10 double salary )
11 {
12 super( first, last, ssn ); // pass to Employee constructor
13 setWeeklySalary( salary ); // validate and store salary
14 } // end four-argument SalariedEmployee constructor
15
16 // set salary
17 public void setWeeklySalary( double salary )
18 {
19 weeklySalary = salary < 0.0 ? 0.0 : salary; 20 } // end method setWeeklySalary 21 22 // return salary 23 public double getWeeklySalary() 24 { 25 return weeklySalary; 26 } // end method getWeeklySalary 27 28 // calculate earnings; override abstract method earnings in Employee 29 public double earnings() 30 { 31 return getWeeklySalary(); 32 } // end method earnings 33 34 // return String representation of SalariedEmployee object 35 public String toString() 36 { 37 return String.format( "salaried employee: %s\n%s: $%,.2f", 38 super.toString(), "weekly salary", getWeeklySalary() ); 39 } // end method toString 40 } // end class SalariedEmployee 1 // Fig. 10.6: HourlyEmployee.java 2 // HourlyEmployee class extends Employee. 3 4 public class HourlyEmployee extends Employee 5 { 6 private double wage; // wage per hour 7 private double hours; // hours worked for week 8 9 // five-argument constructor 10 public HourlyEmployee( String first, String last, String ssn, 11 double hourlyWage, double hoursWorked ) 12 { 13 super( first, last, ssn ); 14 setWage( hourlyWage ); // validate hourly wage 15 setHours( hoursWorked ); // validate hours worked 16 } // end five-argument HourlyEmployee constructor 17 18 // set wage 19 public void setWage( double hourlyWage ) 20 { 21 wage = ( hourlyWage < 0.0 ) ? 0.0 : hourlyWage; 22 } // end method setWage 23 24 // return wage 25 public double getWage() 26 { 27 return wage; 28 } // end method getWage 29 30 // set hours worked 31 public void setHours( double hoursWorked ) 32 { 33 hours = ( ( hoursWorked >= 0.0 ) && ( hoursWorked <= 168.0 ) ) ? 34 hoursWorked : 0.0; 35 } // end method setHours 36 37 // return hours worked 38 public double getHours() 39 { 40 return hours; 41 } // end method getHours 42 43 // calculate earnings; override abstract method earnings in Employee 44 public double earnings() 45 { 46 if ( getHours() <= 40 ) // no overtime 47 return getWage() * getHours(); 48 else 49 return 40 * getWage() + ( gethours() - 40 ) * getWage() * 1.5; 50 } // end method earnings 51 52 // return String representation of HourlyEmployee object 53 public String toString() 54 { 55 return String.format( "hourly employee: %s\n%s: $%,.2f; %s: %,.2f", 56 super.toString(), "hourly wage", getWage(), 57 "hours worked", getHours() ); 58 } // end method toString 59 } // end class HourlyEmployee 1 // Fig. 10.7: CommissionEmployee.java 2 // CommissionEmployee class extends Employee. 3 4 public class CommissionEmployee extends Employee 5 { 6 private double grossSales; // gross weekly sales 7 private double commissionRate; // commission percentage 8 9 // five-argument constructor 10 public CommissionEmployee( String first, String last, String ssn, 11 double sales, double rate ) 12 { 13 super( first, last, ssn ); 14 setGrossSales( sales ); 15 setCommissionRate( rate ); 16 } // end five-argument CommissionEmployee constructor 17 18 // set commission rate 19 public void setCommissionRate( double rate ) 20 { 21 commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
22 } // end method setCommissionRate
23
24 // return commission rate
25 public double getCommissionRate()
26 {
27 return commissionRate;
28 } // end method getCommissionRate
29
30 // set gross sales amount
31 public void setGrossSales( double sales )
32 {
33 grossSales = ( sales < 0.0 ) ? 0.0 : sales;
34 } // end method setGrossSales
35
36 // return gross sales amount
37 public double getGrossSales()
38 {
39 return grossSales;
40 } // end method getGrossSales
41
42 // calculate earnings; override abstract method earnings in Employee
43 public double earnings()
44 {
45 return getCommissionRate() * getGrossSales();
46 } // end method earnings
47
48 // return String representation of CommissionEmployee object
49 public String toString()
50 {
51 return String.format( "%s: %s\n%s: $%,.2f; %s: %.2f",
52 "commission employee", super.toString(),
53 "gross sales", getGrossSales(),
54 "commission rate", getCommissionRate() );
55 } // end method toString
56 } // end class CommissionEmployee


1 // Fig. 10.8: BasePlusCommissionEmployee.java
2 // BasePlusCommissionEmployee class extends CommissionEmployee.
3
4 public class BasePlusCommissionEmployee extends CommissionEmployee
5 {
6 private double baseSalary; // base salary per week
7
8 // six-argument constructor
9 public BasePlusCommissionEmployee( String first, String last,
10 String ssn, double sales, double rate, double salary )
11 {
12 super( first, last, ssn, sales, rate );
13 setBaseSalary( salary ); // validate and store base salary
14 } // end six-argument BasePlusCommissionEmployee constructor
15
16 // set base salary
17 public void setBaseSalary( double salary )
18 {
19 baseSalary = ( salary < 0.0 ) ? 0.0 : salary; // non-negative
20 } // end method setBaseSalary
21
22 // return base salary
23 public double getBaseSalary()
24 {
25 return baseSalary;
26 } // end method getBaseSalary
27
28 // calculate earnings; override method earnings in CommissionEmployee
29 public double earnings()
30 {
31 return getBaseSalary() + super.earnings();
32 } // end method earnings
33
34 // return String representation of BasePlusCommissionEmployee object
35 public String toString()
36 {
37 return String.format( "%s %s; %s: $%,.2f",
38 "base-salaried", super.toString(),
39 "base salary", getBaseSalary() );
40 } // end method toString
41 } // end class BasePlusCommissionEmployee


1 // Fig. 10.9: PayrollSystemTest.java
2 // Employee hierarchy test program.
3
4 public class PayrollSystemTest
5 {
6 public static void main( String args[] )
7 {
8 // create subclass objects
9 SalariedEmployee salariedEmployee =
10 new SalariedEmployee( "John", "Smith", "111-11-1111", 800.00 );
11 HourlyEmployee hourlyEmployee =
12 new HourlyEmployee( "Karen", "Price", "222-22-2222", 16.75, 40 );
13 CommissionEmployee commissionEmployee =
14 new CommissionEmployee(
15 "Sue", "Jones", "333-33-3333", 10000, .06 );
16 BasePlusCommissionEmployee basePlusCommissionEmployee =
17 new BasePlusCommissionEmployee(
18 "Bob", "Lewis", "444-44-4444", 5000, .04, 300 );
19
20 System.out.println( "Employees processed individually:\n" );
21
22 System.out.printf( "%s\n%s: $%,.2f\n\n",
23 salariedEmployee, "earned", salariedEmployee.earnings() );
24 System.out.printf( "%s\n%s: $%,.2f\n\n",
25 hourlyEmployee, "earned", hourlyEmployee.earnings() );
26 System.out.printf( "%s\n%s: $%,.2f\n\n",
27 commissionEmployee, "earned", commissionEmployee.earnings() );
28 System.out.printf( "%s\n%s: $%,.2f\n\n",
29 basePlusCommissionEmployee,
30 "earned", basePlusCommissionEmployee.earnings() );
31
32 // create four-element Employee array
33 Employee employees[] = new Employee[ 4 ];
34
35 // initialize array with Employees
36 employees[ 0 ] = salariedEmployee;
37 employees[ 1 ] = hourlyEmployee;
38 employees[ 2 ] = commissionEmployee;
39 employees[ 3 ] = basePlusCommissionEmployee;
40
41 System.out.println( "Employees processed polymorphically:\n" );
42
43 // generically process each element in array employees
44 for ( Employee currentEmployee : employees )
45 {
46 System.out.println( currentEmployee ); // invokes toString
47
48 // determine whether element is a BasePlusCommissionEmployee
49 if ( currentEmployee instanceof BasePlusCommissionEmployee )
50 {
51 // downcast Employee reference to
52 // BasePlusCommissionEmployee reference
53 BasePlusCommissionEmployee employee =
54 ( BasePlusCommissionEmployee ) currentEmployee;
55
56 double oldBaseSalary = employee.getBaseSalary();
57 employee.setBaseSalary( 1.10 * oldBaseSalary );
58 System.out.printf(
59 "new base salary with 10%% increase is: $%,.2f\n",
60 employee.getBaseSalary() );
61 } // end if
62
63 System.out.printf(
64 "earned $%,.2f\n\n", currentEmployee.earnings() );
65 } // end for
66
67 // get type name of each object in employees array
68 for ( int j = 0; j < employees.length; j++ )
69 System.out.printf( "Employee %d is a %s\n", j,
70 employees[ j ].getClass().getName() );
71 } // end main
72 } // end class PayrollSystemTest

JLabel with text and with images

1 // Fig 9.19: LabelDemo.java
2 // Demonstrates the use of labels.
3 import java.awt.BorderLayout;
4 import javax.swing.ImageIcon;
5 import javax.swing.JLabel;
6 import javax.swing.JFrame;
7
8 public class LabelDemo
9 {
10 public static void main( String args[] )
11 {
12 // Create a label with plain text
13 JLabel northLabel = new JLabel( "North" );
14
15 // create an icon from an image so we can put it on a JLabel
16 ImageIcon labelIcon = new ImageIcon( "GUItip.gif" );
17
18 // create a label with an Icon instead of text
19 JLabel centerLabel = new JLabel( labelIcon );
20
21 // create another label with an Icon
22 JLabel southLabel = new JLabel( labelIcon );
23
24 // set the label to display text (as well as an icon)
25 southLabel.setText( "South" );
26
27 // create a frame to hold the labels
28 JFrame application = new JFrame();
29
30 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
31
32 // add the labels to the frame; the second argument specifies
33 // where on the frame to add the label
34 application.add( northLabel, BorderLayout.NORTH );
35 application.add( centerLabel, BorderLayout.CENTER );
36 application.add( southLabel, BorderLayout.SOUTH );
37
38 application.setSize( 300, 300 ); // set the size of the frame
39 application.setVisible( true ); // show the frame
40 } // end main
41 } // end class LabelDemo

Contoh program java

class Shape {}

class Point extends Shape {
protected float x,y;
Point (float x, float y) { this.x=x; this.y=y; }
public String toString () {
return "("+x+","+y+")";
}
void move (float dx, float dy) { x += dx; y += dy; }
}

class Rectangle extends Point {
protected float height, width;
Rectangle (float x, float y, float h, float w) {
super (x,y); height=h; width=w;
}
public String toString () {
return "("+x+","+y+";h="+height+",w="+width+")";
}
}

class Circle extends Point {
protected float radius;
Circle (float x, float y, float r) {
super (x,y); radius=r;
}
public String toString () {
return "("+x+","+y+";r="+radius+")";
}
}

class Shapes {
public static void main (String [] args) {
final Point p = new Point (2.3f, 4.5f);
final Rectangle r = new Rectangle (2.3f, 4.5f, 45.1f, 89.1f);
final Circle c = new Circle (2.3f, 4.5f, 0.3f);
r.move (3.4f, 0.0f);
c.move (-3.4f, 1.0f);
System.out.println (p);
System.out.println (r);
System.out.println (c);
}
}

Minggu, 30 Oktober 2011

1 // Fig. 12.18: LinesRectsOvalsJPanel.java
2 // Drawing lines, rectangles and ovals.
3 import java.awt.Color;
4 import java.awt.Graphics;
5 import javax.swing.JPanel;
6
7 public class LinesRectsOvalsJPanel extends JPanel
8 {
9 // display various lines, rectangles and ovals
10 public void paintComponent( Graphics g )
11 {
12 super.paintComponent( g ); // call superclass's paint method
13
14 this.setBackground( Color.WHITE );
15
16 g.setColor( Color.RED );
17 g.drawLine( 5, 30, 380, 30 );
18
19 g.setColor( Color.BLUE );
20 g.drawRect( 5, 40, 90, 55 );
21 g.fillRect( 100, 40, 90, 55 );
22
23 g.setColor( Color.CYAN );
24 g.fillRoundRect( 195, 40, 90, 55, 50, 50 );
25 g.drawRoundRect( 290, 40, 90, 55, 20, 20 );
26
27 g.setColor( Color.YELLOW );
28 g.draw3DRect( 5, 100, 90, 55, true );
29 g.fill3DRect( 100, 100, 90, 55, false );
30
31 g.setColor( Color.MAGENTA );
32 g.drawOval( 195, 100, 90, 55 );
33 g.fillOval( 290, 100, 90, 55 );
34 } // end method paintComponent
35 } // end class LinesRectsOvalsJPanel


1 // Fig. 12.19: LinesRectsOvals.java
2 // Drawing lines, rectangles and ovals.
3 import java.awt.Color;
4 import javax.swing.JFrame;
5
6 public class LinesRectsOvals
7 {
8 // execute application
9 public static void main( String args[] )
10 {
11 // create frame for LinesRectsOvalsJPanel
12 JFrame frame =
13 new JFrame( "Drawing lines, rectangles and ovals" );
14 frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
15
16 LinesRectsOvalsJPanel linesRectsOvalsJPanel =
17 new LinesRectsOvalsJPanel();
18 linesRectsOvalsJPanel.setBackground( Color.WHITE );
19 frame.add( linesRectsOvalsJPanel ); // add panel to frame
20 frame.setSize( 400, 210 ); // set frame size
21 frame.setVisible( true ); // display frame
22 } // end main
23 } // end class LinesRectsOvals


java pewarisan

1 // Fig. 9.4: CommissionEmployee.java
2 // CommissionEmployee class represents a commission employee.
3
4 public class CommissionEmployee extends Object
5 {
6 private String firstName;
7 private String lastName;
8 private String socialSecurityNumber;
9 private double grossSales; // gross weekly sales
10 private double commissionRate; // commission percentage
11
12 // five-argument constructor
13 public CommissionEmployee( String first, String last, String ssn,
14 double sales, double rate )
15 {
16 // implicit call to Object constructor occurs here
17 firstName = first;
18 lastName = last;
19 socialSecurityNumber = ssn;
20 setGrossSales( sales ); // validate and store gross sales
21 setCommissionRate( rate ); // validate and store commission rate
22 } // end five-argument CommissionEmployee constructor
23
24 // set first name
25 public void setFirstName( String first )
26 {
27 firstName = first;
28 } // end method setFirstName
29
30 // return first name
31 public String getFirstName()
32 {
33 return firstName;
34 } // end method getFirstName
35
36 // set last name
37 public void setLastName( String last )
38 {
39 lastName = last;
40 } // end method setLastName
41
42 // return last name
43 public String getLastName()
44 {
45 return lastName;
46 } // end method getLastName
47
48 // set social security number
49 public void setSocialSecurityNumber( String ssn )
50 {
51 socialSecurityNumber = ssn; // should validate
52 } // end method setSocialSecurityNumber
53
54 // return social security number
55 public String getSocialSecurityNumber()
56 {
57 return socialSecurityNumber;
58 } // end method getSocialSecurityNumber
59
60 // set gross sales amount
61 public void setGrossSales( double sales )
62 {
63 grossSales = ( sales < 0.0 ) ? 0.0 : sales; 64 } // end method setGrossSales 65 66 // return gross sales amount 67 public double getGrossSales() 68 { 69 return grossSales; 70 } // end method getGrossSales 71 72 // set commission rate 73 public void setCommissionRate( double rate ) 74 { 75 commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0; 76 } // end method setCommissionRate 77 78 // return commission rate 79 public double getCommissionRate() 80 { 81 return commissionRate; 82 } // end method getCommissionRate 83 84 // calculate earnings 85 public double earnings() 86 { 87 return commissionRate * grossSales; 88 } // end method earnings 89 90 // return String representation of CommissionEmployee object 91 public String toString() 92 { 93 return String.format( "%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f", 94 "commission employee", firstName, lastName, 95 "social security number", socialSecurityNumber, 96 "gross sales", grossSales, 97 "commission rate", commissionRate ); 98 } // end method toString 99 } // end class CommissionEmployee 1 // Fig. 9.5: CommissionEmployeeTest.java 2 // Testing class CommissionEmployee. 3 4 public class CommissionEmployeeTest 5 { 6 public static void main( String args[] ) 7 { 8 // instantiate CommissionEmployee object 9 CommissionEmployee employee = new CommissionEmployee( 10 "Sue", "Jones", "222-22-2222", 10000, .06 ); 11 12 // get commission employee data 13 System.out.println( 14 "Employee information obtained by get methods: \n" ); 15 System.out.printf( "%s %s\n", "First name is", 16 employee.getFirstName() ); 17 System.out.printf( "%s %s\n", "Last name is", 18 employee.getLastName() ); 19 System.out.printf( "%s %s\n", "Social security number is", 20 employee.getSocialSecurityNumber() ); 21 System.out.printf( "%s %.2f\n", "Gross sales is", 22 employee.getGrossSales() ); 23 System.out.printf( "%s %.2f\n", "Commission rate is", 24 employee.getCommissionRate() ); 25 26 employee.setGrossSales( 500 ); // set gross sales 27 employee.setCommissionRate( .1 ); // set commission rate 28 29 System.out.printf( "\n%s:\n\n%s\n", 30 "Updated employee information obtained by toString", employee ); 31 } // end main 32 } // end class CommissionEmployeeTest 1 // Fig. 9.12: CommissionEmployee3.java 2 // CommissionEmployee3 class represents a commission employee. 3 4 public class CommissionEmployee3 5 { 6 private String firstName; 7 private String lastName; 8 private String socialSecurityNumber; 9 private double grossSales; // gross weekly sales 10 private double commissionRate; // commission percentage 11 12 // five-argument constructor 13 public CommissionEmployee3( String first, String last, String ssn, 14 double sales, double rate ) 15 { 16 // implicit call to Object constructor occurs here 17 firstName = first; 18 lastName = last; 19 socialSecurityNumber = ssn; 20 setGrossSales( sales ); // validate and store gross sales 21 setCommissionRate( rate ); // validate and store commission rate 22 } // end five-argument CommissionEmployee3 constructor 23 24 // set first name 25 public void setFirstName( String first ) 26 { 27 firstName = first; 28 } // end method setFirstName 29 30 // return first name 31 public String getFirstName() 32 { 33 return firstName; 34 } // end method getFirstName 35 36 // set last name 37 public void setLastName( String last ) 38 { 39 lastName = last; 40 } // end method setLastName 41 42 // return last name 43 public String getLastName() 44 { 45 return lastName; 46 } // end method getLastName 47 48 // set social security number 49 public void setSocialSecurityNumber( String ssn ) 50 { 51 socialSecurityNumber = ssn; // should validate 52 } // end method setSocialSecurityNumber 53 54 // return social security number 55 public String getSocialSecurityNumber() 56 { 57 return socialSecurityNumber; 58 } // end method getSocialSecurityNumber 59 60 // set gross sales amount 61 public void setGrossSales( double sales ) 62 { 63 grossSales = ( sales < 0.0 ) ? 0.0 : sales; 64 } // end method setGrossSales 65 66 // return gross sales amount 67 public double getGrossSales() 68 { 69 return grossSales; 70 } // end method getGrossSales 71 72 // set commission rate 73 public void setCommissionRate( double rate ) 74 { 75 commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
76 } // end method setCommissionRate
77
78 // return commission rate
79 public double getCommissionRate()
80 {
81 return commissionRate;
82 } // end method getCommissionRate
83
84 // calculate earnings
85 public double earnings()
86 {
87 return getCommissionRate() * getGrossSales();
88 } // end method earnings
89
90 // return String representation of CommissionEmployee3 object
91 public String toString()
92 {
93 return String.format( "%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f",
94 "commission employee", getFirstName(), getLastName(),
95 "social security number", getSocialSecurityNumber(),
96 "gross sales", getGrossSales(),
97 "commission rate", getCommissionRate() );
98 } // end method toString
99 } // end class CommissionEmployee3


1 // Fig. 9.13: BasePlusCommissionEmployee4.java
2 // BasePlusCommissionEmployee4 class inherits from CommissionEmployee3 and
3 // accesses CommissionEmployee3's private data via CommissionEmployee3's
4 // public methods.
5
6 public class BasePlusCommissionEmployee4 extends CommissionEmployee3
7 {
8 private double baseSalary; // base salary per week
9
10 // six-argument constructor
11 public BasePlusCommissionEmployee4( String first, String last,
12 String ssn, double sales, double rate, double salary )
13 {
14 super( first, last, ssn, sales, rate );
15 setBaseSalary( salary ); // validate and store base salary
16 } // end six-argument BasePlusCommissionEmployee4 constructor
17
18 // set base salary
19 public void setBaseSalary( double salary )
20 {
21 baseSalary = ( salary < 0.0 ) ? 0.0 : salary;
22 } // end method setBaseSalary
23
24 // return base salary
25 public double getBaseSalary()
26 {
27 return baseSalary;
28 } // end method getBaseSalary
29
30 // calculate earnings
31 public double earnings()
32 {
33 return getBaseSalary() + super.earnings();
34 } // end method earnings
35
36 // return String representation of BasePlusCommissionEmployee4
37 public String toString()
38 {
39 return String.format( "%s %s\n%s: %.2f", "base-salaried",
40 super.toString(), "base salary", getBaseSalary() );
41 } // end method toString
42 } // end class BasePlusCommissionEmployee4




1 // Fig. 9.14: BasePlusCommissionEmployeeTest4.java
2 // Testing class BasePlusCommissionEmployee4.
3
4 public class BasePlusCommissionEmployeeTest4
5 {
6 public static void main( String args[] )
7 {
8 // instantiate BasePlusCommissionEmployee4 object
9 BasePlusCommissionEmployee4 employee =
10 new BasePlusCommissionEmployee4(
11 "Bob", "Lewis", "333-33-3333", 5000, .04, 300 );
12
13 // get base-salaried commission employee data
14 System.out.println(
15 "Employee information obtained by get methods: \n" );
16 System.out.printf( "%s %s\n", "First name is",
17 employee.getFirstName() );
18 System.out.printf( "%s %s\n", "Last name is",
19 employee.getLastName() );
20 System.out.printf( "%s %s\n", "Social security number is",
21 employee.getSocialSecurityNumber() );
22 System.out.printf( "%s %.2f\n", "Gross sales is",
23 employee.getGrossSales() );
24 System.out.printf( "%s %.2f\n", "Commission rate is",
25 employee.getCommissionRate() );
26 System.out.printf( "%s %.2f\n", "Base salary is",
27 employee.getBaseSalary() );
28
29 employee.setBaseSalary( 1000 ); // set base salary
30
31 System.out.printf( "\n%s:\n\n%s\n",
32 "Updated employee information obtained by toString",
33 employee.toString() );
34 } // end main
35 } // end class BasePlusCommissionEmployeeTest4

Creatre Smile graphics

1 // Fig. 6.16: DrawSmiley.java
2 // Demonstrates filled shapes.
3 import java.awt.Color;
4 import java.awt.Graphics;
5 import javax.swing.JPanel;
6
7 public class DrawSmiley extends JPanel
8 {
9 public void paintComponent( Graphics g )
10 {
11 super.paintComponent( g );
12
13 // draw the face
14 g.setColor( Color.YELLOW );
15 g.fillOval( 10, 10, 200, 200 );
16
17 // draw the eyes
18 g.setColor( Color.BLACK );
19 g.fillOval( 55, 65, 30, 30 );
20 g.fillOval( 135, 65, 30, 30 );
21
22 // draw the mouth
23 g.fillOval( 50, 110, 120, 60 );
24
25 // "touch up" the mouth into a smile
26 g.setColor( Color.YELLOW );
27 g.fillRect( 50, 110, 120, 30 );
28 g.fillOval( 50, 120, 120, 40 );
29 } // end method paintComponent
30 } // end class DrawSmiley




1 // Fig. 6.17: DrawSmileyTest.java
2 // Test application that displays a smiley face.
3 import javax.swing.JFrame;
4
5 public class DrawSmileyTest
6 {
7 public static void main( String args[] )
8 {
9 DrawSmiley panel = new DrawSmiley();
10 JFrame application = new JFrame();
11
12 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
13 application.add( panel );
14 application.setSize( 230, 250 );
15 application.setVisible( true );
16 } // end main
17 } // end class DrawSmileyTest

membuat kotak

public class kotak
{
public static void main(String args[])
{
int x,y;
int pola[][]={{1,1,1,1,1},{1,0,0,0,1},{1,0,0,0,1},{1,0,0,0,1},{1,1,1,1,1}};

for(y=0;y<5;y++)
{
for(x=0;x<5;x++)
{
if(pola[y][x]==1)
System.out.print("#");
else
System.out.print(" ");
}
System.out.println(" ");
}
}
}