Solved

Java programming

Posted on 2004-03-22
7
200 Views
Last Modified: 2010-03-31
if I want to modify this program to display 3 mortgage loans like 7yrs at 5.35%, 15 yrs at 5.5%, and 30 yrs at 5.75% using arrays to display mortgage payment for each loan.  

How do I start?  Just give me clues to work on.  This is very frustrating to me as much as I research it.

Here is the code.

class MortgageSetup {

     private int Price = 200000;
     //hardcoded
     private double Interest = 0.0575;
     //hardcoded
     private int MortYears = 30;
     //hardcoded

     public MortgageSetup() {
     }

     public int getPrice() {
          return Price;
     }

     public void setPrice(int num) {
          Price = num;
     }


     public double getInterest() {
          return Interest;
     }


     public void setInterest(double num) {
          Interest = num;
     }


     public int getYears() {
          return MortYears;
     }


     public void setYears(int years) {
          MortYears = years;
     }


     public double totCost() {

          return (Price * Interest) / MortYears;
     }


     public static void main(String[] args) {

          MortgageSetup a = new MortgageSetup();

          double totalMorgagePerYear = a.getPrice() * a.getInterest();
          System.out.println("You'll have to pay as the interest rate " + totalMorgagePerYear + " per year");
          int years = a.getYears();
          System.out.println("This is " + totalMorgagePerYear * years + " for " + years + " years");

          double total = a.getPrice() + (totalMorgagePerYear * years);
          System.out.println("The total is: " + total);

          a.setPrice(200000);
          a.setInterest(0.575);
          a.setYears(30);

          System.out.println("Price\tInterest\tYears\tTotal\n");

          for (int i = 30; i > 0; i--) {  //using for loop

               System.out.println(
                         a.getPrice()
                          + "\t" + a.getInterest()
                          + "\t\t" + a.getYears()
                          + "\t" + a.totCost());
               if (i % 10 == 0) {
                    System.out.println("Press enter for more");

          int x = 1;  //using while loop
              while (x <= 10) {
                System.out.println("Price\tInterest\tYears\tTotal\n");
                x = x + 1;
    }

               }
          }
     }
}

0
Comment
Question by:bumsy66
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
7 Comments
 
LVL 30

Accepted Solution

by:
Mayank S earned 250 total points
ID: 10648423
MortgageSetup a[] = new MortgageSetup[3] ;

Use for loop and array-indices to read/ write values.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10648432
for ( int i = 0 ; i < 3 ; i ++ )
{
  a[i].setPrice ( whatever ) ;
  a[i].setInterest ( whatever ) ;
  a[i].setYears ( whatever ) ;

}
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10648439
>> 7yrs at 5.35%, 15 yrs at 5.5%, and 30 yrs at 5.75% using arrays

int years[] = { 7, 15, 30 } ;
double rates[] = { 0.0535, 0.055, 0.575 } ;

for ( int i = 0 ; i < 3 ; i ++ )
{
  a[i].setPrice ( whatever ) ;
  a[i].setInterest ( rates[i] ) ;
  a[i].setYears ( years[i] ) ;

}
0
 
LVL 8

Expert Comment

by:william_jwd
ID: 10648460
try this,
public class MortgageSetup {

     private int Price;

     private double Interest;

     private int MortYears;


     public MortgageSetup(int price, double interest, int mortYears) {
             this.Price = price;
             this.Interest = interest;
             this.MortYears = mortYears;
     }

     public int getPrice() {
          return Price;
     }

     public void setPrice(int num) {
          Price = num;
     }


     public double getInterest() {
          return Interest;
     }


     public void setInterest(double num) {
          Interest = num;
     }


     public int getYears() {
          return MortYears;
     }


     public void setYears(int years) {
          MortYears = years;
     }


     public double totCost() {

          return (Price * Interest) / MortYears;
     }


     public static void main(String[] args) {

             MortgageSetup a[] = new MortgageSetup[3];
              a[0] = new MortgageSetup(1000, 0.0535, 7);
          a[1] = new MortgageSetup(1000, 0.055, 15);
          a[2] = new MortgageSetup(1000, 0.0575, 30);

         
          for (int i = 0; i < 3; i++) {  //using for loop

               System.out.println(
                         a[i].getPrice()
                          + "\t" + a[i].getInterest()
                          + "\t\t" + a[i].getYears()
                          + "\t" + a[i].totCost());

          }        
     }
}

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10860239
Please proceed with that recommendation.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question