Honors Computer Programming 1

Honors Computer Programming 1-2
Chapter 2 Programming Problems
program 1
Rectangle Intersection program The intersection method of the
Rectangle class computes the intersection of
two rectangles – that is, the rectangle that is formed by two overlapping rectangles.
Your program should declare three Rectangle variables whose names are a, b, and c.
Each rectangle should be initialized at declaration using the coordinates suggested by
the diagram.
Use the System.out.println method to display information about each rectangle.
Then use the intersection method of the Rectangle class to find and print the rectangle that is the intersection
of rectangles a and b which have an intersection as well as the intersection of rectangles a and c which have no
intersection.
The heading for the intersection method is:
public Rectangle intersection(Rectangle
secondRect
Sample output should be:
Rectangle A: java.awt.Rectangle[x=2,y=2,width=5,height=2]
Rectangle B: java.awt.Rectangle[···]
Rectangle C: java.awt.Rectangle[···]
Intersection of A and B: java.awt.Rectangle[···]
Intersection of A and C: java.awt.Rectangle[···]
program 2
Bank Account program Write a program that constructs a bank account with an initial deposit of $1000, deposits
$200, withdraws $500, withdraws another $400. After each transaction, print the balance.
Use the BankAccount class discussed in this chapter. Your BankAccountTest program should follow the directions
above. The output is shown below.
Initial balance: 1000.0
After a $200 deposit: 1200.0
After a $500 withdrawal: 700.0
After a $400 withdrawal: 300.0
program 3
Add Interest program Add a method
void addInterest (double rate)
to the BankAccount class
that adds interest at the given rate. For example, after the statements
BankAccount momsSavings = new BankAccount(1000);
momsSavings.addInterest(10); // 10% interest
are executed, the balance in momsSavings will be $1,100. Use the statements above and a call to
System.out.println to test your program. Your program will have classes BankAccount and
BankAccountTest.
program 4
Employee program Implement a class Employee. An employee has a name (a string) and a salary (a double).
Write a default constructor -- in this case it has no parameters, a constructor with two parameters
(name and salary), and methods to return the name and salary. Write a class EmployeeTest that tests the Employee
class. Sample output is shown below. The name "John Doe" and salary 0.0 are set in the constructor as default
values. These default values are not parameters.
Employee 1:
Name: John Doe
Salary: 0.0
Employee 2:
Name: Horatio Hornblower
Salary: 80000.0
program 5
Raise Salary program Enhance the Employee class (call this new class the Employee2 class) in the preceding
program by adding a method void raiseSalary (double byPercent) that
raises the employee’s salary by a certain percentage. Sample usage:
Employee2 harry = new Employee2(“Hacker, Harry”, 55000);
harry.raiseSalary(10);
// Harry gets a 10% raise
Note that when you test class Employee2 and print Harry's new salary, it should be $60,500. Your test class should
have the name Employee2Test.
program 6
Drive A Car program Implement a class Car with the following properties. A car has a certain fuel efficiency
(measured in miles/gallon) and a certain amount of fuel in the gas tank. The efficiency is
specified in the constructor -- just one parameter in the constructor, and the initial fuel level is 0. Supply a method
drive that simulates driving the car for a certain distance, reducing the fuel level in the gas tank, a method getGas
that returns the current fuel level, and a method addGas to tank up:
Car mybeemer = new Car(29);
mybeemer.addGas(20);
mybeemer.drive(100);
System.out.println (mybeemer.getGas( ));
//
//
//
//
29 mpg
tank 20 gallons
drive 100 miles
print remaining fuel
Hint: it is suggested that you make both of your instance fields of type double.
program 7
Student program Implement a class Student. For the purposes of this program, a student has a name, a total test
score -- sumScores is suggested, and a number of scores -- numScores is suggested. Supply
an appropriate constructor and methods getName( ), addTest(int score), and getAverageScore( ). To
compute the latter you will need to use the numScores and sumScores instance fields.
A class StudentTest should call the getName and getAverageScore to print the summary for a student So
the output might be a statement such as: George Bush has an average test score of 92
Make sure that the student takes at least 4 tests.
program 8
Soda Can program Implement a class SodaCan with methods getSurfaceArea( ) and getVolume( ). In
the constructor, supply the height and radius of the can. Remember the formula for surface
area of a cylinder is: SA = 2 π r 2 + 2 π rh and the volume formula is: V = π r 2 h .
Hint: your methods should return a number of type double. Test your class using a height of 4.25 inches and a
radius of 2.0 inches. In this program, declare an instance field pi of type double and set it to 3.14159 in the
class. The classes for this program are SodaCan and SodaCanTest.
Output is shown below.
Dimensions of the can:
height = 4.25
radius = 2.0
Calculated values:
surface area = 78.53975
volume = 53.40703
program 9
Roach Population program Implement a class RoachPopulation that simulates the growth of a roach
population. The constructor takes the size of the initial population . The
waitForDoubling method simulates a period in which the population doubles. The spray method simulates
spraying with insecticide, which reduces the population by 10%. The getRoaches method returns the current
number of roaches. Implement the RoachPopulation class and a test class RoachPopulationTest that simulates a
kitchen that starts out with 10 roaches. Wait, spray, and then print the roach count. Repeat three times.
Output is shown below.
After 1 wait period: 18.0 roaches
After 2 wait periods: 32.4 roaches
After 3 wait periods: 58.32 roaches