Lecture12

Lecture12 - Q1) Consider the following code: (15 marks)...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Q1) Consider the following code: (15 marks) public class D { public static void f (C c, int y) { System.out.println("Line #2 is " + c.getX()); c.setX(y); y++; System.out.println("Line #3 is " + c.getX()); c c = new C(); c.setX(y+2); System.out.println("Line #4 is " + c.getX()); c } public static void main (String[] args) { int z = 4; C c = new C(); c.setX(3); System.out.println("Line #1 is " + c.getX()); f(c, z); System.out.println("Line #5 is " + c.getX()); System.out.println("Line #6 is " + z); } } What would be printed if this program were run? public class C { private int x; public int getX(){ return x; } public void setX(int z){ x = z; } } x: 0 x: 0 Solution: Line#1: 3 Line#2: 3 Line#3: 4 Line#4: 7 Line#5: 4 Line#6: 4
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Q2) What would be printed by the following program? public class Test { String instVar; static String statVar = "A"; Test() { System.out.println("test: " + instVar + statVar); t1 instVar = "B"; statVar = "C"; } public static void main(String[] args) { t2 Test t1 = new Test(); t1.instVar = "D"; t1.statVar = "E"; Test t2 = new Test(); System.out.println("inst: " + t1.instVar + t2.instVar); System.out.println("stat: " + t1.statVar + t2.statVar); }} Answer: test: nullA test: nullE inst: DB stat: CC instVar: null instVar: null
Background image of page 2
3 Q3) Here is an incomplete declaration for the Car class. (10marks) public class Car { private String make; private int year; // Constructor // Initializes the instance variables make and year with the // specified arguments public Car(String m, int y) { // (a) Write the constructor code here make = m; year = y; }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 // Returns a string containing the make and year of this Car // object. The make and year should be separated by a comma. // For example, for a car whose make is “Honda” and year “2007”, // this method should return the string “Honda, 2007” public String toString() { // (b) Write the toString method code here return (make + "," + year); } } a) Write the code for the constructor of the Car class. b) Write the code for the toString method of the Car class.
Background image of page 4
5 Q4) Objects of type P (shown below) can be compared to each other based on the values of their x and y instance variables. (10 marks) Two P objects are considered equal if they have the same values for the x and y instance variables. If the x values differ, the object with the larger x value is the bigger of the two. (The y values do not matter in this case.) If the x values are the same and the y values differ, the object with the larger y value is the bigger of the two. Using these requirements, fill in the code for the compareTo method below: public class P { int x; //Always a non-negative single digit number int y; //Always a number between 0 and 99 //. ....not all code shown /** Returns 0 if this equals other. * Returns a positive integer if this > other
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/12/2010 for the course COE 318 taught by Professor Ken during the Spring '08 term at Ryerson.

Page1 / 35

Lecture12 - Q1) Consider the following code: (15 marks)...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online