Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    CGHMN is offline Member
    Join Date
    Jun 2011
    Posts
    40
    Rep Power
    0

    Default Anyone feel like helping me out some more?

    Hi everybody, I got this new assignment in, it's proving to be difficult. Basically it builds off the last program I made which was what this topic was all about. Yet it really has nothing to do with the subject other then the code. I have to create 3 classes a Student class which keeps track of the student's major, a Tutor class which keeps an hourly rate, also a smallRoom class which is some stupid room deal that only has room for one person.

    My first and foremost problem is how do I get it to extend my last program? is says right in the class header extends Person, yet when I try to compile it says can not find symbol.

    Here's another tidbit it say's that " the Tutor and Student classes are just made up of a constructor and a toString method." So am I suppose to indicate from that this is going to be relatively small program? and what exactly do I code new, and what am I suppose to inherit from the Person class is kind of my issue.
    Last edited by CGHMN; 06-27-2011 at 07:36 AM.

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Provide us with the full error so we can help you more. You have given us nothing worthwhile. Also if you don't include code when you give us the error, we can only tell you what the error means. Which is ultimately a waste of time because you could read the error code yourself.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  3. #3
    CGHMN is offline Member
    Join Date
    Jun 2011
    Posts
    40
    Rep Power
    0

    Default

    Okay here's the code and the error, the only reason i left it out is because last time people got all bugged at all the code I was posting.

    I'll give the Person class first this is what I made for the last assignment and I am suppose to build off it with two new classes Tutor ans Student.
    Java Code:
    public class Person extends Object
    {
      private String lastName;
      private int Money;
      private boolean mentalState; 
      
      
      public Person(String last, int wealth, boolean happy)
    
    
      {
        lastName = last;
    	 setWealth( wealth );
    	 mentalState = happy;
    	 
    	 
    	 	 	 
      }
      
      public void setLastName( String last )
      {
        lastName = last;
      }
      
      public String getLastName()
      {
        return lastName;
      }
      
      public void setWealth( int wealth )
      {
        Money = ( wealth < 0.0 ) ? 50 : wealth;
      }
      
        public void spend(int value) {
    		  if(value<0)
    		  Money = 500;
    		  else
    		  Money = Money - value;
    		  
    		  while(Money<0)
    		  {
    		    Money = 0;
    		  }	 
        }
     
    
      @Override
      public String toString()
      {
        return String.format(lastName+" has "+Money+" dollars.");
      }
      
      
      
      public void says()
      {
        if(mentalState == true)
    	 System.out.println("My name is "+lastName+" and I am doing great!");
    	 else
    	 System.out.println("My name is "+lastName+" and I could be better.");;
    	 
      }
      
      public boolean canAfford(int wealth)
      {
        if(wealth >= Money)
    	 
    	   return false;
    	else
    	  return true;	
    	 
    	 
      }
        public void earned(int value) {
            
    		  if(value<0)
    		  Money = 500;
    		  else
    		  Money = Money + value;
    		  
        }
      
      public boolean isHappy()
      {
        if(Money>100)
    	 return true;
    	 else
    	 return false;
      }
      public int getDollars()
      {
        return Money;
      }
      
      public void setHappy(boolean happy)
      {
        mentalState = happy;
      }
      
      public String Person(String nameDollars)
      {
        return nameDollars;
      }
    }
    Now here is the Tutor class:
    Java Code:
    public class Tutor extends Person
    {
    	private double hryRate;  // the hourly rate that the tutor charges	
    	private String lstNm;
    	private double money;
    	private boolean smthng;
    	
    	public Tutor(String lastName,double wealth,boolean something,double hourlyRate)
    	{
    	  lstNm = lastName;
    	  money = wealth;
    	  smthng = something;
    	  hryRate = hourlyRate;
    	}
    
    }
    Now when I try to compile it for kicks I get this error :

    Tutor.java:9: cannot find symbol
    symbol : constructor Person()
    location: class Person
    {
    ^
    1 error

    and they are saved in the same directory and all that.

    Also here is the code the assignment gave us this is what we have to construct our program.
    Java Code:
    public class TestSmallRoom
    {
    	public static void main(String [] args)
    	{	
    		Tutor alan = new Tutor("Jackson", 100, true, 26.00);
    		Tutor carol = new Tutor("Bennett", 100, true, -32.00);  // bad rates default to $10.00
    		
    		Student happy = new Student("Smiles", 500, true, "Software Engineering" );
    		Student ming = new Student("Li", 75, false, "Accounting");
    		Student prakash = new Student("Ramanan", 110, false, "Nursing" 		);
    		Student bill = new Student("Clinton", 5000, true, "Poli Sci");
    		
    		alan.earned(52);
    		bill.spend(3300);
    		
    		System.out.println(alan);
    		System.out.println(carol);
    		System.out.println(happy);
    		System.out.println(ming);
    		
    		SmallRoom room1 = new SmallRoom("F105");
    		SmallRoom room2 = new SmallRoom("G110");
    		SmallRoom room3 = new SmallRoom("H300");
    		
    		if (room1.isEmpty())
    			room1.enter(alan);
    
    		if (room2.isEmpty())
    			room2.enter(prakash);
    
    		System.out.println(room1);
    		System.out.println(room2);
    		System.out.println(room3);
    		
    		room1.enter(bill);  // rejected since already occupied
    		
    		Person p = null;
    		
    		// The "leave" method set the room to empty and returns a
    		// reference to whoever was in the room.  Below, the reference
    		// p will point to student prakash and room2 becomes empty.
    		// A null reference is returned if the room was already empty
    		// when the leave method was called.
    		
    		if (room2.isOccupied())
    			p = room2.leave();
    
    			
    		System.out.println("Just left room 2: " + p);
    		System.out.println("Room2 is now: " + room2);
    		
    		SmallRoom room4 = new SmallRoom("L002");
    		room3.enter(bill);
    		
    		room4.enter(room3.leave());  // Bill moves from room3 to room4
    		
    		System.out.println("AT THE END:");
    		System.out.println(room1);
    		System.out.println(room2);
    		System.out.println(room3);
    		System.out.println(room4);
    	}
    }
    I can post the instructions if that helps.

    I guess my main problem is that I do not really understand how to utilize super classes and inheritance
    Last edited by CGHMN; 06-27-2011 at 08:02 AM.

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

    Default

    A constructor has an important job to do: it must set things up so that an instance is "ready to use". (A person must have a name etc)

    When one class extends another (like a Tutor is-a Person) then the *first* thing a constructor must do is call the superclass constructor. The thinking is that to gdt a fully functional tutor we must first have a fully functional person. If you don't call this constructor yourself the compiler will assume there is a no-argument Person() constructor to be used. In your case there is no such constructor, so the compiler grumbles.

    The solution is to call the Person() constructor as the very first line of the Tutor() constructor. This will ensure that you have a person and the rest of the Tutor() constructor will mwke sure the resulting object is a ready-to-go tutor. When you call the Person() constructor you can -and should- pass any arguments that are needed to construct a Person.

  5. #5
    CGHMN is offline Member
    Join Date
    Jun 2011
    Posts
    40
    Rep Power
    0

    Default

    Cool, now I have a Tutor and Student class that compile.
    Java Code:
    public class Tutor extends Person
    {
    	private double hryRate;  // the hourly rate that the tutor charges	
    
    	
    	public Tutor(String last, int wealth, boolean happy)
    	{
    	  super(last,wealth,happy);
    
    	}
    
    }
    So that works now, I just need to figure out how to set everything. I will be coming back to this topic shortly with another issue I appreciate the help.

    it seems TestSmallRoom won't compile it gives the following error.

    symbol : constructor Tutor(java.lang.String,int,boolean,double)
    location: class Tutor
    Tutor alan = new Tutor("Jackson", 100, true, 26.00);
    among many other errors, why can't this seem to see the Tutor class?
    Last edited by CGHMN; 06-27-2011 at 08:31 AM.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    1. There are two ways a class can have a no-argument constructor: either you provide one explicitly, or you don't provide any constructor at all in which case the compiler creates a no-argument constructor for you.

    2. There are two ways a subclass constructor can invoke its direct superclass constructor: either you explicitly invoke a specific superclass constructor, or the compiler inserts a call to the no-argument superclass constructor.

    Since you didn't provide a constructor for Tutor, the compiler has created a no-argument constructor which attempts to invoke a no-argument constructor of Person -- which doesn't exist, because you have created a constructor for Person -- one that takes 3 arguments.

    In slightly less technical terms, a Tutor is-a Person, but as things stand, what values will be given to String last, int wealth and boolean happy when you construct a new Tutor()?

    db

    edit: I need to improve my typing speed
    Last edited by DarrylBurke; 06-27-2011 at 08:34 AM.

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

    Default

    Glad you've got that sorted.

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

    Default

    The declaration of the Tutor() constructor was fine - it needs that something argument. First it calls the person constructor, then it does the right thing with the something.

    Btw you don't need to repeat the surname fields etc. A tutor will have those because it is-a person.

    @darryl And I'm struggling with an Android! hence the random spellig...

  9. #9
    CGHMN is offline Member
    Join Date
    Jun 2011
    Posts
    40
    Rep Power
    0

    Default

    Okay I believe I have the Tutor and Student class finished. However I am confused on how to make smallRoom because it doesn't inherit so i am not sure how to set all these room names and this occupied thing

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

    Default

    Quote Originally Posted by pbrockway2 View Post
    Btw you don't need to repeat the surname fields etc. A tutor will have those because it is-a person.
    Except that they are private.

    Has this been crossposted or is someone else doing the exact same assignment? I have seen this before .

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Quote Originally Posted by pbrockway2 View Post
    @darryl And I'm struggling with an Android! hence the random spellig...
    Well illustrated! But your Android device is likely faster than this office computer (P4/2.4 GHz/768 MB and an old, slow 40 GB HDD)

    db

  12. #12
    CGHMN is offline Member
    Join Date
    Jun 2011
    Posts
    40
    Rep Power
    0

    Default

    Okay I have a problem here, in my smallRoom class I have the following code
    Java Code:
    public class SmallRoom
    {	
    	private Person occupie;  // make up a name for the person who is occupying the room
    	private String roomName;  // this field represents the name of the room
    	
    	public SmallRoom(Person occu, String roomNm)
    	{
    	  occupie = occu;
    	  roomName = roomNm;
    	  
    	  room1(roomNm);
    	}
    	
    	public void room1(String roomNm)
    	{
    	  roomName = roomNm;
    	}
    	
    
    }
    Now I made a dumby method room1 for the smallRoomTest file to accept then and move on to the next error but it didn't it actually caught it and said this:

    symbol : constructor SmallRoom(java.lang.String)
    location: class SmallRoom
    SmallRoom room1 = new SmallRoom("F105");
    ^

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Quote Originally Posted by Junky View Post
    Except that they are private.
    So? The public mutators and accessors are inherited.

    db

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

    Default

    I guess I should have been more specific. There is a potential for problems if the Tutor class attempts to use those variables directly.

  15. #15
    CGHMN is offline Member
    Join Date
    Jun 2011
    Posts
    40
    Rep Power
    0

    Default

    For some reason the TestRoom class is catching a symbol not found error at a method that does exist heres the error

    symbol : constructor SmallRoom(java.lang.String)
    location: class SmallRoom
    SmallRoom room1 = new SmallRoom("F105");
    ^

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Quote Originally Posted by Junky View Post
    I guess I should have been more specific. There is a potential for problems if the Tutor class attempts to use those variables directly.
    Not a problem. Rather, it's the essence of encapsulation.

    db

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

    Default

    It is complaining about a constructor not a method. We don't read minds. Post your SmallRoom class.

  18. #18
    CGHMN is offline Member
    Join Date
    Jun 2011
    Posts
    40
    Rep Power
    0

    Default

    Here's my smallRoom class
    Java Code:
    public class SmallRoom
    {	
    	private Person occupie;  // make up a name for the person who is occupying the room
    	private String roomName;  // this field represents the name of the room
    	
    	public SmallRoom(Person occu, String roomNm)
    	{
    	  occupie = occu;
    	  roomName = roomNm;
    	  
    
    	}
    	
    	public void SmallRoom(String roomNm)
    	{
    	  roomName = roomNm;
    	}
    	
    
    }
    Then when I try to compile TestSmallRoom I get this error

    symbol : constructor SmallRoom(java.lang.String)
    location: class SmallRoom
    SmallRoom room1 = new SmallRoom("F105");

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

    Default

    Java Code:
    public SmallRoom(Person occu, String roomNm)
    
    SmallRoom room1 = new SmallRoom("F105");
    Check the parameters.

  20. #20
    CGHMN is offline Member
    Join Date
    Jun 2011
    Posts
    40
    Rep Power
    0

    Default

    When I try to compile that code then smallRoom gives me a error

    SmallRoom.java:17: cannot find symbol
    symbol : constructor SmallRoom(java.lang.String)
    location: class SmallRoom
    SmallRoom room1 = new SmallRoom("F105");
    ^

Page 1 of 2 12 LastLast

Similar Threads

  1. Helping on Calculating Pie
    By bubbamcgoo in forum New To Java
    Replies: 10
    Last Post: 02-22-2011, 03:23 AM
  2. No one is helping
    By anithajerome in forum Suggestions & Feedback
    Replies: 3
    Last Post: 12-06-2010, 01:52 PM
  3. Helping about SWIFT MT Message
    By 82rathi4u in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2009, 02:06 AM
  4. Can someone give me a helping hand...
    By kwesiaryee in forum Advanced Java
    Replies: 1
    Last Post: 09-17-2008, 08:11 PM
  5. Calculator Problem. Thanks for helping! ^^
    By clark_sandy in forum New To Java
    Replies: 3
    Last Post: 07-06-2008, 05:01 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
  •