Solved

Java programming

Posted on 2004-03-22
7
194 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:
mayankeagle 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:mayankeagle
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:mayankeagle
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:mayankeagle
ID: 10860239
Please proceed with that recommendation.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count7 challenge 12 70
noX challenge 17 77
countAbc challenge 9 50
Running Jira on Raspberry PI 2? 3 139
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now