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 5 of 5

Thread: Constructor/Accessors/methods problem!!

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Constructor/Accessors/methods problem!!

    Hi Here is my following problem:

    I have 3 fields:
    yearModel
    make
    speed

    the class is labeled Car.

    I must create a cosntructor thast accepts the cars year model and make as arguments. these values should be asssigned to the objects yearModel and make fields. the constructor should also assign 0 to the speed field.

    I must create get methods that get the values stored in yearmodel make and speed fields

    i must create an accelerate method that should add 5 to the speed each time it is called

    i must also create a brake method that will minus 5 to the speed each time it is called

    I must demonstrate this class in a program that calls the accelerate method 5 times. after each time it is called i must get the current speed and display it.

    then i must call the brake method 5 times and after each time it is called get the current speed and display it.

    I think i have the class set up correctly but not sure: I also am not quite sure how to start the program and how to execute propely. mainly how to call the methods in the program.

    package CAR_PKG;

    public class car {
    private int yearModel;
    private String make;
    private int speed;



    public car(int year, String m, int s){
    yearModel=year;
    make=m;
    speed=s;
    speed=0;

    }
    public int getyearModel()
    {
    return yearModel;
    }
    public String getmake()
    {
    return make;
    }
    public int getspeed()
    {
    return speed;
    }
    public int accelerate(int s){
    int sum=s+5;
    return sum;

    }
    public int brake(int s){
    int sub=s-5;
    return sub;
    }

    }


  2. #2
    Junior Member
    Join Date
    Aug 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Re: Constructor/Accessors/methods problem!!

    Why you making speed zero.

    Here is how you should call your methods
    MainClass to call your car Class

    public class MainClass {

    public static void main(String[] args) {

    car cc=new car(1990,"Maruti",55);
    //for(int i=0;i<5;i++)
    cc.accelerate();
    //for(int i=0;i<2;i++)
    cc.brake();

    System.out.println(cc);


    }

    }

    your Car class

    public class car
    {
    private int yearModel;
    private String make;
    private int speed;



    public car(int year, String m,int s)
    {
    yearModel=year;
    make=m;
    speed=s;

    }
    public int getyearModel()
    {
    return yearModel;
    }
    public String getmake()
    {
    return make;
    }
    public int getspeed()
    {
    return speed;
    }
    public int accelerate()
    {
    this.speed=speed+5;
    return speed;

    }
    public int brake()
    {
    this.speed=speed-5;
    return speed;
    }
    @Override
    public String toString() {
    return "car [Year Model= " + getyearModel() + ", Car Make= " + getmake()
    + ", Speed = " + getspeed() + "]";
    }
    }

    Here y you using another variable sum.If you changing the speed by brake n accelerator method u should use same variable to give current speed.
    I think you must not initialize speed to zero.

    Reply for more query !!!

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Constructor/Accessors/methods problem!!

    Thank you! one problem though. I am supposed to display the speed each time the accelrate method is called and each time the brake method is called

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

    Default Re: Constructor/Accessors/methods problem!!

    Look at the how you did this: System.out.println(cc);

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Constructor/Accessors/methods problem!!

    1. Use the
     Your code here
    and format that properly in some editor.
    2. Make a display function that will display what do you want to and call that function in accelerate and brake function.

Similar Threads

  1. JButton constructor problem
    By Nieuwenhuizen-jk in forum AWT / Java Swing
    Replies: 14
    Last Post: September 12th, 2011, 11:35 AM
  2. Problem with subclass and superclass methods
    By Farmer in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 15th, 2011, 08:43 PM
  3. how to solve the constructor problem? " return new Accounts
    By defyzer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 10th, 2011, 11:16 AM
  4. Synchronized Methods problem!
    By RiskyShenanigan in forum AWT / Java Swing
    Replies: 1
    Last Post: November 28th, 2010, 12:04 PM
  5. Completely Lost in this problem(multiple methods)
    By wacarter in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 31st, 2010, 04:44 PM