3 Replies - 1209 Views - Last Post: 27 October 2011 - 10:58 AM Rate Topic: -----

#1 omgwtfben  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-October 11

Question about the math in my java homework

Posted 27 October 2011 - 10:32 AM

Hi all, Im in an introduction to programming course, and I have a question about an assignment that I completed. I am unsure if my math in my program is correct...it seems as if something else is being added to my methods and giving me a bad number, I am pretty sure that I got the program correct for the most part but if anyone has any suggestions please let me know. Thanks again for the help I am a newbie at programming.

The directions for my assignment were:

Build a program that uses notepad or another text editor to create a text file named deposits.txt. The file should contain the following
numbers, one per line:
100.00
124.00
78.92
37.55

Next, create a text file named withdrawals.txt. The file should contain the following numbers, one per line:
29.88
110.00
27.52
50.00
12.90

The numbers in the deposits.txt file are the amounts of deposits that were made to a savings account during the month, and the numbers in the withdrawals.txt file are the amounts of withdrawals that were made during the month. Write a program that creates an instance of the SavingsAccount class that you wrote in the programming challenge 10. The starting balance for the object is 500.00. The program should read the values from the Deposits.txt file and use the object's method to add them to the account balance. The program should read the values from the Withdrawals.txt file and use the object's method to substract them from the account balance. The program should call the class method to calculate the monthly interest, and then display the ending balance and the total interest earned.








import java.io.*;  
import java.util.Scanner;

public class DepositWithdraw
{

public static void main(String[] args)throws IOException
{

     double intRate ;
     double amount=0.00;
     double bal=500.00;

      
       Scanner keyboard = new Scanner(System.in);


   

     System.out.print("What is the annual interest rate?");

     intRate= keyboard.nextDouble();

     

     SavingAccount SavingAccount1= new SavingAccount(bal,intRate);

     
        File file = new File ("Deposits.txt");


		Scanner inputFile = new Scanner(file);



     
while (inputFile.hasNext())
		{
			
			 double num1 = inputFile.nextDouble();

			
			amount += num1;

		}

	
		SavingAccount1.deposit(amount);

	
		inputFile.close();


      
		File file2 = new File("Withdrawals.txt");
		Scanner inputFile2 = new Scanner(file2);



      while (inputFile2.hasNext())
	  		{
	  		
	  			double num2 = inputFile2.nextDouble();

	  		
	  			amount-= num2;
	  		}


      inputFile2.close();  

    
      SavingAccount1.getBalance();

    
SavingAccount1.addInterest();

     
SavingAccount1.getLastInterest();


        System.out.println("Account balance $" + SavingAccount1.getBalance());
		System.out.println("Total interest earned $" + SavingAccount1.getLastInterest());
	}
}










public class SavingAccount
{
   private double balance;
   private double interestRate;
   private double lastInterest;



   public SavingAccount( double bal,double intRate)
   {

      balance = bal;
      interestRate = intRate;
      lastInterest = 0.0;

}


   public void withdraw(double amount)
   {
      balance -= amount;
   }




   public void deposit(double amount)
  {
	  balance+=amount;
  }




   public void addInterest()
{

     
      double monthlyInterestRate = interestRate / 12;

      
      lastInterest = monthlyInterestRate * balance;

    
      balance += lastInterest;


}


public double getBalance()
{
	return balance;

}



 
    public double getIntrestrate()
	{
		return interestRate;

	}




   

    public double getLastInterest()
	{
		return lastInterest;

	}



}




Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Question about the math in my java homework

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7964
  • View blog
  • Posts: 13,588
  • Joined: 19-March 11

Re: Question about the math in my java homework

Posted 27 October 2011 - 10:43 AM

If you could post the results that you're concerned about, that would help.

General debugging tip: if you're coming out with the wrong result at the end of a sequence of calculations, isolate the problem by printing each intermediate result to the screen, and check it manually.

For example, when you read the deposits screen, you should see
500.00
600.00
724.00
802.92
840.47 (or something like that - doing the math in my head)

and then a series of subtractions, and so forth.
This will help you zero in on the place where the problem is happening.
Was This Post Helpful? 1
  • +
  • -

#3 omgwtfben  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-October 11

Re: Question about the math in my java homework

Posted 27 October 2011 - 10:52 AM

View Postjon.kiparsky, on 27 October 2011 - 10:43 AM, said:

If you could post the results that you're concerned about, that would help.

General debugging tip: if you're coming out with the wrong result at the end of a sequence of calculations, isolate the problem by printing each intermediate result to the screen, and check it manually.

For example, when you read the deposits screen, you should see
500.00
600.00
724.00
802.92
840.47 (or something like that - doing the math in my head)

and then a series of subtractions, and so forth.
This will help you zero in on the place where the problem is happening.





I get 841.47 as my output (not bad for doing it in your head) :P.

I think I actually have my program correct, thank you for taking the time to help me. I think I just had a brain fart and was confused for a second.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7964
  • View blog
  • Posts: 13,588
  • Joined: 19-March 11

Re: Question about the math in my java homework

Posted 27 October 2011 - 10:58 AM

Brain farts happen. A big part of debugging is making sure that you're actually looking at a bug. This is where attention to detail and breaking it down are very helpful.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1