Results 1 to 17 of 17
  1. #1
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default Tiny problem with Inheritance

    I have created three classes from the Base class Movie(it also contains the Main method):
    Java Code:
    import java.util.*;
    
    public class Movie
    {
    	String rating = null;
    	String title = null;
    	int ID = 0;
    	int rentTime;
    	double lateFee;
    	double Fee;
    
    	public Movie(String theTitle, String theRating, int theID, int theRentTime)
    	{
    		setRating(theRating);
    		setTitle(theTitle);
    		setID(theID);
    		setRentTime(theRentTime);
    	}
    
    	public String getRating()
    	{
    		return rating;
    	}
    
    	public void setRating(String theRating)
    	{
    		if (theRating != null)
    		{
    			rating = theRating;
    		}
    	}
    
    	public String getTitle()
    	{
    		return title;
    	}
    
    	public void setTitle(String theTitle)
    	{
    		if (theTitle != null)
    		{
    			title = theTitle;
    		}
    	}
    
    	public int getID()
    	{
    		return ID;
    	}
    
    	public void setID(int theID)
    	{
    		if (theID > 0)
    		{
    			ID = theID;
    		}
    	}
    
    	public int getRentTime()
    	{
    		return rentTime;
    	}
    
    	public void setRentTime(int theRentTime)
    	{
    		if (theRentTime > 0)
    		{
    			rentTime = theRentTime;
    		}
    	}
    
    	public int equals(int theID)
    	{
    		if (ID == theID)
    		{
    			System.out.println("Movies are identical.");
    		}
    		return ID;
    	}
    
    	public double calcLateFees(double lateFee, int rentTime, double Fee)
    	{
    		if (rentTime > 1)
    		{
    			Fee = (rentTime * lateFee);
    		}
    		return Fee;
    	}
    
    
    	public void MovieDetails()
    	{
    		System.out.println("The movie title is " + title + ". The rating is " + rating + ". The ID is " + ID + ".");
    		System.out.println("The late fee cost is: " + Fee + ".");
    	}
    
    
    	public static void main(String[] args)
    	{
    		Movie rental1 = new Comedy("Men In Black", "PG 13", 123456789, 5, "Comedy");
    		Movie rental2 = new Action("Lord of the Rings: Return of the King", "PG 13", 223456789, 4, "Action");
    		Movie rental3 = new Drama("Dispicable Me", "G", 243534, 7, "Drama");
    
    		System.out.println("Rental1 details:");
    		rental1.MovieDetails();
    		System.out.println("Rental2 details:");
    		rental2.MovieDetails();
    		System.out.println("Rental3 details:");
    		rental3.MovieDetails();
    	}
    }
    Now the three other files have the same "calcLateFees" method, and the problem is the same with all the classes, so I will just give you one of the classes as the problem part of the code is the same with them all:
    Java Code:
    import java.util.*;
    
    public class Action extends Movie
    {
    	String Genre = null;
    	double lateFee = 3;
    	int rentTime;
    	double Fee;
    
    	public Action(String theRating, String theTitle, int theID, int theRentTime, String theGenre)
    	{
    		super(theRating, theTitle, theID, theRentTime);
    		setGenre(theGenre);
    	}
    
    	public String getGenre()
    	{
    		return Genre;
    	}
    
    	public void setGenre(String theGenre)
    	{
    		if (theGenre != null)
    		{
    			Genre = theGenre;
    		}
    	}
    
    	public double calcLateFees(double lateFee, int rentTime, double Fee)
    	{
    		if (rentTime > 1)
    		{
    			Fee = (rentTime * lateFee);
    		}
    		return Fee;
    	}
    
    	public void MovieDetails()
    	{
    		System.out.println("The movie title is: " + title + ". The rating is: " + rating + ". The ID is: " + ID + ". The rent time is: " + rentTime + ". The genre is: " + Genre + ".");
    		System.out.println("The late fee costs are: " + Fee + ".");
    	}
    }
    My output when I run the Movie is as such:
    Java Code:
    Rental1 details:
    The movie title is: Men In Black. The rating is: PG 13. The ID is: 123456789. The rent time is: 0. The genre is: Comedy.
    The late fee costs are: 0.0.
    Rental2 details:
    The movie title is: Lord of the Rings: Return of the King. The rating is: PG 13. The ID is: 223456789. The rent time is: 0. The genre is: Action.
    The late fee costs are: 0.0.
    Rental3 details:
    The movie title is: Dispicable Me. The rating is: G. The ID is: 243534. The rent time is: 0. The genre is: Drama.
    The late fee is: 0.0.
    
    Process completed.
    I have no errors, so I it is a logic error that is throwing of the program from what I want it to output. I am supposed to track the title, rating, ID, rent period, and Genre. I am also supposed to output the late fee. for some reason, the late fee and rent time will not output. Does anybody see what I did wrong??:confused:
    When in doubt, use a lightsaber

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

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

    Default

    nitpicks

    If a child class overrides a method in the parent class then it does so because it wants to provide a different implementation. However the calcLateFee method in the child class is exactly the same as the parent class.

    The lateFee is an instance variable. Why are you passing it into the method as a parameter.

    Likewise, the method calculates the Fee. Why are you passing that in as a parameter?

  4. #4
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    @Fubarable
    I did not call that method in the Main Method, Should I? I was under the impression that if I called the class, for example, Action, it would then atomaticly call the method calcLateFees becuase it is in the class.

    @Junky
    so you are saying I don't need the calcLateFees in my three differnet classes? I was aware it was teh saem, but needed it there because the lateFees are different for each class. How would I implement three different fee amounts in the class Movie?
    When in doubt, use a lightsaber

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by MaceMan View Post
    @Fubarable
    I did not call that method in the Main Method, Should I? I was under the impression that if I called the class, for example, Action, it would then atomaticly call the method calcLateFees becuase it is in the class.
    There are no "automatics" in programming. Methods will be called only if you call them, period.

  6. #6
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    Okay, thanks. I tried to call the method calcLateFees in the main method by using the code:
    Java Code:
    Action.calcLteFees();
    But the error said,
    "non-static method calcLateFees() cannot be referenced from a static context".

    I can see that the calcLateFees() is a public double, and the main method is void. But in my textbook, they do the exact same thing, and it works(at least in the book). I must be doing something wrong, becuase the book has to be right. How would /i properly call calcLateFees?
    When in doubt, use a lightsaber

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by MaceMan View Post
    Okay, thanks. I tried to call the method calcLateFees in the main method by using the code:
    Java Code:
    Action.calcLteFees();
    But the error said,
    "non-static method calcLateFees() cannot be referenced from a static context".

    I can see that the calcLateFees() is a public double, and the main method is void. But in my textbook, they do the exact same thing, and it works(at least in the book). I must be doing something wrong, becuase the book has to be right. How would /i properly call calcLateFees?
    This is an instance method, not a static method, and as such cannot be called on the class but rather on an object of the class. It really makes no sense to call this on the class anyway. Myself, I'd call it right before I needed it, perhaps right before you print out the data.

  8. #8
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    Okay, I don't quite follow you, as I am new to Java, but I need to knwo how to get the late fee and the rent time to output. Could you perhaps point me in the right direction or something? thanks for your help, btw.
    When in doubt, use a lightsaber

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by MaceMan View Post
    Okay, I don't quite follow you, as I am new to Java, but I need to knwo how to get the late fee and the rent time to output. Could you perhaps point me in the right direction or something? thanks for your help, btw.
    Again, where are you needing to use the late fee? Again, just call this method right before you need to use late fee. Please try this, and if it doesn't work, post back.

  10. #10
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    I am really sorry that I still don't understand what you are asking me to do. (Frustrating to you, so, sorry.)

    I need to output the Late Fee costs when I am outputing everything else. I don't know where I am supposed to call calcLateFees to do this. Acording to my code, the program should know what Fee is, becuase I in calcLateFees() I set Fee = (rentTime * lateFee).
    I then hava a System.out.println statement, which says to output Fee. Why won't this work for the compiler?
    When in doubt, use a lightsaber

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

    Default

    Quote Originally Posted by MaceMan View Post
    but needed it there because the lateFees are different for each class.
    So have your subclasses each set the lateFee to a different value, intead of each subclass shadowing the variable in the parent class.

  12. #12
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    Each of my subclasses set lateFee to a different value. I use a statement, but should I actually do it using a mutator method?
    Like so?

    public double setLateFee()
    {
    lateFee = 2;
    }

    instead of just having the statement at the beginging of the class:

    double lateFee = 2;
    When in doubt, use a lightsaber

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

    Default

    No, each class has its own lateFee variable. Don't do that.
    Java Code:
    class Parent {
        int var;
    }
    
    class ChildOne extends Parent {
        public ChildOne() {
            var = 10;
        }
    }
    
    class ChildTwo extends Parent {
        public ChildTwo() {
            var = 50;
        }
    }

  14. #14
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    Okay, let me try that and I will get back.....
    When in doubt, use a lightsaber

  15. #15
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    Okay, I did that. It has no errors, but still does not output the Fee and rent time.:mad:
    When in doubt, use a lightsaber

  16. #16
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    I am going to put the original question here, so people can see just what I am supposed to do:

    AVA Create a class named Movie that can be used with your video rental business. The Movie class should track the Motion Picture Association of America (MPAA) rating (i.g., Rated G, PG-13,R), ID Number, and movie title with appropriate accessor, and mutator methods. Also create an equals() method that overrides Object's equals() method, where two movies are equal if their ID number is identical. Next, create three additional classes named Action, Comedy, and Drama that are derived from Movie. Finally, create an overridden method named calcLateFees that takes as input the number of days a movie is late and returns the late fee for that movie. The default late fee is $2/day. Action movies have a late fee of $3/day, comedies are $2.50/day, and dramas are $2/day. Test your classes from a main method.

    Should I use an abstract calcLateFees in Movie and then fill it out in the other three classes?
    When in doubt, use a lightsaber

  17. #17
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    Come on guys, I need some help here! you all have been great, but I have that one problem with the Fee not outputing.
    When in doubt, use a lightsaber

Similar Threads

  1. Tiny Problem with JFrame
    By mr_anderson in forum AWT / Swing
    Replies: 3
    Last Post: 05-27-2010, 08:50 PM
  2. Client Server program, a tiny problem.
    By skarosg3 in forum Networking
    Replies: 12
    Last Post: 05-27-2010, 01:03 PM
  3. Inheritance Problem
    By g2beastie in forum New To Java
    Replies: 4
    Last Post: 03-25-2010, 08:23 PM
  4. inheritance problem
    By er1c550n20 in forum New To Java
    Replies: 2
    Last Post: 03-10-2010, 06:01 PM
  5. 'Tiny' 3D Problem
    By Supamagier in forum Advanced Java
    Replies: 2
    Last Post: 09-03-2009, 09:34 AM

Posting Permissions

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