Solved

Java programming

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
javap bin 2 34
arguments to jar 5 26
spring jars download 1 27
mysql jsp example issue 32 33
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

770 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