Objects of A, B, C ================== class A { public void reply() {System.out.println("Hi!");} } class B { public void reply() {System.out.println("Hey!");} } class C { public void talkTo(A x) {System.out.println("Hi A object!"); x.reply();} public void talkTo(B x) {System.out.println("Hi B object!"); x.reply();} } public class Main { public static void main(String[] args) { A a = new A(); B b = new B(); C c = new C(); c.talkTo(a); c.talkTo(b); } } Redundant Object ================ public class Main { public static void main(String[] args) { //get values for int y, m, d from user int y=2014; int m=12; int d=31; Day d1 = new Day(y,m,d); Day d2 = new Day(y,m,d); d2 = d1.next(); //return the next day of d1 System.out.println("Next of " + d1 + " is " + d2); } } Today is a good day =================== import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s1,s2; s1 = in.next(); //type " Today is a good day." s2 = in.nextLine(); System.out.println(s1); //"Today" System.out.println(s2); //" is a good day“ in.close(); } } Next year your age (given) ================== import java.util.*; public class Main { public static void main(String[] args) { String name; int age; System.out.print("What is your name? "); System.out.print("How old are you? "); System.out.println("Hello, " + _____ + ". Next year, you will be " + ____); in.close(); } } Next year your age ================== import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String name; int age; System.out.print("What is your name? "); name = in.nextLine(); System.out.print("How old are you? "); age = in.nextInt(); System.out.println("Hello, " + name + ". Next year, you will be " + age + 1); System.out.println("Hello, " + name + ". Next year, you will be " + (age + 1)); in.close(); } } Topic03 Page 1 =============== public void advance() { if (isEndOfAMonth()) { if (month==12) { year = __________; month = _________; day = ___________; } else { } } else { } } ------------------------ public class Main { public static void main(String[] args) { Day d1 = new Day(2014, 1, 28); System.out.println(d1.toString()); //Show 28 Jan 2014 __.advance(); //Advance one day ______.next(); //Advance one day System.out.println(d1.toString()); //Show 30 Jan 2014 } } Employee and Hire day ===================== class Employee { private final String name; private double salary;//=0;//= Math.random()*10000; private final Day hireDay; public Employee(String n, double s, int year, int month, int day) { name = n; salary = s; hireDay = new Day(year,month,day); } public String getName() {return name;} public double getSalary() {return salary;} public Day getHireDay() {return hireDay;} public void raiseSalary(double percent) { double raise = salary * percent/100; salary += raise; } } public class Main { public static void main(String[] args) { Employee[] staff = new Employee[3]; staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15); staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1); staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15); for (Employee e : staff) e.raiseSalary(5); for (Employee e : staff) System.out.println( "name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" + e.getHireDay()); } } Day: More Day constructors ========================== /* public Day(int y) { // to be completed } public Day(int y, int nth_dayInYear) { // to be completed } */ public class Main { public static void main(String[] args) { Day d1; d1 = new Day(2014, 1, 28); System.out.println(d1); // 28 Jan 2014 d1 = new Day(2018); // The first day in 2018 System.out.println(d1); // 1 Jan 2018 d1 = new Day(2014, 45); // The 45th day in 2014 System.out.println(d1); // 14 Feb 2014 } } birthday, deadline ================== import java.util.*; public class Main { public static void main(String[] args) { Day birthday, deadline; birthday = new Day(2014, 1, 15); deadline = birthday; System.out.println(birthday); // 15 Jan 2014 System.out.println(deadline); // 15 Jan 2014 birthday.advance(); System.out.println(birthday); // 16 Jan 2014 System.out.println(deadline); // ________________ birthday.next(); System.out.println(birthday); // ________________ System.out.println(deadline); // ________________ birthday = birthday.next(); System.out.println(birthday); // ________________ System.out.println(deadline); // ________________ } } Topic03 Page 7 Variables and Objects - 3 tests ==================================== public class Main { public static void main(String[] args) { //testing1 // Day d1; // System.out.println(d1); //Error: The local variable d1 may not have been initialized // System.out.println(d1.toString()); //Error: The local variable d1 may not have been initialized //testing2 // Day d1; // d1=null; // System.out.println(d1); //print: null // System.out.println(d1.toString()); //Runtime exception: java.lang.NullPointerException // System.out.println(d1); //print: null // System.out.println(d1); //print: null //testing3 // Day d1=new Day(2014,1,15); // System.out.println(d1); // System.out.println(d1.toString()); } }