Unit 5: Practice

Table of Contents

  1. Note on all problems
  2. Practice
    1. Point
    2. Finance (Part 1)
    3. Geometric
  3. Challenges
    1. College
    2. Finance (Part 2)

Note on all problems

If you can write JavaDoc comments, do so for all of the methods you write! Also add the @Override annotation whenever applicable.

Also, class specifications are given in loose UML diagram format.

Practice

Point

classes

methods

overloading

overriding

driver class

Point

Create a class called Point that has 2 public fields x and y (double). The class should have the following methods:

  • A default constructor which sets the Point to be at the origin
  • A 2-argument constructor
  • A getDistance(Point pt) method which returns the distance between this Point and the given Point
  • A getDistance(double x, double y) method which overloads the previous method
  • A toString method which displays the Point as an (x, y) coordinate

Do not create getters/setters (since x and y are public).

public class Point {
    // write your code here
}

Solution

TestPoint

Create a class called TestPoint which tests the Point class. In the main method, you should create 2 Point objects by using the default constructor and 2-argument constructor. Then call both getDistance methods to test them.

public class TestPoint {
    public static void main(String[] args) {
        // write your code here
    }
}

Solution

Finance (Part 1)

classes

methods

getters and setters

overloading

overriding

driver class

Account

Create a class called Account which has the following fields:

  • number (long): the account number
  • balance (double)
  • name (String): name of the person who owns the account

Implement the following methods (all are public):

  • Account()
  • Account(name: String, number: long)
  • Account(name: String, number: long, balance: double)
  • getters and setters for name, number, and balance.
    • HOWEVER, do not make a setter for number, since you don’t change your account number once the account is made.
  • deposit(amount: double): boolean (if deposit was successful, return true)
    • You should not be able to deposit a negative amount.
  • withdraw(amount: double): boolean (if withdrawal was successful, return true)
    • You should not be able to withdraw more than you have in the account.
  • transferTo(acct: Account, amount: double): boolean (if transfer was successful, return true)
    • You should not be able to transfer more than you have in the account.
    • You should not be able to transfer a negative amount.

Override the toString method such that a print statement prints the name, number, and balance.

Override the equals method such that 2 accounts that have the same account number are equal.

public class Account {
    // write your code here
}

Solution

TestAccount

Create a driver class for Account called TestAccount. In the main method, test all of the methods in Account at least once.

public class TestAccount {
    public static void main(String[] args) {
        // write your code here
    }
}

Solution

Geometric

classes

inheritance

methods

getters and setters

driver class

Rectangle

Create a class called Rectangle which has the following specifications.

Fields (all private):

  • length: double
  • width: double

Methods (all public):

  • Rectangle()
  • Rectangle(length: double, width: double)
  • Getters and setters for length and width
  • getArea(): double
  • getPerimeter(): double

Override:

  • toString so that it displays the length and width of the Rectangle
  • equals so that 2 Rectangles are equal if they have the same area
public class Rectangle {
    // write your code here
}

Solution

RectangleInherit

Create a class called RectangleInherit which extends GeometricObject. (You can view the code for GeometricObject here). It also has the following specifications.

Fields (all private):

  • length: double
  • width: double

Methods (all public):

  • All the methods that Rectangle has, in addition to a 3rd constructor (below)
  • RectangleInherit(length: double, width: double, x: double, y: double, color: String, filled: boolean)

Override:

  • Same methods as Rectangle
// import GeometricObject if needed

public class RectangleInherit extends GeometricObject {
    // write your code here
}

Solution

TestRectangle

Create a driver class called TestRectangle which tests every method in the Rectangle and RectangleInherit classes.

public class TestRectangle {
    public static void main(String[] args) {
        // write your code here
    }
}

Solution

Challenges

College

classes

inheritance

aggregation

methods

driver classes

polymorphism

UML diagrams

Modified from Exercise 11.2 in Introduction to Java Programming (Comprehensive), 10th ed. by Y. Daniel Liang

Create the following classes:

  • Person
  • Employee extends Person
  • Faculty extends Employee
  • Staff extends Employee
  • MyDate
  • Student extends Person

Your classes should follow the specifications in the UML diagrams.

TestCollegeRoles

Create a class called TestCollegeRoles to test the classes you wrote. In the main method, create a Person array with Person, Student, Employee, Faculty, and Staff objects. Then print all of them.

Template Code

public class Person {
    // write your code here
}
public class Employee extends Person {
    // write your code here
}
public class Faculty extends Employee {
    // write your code here
}
public class Staff extends Employee {
    // write your code here
}
public class MyDate {
    // write your code here
}
public class Student extends Person {
    // write your code here
}
public class TestCollegeRoles {
    public static void main(String[] args) {
        // write your code here
    }
}

Solutions

Finance (Part 2)

classes

inheritance

methods

overriding

driver classes

polymorphism

CreditCardAccount

Create a class called CreditCardAccount which extends Account (see Finance (Part 1)).

Fields (all private):

  • apr: double, the annual percentage rate (APR) for interest, as a decimal
  • creditLimit: double, the credit limit for the card

Wait, what’s APR?

It’s an interest rate used to calculate the monthly minimum payment. (See that method below.)

Wait, what’s a credit limit?

Credit cards allow you to withdraw money even if you’d go into debt as a result of the withdrawal. The credit limit allows you to do this up to an extent - you can withdraw any amount less than or equal to balance + creditLimit.

Methods (all public):

  • CreditCardAccount()
  • CreditCardAccount(name: String, number: long, balance: double)
  • CreditCardAccount(name: String, number: long, balance: double, apr: double, creditLimit: double)
  • getApr(): double
  • setApr(): void
  • getCreditLimit(): double
  • setCreditLimit(): void
  • withdraw(amount: double): boolean
    • Overrides superclass method and lets you withdraw more than you have in your balance, as long as it is not over the credit limit.
    • Returns true if the withdrawal was successful.
  • calculatePayment(): double
    • Returns monthly minimum payment based on current balance
    • How is this calculated? If your balance is positive, you don’t have to pay anything. Otherwise, your monthly payment is whichever is less: $20, or (apr / 12) * (-balance)

Override:

  • toString so that it includes name, number, balance, apr, creditLimit, and monthly minimum payment
// import Account if needed

public class CreditCardAccount extends Account {
    // write your code here
}

Solution

SavingsAccount

Create a class called SavingsAccount which extends Account.

Fields (all private):

  • apr: double, annual percentage rate (APR) for interest as a decimal

Methods (all public):

  • SavingsAccount()
  • SavingsAccount(number: long, balance: double, apr: double),
  • getApr(): double
  • setApr(): void
  • calculateInterest(): double
    • Returns annual interest earned based on current balance and APR
    • How is this calculated? It’s balance * apr

Override:

  • toString: String, includes name, number, balance, apr, and annual interest earned
// import Account if needed

public class SavingsAccount extends Account {
    // write your code here
}

Solution

TestAccounts

Create a driver program called TestAccounts to test the CreditCardAccount and SavingsAccount classes.

In the main method, create an array of Account objects and print the status of each account after depositing and withdrawing a certain amount.

public class TestAccounts {
    public static void main(String[] args) {
        // write your code here
    }
}

Solution