Casting a class:
Final classes cannot be parent classes. To define the syntax: Final methods cannot be overriden. To define a method as final, put the word final just before the return type of the method. A compiler error will occur if you attempt to override (define with the same name) a method in a subclass. Example: public final void myFinalMethod()
public class Exercise_7_1_Employee { protected String name; protected String ssn; public Exercise_7_1_Employee( String n, String s ) { name = n; ssn = s; } public String getName() { return name; } public String getSsn() { return ssn; } // could add mutators public String toString() { return "Employee: name=" + name + ", SSN= " + ssn; } } public class Exercise_7_1_Salaried extends Exercise_7_1_Employee { private double salary; public Exercise_7_1_Salaried(String name, String ssn, double s) { super(name, ssn); salary = s; } public void setSalary( double sal ) { salary = sal; } public double getSalary(){ return salary; } public String toString() { return "Salaried " + super.toString()+ ", Salary=" + salary; } } // end class Salaried public class Exercise_7_1_Hourly extends Exercise_7_1_Employee { private double hourlyRate; public Exercise_7_1_Hourly(String name, String ssn, double d_hourlyRate) { super(name, ssn); hourlyRate = d_hourlyRate; } public void setHourlyRate( double d_HourlyRate ) { hourlyRate = d_HourlyRate; } public double getHourlyRate() { return hourlyRate; } public String toString() { return "Hourly Rate " + super.toString() + ", Hourly Rate=" + hourlyRate; } public static void main(String[] args) { Exercise_7_1_Hourly oExercise_7_1_Hourly = new Exercise_7_1_Hourly("ABC", "111-222-3333", 20); System.out.println(oExercise_7_1_Hourly.toString()); } } Object classObject Class (java.lang.Object):
Some of the Object methods:
Abstract ClassesA class with an abstract method is an abstract class
/* Exercise 7.2: Using the example code for the abstract Employee class. W rite a subclass of the abstract Employee class called Hourly, which includes private instance variables for hours worked and hourly rate, and a constructor that includes parameters to initialize those as well as the superclass variables. Don't forget to override the computePay method. - Write a driver program that will contain an array Employee objects, initialized to instances of Hourly and Salaried shown below.Then write a loop to print the toString AND return value of computePay on each Employee array element (you DON'T need to check if Salaried or Hourly!) */ public abstract class Exercise_7_2_Employee { protected String name; protected String ssn; public Exercise_7_2_Employee( String n, String s ) { name = n; ssn = s; } public String getName(){ return name; } public String getSsn(){ return ssn; } // could add mutators public abstract double computePay(); } // end abstract class Employee public class Exercise_7_2_Salaried extends Exercise_7_2_Employee { private double salary; private static int payPeriods = 12; public Exercise_7_2_Salaried( String name, String ssn, double s) { super(name, ssn); salary = s; } public void setSalary( double sal ){ salary = sal; } public double getSalary(){ return salary; } public double computePay() { return Math.round(salary/payPeriods*100.)/100.; } public String toString() { return "Salaried: \n name = " + name + ",\n SSN = " + ssn + ",\n Salary = " + salary; } } // end class Salaried public class Exercise_7_2_Hourly extends Exercise_7_2_Employee { private double d_HoursWorked = 0; private double d_HourlyRate = 0; public Exercise_7_2_Hourly( String name, String ssn, double hoursWorked, double hourlyRate) { super(name, ssn); d_HoursWorked = hoursWorked; d_HourlyRate = hourlyRate; } public void setHoursWorked(double hoursWorked) { d_HoursWorked = hoursWorked; } public double getHoursWorked(){ return d_HoursWorked; } public void setHourlyRate(double hourlyRate){ d_HourlyRate = hourlyRate; } public double getHourlyRate(){ return d_HourlyRate; } public double computePay() { return Math.round(d_HoursWorked * d_HourlyRate * 100.0)/100.0; } public String toString() { return "Hourly:\n name = " + name + ",\n SSN = " + ssn + ",\n Hours Worked = " + d_HoursWorked + ",\n Hourly Rate = " + d_HourlyRate; } } public class Exercise_7_2_Test { public static void main(String[] args) { Exercise_7_2_Employee [] empArray = new Exercise_7_2_Employee[] { new Exercise_7_2_Hourly("Donald Duck", "123-45-6789", 20., 30.), new Exercise_7_2_Salaried("Mickey Mouse", "987-65-4321", 100000.), new Exercise_7_2_Salaried("Bugs Bunny", "121-21-2121", 90000.), new Exercise_7_2_Hourly("Elmer Fudd", "343-43-4343", 10.97, 15.25) }; for (int i = 0; i < empArray.length; i++) { System.out.println(empArray[i].toString() + "\n Computed Pay: " + empArray[i].computePay()); } } } InterfacesInterfaces indicate only the "design features" (method access, return value and parameters) of a class. Therefore, interfaces do not have any instance variables, and the methods have no body. They are primarily used to indicate how to communicate with an object. Interfaces are sometimes used to simulate multiple inheritance.
Syntax to define an interface:
Syntax to implement an interface:
public interface Exercise_7_3_Comparable { public int compareTo(Object obj); } public class Exercise_7_3_Customer implements Exercise_7_3_Comparable { private String s_CustomerName; private long l_CustomerAcctNum; private Exercise_5_3_SavingsAccount oExercise_5_3_SavingsAccount; public Exercise_7_3_Customer() { oExercise_5_3_SavingsAccount = new Exercise_5_3_SavingsAccount(); // in case, might prevent exception } public Exercise_7_3_Customer(String customerName, long customerAcctNum, double balance) { s_CustomerName = customerName; l_CustomerAcctNum = customerAcctNum; oExercise_5_3_SavingsAccount = new Exercise_5_3_SavingsAccount(balance); } public String getCustomerName() { return s_CustomerName; } public long getCustomerAcctNum() { return l_CustomerAcctNum; } public Exercise_5_3_SavingsAccount getSavingsAccount() { return oExercise_5_3_SavingsAccount; } public void setCustomerName(String CustomerName) { s_CustomerName = CustomerName; // is it require to check for empty string ?? } public void display() { System.out.println("Customer Name: " + s_CustomerName); System.out.println("Customer A/C no: " + l_CustomerAcctNum); System.out.println("Customer Balance: " + oExercise_5_3_SavingsAccount.getBalance()); } // Interface method definition public int compareTo (Object obj) { if (obj instanceof Exercise_7_3_Customer) { Exercise_7_3_Customer oCustomer = (Exercise_7_3_Customer)obj; int i_compareName; //int i_compareAccNum, i_compareSavingBal; // Not needed // get the results of comparing each instance variable of this and param. i_compareName = this.s_CustomerName.compareToIgnoreCase(oCustomer.s_CustomerName); if(i_compareName == 0) { if(this.l_CustomerAcctNum < oCustomer.l_CustomerAcctNum) return -1; else if(this.l_CustomerAcctNum > oCustomer.l_CustomerAcctNum) return 1; else // if a/c number is equal { if(this.getSavingsAccount().getBalance() < oCustomer.getSavingsAccount().getBalance()) return -1; if(this.getSavingsAccount().getBalance() > oCustomer.getSavingsAccount().getBalance()) return 1; else return 0; // object is equal } } else { return i_compareName; } } else // parameter not a Customer class object return this.compareTo(obj); // calls Object's compareTo? --> Don't get it (copied from professor's answer in forum reply) -> it will become recursive } // end compareTo public static void main (String[] args) { // Test case // 1st object Exercise_7_3_Customer oExercise_7_3_Customer1 = new Exercise_7_3_Customer("ABC", 1234567890, 1000.00); oExercise_7_3_Customer1.display(); oExercise_7_3_Customer1.oExercise_5_3_SavingsAccount.transaction(500); oExercise_7_3_Customer1.display(); // 2nd object Exercise_7_3_Customer oExercise_7_3_Customer2 = new Exercise_7_3_Customer("ABCDE", 1234567890, 1000.00); oExercise_7_3_Customer2.display(); oExercise_7_3_Customer2.oExercise_5_3_SavingsAccount.transaction(500); oExercise_7_3_Customer2.display(); // Test interface System.out.println("Comparision result: " + oExercise_7_3_Customer1.compareTo(oExercise_7_3_Customer2)); } } Wrapper ClassesWrapper classes are for creating objects that represent primitive types. They are used when an object is required, but the data is of a primitive type. The wrapper classes are in the java.lang package and are similar to the primitive type names, except they start with a capital letter: Boolean, Character,Byte, Short, Integer, Long, Float and Double. To instantiate a wrapper object: new WrapperClassName( primitive-type-value ) where the primitive-type-value MUST be the same type as what the wrapper class represents (e.g., pass an int value to Integer constructor) OR a String with the characters that represent the same type (except NOT with Character). Examples: Double doubObj = new Double( 3.45 ); Integer intObj = new Integer( "9876" ); Note: You may pass a double to the Float constructor. Wrapper Class Constants & Methods: There are many methods and constants in each wrapper class that you may look up here: http://docs.oracle.com/javase/7/docs/api/ for example, MAX_VALUE, MIN_VALUE (constants), compareTo (method) in the numeric classes. Every wrapper class (except Boolean & Character) has the following methods to convert back to primitive types:
Examples of calls:
AUTOMATIC BOXING AND UNBOXING:
/* Output: Enter the node value 10 Node1: Data: 10 Next: 5null Node2: Data: 20 Next: 5Exercise_7_4_Node@500fbfa2 Node1 is same as Node2's next: Exercise_7_4_Node@500fbfa2 */ |
Programming Languages > Java >