- class A
{
public A() { n = 0; }
public A(int a) { n = a; }
public void f() { n++; }
public void g() { f(); n = 2 * n; f(); }
public int h() { return n; }
public void k() { System.out.println(n); }
private int n;
}
Identify the constructors, mutator functions, and accessor functions. What kind of variable is n?
- With the nonsense class from the preceding exercise, determine what the following program prints.
public static void main(String[] args) {
A a = new A();
A b = new A(2);
A c = b;
A d = new A(3);
a.f();
b.g();
c.f();
d.g();
d.k();
A e = new A(a.h()+ b.h()+ c.h());
e.k();
}
- Implement all the methods of the following class:
class Person {
private String name;
private int birthdayYear;
public Person() {
.
}
public Person(String givenName, int yearOfBirth) {
}
public String getName() {
.
}
public String changeName(String name) {
}
public int getAgeInYears(int currentYear) {
..
}
};
void main()
{
}
Write a small test program that creates and works with objects of class Person as well.
Design exercises:
- Implement a class Address. An address has
- a house number,
- a street,
- an optional apartment number,
- a city,
- a state and a
- postal code.
Supply two constructors:
- one with an apartment number
- and one without.
Supply a print function that prints the address with the street on one line and the city, state, and postal code on the next line.
Supply a method compareTo that tests whether one address comes before another when the addresses are compared by postal code.
- Implement a class Account. An account has
- a balance,
- functions to add
- and withdraw money,
- and a function to inquire the current balance.
Pass a value into a constructor to set an initial balance.
If no value is passed the initial balance should be set to $0.
Charge a $5 penalty if an attempt is made to withdraw more money than available in the account.
Enhance the Account class to compute interest on the current balance.
- Implement a class Bank. This bank has two objects
- checking
- and savings
of the type Account that was developed in the preceding exercise.
Implement four instance methods:
deposit(double amount, String account)
withdraw(double amount, String account)
transfer(double amount, String account)
printBalances()
Here the account string is S or C. For the deposit or withdrawal, it indicates which account is affected. For a transfer it indicates the account from which the money is taken; the money is automatically transferred to the other account.
- Define a class Country that stores the name of the country, its population, and its area. Using that class, write a test program that creates a few countries and stores them in an array and then prints
- The country with the largest area
- The country with the largest population
- The country with the largest population density (people per square mile)
- Write a class called Triangle that can be used to represent a triangle. It should include the following methods that return boolean values indicating if the particular property holds:
- isRight (a right triangle)
- isScalene (no two sides are the same length)
- isIsosceles (exactly two sides are the same length)
- isEquilateral (all three sides are the same length)
Write a simple tester program that creates a few triangles and asks them about their type.
- This problem has several parts:
- Write a simple Vehicle class that has fields for (at least) current speed, current direction in degrees, and owner name.
- Add a static field to your Vehicle class for the highest Vehicle Identification Number issued, and a non-static field that holds each vehicles ID number.
- Write a main method for your Vehicle class that creates a few vehicles and prints out their field values. Note that you can also write a separate tester program as well.
- Add two constructors to Vehicle. A no-arg constructor and one that takes an initial owners name. Modify the tester program from the previous step and test your design
- Make the fields in your Vehicle class private, and add accessor methods for the fields. Which fields should have methods to change them and which should not?
- Add a changeSpeed method that changes the current speed of the vehicle to a passed-in value, and a stop method that sets the speed to zero.
- Add two turn methods to Vehicle. One that takes a number of degrees to turn, and one that takes simply either a Vehicle.TURN_LEFT or a Vehicle.TURN_RIGHT constant. Define the two constants accordingly.
- Add a static method to Vehicle that returns the highest identification number used so far.
- Add a toString method to Vehicle.
Reviews
There are no reviews yet.