Stryker1990
asked on
Java program issue
Hi, I am working on a Hiearchy bank program that takes the info from the bankTest class and deals with it accordingly, however, I am having issues, it compiles and runs but the output does not seem right. I think Im having issues with the contructors and/or set and get methods. Any input or pointing out what I obviously did wrong would be great, Thanks.
First set of code will be bankTest followed by 3 comments from myself that are the classes.
First set of code will be bankTest followed by 3 comments from myself that are the classes.
class bankTest
{
public static void main ( String argv[] )
{
double interest;
Account test = new Account();
Account act = new Account(200.00);
SavingsAccount sav = new SavingsAccount(1500.00, 0.0525);
CheckingAccount chk = new CheckingAccount(145.38, 0.10);
// Test constructors
System.out.println("test account balance is " + test.getBalance() );
System.out.println("act account balance is " + act.getBalance() );
System.out.println("sav account balance is " + sav.getBalance() );
System.out.println("sav account interest is " + sav.getInterestRate() );
System.out.println("chk account balance is " + chk.getBalance() );
System.out.println("chk account fee is " + chk.getFee() );
// Test Account superclass
act.credit(55.00);
System.out.println("act balance is " + act.getBalance() );
if (act.debit(75.00))
System.out.println("act balance is " + act.getBalance());
else
System.out.println("No debit (would overdraw account)");
if (act.debit(275.00))
System.out.println("act balance is " + act.getBalance());
else
System.out.println("No debit (would overdraw account)");
System.out.println("act balance is " + act.getBalance() );
act.setBalance(12345.67);
System.out.println("sav account balance is " + sav.getBalance() );
// Test SavingsAccount
sav.debit(250.00);
System.out.println("sav account balance is " + sav.getBalance() );
sav.credit(575.25);
System.out.println("sav account balance is " + sav.getBalance() );
interest = sav.calculateInterest();
System.out.println("Interest is: " + interest);
sav.credit(interest);
System.out.println("sav account balance after interest is: " + sav.getBalance() );
sav.setInterestRate(.065);
System.out.println("new interest rate is " + sav.getInterestRate() );
sav.setBalance(123456.78);
System.out.println("sav account balance is " + sav.getBalance() );
// Test CheckingAccount
chk.credit(135.25);
System.out.println("chk account balance is " + chk.getBalance() );
chk.debit(123.45);
System.out.println("chk account balance is " + chk.getBalance() );
chk.debit(456.78);
System.out.println("chk account balance is " + chk.getBalance() );
chk.setFee(0.25);
System.out.println("chk account fee is " + chk.getFee() );
chk.setBalance(1234.56);
System.out.println("chk account balance is " + chk.getBalance() );
}
}
ASKER
public class SavingsAccount extends Account {
double Interest;
int IR;
SavingsAccount(double Balance, double Interest) {
Balance=bal;
Interest=IR;
}
public void setInterestRate(double IR) {
Interest = IR;
}
public double getInterestRate() {
return Interest;
}
double calculateInterest()
{
return getBalance()*getInterestRate();
}
}
ASKER
public class CheckingAccount extends Account {
double fee;
public CheckingAccount(double newBal, double theFee) {
setBalance(newBal);
fee = theFee;
}
public void setFee(double theFee) {
fee = theFee;
}
public double getFee() {
return fee;
}
public boolean debit(double debitamt)
{
return super.debit(debitamt+getFee());
}
}
Can you post a zip file containing all the code pls??
There are a number of places, it would appear you are setting the values incorrectly like :
public Account(double Balance) {
Balance = bal;
}
SavingsAccount(double Balance, double Interest) {
Balance=bal;
Interest=IR;
}
Check that you are properly setting the instance variables on construction and in mutator methods.
With an inherited class, I would also expect to see this code :
SavingsAccount(double Balance, double Interest) {
Balance=bal;
Interest=IR;
}
More like this :
SavingsAccount(double Balance, double Interest) {
super(Balance);
this.Interest=Interest;
}
Kevin
public Account(double Balance) {
Balance = bal;
}
SavingsAccount(double Balance, double Interest) {
Balance=bal;
Interest=IR;
}
Check that you are properly setting the instance variables on construction and in mutator methods.
With an inherited class, I would also expect to see this code :
SavingsAccount(double Balance, double Interest) {
Balance=bal;
Interest=IR;
}
More like this :
SavingsAccount(double Balance, double Interest) {
super(Balance);
this.Interest=Interest;
}
Kevin
Also be careful with your syntax :
Balance += Balance + credit;
Note:
Balance += 1; // <<< this already means add 1 to current balance
Same as :
Balance = Balance + 1;
Therefore, if you have :
Balance += Balance + 1;
It is same as :
Balance = Balance + Balance + 1;
So long story short, you want :
Balance += credit;
As a side note, try to follow Java naming conventions and other best practices where possible.
variables should be camelCase, where as class names should be CamelCase. :)
double balance;
balance += credit;
Balance += Balance + credit;
Note:
Balance += 1; // <<< this already means add 1 to current balance
Same as :
Balance = Balance + 1;
Therefore, if you have :
Balance += Balance + 1;
It is same as :
Balance = Balance + Balance + 1;
So long story short, you want :
Balance += credit;
As a side note, try to follow Java naming conventions and other best practices where possible.
variables should be camelCase, where as class names should be CamelCase. :)
double balance;
balance += credit;
ASKER
Ah, thank you for the advice once again Kevin, your dual accounts have helped me once again ;)
Yes, I knew i would probably run into problems with the way i set values, i was confused as to where I should put them and when. Also, with your one tip of using this.Interest = Interest...If im understanding correctly could that same concept be used in my set functions instead of using a different variable?
for example,
public void setFee() {
this.Fee = Fee;
}
Yes, I knew i would probably run into problems with the way i set values, i was confused as to where I should put them and when. Also, with your one tip of using this.Interest = Interest...If im understanding correctly could that same concept be used in my set functions instead of using a different variable?
for example,
public void setFee() {
this.Fee = Fee;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Awesome, thanks! That works much better, any other tips as to whats wrong? Im only getting some of the right output, is it still my values being set incorrectly?
What does your new source look like (you can start with just the BankTest.java) and what you are getting as output and what you expect. One thing I saw earlier was a copy and paste error so just checking to see if you have any others, although you may be able to spot them for yourself now that you know what to look for.
act.setBalance(12345.67);
System.out.println("sav account balance is " + sav.getBalance() );
Notice here you are setting the act balance but then checking the sav balance.
act.setBalance(12345.67);
System.out.println("sav account balance is " + sav.getBalance() );
Notice here you are setting the act balance but then checking the sav balance.
ASKER
I have actually not wrote the bankTest, I just needed to make my classes work with it. After redoing my set functions, my classes are as follows...multiple posts..
public class Account {
double Balance;
public Account(){}
public Account(double Balance) {
this.Balance = Balance;
}
public void setBalance(double Balance) {
this.Balance=Balance;
}
public double getBalance() {
return Balance;
}
public double credit(double credit) {
return Balance += credit;
}
public boolean debit(double debitamt)
{
if(getBalance()-debitamt<0)
return false;
setBalance(getBalance()-debitamt);
return true;
}
}
ASKER
public class SavingsAccount extends Account {
double Interest;
SavingsAccount(double Balance, double Interest) {
super(Balance);
this.Interest=Interest;
}
public void setInterestRate(double Interest) {
this.Interest=Interest;
}
public double getInterestRate() {
return Interest;
}
double calculateInterest()
{
return getBalance()*getInterestRate();
}
}
ASKER
public class CheckingAccount extends Account {
double fee;
public CheckingAccount(double fee) {
this.fee = fee;
}
public void setFee(double fee) {
this.fee=fee;
}
public double getFee() {
return fee;
}
public boolean debit(double debitamt)
{
return super.debit(debitamt+getFee());
}
ASKER
a few more changes were made between posts and I believe it is working, unless it is supposed to be resetting the balances throughout the program. It seems to be adding them together because of the += instead of resetting them. Seeing as the interest rate late in the program is 95..it leads me to believe it is supposed to keep resetting the values
If the program uses credit() method then yes it will be a running balance which is accurate. If you are wanting to reset the balance, you need to use setBalance().
ASKER
Oooh, I am understanding this more now after looking through it more thoroughly.. Everything looks to be in order, and if I find it isn't I should be able to take care of it from what I learned. Thank you once again Kevin.
ASKER
Thanks again!
You are most welcome!
Best regards and happy coding,
Kevin
Best regards and happy coding,
Kevin
ASKER
Open in new window