I am trying to figure out this one out. The directions:

Write a class named car in the following fields:

  1. yearModel - The yearModel field is an int that holds the car's yea model.
  2. make - The make field references a String object that holds the make of the car.
  3. 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: - Constructor - The constructor should accept the car's year model and make as arguments. These values should be assigned to the object's yearModel and make fields. The constructor should also assign 0 to speed field. - Accessor (getters). Appropriate getter methods should get the values stored in an object's yearModel, make and speed fields. - accelerate. The accelerate method should add 5 to the speed each time it is called. - brake. The brake method should subtract 5 from the speed field each time it is called.

Demonstrate that class in a program that creates a Car object and then calls the accelerate method five times. After each 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.

I get a "CarDemo.java:47: reached end of file while parsing" when I compile it the CarDemo.java program.

The code is as follows (Car.java) and (CarDemo.java):

// Car.java
// This is a class called Car with 3 different fields, holds data about a car.
// Programmer: X
// Chapter 6, Programming Challenge #2 Car Class.
// 3/10/2012

 public class Car
{
private int yearModel; // The car's year model
private String make; // The car's make
private int speed; // The current speed


Car(int year, String carMake, int newSpeed)

{
yearModel = year;
make = carMake;
speed = newSpeed;
}


public void setYearModel(int y)
{
yearModel = y;
}



public void setMake(String m)
{
make = m;
}



public void setSpeed(int s)
{
speed = s;
}


public int getYearModel()
{
return yearModel;
}


public String getMake()
{
return make;
}


public int getSpeed()
{
return speed;
}


public void accelerate()
{
speed += 5;
}


public void brake()
{
speed -= 5;
}
}
// CarDemo.java
// This is a program called CarDemo that tells the person running the program that the car is going 0 mph,
// then accelerates to 5 mph, then brakes 5mph to 0mph again.
// Programmer: X
// CISC115
// Chapter 6, Programming Challenge #2 Car Class.
// 3/10/2012


public class CarDemo
   
   {
	public static void main(String[] args)
	{
	int s = speed;	
	int accelerate;
	int brake;
	String c;
	
	{
		Car c = new Car(2012, "Mercedes-Benz S55 AMG");
		int s = 0;
		s = c.getSpeed();
		for(int i = 0; i < 5; i++)
		{
			System.out.println("The " + c.getYearModel() + " " + c.getMake()
			+ "\n is going " + s(accelerate));
 		
			
 
			System.out.println("Now the " + c.getYearModel() + " " + c.getMake()
			+ "\n is going " + s(brake));			
 
		}
	
		
     // other code to accelerate or decelerate
	  {
	  Car c = new Car(2012, "Mercedes-Benz S55 AMG");
	  int s = 0;
     s = c.getSpeed();
	  for(int i = 0; i < 5; i++)
		{
	  
     System.out.println("The car's current speed is " + s);
	}
 }
Member Avatar for ztini

You've got a lot of fundamental problems here. It may be more prudent to return to Chapters 2 or 3, before trying 6.

I digress, your Car class is fine. Your CarDemo...has issues.

Line 15

int s = speed;

speed is not defined anywhere. If you're trying to access Car.speed, then you need to instantiate a Car object and use the getSpeed() method. According to the Car class, it might look like this:

// year, make/model, current speed
	Car car = new Car(2005, "Jeep Wrangler Rubicon", 0);

Line 13-20

public static void main(String[] args)
	{

	....
	
	{

This last hanging bracket doesn't belong to a method or a constructor. Did you intend it to?

Lines 18-21

String c;
	
	{
		Car c = new Car(2012, "Mercedes-Benz S55 AMG");

You define c as a String, then try to define it as a Car class. Most likely you wanted a Car class all along, remove String c.

Lines 21-23

Car c = new Car(2012, "Mercedes-Benz S55 AMG");
		int s = 0;
		s = c.getSpeed();

Your Car constructor takes 3 inputs: year, make/model, and current speed. You have only given it 2. Then you set s to 0 and then to your car's current speed (but you haven't defined your car's current speed in the constructor. You were probably looking for something like this:

Car car = new Car(2012, "Mercedes-Benz S55 AMG", 0);
		int speed = car.getSpeed();

There are more errors, but its not worth the effort to explain them. You need to go back to the very early chapters of whatever book you are working through and re-read the fundamentals of class and method creation.

Small observation: The spec explicitly requires a two argument constructor (year and make). It assigns 0 to speed, but does not have speed as a parameter.

I revised the code, but all I get when I compile it is the current speed as '0'.

public class CarDemo
   
   {
      public static void main(String[] args)
      {
         int speed = 0;	
         int accelerate = 5;
         int brake = 5;
      
		   Car car = new Car(2012, "Mercedes-Benz S55 AMG", 0);
         speed = car.getSpeed();
         for(int i = 0; i < 5; i++)
			{
            System.out.println("The " + car.getYearModel() + " " + car.getMake()
               + "\n is going " + (speed*accelerate));
         
         
         
            System.out.println("Now the " + car.getYearModel() + " " + car.getMake()
               + "\n is going " + (speed*accelerate));			
         }
         
      
      
      // other code to accelerate or decelerate
         
         
            for(int i = 0; i < 5; i++)
            {
            
               System.out.println("The car's current speed is " + (speed*brake));
				}
			}
			
		}

well, you still are passing 0 as a parameter, since that constructor shouldn't take a speed parameter, that's wrong. how did you change your car code, how did you change your main method and what is the exact error message you get?

You are multiplying the speed by accel/brake when you should be adding/subtracting. Since the speed starts as zero, when you multiply or divide by anything you still get zero.

// CarDemo.java
// This is a program called carDemo that tells the person running the program that the car is going 0 mph,
// then accelerates to 5 mph, then brakes 5mph to 0mph again.
// Programmer: X
// CISC115
// Chapter 6, Programming Challenge #2 car Class.
// 3/10/2012


   public class CarDemo
   
   {
      public static void main(String[] args)
      {
         int speed = 0;	
         int accelerate = 5;
         int brake = 0;
      
         Car car = new Car(2012, "Mercedes-Benz S55 AMG", 0);
         speed = car.getSpeed();
         for(int i = 0; i < 5; i++)
         {
            System.out.println("The " + car.getYearModel() + " " + car.getMake()
               + "\n is going " + (speed+accelerate));
         
         
         
            System.out.println("Now the " + car.getYearModel() + " " + car.getMake()
               + "\n is going " + (speed+accelerate));			
         }
         
      
      
      // other code to accelerate or decelerate
         
         
         for(int i = 0; i < 5; i++)
         {
            
            System.out.println("The car's current speed is " + (speed-brake));
				
								
         }
      }
   		
   }

The output is:

The 2012 Mercedes-Benz S55 AMG
is going 5
Now the 2012 Mercedes-Benz S55 AMG
is going 5
The 2012 Mercedes-Benz S55 AMG
is going 5
Now the 2012 Mercedes-Benz S55 AMG
is going 5
The 2012 Mercedes-Benz S55 AMG
is going 5
Now the 2012 Mercedes-Benz S55 AMG
is going 5
The 2012 Mercedes-Benz S55 AMG
is going 5
Now the 2012 Mercedes-Benz S55 AMG
is going 5
The 2012 Mercedes-Benz S55 AMG
is going 5
Now the 2012 Mercedes-Benz S55 AMG
is going 5
The car's current speed is 0
The car's current speed is 0
The car's current speed is 0
The car's current speed is 0
The car's current speed is 0

I was wondering if this is correct.

is it giving the output you want it to return, based on the input?
it returns exactly what you programmed, so it can be regarded as either a working application, or code containing logical errors if you want/expect other output.

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.