Casual Discussion, Warm-up Questions, and Lecture Demo Exercises

advertisement
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: 3‐Mar‐2016 1
Lecture Exercise 07
CS2312 Problem Solving and Programming | www.cs.cityu.edu.hk/~helena
Q4 Which of the following statements, if inserted into the program below, will output 200? (4 marks)
class A { private int x; public A() {x=100;} public void m1() { System.out.println(x);} } class B extends A { private int y; public B() {y=200;} public void m1() {System.out.println(y);} } public static void main(String[] args) { A i = new A(); i.m1(); B j = new B(); j.m1(); ((A)j).m1(); ((B)i).m1(); } Use any 2 statements in the above program, explain what is up-casting, what is down-casting. (4 marks)
[Ref: Page 8 of Topic 4.]
Q5 Explain why the commented statements in line X1, X2 are wrong.
class A { public int x1; public static int x2; public void m1() { m2(); } public static void m2() { //x1+=100; //<== this is invalid. [line X1] x2+=100; //m1(); //<== invalid [line X2] } } public static void main(String[] args) { (new A()).m1(); (new A()).m1(); } Last modified: 3‐Mar‐2016 2
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: 3‐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 Member { private String name; private Role role; .. ______________setRole(_____________) { } }
public class Team{ private Member[] allMembers; } // methods: public Member getLeader() {..} // etc.. public Member findMember(String name) { } public void changeLeader(String newLeaderName) { } Q9. [Lab06-Q6]
Search a member within some teams
How to read team data?
public static void main(String [] args) { .. //Prompt for filepathname Team t = new Team(filepathname); .. System.out.print("Enter new leader: "); System.out.println("Result: " }
Please input the file pathname of each team: c:\m3.txt c:\m3a.txt c:\m3b.txt
Listing of teams:
[Team 1] 4 members: Ann Daisy[Leader] Jack Joey[x]
public static void main(String [] args) [Team 2] 5 members: Helena[x] Peter[x] Mary Paul Jacky[Leader]
{ [Team 3] 3 members: Amy Jane[Leader] Johnson[x]
Scanner in = new Scanner(System.in); Enter a name for searching: Helena
Result: Helena is a disappeared member in Team 2
String filepathnames; System.out.print("Please input the file pathname of each team: "); filepathnames = in.nextLine(); Scanner infnames = ____________________________ ArrayList <Team> teams = ______________________________ while (infnames.hasNext()) { teams.add(______________________); } infnames.close(); System.out.println("\nListing of teams: "); int seq=1; for (Team t: teams) System.out.printf("[Team %d] %d members: %s\n", seq++, t.getMemberCount(), t.getStringOfAllMembers()); System.out.print("\nEnter a name for searching: "); .. Last modified: 3‐Mar‐2016 } 4
Download