4 Replies - 709 Views - Last Post: 04 October 2011 - 10:38 AM Rate Topic: -----

#1 Dnguyen21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 04-October 11

main method for dog class

Posted 04 October 2011 - 09:08 AM

Hi unfortunately I am having problem running a java class dog. It says that i do not have a main method. I am brand new to java. any help is appreciated.

 

package javaassignment;

public class dog 
{
    private String Name;
    private int age;
            
    
    //**************************************************************************
    // This will set up dog object with instance data
    //**************************************************************************
    
            public dog(int dogAge, String dogName)
            {
                this.age = dogAge;
                this.Name = dogName;
            }
           
    //**************************************************************************
    // Name accessor
    //**************************************************************************

            public String getName()
            {
                return Name;
            }
            
   //***************************************************************************
   // Name mutator
   //***************************************************************************
            
            public void setName(String newName) 
            {
                Name = newName;
            }
            
   //***************************************************************************
   // Age accessor
   //***************************************************************************
  
            public int getAge()
            {
                return age;
            }
     
   //***************************************************************************
   // Name mutator
   //***************************************************************************
            
            
            public void setAge(int newAge)
            {
                age = newAge;
            }        
            
   //***************************************************************************
   // this will translate dog years into human years
   //***************************************************************************

            public int humanYears()
            {
                return age * 7;
            }

   //***************************************************************************
   // Returns a string representation of this dog
   //***************************************************************************

            public String toString ()
            {
                return "Dog: " + Name + "Age: " + age + "Human-Years: " +
                        humanYears();
            }

}






Is This A Good Question/Topic? 0
  • +

Replies To: main method for dog class

#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: main method for dog class

Posted 04 October 2011 - 09:15 AM

In java when you want to run a class it needs a main method as your compiler is telling you. If you take a look at some of our basic tutorials in the Java Section it will show you what you need to know. basically you need a main method that will create an object and handle the methods. Like yours would be something like this:
public class Testdog
{
// A main method to handle everything
public static void main(String args[])
{
     dog charlie = new dog(3, "Charlie");
     System.out.println(charlie.getName());
     System.out.println(charlie.humanYears());
     System.out.println(charlie.toString());
}
}

This is what we would call a driver class, it is used to create an object of a different class and run some of its methods. Pretty basic things that you can find out more about in our Java Tutorial Section. The only stipulation is that the driver class and the regular class have to be in the same directory, or location. Only then can it be seen by inheritance. Im sure there is something about that in the Java Tutorial Section as well! ;)
Was This Post Helpful? 1
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: main method for dog class

Posted 04 October 2011 - 09:17 AM

Every program in java needs a main method to start running. This is the first method the compiler looks for. And you have two ways to add it:
1. Add it in the same class,
2. Add it in different class, but then this class should be the main one, the one to be run.

Either way,In your main you will instantiate your dog class and start using its methods and so.
Was This Post Helpful? 1
  • +
  • -

#4 Dnguyen21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 04-October 11

Re: main method for dog class

Posted 04 October 2011 - 10:35 AM

ok here is what i entered but am receiving an error still illegal start of expression on line 9 "private String Name;"

package javaassignment;
public class dog 
{
    // a main method to handle everything
public static void main (String [] args)
{
        private String Name;
        private int age;
    
    
    //**************************************************************************
    // This will set up dog object with instance data
    //**************************************************************************
    
            public dog(int dogAge, String dogName)
            {
                this.age = dogAge;
                this.Name = dogName;
            }
           
    //**************************************************************************
    // Name accessor
    //**************************************************************************

            public String getName()
            {
                return Name;
            }
            
   //***************************************************************************
   // Name mutator
   //***************************************************************************
            
            public void setName(String newName) 
            {
                Name = newName;
            }
            
   //***************************************************************************
   // Age accessor
   //***************************************************************************
  
            public int getAge()
            {
                return age;
            }
     
   //***************************************************************************
   // Name mutator
   //***************************************************************************
            
            
            public void setAge(int newAge)
            {
                age = newAge;
            }        
            
   //***************************************************************************
   // this will translate dog years into human years
   //***************************************************************************

            public int humanYears()
            {
                return age * 7;
            }

   //***************************************************************************
   // Returns a string representation of this dog
   //***************************************************************************

            public String toString ()
            {
                return "Dog: " + Name + "Age: " + age + "Human-Years: " +
                        humanYears();
            }
}


    



Was This Post Helpful? 0
  • +
  • -

#5 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: main method for dog class

Posted 04 October 2011 - 10:38 AM

public static void main (String [] args)
{

You never put a closing bracket on the main method. Other methods in the class do not go in the main method. It would have to be something like this:
private String name;
private int age;

public static void main (String [] args)
{
     // Object creation and manipulation
}

public dog(int dogAge, String dogName)
{ ... }


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1