Results 1 to 5 of 5
  1. #1
    noble is offline Member
    Join Date
    Feb 2010
    Posts
    24
    Rep Power
    0

    Default Accessor/Mutator Question

    I'm very new to Java and completely lost with this accessor/mutator concept. I have my main class that looks like this:

    public class practice
    {
    public static void main (String args[])
    {

    car Firstcar = new car("Honda");
    car Secondcar = new car(2);

    boat Firstboat = new boat("stingray");
    boat Secondboat = new boat(3);

    System.out.printf ("Name is %s\n", Firstcar.getName);
    System.out.printf ("Passengers: %d\n", Secondcar.Passenger);

    System.out.printf ("Name is %s\n", Firstboat.getName);
    System.out.printf ("Passengers: %d\n", Secondboat.Passenger);
    }
    }



    And I have 2 objects, one for the boat and one for the car, here is the car object:

    public class car
    {
    private String Name;
    private int Passenger;

    car (String Carname)
    {
    this.Name = Carname;
    }

    car (int Pass)
    {
    this.Passenger = Pass;
    }

    public int getPassenger()
    {
    return this.Passenger;
    }

    public String getName()
    {
    return this.Name;
    }

    }



    I know this is not correct, but I have no idea what to do to print out the private values from "car" and "boat" Do I need a mutator? What do I need in my "practice" class?. I would really appreciate any help or advice!

  2. #2
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    Java Code:
    public class practice
    {
    public static void main (String args[])
    {
    
    car Firstcar = new car("Honda");
    car Secondcar = new car(2);
    
    boat Firstboat = new boat("stingray");
    boat Secondboat = new boat(3);
    
    System.out.printf ("Name is %s\n", Firstcar.getName);
    System.out.printf ("Passengers: %d\n", Secondcar.Passenger);
    
    System.out.printf ("Name is %s\n", Firstboat.getName);
    System.out.printf ("Passengers: %d\n", Secondboat.Passenger);
    }
    }
    
    
    
    And I have 2 objects, one for the boat and one for the car, here is the car object:
    
    public class car
    {
    private String Name;
    private int Passenger;
    
    car (String Carname)
    {
    this.Name = Carname;
    }
    
    car (int Pass)
    {
    this.Passenger = Pass;
    }
    
    public int getPassenger()
    {
    return this.Passenger;
    }
    
    public String getName()
    {
    return this.Name;
    }
    
    }
    First i think you got confused by c and java.

    in java when you try to print something theres no need to add "%s" or "%.." in its place

    and in your passenger class. its not right thing to make 2 objects for 1 car isnt it. i mean if your trying to make a Honda to have 2 passengers in it then thats not the correct way to do it.

    instead you have to make a constructor that handles both carname and number of passengers in the car class as car(String carname,int passengers){..} then when you try to call the accessor methods that would work.

    accessors are used to access private data. while mutators are used to change.

    Hope it helped

  3. #3
    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 [RaIdEn] View Post
    First i think you got confused by c and java.

    in java when you try to print something theres no need to add "%s" or "%.." in its place
    Actually, his use of %s is correct as he is printing with printf, not println. To be fully correct, though, I think he'll want to use %n instead of \n for new line.

    and in your passenger class. its not right thing to make 2 objects for 1 car isnt it. i mean if your trying to make a Honda to have 2 passengers in it then thats not the correct way to do it.

    instead you have to make a constructor that handles both carname and number of passengers in the car class as car(String carname,int passengers){..} then when you try to call the accessor methods that would work.

    accessors are used to access private data. while mutators are used to change.
    ++
    yup

  4. #4
    noble is offline Member
    Join Date
    Feb 2010
    Posts
    24
    Rep Power
    0

    Default

    So I'm doing it a bit differently but still getting some errors. Here is my main class:

    public class practice
    {
    public static void main (String args[])
    {

    boat firstBoat= new boat();
    car firstCar = new car();
    }
    }


    And here is my car class:

    public class car
    {
    private String Name;
    private int Passenger;

    boat (String x, int y)
    {
    Name= this.getName();
    Passenger= this.getPass();
    }

    public String getName()
    {
    return this.setName();
    }

    public String setName()
    {
    Name="Car Name";
    }

    public int getPass()
    {
    return this.setPass();
    }

    public int setPass()
    {
    Passenger= 10;
    }
    }


    I'm getting a "cannot find constructor" error message. Should I have something within the () in the practice class???

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

    Default

    1) Classes should begin with a capital letter
    2) the "car" class cannot have a constructor called "boat". It has to be called "car", the same as the class.
    3) If you have a only one constructor and it takes a parameter, then when you call the constructor, you must pass the parameter.
    4) When posting code here, please use code tags (see my signature below).

Similar Threads

  1. passing vs accessor
    By gcampton in forum New To Java
    Replies: 15
    Last Post: 01-08-2010, 04:21 AM
  2. Accessor method
    By DC200 in forum New To Java
    Replies: 19
    Last Post: 06-17-2009, 10:11 AM
  3. Accessor help needed (I think...- little confussed)
    By thomase in forum New To Java
    Replies: 7
    Last Post: 03-11-2009, 11:29 AM
  4. Replies: 6
    Last Post: 12-03-2008, 12:15 AM
  5. mutator method
    By dirtycash in forum New To Java
    Replies: 7
    Last Post: 11-22-2007, 11:29 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
  •