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);
}
}