Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Homework help please

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Homework help please

    I am having issues with creating class for my home work.

    The project: Designing and implementing a class called dog that contains instance datat that represents the dogs name and age. i have to define the dog constructor to accept and initialize instance data. include getter and setter methods for the name and age. include a metod to compute and return the age of the dog in person years. include a tostring method that returns a oneline description of the dog. crate a driver class call kennel, whose main method instantiates and updates several dog objects.

    So first is my dog class it is complies just fine

    //********************************************************************
    //  Dog.java       Author: Anastacia Williams
    //
    //  Get and set doog name and age
    //
    //	  ______________________________
    //           	  name of class
    //                    Dog
    //        ______________________________
    //            list of class variables
    //                  Age      int
    //					petAge   int
    //					Value    int
    //
    //
    //
    //        ______________________________
    //            list of class methods
    //                -int
    //				  -String Dog
    //				  +Dog
    //				  +getName
    //				  +setName
    //				  +getAge
    //				  +setAge
    //				  +HumanYear
    //				  +String toString
    //        ______________________________
    //
    //********************************************************************
     
    public class Dog
    {
       	private int Age ;  //age of animal
     
    	private String Name;  // name
     
    	private int HumanAge; //human age
     
       //-----------------------------------------------------------------
       //  Constructor: vallue
       //-----------------------------------------------------------------
       public Dog(String petname, int petAge, int Agetime7)
       {
           Name = petname;
           Age = petAge;
           HumanAge = Agetime7;
       }
     
       //-----------------------------------------------------------------
       //  Set name
       //-----------------------------------------------------------------
       public void setName(String petname)
       {
      	      Name = petname;
       }
     
       //-----------------------------------------------------------------
       //  get name
       //-----------------------------------------------------------------
       public String getName()
       {
    		      return Name;
       }
     
       //-----------------------------------------------------------------
       //  set age
       //-----------------------------------------------------------------
       public void setAge(int petAge)
       {
      		      int Age = petAge;
       }
     
       //-----------------------------------------------------------------
       //  Get age
       //-----------------------------------------------------------------
       public int getAge()
       {
      		      return Age;
       }
     
       //-----------------------------------------------------------------
       //  Humna years. formula age times 7
       //-----------------------------------------------------------------
       public void setHumanAge(int Agetime7)
       {
     
    			int HumanAge = Agetime7;
       }
     
     
    	public int getHumanAge()
    	 {
    	  		      return HumanAge;
       }
       //-----------------------------------------------------------------
       //  Returns a string representation to dog name an age
       //-----------------------------------------------------------------
       public String toString()
       {
     
     
      		return "Name of dog: " + Name + "Age of dog: " + Age + "Human age: " + HumanAge;
     
       }
    }


    This is my kennel
    //********************************************************************
    //  Kennel.java       Author: Anastacia Williams
    //
    //  Get and set doog name and age
    //
    //	  ______________________________
    //           	  name of class
    //
    //        ______________________________
    //            list of class variables
    //                  None
    //        ______________________________
    //            list of class methods
    //                +main
    //        ______________________________
    //
    //********************************************************************
     
     
    public class Kennel
    {
       //-----------------------------------------------------------------
       //  Generate aniaml name and age.
       //-----------------------------------------------------------------
     
     
      public static void main (String[] args)
       {
     
      		Dog dog1 = new Dog ("willy", 9, 63);
     
          System.out.println ( "First Dog " dog1.toString);
     
     
       }
    }

    the errors i encounter with kennel are:
    E:\New Folder\Kennel.java:32: error: ')' expected
    System.out.println ( "First Dog " dog1.toString);
    ^
    E:\New Folder\Kennel.java:32: error: illegal start of expression
    System.out.println ( "First Dog " dog1.toString);
    ^
    E:\New Folder\Kennel.java:32: error: ';' expected
    System.out.println ( "First Dog " dog1.toString);
    ^
    3 errors

    Tool completed with exit code 1

    Please help me


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Homework help please

    The arg to the println() method is a String. Separate Strings can be joined together to make one String by using the + operator.
    The compiler wants a ) after the String it has found after println(
    Join the two Strings to make one by putting a + between them
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework help please

    Quote Originally Posted by Norm View Post
    The arg to the println() method is a String. Separate Strings can be joined together to make one String by using the + operator.
    The compiler wants a ) after the String it has found after println(
    Join the two Strings to make one by putting a + between them

    Well since i added what you want this si the error code i get now
    I:\New Folder\Kennel.java:32: error: cannot find symbol
    System.out.println ( "First Dog " + dog1.toString);
    ________________________________^
    symbol: variable toString
    location: variable dog1 of type Dog
    1 error

    Tool completed with exit code 1

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Homework help please

    Is toString a variable in the Dog class? The compiler can not find its definition.

    The syntax for a method uses () at the end of the name.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework help please

    yes. the first code is my Dog. public String toString() i am not sure if i need to add anything in there and i am very confused at why it is not working.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Homework help please

    Do you know the difference between a variable and a method?
    A method is coded with () at the end of the name.
    What is toString?
    A variable or a method?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework help please

    I am sorry i am not sure.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework help please

    What the differences is. I read my text but i do not understand it.

  10. #10
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework help please

    So i redid my code for Dog.

    //********************************************************************
    //  Dog.java       Author: Anastacia Williams
    //
    //  Get and set doog name and age
    //
    //	  ______________________________
    //           	  name of class
    //                    Dog
    //        ______________________________
    //            list of class variables
    //                  Age      int
    //					petAge   int
    //					Value    int
    //
    //
    //
    //        ______________________________
    //            list of class methods
    //                -int
    //				  -String Dog
    //				  +Dog
    //				  +getName
    //				  +setName
    //				  +getAge
    //				  +setAge
    //				  +HumanYear
    //				  +String toString
    //        ______________________________
    //
    //********************************************************************
     
    public class Dog
    {
       	private int Age ;  //age of animal
     
    	private String Name;  // name
     
    	private int HumanAge; //human age
     
       //-----------------------------------------------------------------
       //  Constructor: vallue
       //-----------------------------------------------------------------
       public Dog(String petname, int petAge, int Agetime7, String NAH)
       {
           Name = petname;
           Age = petAge;
           HumanAge = Agetime7;
           String toString = NAH;
       }
     
       //-----------------------------------------------------------------
       //  Set name
       //-----------------------------------------------------------------
       public void setName(String petname)
       {
      	      Name = petname;
       }
     
       //-----------------------------------------------------------------
       //  get name
       //-----------------------------------------------------------------
       public String getName()
       {
    		      return Name;
       }
     
       //-----------------------------------------------------------------
       //  set age
       //-----------------------------------------------------------------
       public void setAge(int petAge)
       {
      		      int Age = petAge;
       }
     
       //-----------------------------------------------------------------
       //  Get age
       //-----------------------------------------------------------------
       public int getAge()
       {
      		      return Age;
       }
     
       //-----------------------------------------------------------------
       //  Humna years. formula age times 7
       //-----------------------------------------------------------------
       public void setHumanAge(int Agetime7)
       {
     
    			int HumanAge = Agetime7;
       }
     
     
    	public int getHumanAge()
    	 {
    	  		      return HumanAge;
       }
       //-----------------------------------------------------------------
       //  Returns a string representation to dog name an age
       //-----------------------------------------------------------------
       public String toString()
       {
     
    		String NAH = Dog.toString ("Name of dog: " + Name + "Age of dog: " + Age + "Human age: " + HumanAge);
      		return (NAH);
     
       }
    }

    now i am getting an error

    C:\Users\kids\Documents\bobobob\Dog.java:103: error: no suitable method found for toString(String)
    String NAH = Dog.toString ("Name of dog: " + Name + "Age of dog: " + Age + "Human age: " + HumanAge);
    ^
    method Dog.toString() is not applicable
    (actual and formal argument lists differ in length)
    method Object.toString() is not applicable
    (actual and formal argument lists differ in length)
    1 error

    Tool completed with exit code 1

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Homework help please

    The standard toString() method does not take any arguments. It's purpose is to return a String that describes the contents of the instance of the class it is a member of.

    Also if the toString() method in a class calls the toString() method in that same class, that will be a recursive call and can cause an infinite recursion of calls causing an exception.

    Remove the call to the toString() method and just build and return a String.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Oct 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework help please

    [Hi there. For the kennel, please do the following ]

    public class Kennel
    {

    public static void main (String[] args)
    {

    Dog dog1 = new Dog ("willy", 9, 63);

    System.out.println ( "First Dog " + dog1.toString());

    }
    }

    It works for me..

  13. #13
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Homework help please

    This post is 4 years old. I doubt it is still an issue.

    Regards,
    Jim

Similar Threads

  1. Help with homework
    By gta1 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 11th, 2013, 08:58 PM
  2. Homework help?
    By regi.dg in forum Object Oriented Programming
    Replies: 0
    Last Post: October 16th, 2012, 08:17 PM
  3. HELP WITH HOMEWORK!!
    By jmillernc01 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 10th, 2012, 11:13 AM
  4. Homework help
    By hockey15 in forum Object Oriented Programming
    Replies: 3
    Last Post: September 23rd, 2012, 11:28 PM
  5. Homework
    By jdonaldson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2011, 11:09 AM