I need to create a Car class that has the following fields: brand, model, and speed. Each of the fields should have accessor and a mutator methods. In addition the class should have methods that allows the user of the Car class to accelerate and to brake. Accelerating or braking should change the car speed by increments of 5 mph. You must provide a constructor that takes values for the three fields as arguments. Finally, demonstrate the usage of this class in a different file containing a main() method by using the methods to accelerate and brake methods.

my head hurts just thinking about this. Just need a help in starting, i'm always second guessing myself.

Thanks

Recommended Answers

All 18 Replies

Ok here's a start:

class Car {
}

You'll have to fill it in a bit. Take it one step at a time. For example decide what fields it needs, define them and then add the accessor methods. Write a small test pgm to exercise it by creating it, add data to some fields, get data from them and print it out.

/**
 *Create a Car class that has the following fields: brand, model, and speed. 
 *Each of the fields should have accessor and a mutator methods. In addition 
 *the class should have methods that allows the user of the Car class to 
 *accelerate and to brake. Accelerating or braking should change the car 
 *speed by increments of 5 mph. You must provide a constructor that takes 
 *values for the three fields as arguments. Finally, demonstrate the usage of 
 *this class in a different file containing a main() method by using the 
 *methods to accelerate and brake methods. 
 *
 * @author 
 * @version 1.00 2010/6/13
 */
 import java.util.Scanner;
public class Car 
{
    private double mSpeed = 0.0;
    private String mBrand = "";
    private String mModel = "";

    /**constructor
     *@param speed,, the speed of the car
     *@param model, model of the car
     *@param brand, brand of the car
     */
     //to use this Car car = new Car(5.0, accord, honda);
    public Car(double speed, String brand, String model)
    {
        mSpeed = speed;
        mBrand = brand;
        mModel = model;

    }

    //accessors 
    public void setSpeed(double speed)
    {
        mSpeed = speed;
    }

    public void setBrand(String brand )
    {
        mBrand = brand;
    }


    public void setModel(String model)
    {
        mModel = model;
    }

    public double getSpeed()
    {
        return mSpeed;
    }

    public String getBrand()
    {
        return mBrand;
    }

    public String getModel()
    {
        return mModel;
    }

    public double setAccelerate(double speed)
    {
        speed += 5;
        mSpeed = speed;
        return mSpeed;
    }

    public double brake()
    {
        brake -= 5;

        return mSpeed;
    }

    public static void main(String[] args) 
    {
        double tSpeed;
        String tBrand;
        String tModel;

        Scanner keyboard = new Scanner(System.in);

        System.out.println("enter the speed of the car:" );
        tSpeed = keyboard.nextDouble();


        //the model
        System.out.println("Enter the brand: ");
        tBrand = keyboard.nextLine();
        System.out.println();

        //the model
        System.out.print("Enter the model:  ");
        tModel = keyboard.nextLine();

        Car ncar = new Car(tSpeed, tBrand, tModel);


        System.out.println("the speed is; " + ncar.getSpeed());
        System.out.println("the brand is; " + ncar.getBrand()); 
        System.out.println("the model is; " + ncar.getModel());


    }

}

so this is what i've done so far. thanks for the input. trying to figure out the acceleration and brake part. having issues with the print out. it doesn't take the brand input at all...scratching head. i've tried several things but not sure. i think there is a line in there that i need to consume. any suggestions

it doesn't take the brand input at all

What happens? No input or program stops?
Look at the Scanner class to see if the last Scanner method you used could leave the Scanner waiting.

it prints the line before and after and takes the input for those. it's just skipped...

--------------------Configuration: Car - JDK version 1.6.0_18 <Default> - <Default>--------------------
enter the speed of the car:
3
Enter the brand:

Enter the model: hona
the speed is; 3.0
the brand is;
the model is; hona

Process completed.

this is the output. i get an error with the brake variable. but still caught up on this issue. then just need to get the brake() and acce() methods working. call them and then print. does that sound about right? looking at other codes right now.

Funny, I get a compile error with your posted code. Can't find brake.

let me build again...hang on

what errors do you get. can you post it so i can take a look and see if i can correct those changes ;)

I don't use Scanner so I have to try some things.
I think to need an extra nextLine() after the nextDouble() to flush that line.

you mean to consume the line...will try it thanks..what are you using?

Here's an experiment to try to show you what's happening. Leave the code as you have it:

System.out.print("enter the speed of the car:" );
tSpeed = keyboard.nextDouble();

//the model
System.out.print("Enter the brand: ");
tBrand = keyboard.nextLine();

When you execute the program enter: 55 Ford is the best car
all on one line as answer to the question about speed before pressing Enter.

What happens is the System's console read routine doesn't read anything into its buffer until Enter is pressed. The Scanner method then pulls data out of that buffer. nextDouble() will only pull out the number up to the space, leaving the rest in the buffer. nextLine() will pull out everything up to the end of the line created by pressing Enter.

yeah i see how you mean. not sure how to fix it. guess i'll read up on it today. i added an accelerate accessor and mutator and a brake one as well. to show the speed by 5 miles. but that's not working either.
office work today so i'll be able to read and research plenty.
thanks for all your help!!!

Do you understand the relationship between what's on a line read from the console or from a file and how the nextDouble() or nextInt() methods scan along that line using spaces to end the data they return vs how nextLine() gets everything on the line up to the end of line from the Enter.
So if you use nextDouble() the end of line char is still there to be read. Using nextLine() will read that end of line char clearing out the buffer.
The next input will come from the next line entered by the user.

i take out the speed and build it and it still ask me for the speed so something isn't right in it. working on it for a bit longer and then i'll turn in what i have and just take the grade as is. and ask from input from my teacher. he has the answer key. i'm sure it's a simple mistake somewhere ;(

If your code changes aren't reflected in the running program, then the compile must have failed and you're still using the old version. Make some simple change in what's being printed to show immediately that your new version is executing. Then remove it after you see it.

ok will do...it's not building. it runs the same thing no matter what i change. i may just start from scratch. this is frustrating!

Why doesn't it display any error messages?

I just had to do the same thing as homework. Mine is working fine, and this is the code for part1:

//This program will model a car
//Beauty1304
//10-09-2010

public class Car

{

	private int yearModel; //This field holds the car year model
	private String make;			 //This field holds the make of the car
	private int speed;		//This field holds the car's current speed

	//Constructor with given year model and make
	public Car (int year, String brand)
{

	yearModel = year;
	make = brand;
	speed = 0;

}

	//Accessor for yearModel
	public int getYearModel()
{
	return yearModel;
}

	//Accessor for make
	public String getMake()
{
	return make;
}

	//Accessor for speed
	public int getSpeed()
{
	return speed;
}

	//Increases the speed by 5
	public void accelerate() 
{
		speed = speed +5;
}
	
	//Decreases the speed by 5
	public void brake()
{
	speed = speed -5;
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.