6 Replies - 921 Views - Last Post: 11 June 2012 - 11:48 AM Rate Topic: -----

#1 thisguyrighthere  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-June 12

problem with Inheritance and Polymorphism

Posted 10 June 2012 - 06:20 PM

I'm on chapter 9(Inheritance and Polymorphism) of this textbook that teaches java and I need to see how this program would be written:

Create PErsonal Acct and BusinessAcct classes that inherit the Account class presented Chapter 8(Classes and Object Oriented Development). A Personal account requires a minimum balance of $100. If the balance falls below this amount then $2.00 is charged(withdrawn) to the account. A business account requires a minimum balance of $500, otherwise the account is charged $10. Create Client Code to test the classes.


I tried using strings but my efforts were futile.

Thanks so much everyone!

Is This A Good Question/Topic? 0
  • +

Replies To: problem with Inheritance and Polymorphism

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: problem with Inheritance and Polymorphism

Posted 10 June 2012 - 07:42 PM

View Postthisguyrighthere, on 10 June 2012 - 09:20 PM, said:

I tried using strings but my efforts were futile.

What do you mean ? What is the relation between String and extending classes ?
Was This Post Helpful? 0
  • +
  • -

#3 thisguyrighthere  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-June 12

Re: problem with Inheritance and Polymorphism

Posted 10 June 2012 - 07:58 PM

I used strings in the program before, which looks like this:

import java.util.*;
import java.lang.*;
 
public class Chapter_9_1 {
    
    public static void main(String[] args) {
   
    Scanner input = new Scanner(System.in);
    String answer;
    String Group1 = "Science Department:\nMembers: John - 1337H4X\n         Jessica - 8192354\n         Dick - 1102934\n"
    + "         Bill - L291A72\n         Sally - 88NG109";
    String Group2 = "English Department:\nMembers: Tyler - 81U03MW\n         Gregory - 902RE76\n         Tim - AA465D3\n"
    + "         Frank - 876EH93\n         Krystal - KF00UE1";
   
    do{
System.out.print("Enter a Faculty or an employee number or 'List' to see groups and members or 'quit' to quit: ");
answer = input.nextLine();
answer.trim();
if(answer.equalsIgnoreCase("sciencedepartment") || answer.equalsIgnoreCase("science department")){
System.out.println(Group1);
}else if(answer.equalsIgnoreCase("englishdepartment") || answer.equalsIgnoreCase("englishdepartment")){
System.out.println(Group2);
}else if(answer.equalsIgnoreCase("list")){
System.out.println(Group1);
System.out.println(Group2);
}else if(answer.equals("1337H4X")){
System.out.println("John, Salary: $35,000, Department: Science, Job Title: Teacher");
}else if(answer.equals("8192354")){
System.out.println("Jessica, Salary: $35,000, Department: Science, Job Title: Teacher");
}else if(answer.equals("1102934")){
System.out.println("Dick, Salary: $15,000, Department: Science, Job Title: Part-time Teacher");
}else if(answer.equals("L291A72")){
System.out.println("Bill, Salary: $50,000, Department: Science, Job Title: Department Head/Teacher");
}else if(answer.equals("81U03MW")){
System.out.println("Tyler, Salary: $15,000, Department: English, Job Title: Part-time Librarian");
}else if(answer.equals("902RE76")){
System.out.println("Gregory, Salary: $35,000, Department: English, Job Title: Teacher");
}else if(answer.equals("AA465D3")){
System.out.println("Tim, Salary: $50,000, Department: English, Job Title: Department Head");
}else if(answer.equals("876EH93")){
System.out.println("Frank, Salary: $35,000, Department: English, Job Title: Teacher");
}else if(answer.equals("KF00UE1")){
System.out.println("Krystal, Salary: $35,000, Department: English, Job Title: Teacher");
}else if(answer.equals("88NG109")){
System.out.println("Sally, Salary: $35,000, Department: Science, Job Tital: Teacher");
}
System.out.println("");
    }while( !(answer.equalsIgnoreCase("quit")) );



    }
}




This was the first exercise in Chap 9, I'm doing self learning so I don't really know where I'm going with it and I've only recently started. I suspected that I should use strings like the program above. (I haven't sent myself my corrupted code for the one I'm doing right now because my computer at home has problems running jcreator pro or eclipse, so I do everything in school. I want to see how this is done so I can compare it in the lab(I don't have a java class for this I'm literally trying to learn this for fun) and I can't access forums there so I was trying to get it tonight to compare tomorrow.

This post has been edited by no2pencil: 10 June 2012 - 07:58 PM
Reason for edit:: Added code tags; renamed title to be more relative

Was This Post Helpful? 0
  • +
  • -

#4 thisguyrighthere  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-June 12

Re: problem with Inheritance and Polymorphism

Posted 10 June 2012 - 08:12 PM

Now the program I showed was exercise 1, I need help with exercise 2 which is the original problem I've asked for help with.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10819
  • View blog
  • Posts: 40,335
  • Joined: 27-December 08

Re: problem with Inheritance and Polymorphism

Posted 11 June 2012 - 08:05 AM

The assignment says to use a class. Start by designing an Account class. There are some key attributes of the class to keep in mind:
  • minimumBalance
  • currentBalance
  • minBalanceFee
  • accountNumber
  • ownerFirstName
  • ownerLastName


Now let's start with the class:
class Account{
   
    private double minimumBalance;
    private double currentBalance;
    //now you fill in the rest of the instance variables

    public Account(){
        this.minimumBalance = 0.0;
        //now you initialize the remaining instance variables
    }

    //create additional constructors that accept the 
    //appropriate instance variables

    //finally, add getter/setter methods

}



Now what you want to do in your main() method is store an Account[]. Loops and arrays are your friend. Check out my tutorial Moving Away From Parallel Arrays for more information on class design.
Was This Post Helpful? 1
  • +
  • -

#6 thisguyrighthere  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-June 12

Re: problem with Inheritance and Polymorphism

Posted 11 June 2012 - 10:20 AM

I actually ended up doing it today and I fixed the problems, but honestly thank you anyways I appreciate it. My program was a bit sloppy so this at least cleaned it up a bit in comparison. Thanks! Really appreciate it, I'll probably post it up later tonight when I get the chance to show how I did it and get your opinion on it.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10819
  • View blog
  • Posts: 40,335
  • Joined: 27-December 08

Re: problem with Inheritance and Polymorphism

Posted 11 June 2012 - 11:48 AM

Absolutely! Glad you got it working! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1