SavingsAccount

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 4393 Views - Last Post: 09 October 2011 - 12:32 AM Rate Topic: -----

#1 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 84
  • Joined: 22-September 11

SavingsAccount

Posted 08 October 2011 - 02:41 PM

I have to design a savingsaccount class that stores a savings account's annual interest rate and balance. The class constructor should accept the amount of the savings account's starting balance. the class should also have methods for subtracting the amount of a withdrawal, adding the amount of a deposit, and adding the amount of monthly interest to the balance. the monthly interest rate is the annual interest rate divided by 12. to add the monthly interest to the balance, multiply the monthly interest rate by the balance and add the result to the balance.

Here's the code for that:
public class DTSavingsAccount {

	private double balance;
	private double interestRate;
	private double totalInterest;
	private double totalDeposit;
	private double totalWithdraw;
	//private double startingBalance;

	public DTSavingsAccount(double a, double B)/>
		{
		balance = a;
		interestRate = b;
		totalDeposit = 0.00;
		totalWithdraw = 0.00;
		totalInterest = 0.00;
		}

		public void setInterestRate(double B)/>
		{
		interestRate = b;
		}

		public void setBalance(double a)
		{
		balance = a;
		}

		public double getInterestRate()
		{
		return interestRate;

		}

		public double getBalance()
		{
		return balance;
		}

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

		public void withdraw( double amount2 )
		{
		totalWithdraw += amount2;
		balance -= amount2;
		}

		public void computeInterest()
		{
		totalInterest += balance * ((interestRate/100)/12);
		balance += balance * ((interestRate/100)/12);

		}

		public boolean equals(Savings account)

		if(interestRate == account.interestRate && balance == account.balance)
		{
		return true;
		}
		if else
		{
		return false;
		}
	}



the errors for this code are:
F:\Foundations of Java\SavingsAccount.java:59: error: ';' expected
public boolean equals(Savings account)
^
F:\Foundations of Java\SavingsAccount.java:65: error: illegal start of type
if else
^
F:\Foundations of Java\SavingsAccount.java:65: error: ';' expected
if else
^
3 errors

Tool completed with exit code 1

The second part of this problem is
Test the class in a program that calculates the balance of a savings account at the end of a period of time. it should ask the user for the the annual interest rate, the starting balance, and the number of months that have passed since the account was established.A loop should then iterate once for every month, performing the following:
a. ask the user for the amount deposited into the account during the month. use the class method to add this amount to the account balance.

b. ask the user for the amount withdrawn from the account during the month. Use the class method to subtract this amount from the account balance.

c.use the class method to calculate the monthly interest.

After the last iteration, the program should display the ending balance, the total amount of deposit, the total amount of withdrawals, and the total interest earned.

The code is:
import java.util.Scanner;

	public class DTSavingsAccountsDemo {


	public static void main(String[] args) {





	int i;
	int option = 0;

	Scanner keyboard = new Scanner(System.in);

        System.out.println("Please enter the month: ");

	int month = keyboard.nextInt();

	System.out.println("Please enter the balance: ");

	double balance = keyboard.nextDouble();

	System.out.println("Please enter the annual interest: ");

	double interestRate = keyboard.nextDouble();

	Savings account = new Savings(balance, interestRate);

	for(i = 1; i <= month; i++)
	{


	System.out.println("*1. Deposit *");

	System.out.println("*2. Withdrawal *");


	option = keyboard.nextInt();

	if(option == 1)

	{

	System.out.println("Please enter the deposited amount: ");

	account.deposit(keyboard.nextDouble());

	account.computeInterest();

	}
	else if(option == 2)
	{
	System.out.println("Please enter the withdrawal amount: ");

	account.withdraw(keyboard.nextDouble());

	account.computeInterest();
	}
	else
	{
	System.out.println("Wrong Input!");

	System.exit(0);

	}
}
	System.out.println(account);
	   }
}



The errors are:
F:\Foundations of Java\DTSavingsAccountDemo.java:3: error: class DTSavingsAccountsDemo is public, should be declared in a file named DTSavingsAccountsDemo.java
public class DTSavingsAccountsDemo {
^
F:\Foundations of Java\DTSavingsAccountDemo.java:29: error: cannot find symbol
Savings account = new Savings(balance, interestRate);
^
symbol: class Savings
location: class DTSavingsAccountsDemo
F:\Foundations of Java\DTSavingsAccountDemo.java:29: error: cannot find symbol
Savings account = new Savings(balance, interestRate);
^
symbol: class Savings
location: class DTSavingsAccountsDemo
3 errors

Tool completed with exit code 1

Can you please help me fix the errors in both the codes so they can run perfectly.
Thank you
(also examples of the corrections are better and will be easier for me to understand what your trying to say)

Is This A Good Question/Topic? 0
  • +

Replies To: SavingsAccount

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5421
  • View blog
  • Posts: 8,716
  • Joined: 19-March 11

Re: SavingsAccount

Posted 08 October 2011 - 02:44 PM

For your first problem, the syntax for an if/else is

if (CONDITION)
{
  STATEMENT(S)
}
else  <-- note: no "if" here
{
  STATEMENT(S)
}



also allowable is
if (CONDITION)
{
  STATEMENT(S)
}
else if   // secondary condition: only evaluated if the first is false
{
  STATEMENT(S)
}
else 
{
  STATEMENT(S)
}


Was This Post Helpful? 0
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: SavingsAccount

Posted 08 October 2011 - 02:49 PM

If this is a method it should open { like public boolean equals(Savings account){
Again every public class should be in a its own file named after the class name
You have no class called Saving
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5421
  • View blog
  • Posts: 8,716
  • Joined: 19-March 11

Re: SavingsAccount

Posted 08 October 2011 - 02:51 PM

Quote

F:\Foundations of Java\SavingsAccount.java:59: error: ';' expected
public boolean equals(Savings account)
^


You're missing the opening curly brace on this. Please proofread your code before you post. Java syntax is not difficult.

Quote

F:\Foundations of Java\DTSavingsAccountDemo.java:3: error: class DTSavingsAccountsDemo is public, should be declared in a file named DTSavingsAccountsDemo.java
public class DTSavingsAccountsDemo {



This seems pretty self-explanatory. You've declared this in a class that's not called DTSavingsAccountsDemo.java
That's the problem, and the solution should be obvious.

Quote

F:\Foundations of Java\DTSavingsAccountDemo.java:29: error: cannot find symbol
Savings account = new Savings(balance, interestRate);
^
symbol: class Savings
location: class DTSavingsAccountsDemo


This means the compiler doesn't know what a Savings is. Have you got a class called Savings?

Quote

Can you please help me fix the errors in both the codes so they can run perfectly.


No. You need to fix them yourself. Now you know how, go to it.
Was This Post Helpful? 0
  • +
  • -

#5 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 84
  • Joined: 22-September 11

Re: SavingsAccount

Posted 08 October 2011 - 02:52 PM

View Postjon.kiparsky, on 08 October 2011 - 02:44 PM, said:

For your first problem, the syntax for an if/else is

if (CONDITION)
{
  STATEMENT(S)
}
else  <-- note: no "if" here
{
  STATEMENT(S)
}



also allowable is
if (CONDITION)
{
  STATEMENT(S)
}
else if   // secondary condition: only evaluated if the first is false
{
  STATEMENT(S)
}
else 
{
  STATEMENT(S)
}




I made the change but i still get this error
F:\Foundations of Java\DTSavingsAccount.java:59: error: ';' expected
public boolean equals(Savings account)
^
F:\Foundations of Java\DTSavingsAccount.java:65: error: illegal start of type
else if // secondary condition: only evaluated if the first is false
^
F:\Foundations of Java\DTSavingsAccount.java:65: error: ';' expected
else if // secondary condition: only evaluated if the first is false
^
3 errors

Tool completed with exit code 1
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5421
  • View blog
  • Posts: 8,716
  • Joined: 19-March 11

Re: SavingsAccount

Posted 08 October 2011 - 02:53 PM

Already answered, twice.

Also: you might want to pick a different handle.
Was This Post Helpful? 0
  • +
  • -

#7 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 84
  • Joined: 22-September 11

Re: SavingsAccount

Posted 08 October 2011 - 02:57 PM

View Postjon.kiparsky, on 08 October 2011 - 02:53 PM, said:

Already answered, twice.

Also: you might want to pick a different handle.


what do u mean different handle??
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5421
  • View blog
  • Posts: 8,716
  • Joined: 19-March 11

Re: SavingsAccount

Posted 08 October 2011 - 02:59 PM

"CodeMasterNinja"? Really?
Was This Post Helpful? 0
  • +
  • -

#9 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 84
  • Joined: 22-September 11

Re: SavingsAccount

Posted 08 October 2011 - 03:01 PM

View Postsmohd, on 08 October 2011 - 02:49 PM, said:

If this is a method it should open { like public boolean equals(Savings account){
Again every public class should be in a its own file named after the class name
You have no class called Saving


okay this is the new code for that
public class DTSavingsAccount {

	private double balance;
	private double interestRate;
	private double totalInterest;
	private double totalDeposit;
	public  double Savings;
	private double totalWithdraw;
	//private double startingBalance;

	public DTSavingsAccount(double a, double B)/>
		{
		balance = a;
		interestRate = b;
		totalDeposit = 0.00;
		totalWithdraw = 0.00;
		totalInterest = 0.00;
		}

		public void setInterestRate(double B)/>
		{
		interestRate = b;
		}

		public void setBalance(double a)
		{
		balance = a;
		}

		public double getInterestRate()
		{
		return interestRate;

		}

		public double getBalance()
		{
		return balance;
		}

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

		public void withdraw( double amount2 )
		{
		totalWithdraw += amount2;
		balance -= amount2;
		}

		public void computeInterest()
		{
		totalInterest += balance * ((interestRate/100)/12);
		balance += balance * ((interestRate/100)/12);

		}

		public boolean equals(Savings account){
	    }

		if(interestRate == account.interestRate && balance == account.balance)
		{
		return true;
		}
		else if // secondary condition: only evaluated if the first is false
		{
		return false;
		}
	}



however i am getting these errors
F:\Foundations of Java\DTSavingsAccount.java:63: error: illegal start of type
if(interestRate == account.interestRate && balance == account.balance)
^
F:\Foundations of Java\DTSavingsAccount.java:63: error: <identifier> expected
if(interestRate == account.interestRate && balance == account.balance)
^
F:\Foundations of Java\DTSavingsAccount.java:63: error: ';' expected
if(interestRate == account.interestRate && balance == account.balance)
^
F:\Foundations of Java\DTSavingsAccount.java:63: error: illegal start of type
if(interestRate == account.interestRate && balance == account.balance)
^
F:\Foundations of Java\DTSavingsAccount.java:63: error: ';' expected
if(interestRate == account.interestRate && balance == account.balance)
^
F:\Foundations of Java\DTSavingsAccount.java:63: error: <identifier> expected
if(interestRate == account.interestRate && balance == account.balance)
^
F:\Foundations of Java\DTSavingsAccount.java:63: error: <identifier> expected
if(interestRate == account.interestRate && balance == account.balance)
^
F:\Foundations of Java\DTSavingsAccount.java:67: error: illegal start of type
else if // secondary condition: only evaluated if the first is false
^
F:\Foundations of Java\DTSavingsAccount.java:67: error: ';' expected
else if // secondary condition: only evaluated if the first is false
^
9 errors

Tool completed with exit code 1

View Postjon.kiparsky, on 08 October 2011 - 02:59 PM, said:

"CodeMasterNinja"? Really?


well i seriously dont know so iam asking you
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5421
  • View blog
  • Posts: 8,716
  • Joined: 19-March 11

Re: SavingsAccount

Posted 08 October 2011 - 03:03 PM

Please, please, please read your code. Instructions go inside the method. Each method has to have an opening curly brace: { and a closing curly brace: } and the code goes in between those.

"CodeMasterNinja" implies some sort of mastery, some high degree of skill.

That is not what I'm seeing here.

This post has been edited by jon.kiparsky: 08 October 2011 - 03:04 PM

Was This Post Helpful? 1
  • +
  • -

#11 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 84
  • Joined: 22-September 11

Re: SavingsAccount

Posted 08 October 2011 - 03:06 PM

also i am getting these errors for this code
the code is

public class DTSavingsAccount {

	private double balance;
	private double interestRate;
	private double totalInterest;
	private double totalDeposit;
	public  double Savings;
	private double totalWithdraw;
	//private double startingBalance;

	public DTSavingsAccount(double a, double B)/>
		{
		balance = a;
		interestRate = b;
		totalDeposit = 0.00;
		totalWithdraw = 0.00;
		totalInterest = 0.00;
		}

		public void setInterestRate(double B)/>
		{
		interestRate = b;
		}

		public void setBalance(double a)
		{
		balance = a;
		}

		public double getInterestRate()
		{
		return interestRate;

		}

        public double getSavings()
        {
		return Savings;
	    }

		public double getBalance()
		{
		return balance;
		}

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

		public void withdraw( double amount2 )
		{
		totalWithdraw += amount2;
		balance -= amount2;
		}

		public void computeInterest()
		{
		totalInterest += balance * ((interestRate/100)/12);
		balance += balance * ((interestRate/100)/12);

		}

		public boolean equals(Savings account){
	    }

		if(interestRate == account.interestRate && balance == account.balance)
		{
		return true;
		}
		else if // secondary condition: only evaluated if the first is false
		{
		return false;
		}
	}F:\Foundations of Java\DTSavingsAccount.java:68: error: illegal start of type
		if(interestRate == account.interestRate && balance == account.balance)
		^
F:\Foundations of Java\DTSavingsAccount.java:68: error: <identifier> expected
		if(interestRate == account.interestRate && balance == account.balance)
		               ^
F:\Foundations of Java\DTSavingsAccount.java:68: error: ';' expected
		if(interestRate == account.interestRate && balance == account.balance)
		                  ^
F:\Foundations of Java\DTSavingsAccount.java:68: error: illegal start of type
		if(interestRate == account.interestRate && balance == account.balance)
		                          ^
F:\Foundations of Java\DTSavingsAccount.java:68: error: ';' expected
		if(interestRate == account.interestRate && balance == account.balance)
		                                       ^
F:\Foundations of Java\DTSavingsAccount.java:68: error: <identifier> expected
		if(interestRate == account.interestRate && balance == account.balance)
		                                                  ^
F:\Foundations of Java\DTSavingsAccount.java:68: error: <identifier> expected
		if(interestRate == account.interestRate && balance == account.balance)
		                                                                     ^
F:\Foundations of Java\DTSavingsAccount.java:72: error: illegal start of type
		else if // secondary condition: only evaluated if the first is false
		^
F:\Foundations of Java\DTSavingsAccount.java:72: error: ';' expected
		else if // secondary condition: only evaluated if the first is false
		    ^
9 errors

Tool completed with exit code 1




the errors are:

View Postjon.kiparsky, on 08 October 2011 - 03:03 PM, said:

Please, please, please read your code. Instructions go inside the method. Each method has to have an opening curly brace: { and a closing curly brace: } and the code goes in between those.

"CodeMasterNinja" implies some sort of mastery, some high degree of skill.

That is not what I'm seeing here.


i know html and javascript
Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5421
  • View blog
  • Posts: 8,716
  • Joined: 19-March 11

Re: SavingsAccount

Posted 08 October 2011 - 03:18 PM

Perhaps I'd have more patience on another day. Today I'm just going to say please learn some Java syntax. The problem here is simple: you need to learn how the language works. There are many beginner tutorials on this site, on the Sunacle site, and elsewhere on the web. Read them, look at the examples, then fix your code. NeoTifa's beginning Java tutorial is supposed to be good, and the Nuts and Bolts tutorial is a good one as well. If that's too advanced, there are others as well.

"We are not a code writing service", and we also aren't a code rewriting service.
Was This Post Helpful? 0
  • +
  • -

#13 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 84
  • Joined: 22-September 11

Re: SavingsAccount

Posted 08 October 2011 - 03:28 PM

okay so after fixing my code i finally have this:
import java.util.Scanner;

	public class DTSavingsAccountsDemo {


	public static void main(String[] args) {





	int i;
	int option = 0;

	Scanner keyboard = new Scanner(System.in);

        System.out.println("Please enter the month: ");

	int month = keyboard.nextInt();

	System.out.println("Please enter the balance: ");

	double balance = keyboard.nextDouble();

	System.out.println("Please enter the annual interest: ");

	double interestRate = keyboard.nextDouble();

	Savings account = new Savings(balance, interestRate);

	for(i = 1; i <= month; i++)
	{

    System.out.println("*1. Deposit *");

	System.out.println("*2. Withdrawal *");
     }

	option = keyboard.nextInt();

	if(option == 1)

	{

	System.out.println("Please enter the deposited amount: ");

	account.deposit(keyboard.nextDouble());

	account.computeInterest();

	}
	else if(option == 2)
	{
	System.out.println("Please enter the withdrawal amount: ");

	account.withdraw(keyboard.nextDouble());

	account.computeInterest();
	}
	else if (System.out.println("Wrong Input!"))

	System.exit(0);
    }
    {
	System.out.println(account);
}
	}




with these errors:
F:\Foundations of Java\DTSavingsAccountsDemo.java:29: error: cannot find symbol
Savings account = new Savings(balance, interestRate);
^
symbol: class Savings
location: class DTSavingsAccountsDemo
F:\Foundations of Java\DTSavingsAccountsDemo.java:29: error: cannot find symbol
Savings account = new Savings(balance, interestRate);
^
symbol: class Savings
location: class DTSavingsAccountsDemo
F:\Foundations of Java\DTSavingsAccountsDemo.java:60: error: incompatible types
else if (System.out.println("Wrong Input!"))
^
required: boolean
found: void
F:\Foundations of Java\DTSavingsAccountsDemo.java:65: error: cannot find symbol
System.out.println(account);
^
symbol: variable account
location: class DTSavingsAccountsDemo
4 errors

Tool completed with exit code 1

and i have this code:
public class DTSavingsAccount {

	private double balance;
	private double interestRate;
	private double totalInterest;
	private double totalDeposit;
	private double totalWithdraw;
	//private double startingBalance;

	public DTSavingsAccount(double a, double B)/>
		{
		balance = a;
		interestRate = b;
		totalDeposit = 0.00;
		totalWithdraw = 0.00;
		totalInterest = 0.00;
		}

		public void setInterestRate(double B)/>
		{
		interestRate = b;
		}

		public void setBalance(double a)
		{
		balance = a;
		}

		public double getInterestRate()
		{
		return interestRate;

		}

        public double getSavings()
        {
		return Savings;
	    }

		public double getBalance()
		{
		return balance;
		}

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

		public void withdraw( double amount2 )
		{
		totalWithdraw += amount2;
		balance -= amount2;
		}

		public void computeInterest()
		{
		totalInterest += balance * ((interestRate/100)/12);
		balance += balance * ((interestRate/100)/12);

		}

		public boolean equals(Savings account)
		{

        if(interestRate == account.interestRate && balance == account.balance)

		return true;

		else if // secondary condition: only evaluated if the first is false

		return false;
	    }
	    public String toString()
		{
		  return(" Total amount deposited: " + totalDeposit + "\n" +
		      " Total amount withdrawn: " + totalWithdraw + "\n" +
		      " Final balance: "  + balance + "\n" +
		      " Total Interest: " + totalInterest);
        }


	}



with these errors:
F:\Foundations of Java\DTSavingsAccount.java:71: error: '(' expected
else if // secondary condition: only evaluated if the first is false
^
F:\Foundations of Java\DTSavingsAccount.java:73: error: illegal start of expression
return false;
^
F:\Foundations of Java\DTSavingsAccount.java:73: error: ')' expected
return false;
^
3 errors

Tool completed with exit code 1
Was This Post Helpful? 0
  • +
  • -

#14 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1978
  • View blog
  • Posts: 4,823
  • Joined: 10-September 10

Re: SavingsAccount

Posted 08 October 2011 - 04:29 PM

For the first listing:

What is line 29 supposed to be doing? The error is complaining that the statement is trying to create an instance of the Savings class called account, yet there's no evidence of the class Savings. Was the intent to create an instance of the class DTSavingsAccount?

The statement at line 60, else if (System.out.println("Wrong Input!")), is wrong on a couple levels. Perhaps if you removed the 'if'?

For the second listing:

Lines 13 and 21, 'b' is undefined.

Line 37 (and 64?), Savings is undefined. At line 37, Savings is used as though it were a variable of type double, but at line 64 it is used as a variable or class type.

Line 71, an 'if' without a condition statement. As before, the 'if' may be redundant.

These errors aren't hard to fix. If you don't see how it's to be done, you need to admit and describe your confusion so that we can help you see the sense of it. You may be lacking some fundamentals. Review if/else statements, variable declarations, and creating instances of classes with the 'new' statement.
Was This Post Helpful? 0
  • +
  • -

#15 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 84
  • Joined: 22-September 11

Re: SavingsAccount

Posted 08 October 2011 - 11:33 PM

View PostGregBrannon, on 08 October 2011 - 04:29 PM, said:

For the first listing:

What is line 29 supposed to be doing? The error is complaining that the statement is trying to create an instance of the Savings class called account, yet there's no evidence of the class Savings. Was the intent to create an instance of the class DTSavingsAccount?

The statement at line 60, else if (System.out.println("Wrong Input!")), is wrong on a couple levels. Perhaps if you removed the 'if'?

For the second listing:

Lines 13 and 21, 'b' is undefined.

Line 37 (and 64?), Savings is undefined. At line 37, Savings is used as though it were a variable of type double, but at line 64 it is used as a variable or class type.

Line 71, an 'if' without a condition statement. As before, the 'if' may be redundant.

These errors aren't hard to fix. If you don't see how it's to be done, you need to admit and describe your confusion so that we can help you see the sense of it. You may be lacking some fundamentals. Review if/else statements, variable declarations, and creating instances of classes with the 'new' statement.


okay i seriously do not know how to do these i am an ametuer when it comes to java and i am learning it for the first time so please can you help me fix these errors it would be a great help man thanks a lot
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2