Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-07-2008, 08:29 PM
Member
 
Join Date: Apr 2008
Posts: 10
StealthRT is on a distinguished road
[SOLVED] Using classes and overriding one class for another
Hey all i am having problems with my current asignment in my java class. I just cant seem to grasp the concept of getting values in one class but not getting the same (or overriding) in another class that calls it. Here is the problem for my class:
Quote:
Create class SeniorOccupant that extends class Occupant. Class SeniorOccupant contains two pricate boolean instance variables, AARP & AAA. Provide "get" methods for both instance variables.

Class SeniorOccupant also overrides the Occupant class' getTotalCharge() method. If either (but not both) SeniorOccupant class instance variables are true, then the getTotalCharge() method should return a double value that is 90% of the value returned by the getTotalCharge() method in the Occupant class. If both instance variables are true, then the getTotalCharge() method should return a double value that is 85% of the value returned by the getTotalCharge() method in the Occupant class. If neither instance variables are true, then the getTotalCharge() method should truen the same double value that is returned by the getTotalCharge() method in the Occupant class.

Class SeniorOccupant should also override the Occupant class' toString() method. For the additional instance variables, include the variable names (AARP and AAA) and their respective values. Additionally, method toString() should return the correct totalCharge value i.e. the total charge should be the same as the value returned by the getTotalCharge() method.
And here is the Occupant class code:
Code:
import java.text.DecimalFormat; class Occupant { final int adultCharge = 30; //------------------------- final int childCharge = 10; //----Define static numbers final double motelTax = .10; //------------------------- int intTotal; //------------------------------ int intAdults; //---Define variable declaration int intChild; // - int intNights; // - int intBase; // - int intTax; //------------------------------ public Occupant(int adults, int children, int nights) { intAdults = adults; intChild = children; intNights = nights; } public int getAdults() { return intAdults; } public int getChildren() { return intChild; } public int getNights() { return intNights; } public double getAdultCharge() { intAdults = getAdults() * adultCharge * getNights(); return intAdults; } public double getChildCharge() { intChild = getChildren() * childCharge * getNights(); return intChild; } public double getBaseCharge() { intBase = getAdults() + getChildren(); return intBase; } public double getMotelTax() { double intTax = intBase * motelTax; return intTax; } public double getTotalCharge() { intTotal = intBase + intTax; return intTotal; } public String toString() { DecimalFormat dollars = new DecimalFormat("$###.00"); return "# of Adults: " + intAdults + "\n" + "# of Children: " + intChild + "\n" + "# of Nights: " + intNights + "\n" + "Adult Charge: " + dollars.format(getAdultCharge()) + "\n" + "Child Charge: " + dollars.format(getChildCharge()) + "\n" + "Base Charge: " + dollars.format(getBaseCharge()) + "\n" + "Tax: " + dollars.format(getMotelTax()) + "\n" + "Total: " + dollars.format(getTotalCharge()) + "\n"; } }
And this is the SeniorOccupant code im currently working on (which i have no idea if i even started right...):
Code:
public class SeniorOccupant extends Occupant { private Occupant occ; private boolean AARP; private boolean AAA; public SeniorOccupant(int inNum1, int inNum2, int inNum3, boolean inTF1, boolean inTF2) { super(inNum1, inNum2, inNum3); AARP = inTF1; AAA = inTF2; } public boolean getAARP() { return AARP; } public boolean getAAA() { return AAA; } public double checkTF() { if (AARP || true) { return occ.getTotalCharge() * .90; } else if (AARP && AAA) { return occ.getTotalCharge() * .85; } else { return occ.getTotalCharge(); } } public String toString() { return super.toString() + "AARP: " + getAARP() + "\n" + "AAA: " + getAAA() + "\n"; } }
And here is the "test" code:
Code:
public class TestClass10 { public static void main(String args[]) { Occupant o1 = new Occupant(1,2,3); Occupant o2 = new Occupant(4,0,4); SeniorOccupant o3 = new SeniorOccupant(1,2,3,true,false); SeniorOccupant o4 = new SeniorOccupant(1,2,3,false,true); SeniorOccupant o5 = new SeniorOccupant(1,2,3,true,true); SeniorOccupant o6 = new SeniorOccupant(1,2,3,false,false); displayOccupant(o1); displayOccupant(o2); displayOccupant(o3); displayOccupant(o4); displayOccupant(o5); displayOccupant(o6); } private static void displayOccupant(Occupant o) { System.out.println(o); System.out.println("Total charge: " + o.getTotalCharge() + "\n\n"); } }
Any help would be great as i am brain dead right now!

David

Last edited by StealthRT : 04-07-2008 at 11:17 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-08-2008, 03:24 AM
Member
 
Join Date: Apr 2008
Posts: 10
StealthRT is on a distinguished road
Solved!
Code:
public class SeniorOccupant extends Occupant { private boolean AARP; private boolean AAA; public SeniorOccupant(int theAdults, int theChildren, int nights, boolean inTF1, boolean inTF2) { super(theAdults, theChildren, nights); AARP = inTF1; AAA = inTF2; } public boolean getAARP() { return AARP; } public boolean getAAA() { return AAA; } public double getTotalCharge() { if (AARP && AAA) { return super.getTotalCharge() * .90; } else if (AARP || AAA) { return super.getTotalCharge() * .85; } else { return super.getTotalCharge(); } } public String toString() { return super.toString() + "AARP: " + getAARP() + "\n" + "AAA: " + getAAA() + "\n"; } }
Code:
import java.text.DecimalFormat; class Occupant { private int adults; private int children; private int numberNights; final double MOTEL_TAX_RATE = .10; final double ADULT_RATE = 25; final double CHILD_RATE = 10; final double ADULT_RATE_DISCOUNT = 25; final double CHILD_CHARGE_DISCOUNT = 8; final double BASE_CHARGE_DISCOUT = .15; int intTotal; int intAdults; int intChild; int intBase; int intTax; public Occupant(int theAdults, int theChildren, int nights) { adults = theAdults; children = theChildren; numberNights = nights; } public int getAdults() { return adults; } public int getChildren() { return children; } public int getNights() { return numberNights; } public double getAdultCharge() { if (adults >= 3) { double intAdults = getAdults() * ADULT_RATE_DISCOUNT * getNights(); return intAdults; } else { double intAdults = getAdults() * ADULT_RATE * getNights(); return intAdults; } } public double getChildCharge() { if (children >= 3) { double intChild = getChildren() * CHILD_CHARGE_DISCOUNT * getNights(); return intChild; } else { double intChild = getChildren() * CHILD_RATE * getNights(); return intChild; } } public double getBaseCharge() { if (numberNights >= 3) { double intBase = (getAdultCharge() + getChildCharge()) * BASE_CHARGE_DISCOUT; return intBase; } else { double intBase = getAdultCharge() + getChildCharge(); return intBase; } } public double getMotelTax() { double intTax = getBaseCharge() * MOTEL_TAX_RATE; return intTax; } public double getTotalCharge() { double intTotal = getBaseCharge() + getMotelTax(); return intTotal; } public String toString() { DecimalFormat dollars = new DecimalFormat("$###.00"); return "# of Adults: " + adults + "\n" + "# of Children: " + children + "\n" + "# of Nights: " + numberNights + "\n" + "Adult Charge: " + dollars.format(getAdultCharge()) + "\n" + "Child Charge: " + dollars.format(getChildCharge()) + "\n" + "Base Charge: " + dollars.format(getBaseCharge()) + "\n" + "Tax: " + dollars.format(getMotelTax()) + "\n" + "Total: " + dollars.format(getTotalCharge()) + "\n"; } }
David
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-08-2008, 07:34 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Stealth, it's great to see you solved your own question.

Welcome to the Forums!
See you around!
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-08-2008, 09:12 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,402
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Yes it's, nice to see that you have solved the problem by yourself. I'm sure you have learn something interesting, isn't it?

Because of you solved the problem, please mark the thread solved, from the Thread Tools menu of the thread.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
using elements from other classes Camden New To Java 1 03-21-2008 09:25 AM
is overriding static method possible raghu Advanced Java 1 01-22-2008 02:38 AM
Using a JAR from other classes Joe2003 Advanced Java 1 01-02-2008 09:08 PM
wrapper classes sireesha New To Java 5 12-11-2007 11:45 PM
When do we use inner classes? cruxblack New To Java 5 08-10-2007 07:00 PM


All times are GMT +3. The time now is 04:42 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org