Project 2

Due date: Feb 28 Thr 6:00 pm

This project has two parts(50 points):

  1. How to use class methods(30 points)
  2. Lab disk turn in(20 points)
  3. Optional: How to solve ambiguous problems(10 points)

Use Prewritten Class Methods(30 points)

Part I (10 pts)

Your colleague is out of town for a while. Your manager asks you to finish his job. Your colleague has roughly finished a program to calculate a mortgage payment, but your manager is not satisfied with the printout. You are asked to solve it. For example, when you run the Mortgage on the command line:

>java Mortgage
The monthly payment is 696.8858922378957
Your manager hopes the printout should be more clear and meaningful, like the following:
>java Mortgage
The principle is $80000
The annual interest rate is 6.5%
The term is 15 years
Your monthly payment is $696.89

The workable code can be downloaded. The original code is as follows:

public class Mortgage {
    public static void main (String[] args){
        double payment=0;
        double principal = 80000;
        double annualInterest = .065;
        int years = 15;
        payment = calcPayment(principal, annualInterest, years);
        System.out.println("The monthly payment is " + payment);
     * calculates a monthly payment given
     * a principal, annual interest rate and term
    public static double calcPayment(double pr, double annRate, int years){
        double monthlyInt = annRate / 12;
        double monthlyPayment = (pr * monthlyInt)
                    / (1 - Math.pow(1/ (1 + monthlyInt), years * 12));
        return monthlyPayment;

Part II (10 pts)

Since the Mortgage program can be modified to an interactive program, you make a proposal to let it work as follows. The user can pass message to the program at the command line. The first message is the principle, the second message is the interest rate, the third message is the payment term.


>java Mortgage 80000 0.065 15
The principle is $80000
The annual interest rate is 6.5%
The term is 15 years
Your monthly payment is $696.89

Part III (10 pts)

When you finished the code and made it work as the above, you figure it out that if the user is not well trained and types in wrong information, you program may not print out correct information. You decide to coach the user and give specific instruction. You rework on the Mortgage program for a while. The final result is shown as follows:


>java Mortgage
Please enter the principle: $80000
Please enter the annual interest rate: 0.065
Please enter the payment term: 15
Your monthly payment is $696.89

Do you want to continue yes/no ? n

If the user types in y or yes, the program should prompt to the user as above, so the user can work on it again and again until typing n or no to quit.

Lab Disk Turn-In(20 pts)

No any subdirectory in your disk. Put the following files on your A:\.

Your Lab disk should have the following files:

  1. pts)
  6. All compiled classes files(*.class)

Solve Ambiguous Problems(10 points)

Your colleague has designed several overloaded methods. These methods will be implemented and added into a class. You are asked to test these methods. You put these methods into a class called Foo like the following and then add a main() method to test these methods.

class Foo {
    void methodOv() {
        System.out.println("no arguments");
    void methodOv(double dbl, int i ) {
        System.out.println("double & int arguments called");
    void methodOv(int i, double dbl) {
        System.out.println("int & double arguments called");
    void methodOv(double dbl, float f) {
        System.out.println("double & float arguments called");
    void methodOv(float f, double dbl) {
        System.out.println("float & double arguments called");
    public static void main(String[] args) {
        double d = 2.2;
        float f = 1.1f
        int i = 2;
        Foo fool = new Foo();
        fool.methodOv(d, i);
        fool.methodOv(i, d);
        fool.methodOv(d, f);
        fool.mehtodOv(f, d);
        fool.mehtodOv(d, d);
        fool.methodOv(f, f);
        fool.methodOv(f, i):
        fool.methodOv(i, f);
        fool.methodOv(i, i);

You think the future user may pass double or float or int type data in any order or any combination to call the methodOv(), so you designed the testing code as above in main(). When you compile the above code, you get some errors saying the reference to methodOv() is ambiguous and cannot resolve symbol method methodOv(double, double), etc.

You think your colleague's design is not perfect and propose a redesign plan. Your proposed method design can let all the calls in your main() work and solved the compile-time errors.

Understanding the primitive types converting may occur in method-call is very important. Check notes to get more info.

NOTE: The extra points you earned will be used when your score becomes lower than your performance. For example, your B minus can be promoted to B or your C minus can be promoted to C, etc.