Solved

Java programming

Posted on 2004-03-22
7
199 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
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 105
eclipse shortcuts 9 63
expectj telnet failing 5 45
Java Restore security prompts not working 10 67
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

828 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