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

Class Time2 with Overloaded Constructors

1 // Fig. 8.5: Time2.java
2 // Time2 class declaration with overloaded constructors.
3
4 public class Time2
5 {
6 private int hour; // 0 - 23
7 private int minute; // 0 - 59
8 private int second; // 0 - 59
9
10 // Time2 no-argument constructor: initializes each instance variable
11 // to zero; ensures that Time2 objects start in a consistent state
12 public Time2()
13 {
14 this( 0, 0, 0 ); // invoke Time2 constructor with three arguments
15 } // end Time2 no-argument constructor
16
17 // Time2 constructor: hour supplied, minute and second defaulted to 0
18 public Time2( int h )
19 {
20 this ( h, 0, 0 ); // invoke Time2 constructor with three arguments
21 } // end Time2 one-argument constructor
22
23 // Time2 constructor: hour and minute supplied, second defaulted to 0
24 public Time2( int h, int m )
25 {
26 this( h, m, 0 ); // invoke Time2 constructor with three arguments
27 } // end Time2 two-argument constructor
28
29 // Time2 constructor: hour, minute and second supplied
30 public Time2( int h, int m, int s )
31 {
32 setTime( h, m, s ); // invoke setTime to validate time
33 } // end Time2 three-argument constructor
34
35 // Time2 constructor: another Time2 object supplied
36 public Time2( Time2 time )
37 {
38 // invoke Time2 three-argument constructor
39 this( time.getHour(), time.getMinute(), time.getSecond() );
40 } // end Time2 constructor with a Time2 object argument
41
42 // Set Methods
43 // set a new time value using universal time; ensure that
44 // the data remains consistent by setting invalid values to zero
45 public void setTime( int h, int m, int s )
46 {
47 setHour( h ); // set the hour
48 setMinute( m ); // set the minute
49 setSecond( s ); // set the second
50 } // end method setTime
51
52 // validate and set hour
53 public void setHour( int h )
54 {
55 hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); 56 } // end method setHour 57 58 // validate and set minute 59 public void setMinute( int m ) 60 { 61 minute = ( ( m >= 0 && m < 60 ) ? m : 0 ); 62 } // end method setMinute 63 64 // validate and set second 65 public void setSecond( int s ) 66 { 67 second = ( ( s >= 0 && s < 60 ) ? s : 0 );
68 } // end method setSecond
69
70 // Get Methods
71 // get hour value
72 public int getHour()
73 {
74 return hour;
75 } // end method getHour
76
77 // get minute value
78 public int getMinute()
79 {
80 return minute;
81 } // end method getMinute
82
83 // get second value
84 public int getSecond()
85 {
86 return second;
87 } // end method getSecond
88
89 // convert to String in universal-time format (HH:MM:SS)
90 public String toUniversalString()
91 {
92 return String.format(
93 "%02d:%02d:%02d", getHour(), getMinute(), getSecond() );
94 } // end method toUniversalString
95
96 // convert to String in standard-time format (H:MM:SS AM or PM)
97 public String toString()
98 {
99 return String.format( "%d:%02d:%02d %s",
100 ( (getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12 ),
101 getMinute(), getSecond(), ( getHour() < 12 ? "AM" : "PM" ) );
102 } // end method toString
103 } // end class Time2

Time1 Class Declaration

public class Time1
5 {
6 private int hour; // 0 - 23
7 private int minute; // 0 - 59
8 private int second; // 0 - 59
9
10 // set a new time value using universal time; ensure that
11 // the data remains consistent by setting invalid values to zero
12 public void setTime( int h, int m, int s )
13
14 hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); // validate hour 15 minute = ( ( m >= 0 && m < 60 ) ? m : 0 ); // validate minute 16 second = ( ( s >= 0 && s < 60 ) ? s : 0 ); // validate second
17 } // end method setTime
18
19 // convert to String in universal-time format (HH:MM:SS)
20 public String toUniversalString()
21 {
22 return String.format( "%02d:%02d:%02d", hour, minute, second );
23 } // end method toUniversalString
24
25 // convert to String in standard-time format (H:MM:SS AM or PM)
26 public String toString()
27 {
28 return String.format( "%d:%02d:%02d %s",
29 ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ),
30 minute, second, ( hour < 12 ? "AM" : "PM" ) );
31 } // end method toString
32 } // end class Time1





Figure 8.2. Time1 object used in an application.
(This item is displayed on page 362 in the print version)
1 // Fig. 8.2: Time1Test.java
2 // Time1 object used in an application.
3
4 public class Time1Test
5 {
6 public static void main( String args[] )
7 {
8 // create and initialize a Time1 object
9 Time1 time = new Time1(); // invokes Time1 constructor
10
11 // output string representations of the time
12 System.out.print( "The initial universal time is: " );
13 System.out.println( time.toUniversalString() );
14 System.out.print( "The initial standard time is: " );
15 System.out.println( time.toString() );
16 System.out.println(); // output a blank line
17
18 // change time and output updated time
19 time.setTime( 13, 27, 6 );
20 System.out.print( "Universal time after setTime is: " );
21 System.out.println( time.toUniversalString() );
22 System.out.print( "Standard time after setTime is: " );
23 System.out.println( time.toString() );
24 System.out.println(); // output a blank line
25
26 // set time with invalid values; output updated time
27 time.setTime( 99, 99, 99 );
28 System.out.println( "After attempting invalid settings:" );
29 System.out.print( "Universal time: " );
30 System.out.println( time.toUniversalString() );
31 System.out.print( "Standard time: " );
32 System.out.println( time.toString() );
33 } // end main
34 } // end class Time1Test