Results 1 to 20 of 24
- 03-31-2011, 02:22 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 11
- Rep Power
- 0
Advanced Inheritance Concepts Problem
I have been assigned this project in my online Java programming class. I'm not here to ask anyone to do my homework, I'm always happy to do it myself, but I have come to a wall with this one.
I'm not sure exactly what it wrong with the code, but when I run the program I'm not getting the proper output.
The two problems I know I'm having is that I cannot find a way to set my account balance, it's always at "0.0" and I cannot get the proper line breaks without loosing some of my output.
I would to explain more of my problem, but I'm not even sure of everything that's not working properly.
I have been trying to code it for two days straight now and just cannot seem to get it right. I have tried to ask my teacher (who never responds to emails or forum questions), I read and re-read my textbook, and I have clicked on just about every Google link I could find, but I still cannot figure out what I am doing wrong. I'm still very new to Java and haven't quite got the hang of it. Any help would be extremely appreciated and would be helping me more than you know.
This is the assignment details:
Advanced Inheritance Concepts Assignment
The sample output is given as a link within the assignment.
This is what I have coded so far:
Account class:
Java Code:public abstract class Account { //creates protected variables protected int accNumber; protected double accBalance; //get methods public int getAccNumber(){return accNumber;} public double getAccBalance(){return accBalance;} //set methods public void setAccNumber(int accNumber) { if (accNumber > 0 ) { this.accBalance = accBalance; } } public void setAccBalance(double accBalance) { if (accBalance > 0 ) { this.accBalance = accBalance; } } public Account(int aNumber) { accNumber = aNumber; accBalance = 0.0; } public String toString() { return ("Account # = " + accNumber + "\n" + "Account Balance = " + accBalance + "\n"); } public abstract double computeInterest(int interest); }
Checking class:
Java Code:public class Checking extends Account { public String toString() { return "Checking\n" + super.toString(); } public Checking (int accNumber) { super(accNumber); } public double computeInterest (int interest) { return ((getAccBalance() - 700) * .02) /* * interest*/; } }
Savings class:
Java Code:public class Savings extends Account { //creates private variable private double intRate; //get method public double getIntRate(){return intRate;} //set method public void setIntRate(double intRate) { if (intRate > 0 ) { this.intRate = intRate; } } public Savings (int accNumber, double intRate) { super(accNumber); } public double computeInterest (int interest) { return Math.pow((1 + intRate),(interest) * getAccBalance() - getAccBalance()); } public String toString() { return "Savings\n" + super.toString(); } }
AccountArray class:
Java Code:public class AccountArray { public static void main(String [] args) { Account [] ref = new Account [10]; ref[0] = new Checking (100); ref[1] = new Checking (101); ref[2] = new Checking (102); ref[3] = new Checking (103); ref[4] = new Checking (104); ref[5] = new Savings (105, .02); ref[6] = new Savings (106, .02); ref[7] = new Savings (107, .02); ref[8] = new Savings (108, .02); ref[9] = new Savings (109, .02); for (int i = 0; i < ref.length; i++) { ref[i].computeInterest(3); } for (int i = 0; i < ref.length; i++) { System.out.println(ref[i].toString() + "Interest earned = "); } } }
Again, any direction would be greatly appreciated. Thank you all in advance.
- 03-31-2011, 02:52 AM #2
Member
- Join Date
- Mar 2011
- Posts
- 64
- Rep Power
- 0
The balance is 0.0 because you never set added a balance to the accounts...
- 03-31-2011, 02:59 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
I cannot find a way to set my account balance, it's always at "0.0"
I can't see any attempt in that code to alter the account balance.
I do notice that you have a setAccBalance() method. Write some code to call that method. And ensure that it is being called and that the account is in the state you expect it to be in.
Java Code:public void setAccBalance(double accBalance) { [color=blue]System.out.printf("setBalance() called on %d with accBalance=%f%n", accNumber, accBalance);[/color] if (accBalance > 0 ) { this.accBalance = accBalance; [color=blue]System.out.println("Balance updated!");[/color] } }Last edited by pbrockway2; 03-31-2011 at 03:03 AM.
- 03-31-2011, 03:03 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
I cannot get the proper line breaks without loosing some of my output.
What are "proper" line breaks? Ie what output you you get with this code, and what output do you intend to get?
- 03-31-2011, 03:12 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 11
- Rep Power
- 0
The output I need to have is here.
Last edited by jbdonnelly; 03-31-2011 at 03:16 AM. Reason: Sorry, wrong link. I fixed it.
- 03-31-2011, 03:16 AM #6
Generally people do not follow links. If you cannot be bothered providing relevant information, why should we be bothered trying to help?
- 03-31-2011, 03:21 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 11
- Rep Power
- 0
1. This is my first time asking for help in forums, so forgive me if I don't know how things generally go around here.
2. This is perfectly relevant information in my opinion. The link is to a picture for what the output is supposed to look like according to my teacher. I thought it would be better to show what it was exactly supposed to look like, rather than risking making a mistake.
- 03-31-2011, 03:33 AM #8
The onus is upon you to make it as easy as possible for us to discern what your problem is and to determine how best to help you. I for one will not follow a link and as I said many others won't either. Therefore all those who do not follow the link will not/cannot help you thus greatly reducing your chances of resolving the problem. But whatever floats your boat.
- 03-31-2011, 04:01 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Also posted to dreamincode.net.
- 03-31-2011, 04:01 AM #10
Member
- Join Date
- Mar 2011
- Posts
- 11
- Rep Power
- 0
This is what the output is supposed to look like:
Checking
Account # = 100
Account Balance = 1000.0
Interest Earned = 18.0
Checking
Account # = 101
Account Balance = 2000.0
Interest Earned = 78.0
Checking
Account # = 102
Account Balance = 3000.0
Interest Earned = 138.0
Checking
Account # = 103
Account Balance = 4000.0
Interest Earned = 198.0
Checking
Account # = 104
Account Balance = 5000.0
Interest Earned = 258.0
Savings
Account # = 105
Account Balance = 6000.0
Savings rate: 0.02
Interest Earned = 367.2480000000005
Savings
Account # = 106
Account Balance = 7000.0
Savings rate: 0.02
Interest Earned = 428.45600000000104
Savings
Account # = 107
Account Balance = 8000.0
Savings rate: 0.02
Interest Earned = 489.66400000000067
Savings
Account # = 108
Account Balance = 9000.0
Savings rate: 0.02
Interest Earned = 550.8720000000012
Savings
Account # = 109
Account Balance = 10000.0
Savings rate: 0.02
Interest Earned = 612.0800000000017
If I missed a couple of zeros, I apologize. I had to type it all out.
- 03-31-2011, 04:03 AM #11
Member
- Join Date
- Mar 2011
- Posts
- 11
- Rep Power
- 0
- 03-31-2011, 04:31 AM #12
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
It's a good idea to be forthright when posting to other sites.
I would add to the comments made there that it's hard to follow a conversation when you are not even be aware of half of its venue. A forum is a place of dialogue: you get help as a result of discussion rather than as "wisdom" dispensed by inspired oracles.
In that vein did you write code to call the setAccBalance() method? WHat happened? Especially if you used the code I suggested did you notice anything odd or useful?
------
I had a hidden agenda when I asked about the output. I know that when I'm trying to solve a problem the real problem is sometimes the fact that I'm unclear about what the problem is! I thought that by expressing, in your own words, both the desired and actual output you might end up with more clarity.
Also I'm too lazy to follow the links or compile the code.
SO how does the output you end up with differ from the output you posted?
- 03-31-2011, 04:32 AM #13
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Not following links isn't really a forum thing, it's generally good for safe practice when online. If you just follow any link you are more likely to pick up a virus. I understand you where posting a link to help, however; you won't get as much help when posting links.
- 03-31-2011, 04:53 AM #14
Member
- Join Date
- Mar 2011
- Posts
- 11
- Rep Power
- 0
Thank you, pbrock, I really appreciate your response. I gotta get to sleep, but I will let you know what I came up with tomorrow.
- 03-31-2011, 09:11 AM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Not much to add, but there is a bug here:
Can you spot what it is?Java Code:public void setAccNumber(int accNumber) { if (accNumber > 0 ) { this.accBalance = accBalance; } }
- 03-31-2011, 09:29 PM #16
Member
- Join Date
- Mar 2011
- Posts
- 11
- Rep Power
- 0
Okay. I'm still really lost with this assignment. I've been looking it over and I have narrowed down the specific elements of the assignment that I don't understand.
For the Account class, step e.) says to create 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.
This is the current code I have:
Did I do that properly?Java Code:public abstract class Account { //creates protected variables protected int accNumber; protected double accBalance; //get methods public int getAccNumber(){return accNumber;} public double getAccBalance(){return accBalance;} //set methods public void setAccNumber(int accNumber) { if (accNumber > 0 ) { this.accBalance = accBalance; } } public void setAccBalance(double accBalance) { if (accBalance > 0 ) { this.accBalance = accBalance; } } public Account(int aNumber) { accNumber = aNumber; accBalance = 0.0; } public abstract double computeInterest(double interestEarned); public String toString() { return ("Account # = " + accNumber + "\n" + "Account Balance = " + accBalance + "\n"); } }
- 03-31-2011, 09:40 PM #17
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You can easily test this by subclassing and printing an instance of the sublcass(note you will have to handle the abstract method, either override it, or just make it abstract while testing)
Java Code:public class x extends Account{ public abstract computeInterest(/**args**/); public static void main(String[] args){ //create instance of x //print instance of x } }
- 04-01-2011, 02:07 AM #18
Member
- Join Date
- Mar 2011
- Posts
- 11
- Rep Power
- 0
First, I would like to say thank you to everyone who posted.
Now, I have figured out the assignment. Or, at least I was able to make it print out properly. I'm not entirely sure if it's exact to what my teacher wanted, but it works and I'm going to stick with that.
I do have one last problem though. When I run the AccountArray class I am missing the first 3-4 lines of the output. This happens when I add "\n" for the lines breaks. If I take out all of the line breaks, all of the output is there, but it's not separated like it's supposed to be.
Here is my current code:
Account class:
Java Code:public abstract class Account { //creates protected variables protected int accNumber; protected double accBalance; //get methods public int getAccNumber(){return accNumber;} public double getAccBalance(){return accBalance;} //set methods public void setAccNumber(int accNumber) { if (accNumber > 0 ) { this.accNumber = accNumber; } accNumber = 0; } public void setAccBalance(double accBalance) { if (accBalance > 0 ) { this.accBalance = accBalance; } accBalance = 0.0; } public Account(int aNumber) { accNumber = aNumber; accBalance = 0.0; } public abstract double computeInterest(int interestPeriod); public String toString() { return ("\nAccount # = " + accNumber + "\nAccount Balance = " + accBalance); } }
Checking class:
Java Code:public class Checking extends Account { public Checking (int accNumber) { super(accNumber); } public double computeInterest (int interestPeriod) { double interestEarned = ((getAccBalance() - 700)* .02)* interestPeriod; return interestEarned; } public String toString() { return "\nChecking" + super.toString(); } }
Savings class:
Java Code:public class Savings extends Account { //creates private variable private double intRate = 0.02; //get method public double getIntRate(){return intRate;} //set method public void setIntRate(double intRate) { if (intRate > 0 ) { this.intRate = intRate; } intRate = .02; } public Savings (int accNumber, double intRate) { super(accNumber); } public double computeInterest (int interestPeriod) { double interestEarned = Math.pow(1.02,3) * getAccBalance() - getAccBalance(); return interestEarned; } public String toString() { return "\nSavings" + super.toString() + "\nSavings rate: " + intRate; } }
AccountArray class:
Java Code:public class AccountArray { public static void main(String [] args) { Account [] ref = new Account [10]; ref[0] = new Checking (100); ref[1] = new Checking (101); ref[2] = new Checking (102); ref[3] = new Checking (103); ref[4] = new Checking (104); ref[5] = new Savings (105, .02); ref[6] = new Savings (106, .02); ref[7] = new Savings (107, .02); ref[8] = new Savings (108, .02); ref[9] = new Savings (109, .02); ref[0].setAccBalance(1000); ref[1].setAccBalance(2000); ref[2].setAccBalance(3000); ref[3].setAccBalance(4000); ref[4].setAccBalance(5000); ref[5].setAccBalance(6000); ref[6].setAccBalance(7000); ref[7].setAccBalance(8000); ref[8].setAccBalance(9000); ref[9].setAccBalance(10000); for (int i = 0; i < ref.length; i++) { //ref[i].getAccNumber(); //ref[i].getAccBalance(); } for (int i = 0; i < ref.length; i++) { System.out.println(ref[i].toString() + "\nInterest earned = " + ref[i].computeInterest(3)); } } }
With where the line breaks are now, this is what it prints out:
Account Balance = 2000.0
Interest earned = 78.0
Checking
Account # = 102
Account Balance = 3000.0
Interest earned = 138.0
Checking
Account # = 103
Account Balance = 4000.0
Interest earned = 198.0
Checking
Account # = 104
Account Balance = 5000.0
Interest earned = 258.0
Savings
Account # = 105
Account Balance = 6000.0
Savings rate: 0.02
Interest earned = 367.2480000000005
Savings
Account # = 106
Account Balance = 7000.0
Savings rate: 0.02
Interest earned = 428.45600000000104
Savings
Account # = 107
Account Balance = 8000.0
Savings rate: 0.02
Interest earned = 489.66400000000067
Savings
Account # = 108
Account Balance = 9000.0
Savings rate: 0.02
Interest earned = 550.8720000000012
Savings
Account # = 109
Account Balance = 10000.0
Savings rate: 0.02
Interest earned = 612.0800000000017
Notice that
Checking
Account # = 100
Account Balance = 1000.0
Interest Earned = 18.0
Checking
Account # = 101
are completely missing.
Any ideas as to why it is doing this?
- 04-01-2011, 03:39 AM #19
Member
- Join Date
- Apr 2011
- Location
- Florida
- Posts
- 6
- Rep Power
- 0
Try this
I feel your pain. I just finished this assignment myself. Here is what worked for me:
[QUOTE=jbdonnelly;193571]
Account class:
Java Code:public abstract class Account { public String toString() { return ("\nAccount # = " + accNumber + "\nAccount Balance = " + accBalance[COLOR="RoyalBlue"]+ "\n"[/COLOR]);[COLOR="RoyalBlue"] //add \n at the end of the statement[/COLOR] } }
Savings class:
Java Code:public class Savings extends Account { public String toString() { return "[COLOR="Red"]\n[/COLOR]Savings" + super.toString() + "\nSavings rate: " + intRate[COLOR="RoyalBlue"] + "\n"[/COLOR]; }[COLOR="RoyalBlue"]//delete the first \n and then add a "\n" to the end of the statement[/COLOR] }
AccountArray class:
Java Code:public class AccountArray { for (int i = 0; i < ref.length; i++) { System.out.println(ref[i].toString() + [COLOR="red"]"\n[/COLOR]Interest earned = " + ref[i].computeInterest(3)); [COLOR="Blue"]//delete the \n before Interest earned[/COLOR] } } }
It worked for me, I hope it works for you! Good luck!
- 04-01-2011, 04:05 AM #20
Member
- Join Date
- Mar 2011
- Posts
- 11
- Rep Power
- 0
Thank you for reply, rugger. I tried that and it printed out the same. I believe that it's my scrolling buffer that's causing the problem. Unfortunately BlueJ doesn't have an option to change it. Someone else told me that it looks like on their end, so I'm just going to go with that. Or download another program to test it in.
Thank you again though. :)
Similar Threads
-
JAI: Java Advanced Imaging Install problem
By panagath in forum New To JavaReplies: 2Last Post: 02-13-2011, 01:29 AM -
Inheritance Problem
By kazumahits in forum New To JavaReplies: 5Last Post: 01-11-2011, 03:46 PM -
Inheritance problem
By ZuperZombie in forum Advanced JavaReplies: 0Last Post: 04-02-2010, 03:55 PM -
inheritance problem
By er1c550n20 in forum New To JavaReplies: 2Last Post: 03-10-2010, 06:01 PM -
Arrays Problem (Advanced Java...Need Help)
By Zebra in forum New To JavaReplies: 9Last Post: 05-02-2008, 01:26 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks