Java array help

problem using array

Page 1 of 1

1 Replies - 676 Views - Last Post: 09 July 2009 - 05:19 PM Rate Topic: -----

#1 Kelela  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-July 09

Java array help

Posted 09 July 2009 - 05:05 PM

I am new to Java. I am trying to create an amortization printout to the screen based on user input of loan amount and an array for the term and rate. I am so close, but I have messed something up! And I do not have a clue what. This is for an assignment so I really want to understand what I am doing wrong. The assignment is to write the program in Java (without a GUI) have it calculate the payment amount for 3 mortgage loans (I am good to this point) The program should require two user input values, 1 for the loan amount one as an indicator to display the balance and interest paid or not. That is all working, but the actual output for the loan balances and interest paid is $0.00 rather than the correct amounts.

import java.util.Scanner;
import java.text.*;



import java.util.Scanner;
import java.text.*;

public class AmortTest
{

  public static void main(String[] args)
	{

		Loan loan = new Loan();
		int[]term = {84, 180, 360};
		  double[] rate = {.0535, .055, .0575};
		  double monthlyPayment = 0;
		  double monthlyInt = 0;



	  NumberFormat nf = NumberFormat.getInstance();//sets number format for term to maintain spacing
	  nf.setMaximumFractionDigits(2);
	  nf.setMinimumIntegerDigits(2);

	  NumberFormat currency = new DecimalFormat("\u00A4 #.00");//sets number format for currency

	  NumberFormat rt = NumberFormat.getInstance();//sets number format for rate
	  rt.setMinimumFractionDigits(4);

	  //Prints out Title
			  System.out.println("\t*********************************");
			  System.out.println("\t**							 **");
			  System.out.println("\t** Mortgage Payment Calculator **");
			  System.out.println("\t**							 **");
			  System.out.println("\t*********************************");
			  System.out.println();


		//Creates a Scanner object to read input
	  Scanner input = new Scanner(System.in);

	  //Infinite loop
	  while(true)
	  {

		//prompting user to enter the requested loan amount
		System.out.print("Enter desired loan amount (Enter 0 to stop)  :  ");

		//reading user's input
		loan.setLoanAmt(input.nextDouble());
		if (loan.getLoanAmt() <=0)//Checks to see if a valid amount is entered, 0 stops the program

				   	{
					System.out.println("Please come again");
					break;
				  }
		else
		{
			System.out.print("\nWould you like to see a payment schedule for your loan?");
			System.out.print("\n\nPlease Enter:\n 1 for YES\n 2 for NO\n 0 to QUIT\n");
			System.out.print("\n");
			System.out.print("Enter your choice:");

			loan.setOptNum(input.nextDouble());
			if (loan.getOptNum() <=0)//Checks to see if user wants to quit

					{
									System.out.println("Please come again");
									break;
				  }

				  else

				  {
					  if (loan.getOptNum() ==2)


		 			{
					  System.out.print("\nFor the requested Loan Amount of " + currency.format(loan.getLoanAmt()) + ":" + "\n" + "\n" );
			 			System.out.print("Loan Term\t Interest Rate\t\t Payment \n" );

			 			for (int i = 00; i < rate.length; i++)
			 			{

						  double payment = (loan.getLoanAmt()*(rate[i]/12))/(1-(Math.pow(1/(1+(rate[i]/12)),(term[i]))));

						  System.out.println(" " + nf.format (term[i]) + " " + "years" + "\t\t"  + rt.format (rate[i]) +  "\t\t" + currency.format(payment));
			 			}
				  }

			 		else
			 			{
						  if (loan.getOptNum() ==1)

					 			{
								  System.out.print("\nFor the requested Loan Amount of " + currency.format(loan.getLoanAmt()) + ":" + "\n" + "\n" );
								 	System.out.print("Loan Term\t Interest Rate\t\t Payment \n" );

								 		for (int i = 00; i < rate.length; i++)
								 		{
										  double payment = (loan.getLoanAmt()*(rate[i]/12))/(1-(Math.pow(1/(1+(rate[i]/12)),(term[i]))));
										  System.out.println(" " + nf.format (term[i]) + " " + "years" + "\t\t"  + rt.format (rate[i]) +  "\t\t" + currency.format(payment));
								 		}
								 	System.out.println("\n\t7 Year Loan\t\t15 Year Loan\t\t30 Year Loan");
								  System.out.println("Payment\tBalance\tInt. Paid\tBalance\tInt. Paid\tBalance\tInt. Paid\n");
									  for (int i = 00; i < rate.length; i++)
									  {
										  //Calculation for Loan Payments


										  double intPaid = (loan.getLoanAmt()*(rate[i]/12));
										  double principalPaid = (monthlyPayment - intPaid);
										  double newBalance = (loan.getLoanAmt() - principalPaid);
										  double loanAmount = newBalance;


												  for (int count = 1; count <=term[i]; count++)
													  {
														  if (count % 12 ==0) {
												   			try
												   			{
													 				Thread.sleep(2000);
												   			}
												   			catch (Exception ex)
												   				{
																	  ex.printStackTrace();
																  }
												   	}



												  java.text.DecimalFormat dfm = new java.text.DecimalFormat(",###.00");

												  System.out.println(count+ "\t" + dfm.format(loan.intPaid) + "\t" + "\t" + dfm.format(loan.newBal));
	  }
									  }
							  }

							  else
							   	{
								 		System.out.println("Please come again");
								 		break;
								  }
					  }
				  }

				}//end else
		  }//end while loop
		}//end main method

  }//end class ArmstrongWeek3


  //Class Loan holds the user information
  class Loan
  {
  //This will hold the values for the Loan Amount



	  private double loanAmt;
	  private double optNum;
	  private double monthlyInt;
	  public double intPaid;
	  public double prinPd;
	  public double newBal;
	  public int rate;
	  public double term;
	  private double monthlyPayment;


	  //default constructor
	  public Loan()
	  {
		  loanAmt = 0;
		  optNum = 0;
			monthlyInt = 0;
			intPaid = 0;
			prinPd = 0;
			newBal = 0;
			rate = 0;
			term = 0;
			monthlyPayment = 0;
	  }//end default constructor

	  //Parameterized Constructor
	  public Loan(double loanAmt, double optNum, double monthlyInt, double intPaid, double prinPd,
			  double newBal, int rate, double term, double monthlyPayment)
	  {
		  this.loanAmt = loanAmt;
		  this.optNum = optNum;
			this.monthlyInt = monthlyInt;
			this.intPaid = intPaid;
			this.prinPd = prinPd;
			this.newBal = newBal;
			this.rate = rate;
			this.term = term;
	  }//end constructor

	  //sets the loanAmt to whatever double is passed in
	  public void setLoanAmt(double loanAmt)
	  {
		  this.loanAmt = loanAmt;

	  }//end set LoanAmt

	  //returns the double loanAmt
	  public double getLoanAmt()
	  {
		  return loanAmt;

	  }//end getLoanAmt

	  //sets the optNum to whatever int is passed in
	  public void setOptNum(double optNum)
	  {
		  this.optNum = optNum;

	  }//end setOptNum

	  //returns the double optNum;
	  public double getOptNum()
	  {
		  return optNum;

	  }//end getOptNum

	  public void setMonthlyInt(double monthlyInt)
		  {
			  this.monthlyInt = monthlyInt;

		  }//end set MonthlyInt

		  //returns the double loanAmt
		  public double getMonthlyInt()
		  {
			  return monthlyInt;

	  }//end getMonthlyInt

	public void setRate(int rate)
			  {
				  this.rate = rate;

			  }//end set Rate

			  //returns the double loanAmt
			  public int getRate()
			  {
				  return rate;

		  }//end getRate

	 public void setTerm(double term)
	 			  {
	 				  this.term = term;

	 			  }//end set Term

	 			  //returns the double loanAmt
	 			  public double getTerm()
	 			  {
	 				  return term;

		  }//end getTerm

// Calculation method
	public double intPdCalculate(int intPd)
	 {

		  return monthlyPayment = (loanAmt*(rate/12))/(1 - 1 / Math.pow((1 + (rate/12)), term));

	 } // end calculation

	 public double intPaidCal (double intPaid)
	 {
		 return loanAmt * rate /12;
	 }

	 public double prinPdCal (double prinPd)
	 {
		 return monthlyPayment - intPaid;
	 }

	 public double newBalCal (double newBal)
	 {
		 return loanAmt - prinPd;
	 }

  }//end class Loan



Is This A Good Question/Topic? 0
  • +

Replies To: Java array help

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Java array help

Posted 09 July 2009 - 05:19 PM

I am half asleep at the moment, but why you making a call to a variable rather than a get method?
loan.intPaid

intPaid is a variable. Try putting a getIntPaid method in your loan class, and have it return inPaid. Then when you print it out, do
loan.getiIntPaid()

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1