Question
// In rare cases a strange hexadecimal output could happen, // something like [I@2a139a55, in those cases just answer HEX // This is a working
// 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);
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started