Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    jbdonnelly is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default 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.

  2. #2
    cultclassic is offline Member
    Join Date
    Mar 2011
    Posts
    64
    Rep Power
    0

    Default

    The balance is 0.0 because you never set added a balance to the accounts...

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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?

  5. #5
    jbdonnelly is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default

    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.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    Generally people do not follow links. If you cannot be bothered providing relevant information, why should we be bothered trying to help?

  7. #7
    jbdonnelly is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Generally people do not follow links. If you cannot be bothered providing relevant information, why should we be bothered trying to help?

    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.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    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.

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Also posted to dreamincode.net.

  10. #10
    jbdonnelly is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    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.
    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.

  11. #11
    jbdonnelly is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Also posted to dreamincode.net.
    Yes it is. I'm trying to find help. I've already pulled out all my hair and could really use the guidance.

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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?

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  14. #14
    jbdonnelly is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default

    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.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Not much to add, but there is a bug here:
    Java Code:
        public void setAccNumber(int accNumber)
        {
            if (accNumber > 0 )  
            {  
                this.accBalance = accBalance;
            }
        }
    Can you spot what it is?

  16. #16
    jbdonnelly is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default

    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:
    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");
        }
    }
    Did I do that properly?

  17. #17
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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
      }
    }

  18. #18
    jbdonnelly is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default

    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?

  19. #19
    rugger06 is offline Member
    Join Date
    Apr 2011
    Location
    Florida
    Posts
    6
    Rep Power
    0

    Default 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!

  20. #20
    jbdonnelly is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default

    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. :)

Page 1 of 2 12 LastLast

Similar Threads

  1. JAI: Java Advanced Imaging Install problem
    By panagath in forum New To Java
    Replies: 2
    Last Post: 02-13-2011, 01:29 AM
  2. Inheritance Problem
    By kazumahits in forum New To Java
    Replies: 5
    Last Post: 01-11-2011, 03:46 PM
  3. Inheritance problem
    By ZuperZombie in forum Advanced Java
    Replies: 0
    Last Post: 04-02-2010, 03:55 PM
  4. inheritance problem
    By er1c550n20 in forum New To Java
    Replies: 2
    Last Post: 03-10-2010, 06:01 PM
  5. Arrays Problem (Advanced Java...Need Help)
    By Zebra in forum New To Java
    Replies: 9
    Last Post: 05-02-2008, 01:26 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •