Results 1 to 4 of 4
  1. #1
    StealthRT is offline Member
    Join Date
    Apr 2008
    Posts
    10
    Rep Power
    0

    Post [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:
    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:
    Java 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...):
    Java 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:
    Java 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 10:17 PM.

  2. #2
    StealthRT is offline Member
    Join Date
    Apr 2008
    Posts
    10
    Rep Power
    0

    Default

    Solved! :)
    Java 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";
        }
    }
    Java 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

  3. #3
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    Stealth, it's great to see you solved your own question. ;)

    Welcome to the Forums!
    See you around! :)
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

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

Similar Threads

  1. using elements from other classes
    By Camden in forum New To Java
    Replies: 1
    Last Post: 03-21-2008, 08:25 AM
  2. is overriding static method possible
    By raghu in forum Advanced Java
    Replies: 1
    Last Post: 01-22-2008, 01:38 AM
  3. Using a JAR from other classes
    By Joe2003 in forum Advanced Java
    Replies: 1
    Last Post: 01-02-2008, 08:08 PM
  4. wrapper classes
    By sireesha in forum New To Java
    Replies: 5
    Last Post: 12-11-2007, 10:45 PM
  5. When do we use inner classes?
    By cruxblack in forum New To Java
    Replies: 5
    Last Post: 08-10-2007, 06:00 PM

Posting Permissions

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