The Artima Developer Community
Sponsored Link

Java Answers Forum
Car java program help

4 replies on 1 page. Most recent reply: May 6, 2018 7:23 AM by siva prasad

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Nay B

Posts: 9
Nickname: blackrose
Registered: Oct, 2009

Car java program help Posted: Oct 5, 2009 12:04 PM
Reply to this message Reply
Advertisement
Hello, I need some help writing 2 classes for a car program. Honestly, I'm kinda lost. Any help would be appreciated. Here's the outline that I was given:

Fields:
yearModel - an integer that holds the car's year model
make - a string that holds the make of the car
speed - an integer that holds the car's current speed

//constructor
the constructor should accept the car's year model and make an arguments. these values should be assigned to the object's yearModel and make fields. The constructor should also assign 0 to the speed field.

Make appropriate accessor methods to get the values and store them in an object's yearModel, make, and speed fields.

*now this is the part i'm truely lost with*

accelerate. the accelerate method should add 5 to the speed field each time it is called

brake. should subtract 5 every time it is called

Demonstrate the class in a program that creates a Car object, and then calls the accelerate method 5 times. After each call to the accelerate method, get the current speed of the car and display it. Then, call the brake method 5 times. After each call to the brake method, get the current speed of the car and display it.
-------------------
I think i have the first half okay-
this is what i have:

public class Car
{
public int yearModel;
public String make;
public int speed;

public Car()
{
yearModel=0;
speed=0;
make="2004 Toyota Echo";
}

public Car(int yearModel, String make, int speed)
{
this.yearModel=yearModel;
this.make=make;
}

public int getYear()
{
return yearModel;
}

public String getMake()
{
return make;
}

public int getSpeed()
{
return speed
}

public int accelerate()
{
speed=speed+5;
return speed;
}

public int break()
{
speed=speed-5;
return speed;
}

public void setYear(int yearValue)
{
this.yearModel=yearValue
}

public void setMake(String make)
{
this.make=make;
}

//not sure how to display twice - telling the person using the program that the car is going 0 mph, then accelerates to 5, then brakes 5mph to 0mph again

public String toString()
{
return "The "+yearModel + make + " is currently going " + speed + "mph.";
}

public static void main(String[]args)
{
Car car1 = new Car(yearModel, make, speed);
}
}//end class Car


Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Car java program help Posted: Oct 6, 2009 1:28 PM
Reply to this message Reply
Car car = new Car();
car.accelerate();
System.out.println("Car speed: " + car.getSpeed();
//etc.

Nay B

Posts: 9
Nickname: blackrose
Registered: Oct, 2009

Re: Car java program help Posted: Oct 7, 2009 7:09 PM
Reply to this message Reply
Thank you for your help. :)
I am getting one error message I don't understand:

Car.java --


public class Car
{
public int yearModel;
public String make;
public int speed;

public Car()
{
yearModel=0;
speed=0;
make="2004 Toyota Echo";
}

public Car(int yearModel, String make, int speed)
{
this.yearModel=yearModel;
this.make=make;
}

public int getYear()
{
return yearModel;
}

public String getMake()
{
return make;
}

public int getSpeed()
{
return speed
}

public int accelerate()
{
speed=speed+5;
return speed;
}

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

public void setYear(int yearValue)
{
this.yearModel=yearValue
}

public void setMake(String make)
{
this.make=make;
}


}//end class Car



and I have
CarDemo.java --


//This is a CarDemo Class

public class CarDemo

{ //Begin Class

public static void main (String args [])

{ //Begin Main Method
//not sure how to display twice - telling the person using the program that the car is going 0 mph, then accelerates to 5, then brakes 5mph to 0mph again

System.out.println("Year Model: " + yearModel);
System.out.println("Make: " + make);
System.out.println("Car Speed: " + speed);
System.out.println("...accelerating...");

public static void main(String args [])
{
Car Car1 = new Car(speed);
Car.accelerate();
Car.accelerate();
Car.accelerate();
Car.accelerat e();
Car.accelerate();
System.out.println("Car speed: " + car.getSpeed());
System.out.println("...braking...");
}

public static void main(String[]args)
{
Car Car1 = new Car(speed);
Car.brake();
Car.brake();
Car.brake();
Car.brake();
Car.brake();
System.out.println("Year Model: " + yearModel);
System.out.println("Make: " + make);
System.out.println("Car speed: " + speed);



} //End of Main Method
} //End of CarDemo Class


The error message I get is:

D:\lotsOfFoldersInBetween\CarDemo.java:37: illegal start of expression
public static void main(String[]args);
^
1 error

Tool completed with exit code 1


please help!!

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Car java program help Posted: Oct 8, 2009 3:58 AM
Reply to this message Reply
The class Car contains two typos (both missing semi-colons) which prevent it compiling.

The class CarDemo is a bit of a mess. I could sort it out for you but you'd be much better of reading this... http://java.sun.com/docs/books/tutorial/java/concepts/class.html ...and then comparing it with your own answer.

siva prasad

Posts: 1
Nickname: spkkoppala
Registered: May, 2018

can you please help this Posted: May 6, 2018 7:23 AM
Reply to this message Reply
create two peddles,one for acceleration and one for braking.when the accelerator is pressed,the wheel is speed up inrotation and when the break is pressed the wheels declerate and stop the peddles should work n pressing a and b keys on keyboard for acceleration and break respectively.when a key s pressed,it should lead to corresponding view on the two paddles

Flat View: This topic has 4 replies on 1 page
Topic: Need help with RuntimeException error Previous Topic   Next Topic Topic: Need a help

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use