Chapter 2 Homework Answers
Chapter 2 – Using Objects
• R2.6 What is wrong with the following sequence of statements?
int mystery = 1;
mystery = mystery + 1;
int mystery = 1 - 2 * mystery;
// ERROR
int mystery = 1 - 2 * mystery;
// TO CORRECT
mystery = 1 - 2 * mystery;
•• R2.18 Find the errors in the following statements:
a. Rectangle r = (5, 10, 15, 20);
// ERROR
Rectangle r = (5, 10, 15, 20);
// TO CORRECT
Rectangle r = new Rectangle (5, 10, 15, 20);
b. double width = Rectangle(5, 10, 15, 20).getWidth();
//ERROR
double width = Rectangle(5, 10, 15, 20).getWidth();
// TO CORRECT
double width = new Rectangle(5, 10, 15, 20).getWidth();
c. Rectangle r;
r.translate(15, 25);
// ERROR
Rectangle r;
r.translate(15, 25);
// TO CORRECT
Rectangle r = new Rectangle();
r.translate(15, 25);
d. r = new Rectangle();
r.translate("far, far away!");
// ERROR
r = new Rectangle();
r.translate(15, 25);
// TO CORRECT
Rectangle r = new Rectangle();
r.translate(15, 25);
•• R2.20 Consult the API documentation to find methods for
• Concatenating two strings, that is, making a string consisting of the first string, followed by the second string.
String first = "eyad ";
String secend = "ayman";
String third = first.concat(secend);
System.out.println(third);
/*
Class : String
return type : String
method name : concat
types of the arguments : String
*/
• Removing leading and trailing white space of a string.
String remove = " This is Trim Method ! ";
System.out.println(remove.trim());
/*
Class : String
return type : String
method name : trim
types of the arguments : no arguments
*/
• Converting a rectangle to a string.
Rectangle box = new Rectangle( );
System.out.println(box.toString( ));
/*
Class : Rectangle
return type : String
method name : toString
types of the arguments : no arguments
*/
• Computing the smallest rectangle that contains two given rectangles.
Rectangle box1 = new Rectangle(5,5,20,30);
Rectangle box2 = new Rectangle(20,30,20,30);
System.out.println(box1.union(box2));
/*
Class : Rectangle
return type : Rectangle
method name : union
types of the arguments : Rectangle
*/
• Returning a random floating-point number.
Random rNumber = new Random();
System.out.println(rNumber.nextFloat());
/*
Class : Random
return type : float
method name : nextFloat
types of the arguments : no arguments
*/
•• E2.9 In the Java library, a color is specified by its red, green, and blue components between 0 and 255 (see Table 4 on page 66). Write a program BrighterDemo that constructs a Color object with red, green, and blue values of 50, 100, and 150. Then apply the brighter method of the Color class and print the red, green, and blue values of the resulting color. (You won’t actually see the color—see Exercise E2.10 on how to display the color.)
•• Graphics E2.10 Repeat Exercise E2.9, but place your code into the following class. Then the color
will be displayed.
import java.awt.Color;
import javax.swing.JFrame;
public class BrighterDemo
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(200, 200);
Color myColor = . . .;
frame.getContentPane().setBackground(myColor);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Answer :
import java.awt.Color;
import javax.swing.JFrame;
public class BrighterDemo {
public static void main(String[] args) {
// Constructs a Color object
Color newColor = new Color(50, 100, 150);
/**
* E2.9
* Print the red, green, and blue values
* of the newColor after apply the brighter method .
*/
System.out.println(newColor.brighter());
// E2.10
JFrame frame = new JFrame();
frame.setSize(200, 200);
// Set newColor after apply the brighter method in myColor Object .
Color myColor = newColor.brighter();
// Show myColor in frame background
frame.getContentPane().setBackground(myColor);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
••• P2.4 The intersection method computes the intersection of two rectangles—that is, the rectangle that would be formed by two overlapping rectangles if they were drawn, as shown .
You call this method as follows:
Rectangle r3 = r1.intersection(r2);
Write a program IntersectionPrinter that constructs two rectangle objects, prints them as described in Exercise P2.1, and then prints the rectangle object that describes the intersection. Then the program should print the result of the intersection method when the rectangles do not overlap. Add a comment to your program that explains how you can tell whether the resulting rectangle is empty.
Answer :
import java.awt.Rectangle;
public class IntersectionPrinter {
public static void main(String[] args) {
Rectangle r1 = new Rectangle(0, 0, 20, 30);
System.out.println(r1);
Rectangle r2 = new Rectangle(10, 15, 30, 20);
System.out.println(r2);
Rectangle r3 = r1.intersection(r2);
System.out.println(r3);
/**
* I will write comment and code to solve this problem
* after this comment you will see code by using if statement
* if :
* sum of X + Width for r1 greater than X for r2
* or
* sum of Y + Height for r1 greater than Y for r2
* There is an intersection
* else if :
* sum of X + Width for r1 less than X for r2
* or
* sum of Y + Height for r1 less than Y for r2
* There is no intersection
* else :
* sum of X + Width for r1 equal X for r2
* or
* sum of Y + Height for r1 equal Y for r2
* Two rectangles touch each other
*/
// p1 = X + Width for r1
double p1 = r1.getX() + r1.getWidth();
// p2 = y + height for r1
double p2 = r1.getY() + r1.getHeight();
// g1 = X for r2
double g1 = r2.getX();
// g2 = Y for r2
double g2 = r2.getY();
// if statemnet to test intersection
if (p1 > g1 || p2 > g2) {
System.out.println("There is an Intersection ");
} else if (p1 < g1 || p2 < g2) {
System.out.println("There is No Intersection");
} else if (p1 == g1 || p2 == g2) {
System.out.println(" Two Rectangles touch each other.");
}
else {
System.out.println("Error !");
}
}
}
•• Graphics E2.10 Repeat Exercise E2.9, but place your code into the following class. Then the color
will be displayed.
import java.awt.Color;
import javax.swing.JFrame;
public class BrighterDemo
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(200, 200);
Color myColor = . . .;
frame.getContentPane().setBackground(myColor);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Answer :
import java.awt.Color;
import javax.swing.JFrame;
public class BrighterDemo {
public static void main(String[] args) {
// Constructs a Color object
Color newColor = new Color(50, 100, 150);
/**
* E2.9
* Print the red, green, and blue values
* of the newColor after apply the brighter method .
*/
System.out.println(newColor.brighter());
// E2.10
JFrame frame = new JFrame();
frame.setSize(200, 200);
// Set newColor after apply the brighter method in myColor Object .
Color myColor = newColor.brighter();
// Show myColor in frame background
frame.getContentPane().setBackground(myColor);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
••• P2.4 The intersection method computes the intersection of two rectangles—that is, the rectangle that would be formed by two overlapping rectangles if they were drawn, as shown .
You call this method as follows:
Rectangle r3 = r1.intersection(r2);
Write a program IntersectionPrinter that constructs two rectangle objects, prints them as described in Exercise P2.1, and then prints the rectangle object that describes the intersection. Then the program should print the result of the intersection method when the rectangles do not overlap. Add a comment to your program that explains how you can tell whether the resulting rectangle is empty.
Answer :
import java.awt.Rectangle;
public class IntersectionPrinter {
public static void main(String[] args) {
Rectangle r1 = new Rectangle(0, 0, 20, 30);
System.out.println(r1);
Rectangle r2 = new Rectangle(10, 15, 30, 20);
System.out.println(r2);
Rectangle r3 = r1.intersection(r2);
System.out.println(r3);
/**
* I will write comment and code to solve this problem
* after this comment you will see code by using if statement
* if :
* sum of X + Width for r1 greater than X for r2
* or
* sum of Y + Height for r1 greater than Y for r2
* There is an intersection
* else if :
* sum of X + Width for r1 less than X for r2
* or
* sum of Y + Height for r1 less than Y for r2
* There is no intersection
* else :
* sum of X + Width for r1 equal X for r2
* or
* sum of Y + Height for r1 equal Y for r2
* Two rectangles touch each other
*/
// p1 = X + Width for r1
double p1 = r1.getX() + r1.getWidth();
// p2 = y + height for r1
double p2 = r1.getY() + r1.getHeight();
// g1 = X for r2
double g1 = r2.getX();
// g2 = Y for r2
double g2 = r2.getY();
// if statemnet to test intersection
if (p1 > g1 || p2 > g2) {
System.out.println("There is an Intersection ");
} else if (p1 < g1 || p2 < g2) {
System.out.println("There is No Intersection");
} else if (p1 == g1 || p2 == g2) {
System.out.println(" Two Rectangles touch each other.");
}
else {
System.out.println("Error !");
}
}
}
End of Answers

Comments
Post a Comment