# Assignment 5: Classes and Objects Revisited Solved

25.00 \$

Category:
Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: . ` docx` solution files instantly, after Payment

## Description

1. 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?

1. 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();

}

1. 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:

• 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.

1. Implement a class Account. An account has
• a balance,
• 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.

1. 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.

1. 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)
1. 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.

1. This problem has several parts:
1. Write a simple Vehicle class that has fields for (at least) current speed, current direction in degrees, and owner name.

1. Add a static field to your Vehicle class for the highest Vehicle Identification Number issued, and a non-static field that holds each vehicle’s ID number.

1. 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.

1. Add two constructors to Vehicle. A no-arg constructor and one that takes an initial owner’s name. Modify the tester program from the previous step and test your design

1. 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?

1. 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.

1. 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.

1. Add a static method to Vehicle that returns the highest identification number used so far.

1. Add a toString method to Vehicle.

• H05.docx