// In rare cases a strange hexadecimal output could happen, // something like [I
ID: 3733010 • Letter: #
Question
// In rare cases a strange hexadecimal output could happen,
// something like [I@2a139a55, in those cases just answer HEX
// This is a working program, indicate output for each (20) numbered line
int i = 3; // initialize your counter for this version of exam
int[] x = { 23, 8, 1, 15, 42, 8};
int[] w = new int[10];
System.out.println( i ); // 1.
System.out.println( x[2] ); // 2.
System.out.println( x[i]+2 ); // 3.
System.out.println( x[i+2] ); // 4.
System.out.println( x[i+2]); // 5.
System.out.println( w[i] ); // 6.
System.out.println( w[x[i]%10] ); // 7.
System.out.println( x[w[i]%10] ); // 8.
System.out.println( x[i/2] ); // 9.
System.out.println( x[i]/2 ); // 10.
System.out.println( x ); // 11.
System.out.println(Arrays.toString(x)); // 12.
// The following will take some time to figure out!!!
for (i=0; i<10; i++)
w[i]=(int)Math.min(x[i%6],x[(i+1)%6]);
System.out.println(i); // 13.
System.out.println(Arrays.toString(w)); // 14.
System.out.println(Arrays.toString(x)); // 15.
// Trace what happens in the array Mystery method below
int[] aaa = {0, 1, 2, 3, 4};
arrayMystery(aaa);
System.out.println(Arrays.toString(aaa)); // 16.
System.out.println(arrayMystery(aaa)); // 17.
System.out.println(Arrays.toString(aaa)); // 18.
System.out.println(arrayMystery(aaa)); // 19.
System.out.println(Arrays.toString(aaa)); // 20.
// used for #16-20
public static int arrayMystery(int[] list) {
for (int i = 0; i < list.length; i++) {
list[i] = i * list[i];
}
return list[list.length/2];
}
// NEW PROGRAM (Chapter 8)
// 10 pts per problem (21-30), even though some are very easy, others tough!
public static void main(String[] args) {
// When we're done here, the code below runs without errors.
// Attached is the Rectangle Class from Practice-IT!
Rectangle rect1 = new Rectangle(10, 10, 20, 30);
Rectangle rect2 = new Rectangle(5, 5, 5, 5);
// 21. Although we're NOT going to make a Graphic here, DO SKETCH
// these two rectangles rect1 and rect2 as they might appear
// on DrawingPanel, or just your mental concept to help with below
// Write expected output for the following
System.out.println(rect1); // 22.
System.out.println(rect2); // 23.
System.out.println(rect1.getWidth()); // 24.
System.out.println(rect1.getHeight()); // 25.
System.out.println(rect1.contains(0,0)); // 26.
// 27. On attached, write default constructor setting all fields to (0,0,0,0)
Rectangle rect0 = new Rectangle();
// 28. On attached paper, write mutators to allow changing width as shown below:
rect0.setWidth(1);
rect0.setHeight(2);
// 29. Also write a mutator to allow setting height of a Rectangle object.
// 30. Write a boolean touches method in the Rectangle Class that tells us if
// two rectangles touch each other, meaning they overlap at any point(s)
System.out.println( rect0.touches(rect1) ); // false
System.out.println( rect1.touches(rect2) ); // true
}
}
// A solution that works for PRACTICE-IT Rectangle Exercise
public class Rectangle {
// Fields
private Point p;
private int width;
private int height;
// Constructs a new rectangle whose top-left corner is specified by
// the given coordinates and with the given width and height.
public Rectangle(int x, int y, int w, int h) {
p = new Point(x,y);
width = w;
height = h;
}
// ACCESSORS
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public int getX() {
return p.x;
}
public int getY() {
return p.y;
}
// Returns a string similar to "Rectangle[x=42,y=42,width=42,height=42]".
public String toString() {
return "Rectangle[x="+p.x+",y="+p.y+",width="+width+",height="+height+ "]";
}
// from Practice-IT Exercise solutions
// Determines if a Point is within this Rectangle
public boolean contains(int xTest, int yTest) {
return (xTest>=p.x && xTest<=p.x+width && yTest<=p.y+height && yTest>=p.y);
}
public boolean contains(Point other) {
return this.contains(other.x,other.y);
}
Explanation / Answer
1. Prints 3, as i =3
2. Prints 1, as x[2] = 1
3. Prints 17, as x[3]+2 = 15 + 2 = 17
4. Prints 8, as x[3+2] = x[5] = 8
5. Prints 8, as x[3+2] = x[5] = 8
6. Prints 0, as w[3] is 0, w is not being initialized
7. Prints 0, as w[5] is 0, w is not being initialized
8. Prints 23, as x[0%10] = x[0] = 23
9. Prints 8, as x[3/2] = x[1] = 8
10. Prints 8, x[3]/2 = 15/2 = 7
11. Prints HEX , printing a prints address
12. Prints [23, 8, 1, 15, 42, 8], Arrays.toString(x) prints contents of the array
13. Prints 10, as i is incremented to 10 after the for loop ends
14. Prints [8, 1, 1, 15, 8, 8, 8, 1, 1, 15], content of array w
15. Prints [23, 8, 1, 15, 42, 8], content of array x
16. Prints [0, 1, 4, 9, 16], each element of array is changed
aaa = [0,1,2,3,4]
after method calling-
[0*0, 1*1, 2*2, 3*3, 4*4] =[0, 1, 4, 9, 16]
17. Prints 8, returned value from arraymystery function -
aaa will become [0*0, 1*1, 4*2, 9*3, 16*4] = [0, 1, 8, 27, 64]
returned value - list[5/2] = list[2] = 8
18. Prints [0, 1, 8, 27, 64], content of aaa
19. Prints 16, returned value from arraymystery function -
aaa will become [0*0, 1*1, 8*2, 27*3, 64*4] = [0, 1, 16, 81, 256]
returned value - list[5/2] = list[2] = 16
20. Prints [0, 1, 16, 81, 256], content of aaa
22. Prints Rectangle[x=10,y=10,width=20,height=30], calls toString method of Rectangle class
23. Prints Rectangle[x=5,y=5,width=5,height=5], calls toString method of Rectangle class
24. Prints 20 , width of rect1
25. Prints 30, height of rect1
26. Print false
27.
public Rectangle() {
p=new Point(0,0);
width=0;
height=0;
}
28.
public void setWidth(int width) {
this.width = width;
}
29.
public void setHeight(int height) {
this.height = height;
}
30.
public boolean touches(Rectangle rect1) {
// Check if x,y points of rect1 contains this rectangle's point
// or if x,y points of this rectangle contains rect1's point
if(this.contains(rect1.getX(),rect1.getY()) || rect1.contains(this.getX(),this.getY())) {
return true;
}
return false;
}
Prints false
Prints true
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.