Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Java programming

Posted on 2004-03-22
7
Medium Priority
?
204 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 1000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

704 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