Presentation is loading. Please wait.

Presentation is loading. Please wait.

/** 1.00 07 Feb 1996 Cay Horstmann */ import java.util.*; import corejava.*; public class EmployeeTest { public static void main(String[]

Similar presentations


Presentation on theme: "/** 1.00 07 Feb 1996 Cay Horstmann */ import java.util.*; import corejava.*; public class EmployeeTest { public static void main(String[]"— Presentation transcript:

1 /** * @version 1.00 07 Feb 1996 * @author Cay Horstmann */ import java.util.*; import corejava.*; public class EmployeeTest { public static void main(String[] args) { Employee[] staff = new Employee[3]; staff[0] = new Employee("Harry Hacker", 35000, new Day(1989,10,1)); staff[1] = new Employee("Carl Cracker", 75000, new Day(1987,12,15)); staff[2] = new Employee("Tony Tester", 38000, new Day(1990,3,15)); int i; for (i = 0; i < 3; i++) staff[i].raiseSalary(5); for (i = 0; i < 3; i++) staff[i].print(); }

2 class Employee { public Employee(String n, double s, Day d) { name = n; salary = s; hireDay = d; } public void print() { System.out.println(name + " " + salary + " " + hireYear()); } public void raiseSalary(double byPercent) { salary *= 1 + byPercent / 100; } public int hireYear() { return hireDay.getYear(); } private String name; private double salary; private Day hireDay; }

3 /** * @version 1.20 27 Mar 1998 * @author Cay Horstmann */ public class Card { public static final int ACE = 1; public static final int JACK = 11; public static final int QUEEN = 12; public static final int KING = 13; public static final int CLUBS = 1; public static final int DIAMONDS = 2; public static final int HEARTS = 3; public static final int SPADES = 4; public Card(int v, int s) { value = v; suit = s; } public int getValue() { return value; }

4 public int getSuit() { return suit; } public int rank() { if (value == 1) return 4 * 13 + suit; else return 4 * (value - 1) + suit; } public String toString() { String v; String s; if (value == ACE) v = "Ace"; else if (value == JACK) v = "Jack"; else if (value == QUEEN) v = "Queen"; else if (value == KING) v = "King"; else v = String.valueOf(value); if (suit == DIAMONDS) s = "Diamonds"; else if (suit == HEARTS) s = "Hearts"; else if (suit == SPADES) s = "Spades"; else /* suit == CLUBS */ s = "Clubs"; return v + " of " + s; } private int value; private int suit; }

5 /** * @version 1.20 27 Mar 1998 * @author Cay Horstmann */ import corejava.*; public class CardDeck { public CardDeck() { deck = new Card[52]; fill(); shuffle(); } public void fill() { int i; int j; for (i = 1; i <= 13; i++) for (j = 1; j <= 4; j++) deck[4 * (i - 1) + j - 1] = new Card(i, j); cards = 52; }

6 public void shuffle() { int next; for (next = 0; next < cards - 1; next++) { int r = new RandomIntGenerator(next, cards - 1).draw(); Card temp = deck[next]; deck[next] = deck[r]; deck[r] = temp; } public Card draw() { if (cards == 0) return null; cards--; return deck[cards]; }

7 public void play(int rounds) { int i; int wins = 0; for (i = 1; i <= rounds; i++) { Card yours = draw(); System.out.print("Your draw: " + yours + " "); Card mine = draw(); System.out.print("My draw: " + mine + " "); if (yours.rank() > mine.rank()) { System.out.println("You win"); wins++; } else System.out.println("I win"); } System.out.println("Your wins: " + wins + " My wins: " + (rounds - wins)); }

8 public static void main(String[] args) { CardDeck d = new CardDeck(); d.play(10); // play ten rounds } private Card[] deck; private int cards; }

9 /** * @version 1.20 07 Apr 1998 * @author Cay Horstmann */ import java.util.*; import corejava.*; public class EmployeeSortTest { public static void main(String[] args) { Employee[] staff = new Employee[3]; staff[0] = new Employee("Harry Hacker", 35000, new Day(1989,10,1)); staff[1] = new Employee("Carl Cracker", 75000, new Day(1987,12,15)); staff[2] = new Employee("Tony Tester", 38000, new Day(1990,3,15)); ArrayAlg.shellSort(staff); int i; for (i = 0; i < staff.length; i++) System.out.println(staff[i]); }

10 abstract class Sortable { public abstract int compareTo(Sortable b); } class ArrayAlg { public static void shellSort(Sortable[] a) { int n = a.length; int incr = n / 2; while (incr >= 1) { for (int i = incr; i < n; i++) { Sortable temp = a[i]; int j = i; while (j >= incr && temp.compareTo(a[j - incr]) < 0) { a[j] = a[j - incr]; j -= incr; } a[j] = temp; } incr /= 2; }

11 class Employee extends Sortable { public Employee(String n, double s, Day d) { name = n; salary = s; hireDate = d; } public void raiseSalary(double byPercent) { salary *= 1 + byPercent / 100; } public String getName() { return name; } public double getSalary() { return salary; } public String toString() { return name + " " + salary + " " + hireYear(); } public int hireYear() { return hireDate.getYear(); }

12 public int compareTo(Sortable b) { Employee eb = (Employee)b; if (salary < eb.salary) return -1; if (salary > eb.salary) return 1; return 0; } private String name; private double salary; private Day hireDate; }

13 /** * @version 1.20 07 Apr 1998 * @author Cay Horstmann */ import java.awt.*; import java.util.*; public class TileTest { public static void main(String[] args) { Tile[] a = new Tile[20]; int i; for (i = 0; i < a.length; i++) a[i] = new Tile(i, i, 10, 20, (int)(100 * Math.random())); Arrays.sort(a); for (i = 0; i < a.length; i++) System.out.println(a[i]); }

14 class Tile extends Rectangle implements Comparable { public Tile(int x, int y, int w, int h, int zz) { super(x, y, w, h); z = zz; } public int compareTo(Object b) { Tile tb = (Tile)b; return z - tb.z; } public String toString() { return super.toString() + "[z=" + z + "]"; } private int z; }

15 // Fig. 9.11: Shape.java // Definition of interface Shape public interface Shape { public abstract double area(); public abstract double volume(); public abstract String getName(); }

16 // Fig. 9.11: Point.java // Definition of class Point public class Point extends Object implements Shape { protected int x, y; // coordinates of the Point // no-argument constructor public Point() { setPoint( 0, 0 ); } // constructor public Point( int a, int b ) { setPoint( a, b ); } // Set x and y coordinates of Point public void setPoint( int a, int b ) { x = a; y = b; } // get x coordinate public int getX() { return x; } // get y coordinate public int getY() { return y; }

17 // convert the point into a String representation public String toString() { return "[" + x + ", " + y + "]"; } // return the area public double area() { return 0.0; } // return the volume public double volume() { return 0.0; } // return the class name public String getName() { return "Point"; } }

18 // Fig. 9.11: Circle.java // Definition of class Circle public class Circle extends Point { // inherits from Point protected double radius; // no-argument constructor public Circle() { // implicit call to superclass constructor here setRadius( 0 ); } // Constructor public Circle( double r, int a, int b ) { super( a, b ); // call the superclass constructor setRadius( r ); } // Set radius of Circle public void setRadius( double r ) { radius = ( r >= 0 ? r : 0 ); } // Get radius of Circle public double getRadius() { return radius; }

19 // Calculate area of Circle public double area() { return Math.PI * radius * radius; } // convert the Circle to a String public String toString() { return "Center = " + super.toString() + "; Radius = " + radius; } // return the class name public String getName() { return "Circle"; } }

20 // Fig. 9.11: Cylinder.java // Definition of class Cylinder public class Cylinder extends Circle { protected double height; // height of Cylinder // no-argument constructor public Cylinder() { // implicit call to superclass constructor here setHeight( 0 ); } // constructor public Cylinder( double h, double r, int a, int b ) { super( r, a, b ); // call superclass constructor setHeight( h ); } // Set height of Cylinder public void setHeight( double h ) { height = ( h >= 0 ? h : 0 ); } // Get height of Cylinder public double getHeight() { return height; }

21 // Calculate area of Cylinder (i.e., surface area) public double area() { return 2 * super.area() + 2 * Math.PI * radius * height; } // Calculate volume of Cylinder public double volume() { return super.area() * height; } // Convert a Cylinder to a String public String toString() { return super.toString() + "; Height = " + height; } // Return the class name public String getName() { return "Cylinder"; } }

22 // Fig. 9.11: Test.java // Driver for point, circle, cylinder hierarchy import javax.swing.JOptionPane; import java.text.DecimalFormat; public class Test { public static void main( String args[] ) { Point point = new Point( 7, 11 ); Circle circle = new Circle( 3.5, 22, 8 ); Cylinder cylinder = new Cylinder( 10, 3.3, 10, 10 ); Shape arrayOfShapes[]; arrayOfShapes = new Shape[ 3 ]; // aim arrayOfShapes[0] at subclass Point object arrayOfShapes[ 0 ] = point; // aim arrayOfShapes[1] at subclass Circle object arrayOfShapes[ 1 ] = circle; // aim arrayOfShapes[2] at subclass Cylinder object arrayOfShapes[ 2 ] = cylinder;

23 String output = point.getName() + ": " + point.toString() + "\n" + circle.getName() + ": " + circle.toString() + "\n" + cylinder.getName() + ": " + cylinder.toString(); DecimalFormat precision2 = new DecimalFormat( "0.00" ); // Loop through arrayOfShapes and print the name, // area, and volume of each object. for ( int i = 0; i < arrayOfShapes.length; i++ ) { output += "\n\n" + arrayOfShapes[ i ].getName() + ": " + arrayOfShapes[ i ].toString() + "\nArea = " + precision2.format( arrayOfShapes[ i ].area() ) + "\nVolume = " + precision2.format( arrayOfShapes[ i ].volume() ); } JOptionPane.showMessageDialog( null, output, "Demonstrating Polymorphism", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); }


Download ppt "/** 1.00 07 Feb 1996 Cay Horstmann */ import java.util.*; import corejava.*; public class EmployeeTest { public static void main(String[]"

Similar presentations


Ads by Google