Results 1 to 6 of 6
  1. #1
    yasir17 is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default Need a Help with Java Class

    Dear All,

    I need your help, please help me. when ever i am compiling i get errors.
    ---------------------------------
    Develop a class to represents a human. The head, arms and legs should be properties of the human and should be represented by separate objects. All of your objects should have at least three properties and one method. Ensure that the head, arms and legs can only be constructed from with the same package. The human should include a walk method that uses the legs to actually move.
    In the main method of the human, create an instance of a human and set the properties so that it looks like you.
    ----------------------------------------------
    MY CODES:
    public class Human
    {
    public Head myHead;
    public Arm[] myArms;
    public Leg myRightLeg;
    public Leg myLeftLeg;
    /*
    * Constructor for the Human class
    */
    public Human(String name, String race, String gender, int age) {
    /*
    * use 'this' to refer to
    * the current object being manipulated
    */
    this.name = name;
    this.race = race;
    this.gender = gender;
    this.age = age;

    myHead = new Head();
    myArms = new Arm[2];
    myLeftLeg = new Leg();
    myRightLeg = new Leg();

    }
    /*
    * walk method
    */
    public void walk(int steps){
    for (int i = 0; i <= steps; i++){
    myRightLeg.moveForward();
    myLeftLeg.moveForward();
    }
    }
    /*
    * declare and create a Human object
    */
    public static void main(String[] args) {
    /*
    * new instance of the Human
    */
    Human me = new Human("Putin", "yellow", "male", 58);
    /*
    * set string values to variables
    */
    me.myHead.setHairColor("white");
    me.myHead.setFaceColor("brown");

    System.out.println("My name is Putin, am " +me.myHead.face_color+ " and I have got " +me.myHead.hair_color+ " hair");
    /*
    * test walk method
    */
    me.walk(5);
    System.out.println("I walked 5 steps from position zero to position " + me.myRightLeg.getPosition());
    }
    /*
    * declare variables
    */
    private String name;
    private String race;
    private String gender;
    private int age;
    }
    ---------------------------
    I am getting these errors, thats where I am little confuse.
    -------------------------------------------C:\Users\Yasir\Documents\java2>javac Human.java
    .\Head.java:2: class Human is public, should be declared in a file named Human.java
    public class Human
    ^
    Human.java:3: cannot access Head
    bad class file: .\Head.java
    file does not contain class Head
    Please remove or make sure it appears in the correct subdirectory of the classpa
    th.
    public Head myHead;
    ^
    ------------
    Please correct me
    Last edited by yasir17; 09-28-2008 at 12:17 AM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    when ever i am compiling i get errors
    You forgot to post the errors.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    WHere is the class Head defined? Is it in a file named Head.java and is that file in the same folder as Human.java?

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

    Default

    Yes it should be that your source files are not in the same location. You are located files in different locations and accessing them in wrong way.

  5. #5
    yasir17 is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default

    WHere is the class Head defined? Is it in a file named Head.java and is that file in the same folder as Human.java?
    --------------------------------
    everything is in same file, the main class is Human.java and others are the Objects this is what i trying to do, but i am little confuse.
    ---------------------------
    this is my case study which i am trying to develop:
    ------------------
    Develop a class to represents a human. The head, arms and legs should be properties of the human and should be represented by separate objects. All of your objects should have at least three properties and one method. Ensure that the head, arms and legs can only be constructed from with the same package. The human should include a walk method that uses the legs to actually move.
    In the main method of the human, create an instance of a human and set the properties so that it looks like you.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    everything is in same file
    You can only have one "public" class in a java source file. The source filename must be the same as the "public" class.
    If the Head class is defined in the same source file as the Human class, Remove the "public" attribute from the Head class definition.

    The code you posted did NOT show the Head class defintion.

Similar Threads

  1. converting jar to .class or .java
    By marmara95 in forum New To Java
    Replies: 12
    Last Post: 10-06-2012, 10:49 PM
  2. How to create a class in java?
    By pawankumarom in forum New To Java
    Replies: 2
    Last Post: 09-05-2008, 07:47 AM
  3. .java > .class conversion
    By Fate_Remai in forum New To Java
    Replies: 18
    Last Post: 07-31-2008, 02:12 AM
  4. class.java to class.exe
    By f_the_cook in forum New To Java
    Replies: 11
    Last Post: 07-16-2008, 02:24 PM
  5. Im new to java. how do i fix the problem with class
    By lexlukkia in forum New To Java
    Replies: 2
    Last Post: 11-18-2007, 04:47 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
  •