PIC10A Lecture 10 Ex

Q1)
(a) Consider the following class Coord that represents a coordinate in 2-dim space.
public class Coord {
    int x;
    int y;
    
    public Coord(int x, int y) {
        this.x = x;
        this.y = y;
    }  
}
What is the output of the following program?
public class CoordTest {
    public static void main(String[] args) {
        Coord c = new Coord(5,10);
        System.out.println(c);
    }
}
(b) We update the Coord.java as:
public class Coord {
    int x;
    int y;
    
    public Coord(int x, int y) {
        this.x = x;
        this.y = y;
    }  
    
    public String toString() {
        // return a string in (x,y) format
        return "(" + x + "," + y + ")";
    }
}
What is the output of CoordTest ? Explain your answer.

Q2)
What is wrong with the following program?
Correct the program without changing the name of the arguments.
public class HotelRoom {
    private int roomNumber;
    private boolean occupy;
    
    public HotelRoom(int roomNumber, boolean occupy) {
        roomNumber = roomNumber;
        occupy = occupy;
    }
    
    public setOccupy(boolean occupy) {
        occupy = occupy;
    }
}


Q3
What is the output of the following program? Explain your answer
public class ConstructorTest {
    private int a, b, c;
    private String s;
    
    ConstructorTest(int a, int b, String s) {
        this.a = a;
        this.b = b;
        this.s = s;
        c = 0;
    }
            
    ConstructorTest(int a, int b) {
        this.a = a;
        this.b = b;
        c = a + b;
        s = a + " + " + b + " = " + c;
    }
    
    ConstructorTest(int x) {
        this(x,x);
    }
    
    ConstructorTest(String s) {
        this(1,2,s.toUpperCase());
    }
    
    public String toString() {
        return "a: " + a + "\n" + "b: " + b + "\n" + "c: " +  c + "\n" + "String: " + s ;
    }
    
    public static void main(String[] args) {
        ConstructorTest test1 = new ConstructorTest(2,5, "Hello");
        ConstructorTest test2 = new ConstructorTest(3,9);
        ConstructorTest test3 = new ConstructorTest(12);
        ConstructorTest test4 = new ConstructorTest("Java");
        System.out.println(test1);
        System.out.println();
        System.out.println(test2);
        System.out.println();
        System.out.println(test3);
        System.out.println();
        System.out.println(test4);
        System.out.println();
        
    }
}
Q4)
What is the output of the following program? Explain your answer.
public class Pass {
    private int x;
    
    public Pass(int x) {
        this.x = x;
    }
    
    public void increase() {
        x++;
    }
    
    public void increase(int x) {
        x++;
    }
    
    public void increase(Pass p) {
        p.x++;
    }
    
    public String toString() {
        return "" + x; // the space "" is used to make sure that it returns a String
    }
    
    public static void main(String[] args) {
        Pass p = new Pass(5);
        System.out.println("Output 1: " + p);
       
        p.increase();
        System.out.println("Output 2: " + p);
        
        p.increase(5);
        System.out.println("Output 3: " + p);
        
        p.increase(p.x);
        System.out.println("Output 4: " + p);
        
        Pass q = new Pass(100);
        q.increase(p);
        System.out.println("Output 5: " + p);
    }
}
    
Q5)
What is the output of the following program? Explain your answer.
public class OverloadTest {
    public void method() {
	System.out.println("no argument");
    }
    
    public int method(int a) {
	System.out.println("argument list : int");
	return 1;
    }
    
    public int method(String  s) {
	System.out.println("argument list : String");
	return 1;
    }
    
    public char method(int a, String b) {
	System.out.println("argument list : int, string");
	return 'a';
    }
    
    public static void main(String[] args) {
        OverloadTest test = new OverloadTest();
	test.method();
	test.method(1);
	test.method("hi");
	test.method(1,"Java");
    }
} 
Q6)
How to create an object of the following class? (the class has no explicit constructor.)
public class NoConstructor {
       int a = 0;
}
Q7)
What is the output of the following program? Explain your anwser.
public class ClassA {
    int x, y, z;
    
    public ClassA(int x, int y, int z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }
    
    public void square() {
        x = x*x;
        y = y*y;
        z = z*z;
    }
    
    public int sum() {
        return x + y + z;
    }
    
    public void add(ClassA a) {
          x+=a.x;
          y+=a.y;
          z+=a.z;
    }
    
    public ClassA change(ClassA a) {
        a = new ClassA(x + a.x, y + a.y, z + a.z);
        return a;
    }
    
    public String toString() {
        return x + "," + y + "," + z;
    }
    
    public static void main(String[] args) {
        ClassA a1 = new ClassA(1,2,3);
        ClassA a2 = new ClassA(0,1,2);
        System.out.println("Output 1: " + a1);
        
        a1.square();
        System.out.println("Output 2: " + a1);
        
        System.out.println("Output 3: " + a1.sum());
        
        a1.add(a2);
        System.out.println("Output 4: " + a1);
        
        System.out.println("Output 5: " + a2);
        
        a1 = a1.change(a2);
        System.out.println("Output 6: " + a1);
        System.out.println("Output 7: " + a2);
        
    }
}