Lecture Exercise 07 CS2312 Problem Solving and Programming | www.cs.cityu.edu.hk/~helena Casual Discussion, Warm-up Questions, and Lecture Demo Exercises Q1. Java does automatic garbage collection. - If a block of memory is no longer needed, it will eventually be recycled. - JAVA identify which objects are in use (being referenced) and which are not, and delete the unused ones. - There are no guarantees regarding when GC will run. It is upto GC's discretion. Your task: [Refer to Q7 in Lec06_Ex] Explain why the error of program 1 is removed if we rewrite it as follow. //Program 1 public static void main(String[] args) { Object[] arr1 = new Object[10000000]; Object[] arr = arr1; for (int i=0;i<100;i++) { arr[0]=new Object[10000000]; arr=(Object[])arr[0]; } } //Program 1 Rewritten public static void main(String[] args) { Object[] arr1 = new Object[10000000]; Object[] arr = new Object[10000000]; for (int i=0;i<100;i++) { arr[0]=new Object[10000000]; arr=(Object[])arr[0]; } } Q2. The code on the right can show the next day of a given day. (Assume that our very familiar Day class is available.) Question: Is any part of the code redundant ? (ie. irrelevant and should be removed) ..//get values for int y, m, d from user Day d1 = new Day(y,m,d); Day d2 = new Day(); d2 = d1.next(); //return the next day of d1 System.out.println("Next of " + d1 + " is " + d2); Q3. Line 2 of the following changes the name of an employee. By drawing, illustrate what line 2 does. /* line 1*/ Employee e = new Employee( "Carl Cracker", 75000, 1987, 12, 15); /* line 2*/ e.setName("Helena"); Last modified: 4‐Mar‐2016 1 Lecture Exercise 07 CS2312 Problem Solving and Programming | www.cs.cityu.edu.hk/~helena Q6. What is the output of the following program? class X { private int xValue; private X xChild; public X(int xValue, X xChild) { this.xValue=xValue; this.xChild = xChild; } public String toString() { return xChild+" "+xValue; } Output: } public class Tester { public static void main(String[] args) { X obj = new X(99, new X(88, new X(77, null))); System.out.println(obj); } } Q7. [Lab06-Q2] Refer to the subclasses of Role in Lab06Q2, each subclass object has no instance fields to keep. Therefore we do not need to maintain multiple objects. We can thus use the Singleton Pattern for each of these subclasses so that each have only one instance. public class RLeader implements Role { public String genTeamContactMsg(Team team) {..} } public String getNameAndRole(Member member) {..} public class RNormalMember implements Role { public String genTeamContactMsg(Team team) {..} } public String getNameAndRole(Member member) {..} public class Team{ private Member[] allMembers; Last modified: 4‐Mar‐2016 public Team(String filepathname) throws FileNotFoundException { ..//setup allMembers and open the file for reading } inFile.close(); } .. for (int i=0;i<tot;i++) { String n = inFile.next(); char roleType = inFile.next().charAt(0); Role r=null; if (roleType=='l') r = new RLeader(); else /*roleType=='n'*/ r = new RNormalMember(); allMembers[i]=new Member(n,r); } 3 Lecture Exercise 07 CS2312 Problem Solving and Programming | www.cs.cityu.edu.hk/~helena Q8. [Lab06-Q4] Please input the file pathname: m3.txt There are 4 members in the team: Ann Daisy[Leader] Jack Joey[x] Change of the leader - How to solve? Enter new leader: Jack Result: Ann Daisy Jack[Leader] Joey[x] Member.java : setRole() Team.java : findMember(String name) changeLeader(String newLeaderName) Main.java : Modify main() public class Team{ private Member[] allMembers; } // methods: public Member getLeader() {..} // etc.. public Member findMember(String name) { public class Member { private String name; private Role role; .. ______________setRole(_____________) { } } } public void changeLeader(String newLeaderName) { } public static void main(String [] args) { .. //Prompt for filepathname Team t = new Team(filepathname); .. System.out.print("Enter new leader: "); System.out.println("Result: " } Q9. [Lab06-Q6] Search a member within some teams How to read team data? Last modified: 4‐Mar‐2016 4