Output doesn't display numbers
I've got this code for an assignment, and for some reason my output displays zero's instead of the information I've added to the constructors. Also, the checking account is also supposed to display the interest but doesn't, even though the instructions say for the toString() method in checking to not include the interest.
The instructions just in case.
For the Account class, create:
two protected variables for the account number and the account balance.
two get methods - one for the account number and one for the account balance
two set methods - one for the account number and one for the balance.
a constructor that requires an account number and sets the balance to 0.
a method to override the toString() method which returns a String containing: Account # = < account number> Account Balance = <account balance> where <account number> is the object's account number and <account balance> is the object's account balance.
an abstract computeInterest() method that takes one integer argument and returns a double.
Do not allow either the account number or the balance to be set to a negative number.
The Checking class will be a subclass of Account. For the Checking class, create:
a method to override the toString() method which returns a String containing: "Checking" , the account number, and the account balance. Do not print the interest earned as part of toString().
a constructor that takes an account number as an argument and calls the superclass constructor passing this account number.
a method to implement the abstract computeInterest() method of the superclass. This method takes one argument for the interest period and returns the interest earned. The interest earned is 2 % of the balance over $700 times the interest period. The interest period is passed as an argument to the computeInterest() method. For example, the interest on $3000 at 2% for three years is (3000 - 700) times .02 times 3 or 138.
The Savings class will be a subclass of Account. For the Savings class, create:
an additional private variable to hold the interest rate.
a get and set method for the interest rate. Do not allow the interest rate to be set to a negative number.
a method named toString() that overrides the toString() method of the Object class. This method should return "Savings", the account number, the account balance and the interest rate. Do not print the interest earned as part of toString().
a constructor with two arguments - the account number and the interest rate. This constructor calls the superclass constructor passing the account number.
a method to implement the abstract computeInterest() method of the superclass. This method takes one argument for the interest period and returns the interest earned. The interest is calculated as (1 + interest rate)period times the balance minus the balance. The interest period is passed as an argument to the computeInterest() method. For example, the interest on $6000 at 2% for three years is (1.02)3 * 6000 - 6000 or 367.
For the AccountArray class, create:
a main() method.
an array to store 10 objects of either the Savings or Checking class. Use 2% (.02) for the savings account rate. Use account numbers 100 through 109. Use initial balances of 1000 through 10000.
a for loop that will instantiate 5 objects of the Savings class and 5 objects of the Checking class. Store these objects in a single array created above.
a for loop that prints the data in all 10 objects of the array using the overridden toString() method and also prints the interest amount computed for each object. Use 3 for the period. The results are displayed in the sample output.
Code java:
public abstract class Account
{
protected long accountNumber;
protected double accountBalance;
public long getAccountNumber()
{
return accountNumber;
}
public double getAccountBalance()
{
return accountBalance;
}
public void setAccountNumber(long number)
{
number = accountNumber;
}
public void setAccountBalance(double balance)
{
balance = accountBalance;
}
public void setUpAccount(long number, double balance) //look up constructors
{
number = accountNumber;
balance = accountBalance;
}
public String toString()
{
return "Account Number: " + accountNumber + " Account Balance: " + accountBalance;
}
public abstract double computeInterest(int intPeriod);
}
Code java:
public class Checking extends Account
{
public String toString()
{
String info = "Checking\nAccount Number: " + accountNumber +
"\nAccount Balance: " + accountBalance ;
return info;
}
public Checking(long number)
{
number = accountNumber;
}
public double computeInterest(int intPeriod)
{
double total = ((accountBalance - 700) * .02) * 3;
return total;
}
}
Code java:
public class Savings extends Account
{
private double interestRate;
public double getInterest()
{
return interestRate;
}
public void setInterest(double inter)
{
inter = interestRate;
}
public String toString()
{
String info = "Savings\nAccount Number: " + accountNumber + " \nAccount Balance: "
+ accountBalance + " \nInterest Rate: " + interestRate;
return info;
}
public Savings(long number, double interest)
{
number = accountNumber;
interest = interestRate;
}
public double computeInterest(int intPeriod)
{
double total = Math.pow((1 + interestRate), intPeriod) * accountBalance - accountBalance;
return total;
}
}
In this class I thought about constructing the methods first, but then realized that they should probably be set up in the for method.
Code java:
public class AccountArray
{
public static void main(String[] args)
{
Account[] refAccount = new Account[10];
/*refAccount[0] = new Checking(100);
refAccount[1] = new Checking(101);
refAccount[2] = new Checking(102);
refAccount[3] = new Checking(103);
refAccount[4] = new Checking(104);
refAccount[5] = new Savings(105, .02);
refAccount[6] = new Savings(106, .02);
refAccount[7] = new Savings(107, .02);
refAccount[8] = new Savings(108, .02);
refAccount[9] = new Savings(109, .02);
refAccount[0].setAccountBalance(1000.0);
refAccount[1].setAccountBalance(2000.0);
refAccount[2].setAccountBalance(3000.0);
refAccount[3].setAccountBalance(4000.0);
refAccount[4].setAccountBalance(5000.0);
refAccount[5].setAccountBalance(6000.0);
refAccount[6].setAccountBalance(7000.0);
refAccount[7].setAccountBalance(8000.0);
refAccount[8].setAccountBalance(9000.0);
refAccount[9].setAccountBalance(10000.0);*/
for(int inc = 0; inc < 10; inc++ )
{
refAccount[0] = new Checking(100);
refAccount[1] = new Checking(101);
refAccount[2] = new Checking(102);
refAccount[3] = new Checking(103);
refAccount[4] = new Checking(104);
refAccount[5] = new Savings(105, .02);
refAccount[6] = new Savings(106, .02);
refAccount[7] = new Savings(107, .02);
refAccount[8] = new Savings(108, .02);
refAccount[9] = new Savings(109, .02);
refAccount[0].setAccountBalance(1000.0);
refAccount[1].setAccountBalance(2000.0);
refAccount[2].setAccountBalance(3000.0);
refAccount[3].setAccountBalance(4000.0);
refAccount[4].setAccountBalance(5000.0);
refAccount[5].setAccountBalance(6000.0);
refAccount[6].setAccountBalance(7000.0);
refAccount[7].setAccountBalance(8000.0);
refAccount[8].setAccountBalance(9000.0);
refAccount[9].setAccountBalance(10000.0);
}
for(int ctr = 0; ctr < 10; ctr++)
{
System.out.println(refAccount[ctr].toString());
}
}
}
thanks for the help in advance.
Re: Output doesn't display numbers
In the Account class you say
Code :
public void setAccountNumber(long number)
{
number = accountNumber;
}
But that's the wrong way round! You should assign the value of number that was passed to the instance's accountNumber variable.
Re: Output doesn't display numbers
Thank you so very much! I had asked that question earlier, but I guess I ask so many questions at once usually, that people don't really want to respond, or whatever.
Thank you very much, that helped a bunch. So it does make a difference! A huge difference.
Re: Output doesn't display numbers
You're welcome. I didn't see the earlier question.
Yes, it's hard to figure out how to ask a question. I know that sounds weird, but with a whole lot of code, and a whole lot of problems and ideas it can be (and, basically is) hard to figure out exactly what the question should be.
One trick people adopt is to try and split up the code and minimise the problem. In this particular case a simple class with one field and a setter, toString(), and main() would have illustrated the problem. (Setter tries to set, but toString() can't see the value). A small amount of code and a specific question will maximise your chances of a response. (And then its just a question of asking lots of them! For well constructed questions no-one will worry about that.)
Re: Output doesn't display numbers
I do have another problem, when I print the code, everything prints correctly, except the interest.
For the checking accounts, it doesn't print at all, and for the savings accounts, it prints zeroes.
I tried to change the savings account toString method() to include the total (for total interest earned, as it's supposed to be) instead of interestRate because that just prints .02....
However when I do this, it changes back to printing zeroes.
And perhaps it's just me, but the checking account instructions specifically say to not print the interest earned via the toString() method in checking. However when looking at the sample output that is given. It clearly displays it. So I'm assuming that it's meant to be displayed. I'm not sure how to go about fixing either of these.
Re: Output doesn't display numbers
Quote:
For the checking accounts, it doesn't print at all
If you told it to print, and there is something to print, it will print. So either it was not told to print or there was nothing there to print. Locate the line of code that should cause checking accounts to print and verify that line of code is being called and that there is a value to print.
Quote:
for the savings accounts, it prints zeroes.
Trace the variable backwards from where it prints zero to where it gets assigned a value and see why the assigned value is zero.
Any time you make changes to code, post the new code so we are all looking at the same thing.
Quote:
when looking at the sample output that is given. It clearly displays it. So I'm assuming that it's meant to be displayed
When I weigh out black and white instructions against a sample output, and there is a conflict, my first idea is to ask the source in charge what to do. But given a guess I would go with the instructions and do what I was told rather than try to conform to what a sample output seems to do. But in the end you have the instructions and the sample to argue your side of the story with. Just try plan A first and ask someone.
Re: Output doesn't display numbers
I agree with jps - the instructions make sense in that all Checking accounts have the same interest rate, so it's not the sort of thing you would want to clutter toString() with. In *neither* class is the interest earned to be included. Note though, that when the instructions say this or that should not be printed as part of toString(), they mean should not be *included* in the String returned by that method. toString() doesn't print anything.
It's a quibble, but it's one your teacher should take seriously. It's never too early to start writing one method to do one specific thing. (ie determine a descriptive string, or print it possibly as part of some longer output. but not both.) And never too early to describe and talk about methods in terms of the one simple thing that they do.
---
Having said that, you are at liberty to sprinkle println() about as much as you like while writing the code. And, in fact, that might be needed to follow jps' advice to check that specific lines of code do get called and what the values of variables are at that point.