-4

The following are the instructions and code for a Java program I have to complete. I am stuck and do not know how to continue. I'm trying to figure this out. I feel like I have no idea what I'm doing. All help, direction, and explanation would be very much appreciated.

Write a class named Car that has the following fields:

yearModel: The yearModel field is an int that holds the car’s year model.

make: The make field references a String object that holds the make of the car.

speed: The speed field is an int that holds the car’s current speed.

In addition, the class should have the following constructor and other methods:

Constructors: One constructor should accept the car's year model, make, and speed as arguments. These values should be assigned to the object's yearModel, make, and speed fields. Another constructor will have no arguments and will assign 0 as the car's year model and speed and an empty string ("") as the make.

Accessors: Appropriate accessor methods should get the values stored in an object's yearModel, make, and speed fields.

Mutators: Appropriate mutator methods should store values in an object's yearModel, make, and speed fields.

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

brake: The brake method should subtract 5 from the speed field each time it is called.

Demonstrate the class in a program that asks the user to input data and then creates a Car object. It then calls the accelerate method five times. After each call to the accelerate method, get the current speed of the car and display it. Then call the brake method five times. After each call to the brake method, get the current speed of the car and display it.

The output from running this program will appear similar to:

Enter the car's year model: 1965
Enter the car's make: Mustang
Enter the car's speed: 30

Current status of the car:
Year model: 1965
Make: Mustang
Speed: 30

Accelerating...
Now the speed is 35

Accelerating...
Now the speed is 40

Accelerating...
Now the speed is 45

Accelerating...
Now the speed is 50

Accelerating...
Now the speed is 55

Braking...
Now the speed is 50

Braking...
Now the speed is 45

Braking...
Now the speed is 40

Braking...
Now the speed is 35

Braking...
Now the speed is 30

This is what I have so far:

public class Car {

// Declaration of variables.
private int yearModel;
private String make;
private int speed;

// Constructor that accepts arguements.
public static void acceptor(int yearModelIn, String makeIn, int speedIn){
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter the car's year model: ");
    yearModelIn = keyboard.nextInt();  
    System.out.println("Enter the car's make: ");
    makeIn = keyboard.next();
    System.out.println("Enter the car's speed: ");
    speedIn = keyboard.nextInt();
}

// Constructor that zeroes fields.
public void zeroer()
{
    yearModel = 0;
    speed = 0;
    make = ("");
}

// Accessor Methods
public int getYearModel()
{
    return yearModel;
}
public String getMake()
{
    return make;
}
public int getSpeed()
{
    return speed;
}    

// Accelerate method for adding 5 to speed.
public void Accelerate()
{
    speed += 5;        
}

// Brake method for reducing speed.
public void Brake()
{
    speed-=5;
}
Andrew T.
  • 4,592
  • 7
  • 38
  • 55
IAmTheWalrus
  • 23
  • 2
  • 8

4 Answers4

1

First, get rid of the acceptor method, it's not doing what you think it should...I'd probably drop the zeroer method as well, as it doesn't provide any useful functionality, other than to screw you up

Constructors. One constructor should accept the car’s year model, make, and speed as arguments. These values should be assigned to the object’s yearModel, make, and speed fields. Another constructor will have no arguments and will assign 0 as the car’s year model and speed and an empty string (“”) as the make.

To start with, you're missing this...

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

From this, you can create an instance of the car...

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter the car's year model: ");
    int year = keyboard.nextInt();
    keyboard.nextLine();
    System.out.println("Enter the car's make: ");
    String make = keyboard.nextLine();
    System.out.println("Enter the car's speed: ");
    int speedIn = keyboard.nextInt();

    Car car = new Car(year, make, speedIn);        
}

Then, all you need to do is call the appropriate methods to change and report the state, for example...

car.Accelerate();
System.out.println(car.getSpeed());

Consult your notes and the tutorials when you're stuck, for example Providing Constructors for Your Classes and Passing Information to a Method or a Constructor

You might also like to have a read through Code Conventions for the Java TM Programming Language, it will make it easier for people to read your code and for you to read others

MadProgrammer
  • 323,026
  • 21
  • 204
  • 329
0

There is a little bit of error in your coding structure.

class Car shouldn't take input's, your Car object should be a POJO, only getters and setters.

so your Car would look like this

public class Car {
    String model;
    int make;
    double speed = 0.0;
    Car(int make, String model, double speed) {
        this.make = make;
        this.model = model;
        this.speed = speed;
   }

   // then the getters and setters for speed, accelerate, decelerate
}

now you access your Car class from an implementing class, say Garage

public class Garage {
    public static void main(String ar[]) {
       // Now take your inputs here say model = Mustang, make = 1995 speed = 50
       Car c = new Car(make, model, speed);
       // and then a simple looping construct
       for(int i = 0; i < 5; i ++) {
           c.accelerate(); // please don't use capitals for method names, because they look like class names
           System.out.println(c.getSpeed());
        }
    }
}
shortCircuit
  • 169
  • 2
  • 10
0

Here is the code for your reference.And remove those zeror and acceptor method.

import java.util.Scanner;

public class Car {

private int speed;
private String make;
private int yearModel;

public int getSpeed() {
    return speed;
}
public void setSpeed(int speed) {
    this.speed = speed;
}
public String getMake() {
    return make;
}
public void setMake(String make) {
    this.make = make;
}
public int getYearModel() {
    return yearModel;
}
public void setYearModel(int yearModel) {
    this.yearModel = yearModel;
}

void accelarate(){
  this.setSpeed(this.getSpeed()+5);
}

void brake(){
    this.setSpeed(this.getSpeed() -5);
}

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


public static void main(String[] args) {
     Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the car's year model: ");
        int yearModel = keyboard.nextInt();
        keyboard.nextLine();
        System.out.println("Enter the car's make: ");
        String make = keyboard.nextLine();
        System.out.println("Enter the car's speed: ");
        int speed = keyboard.nextInt();

    Car car = new Car(yearModel,make, speed);

    //Accelerate
    for(int i=0;i<5;i++){
        car.accelarate();
        System.out.println("speed after accelaration::"+car.getSpeed());
    }   

    //Brake
    for(int i=0;i<5;i++){
        car.brake();;
        System.out.println("speed after applying brake::"+car.getSpeed());
    }   

}

}

vikrant
  • 371
  • 3
  • 12
0
public class Car {

    private int yearmake; // Variable of your yearmake of car
    private String model; // Variable of your car company
    private int speed;  // Variable of your car speed

    // getter and setter method as the pojo class defines that will give getter and setter property for the every variable inside class as according to OP it wont let u change Variable directly

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public int getYearmake() {
        return yearmake;
    }

    public void setYearmake(int yearmake) {
        this.yearmake = yearmake;
    }

    public Car(int speed) {
        this.speed = speed;
    }
   // constructor which will accepts and initialize your car with data i mean class
    public Car(int yearmake, String model, int speed) {
        this.yearmake = yearmake;
        this.model = model;
        this.speed = speed;
        System.out.println("Year Make " + yearmake);
        System.out.println("Model " + model);
        System.out.println("Speed " + speed);

    }
    // method of the making accelerate car by speed of 5 
    public int acclarate(int speed) {
        speed = speed + 5;
        System.out.println("Speed Acclarated " + speed);
        return speed;
    }
   // method for reducing speed of 5
    public int Breaking(int speed) {
        speed = speed - 5;
        System.out.println("Speed Breaking " + speed);
        return speed;
    }
   // main method
    public static void main(String[] args) {
        // accept from user input
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the car's made year model: ");
        int year = keyboard.nextInt();
        keyboard.nextLine();
        System.out.println("Enter the car's make Company: ");
        String make = keyboard.nextLine();
        System.out.println("Enter the car's speed: ");
        int speedIn = keyboard.nextInt();
        // initialize car model with constructor
        Car c = new Car(year, make, speedIn);

        //increasing speed with use of method and loop
        for (int i = 0; i < 5; i++) {
            int speedchange = c.acclarate(c.getSpeed());
            c.setSpeed(speedchange);

        }
        //decreasing speed according to your requriement with use of method and loop
        for (int i = 0; i < 5; i++) {
            int decreasedpeed = c.Breaking(c.getSpeed());
            c.setSpeed(decreasedpeed);
        }

    }
}

u can do it other way too :) but i will suggest too as madprogrammer said. learn some OP and basic things :)

Kishan Bheemajiyani
  • 3,057
  • 3
  • 26
  • 57