1,105,352 Community Members

Car class and Constructor

Member Avatar
mags11
Light Poster
45 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
ztini
Posting Whiz in Training
291 posts since Jan 2011
Reputation Points: 8 [?]
Q&As Helped to Solve: 52 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,751 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

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.

Member Avatar
mags11
Light Poster
45 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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));
				}
			}
			
		}
Member Avatar
stultuske
Posting Expert
5,326 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

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?

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,751 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

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.

Member Avatar
mags11
Light Poster
45 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
// 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

Member Avatar
mags11
Light Poster
45 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I was wondering if this is correct.

Member Avatar
stultuske
Posting Expert
5,326 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: