9 Replies - 2466 Views - Last Post: 25 April 2012 - 09:26 PM Rate Topic: -----

#1 alex528   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-April 12

inheritance java help

Posted 25 April 2012 - 03:05 PM

Hi ! i'm getting errors in the TestCar.java in creating a sportsCar object car1.. please help thank you! here is the exercise and the codes i wrote...



write a Car class, and a SportsCar class that extends the Car class.

1. The Car class assuming a car object is described by three pieces of instance data: make (a String), model (a String), year(an int that holds the car’s year). Study the Car class and save the file.

2. The SportsCar class is a subclass of Car. Complete the SprotsCar class with the following properties:
a. An instance variable speed (an integer that represents the current speed).
b. An instance variable maxSpeed (an integer that represents the maximum speed).
c. A constructor that has four parameters—a String containing the make of the car, a String containing the model of the car, a int containing the yearof the car, a int containing the maximum speed of the car. The constructor should call the superclass constructor to initialize make, model, and year. The constructor should initialize maximum speed and set speed to 0.
d. A method accelerate that takes no parameter. The method should add 15 to the speed field each time it is called if the speed will not exceed the maximum speed
e. A method brake that takes no parameter. The method should subtract 5 to the speed field each time it is called if the speed will not be negative.
f. Override toString method so that it calls the superclass toString method and append the maximum speed. The toString method return a String in a format similar to the following:

Make: Honda, Model: Accord, Year: 2005, Maximum speed: 180


3. File TestCars.java contains a driver program that uses the SportsCar class. Save it to your directory and complete it as indicated by the comments.


// ***************************************************************
// Car.java			
//
// Represents a car.
//
// ***************************************************************

public class Car
{
   // Instance variables
    private String make;
    private String model;
    private int year;

    //----------------------------------------------------------------
    // Constructor - sets up a car by initializing the make, the model, and the year.
    //----------------------------------------------------------------

    public Car(String make, String model, int year)
    {
      this.make = make;
      this.model = model;
	this.year = year;

   }
    //----------------------------------------------------------------
    // toString() method returns a string representation of a car.
    //----------------------------------------------------------------
     public String toString()
   {
      return  "Make: "+ make + "," + " Model: " + model + ","+ " Year: " + year;  
   
   }
}


//*********************************************************************
//  SportsCar.java	
//
//  Represents a sportscar.
//*********************************************************************

public class SportsCar extends Car{

    // Instance variables
	 private String make;
    private String model;
    private int year;
	 private int speed;
	 private int maxSpeed = 0;

    //add your code here.
	
	
//------------------------------------------------------------------
// A constructor that has four parameters-a String containing the
// make of the car, a String containing the model of the car, a int 
// containing the yearof the car, a int containing the maximum speed 
// of the car. The constructor should call the superclass 
// constructor to initialize make, model, and year. The constructor 
//should initialize maximum speed and set speed to 0. 

	public SportsCar (String make, String model, int year, int maxSpeed)
	{
		super(make, model,year);
		this.maxSpeed = maxSpeed;
		speed = 0;
		}

    //----------------------------------------------------------------
   // Method accelerate that takes no parameter. The method updates the 
   // speed by adding 15 to the speed if the speed will not exceed the
   // maximum speed. The method should return the current speed. 
   //----------------------------------------------------------------
    public double Accelerate()
	 { 
	 if (speed != maxSpeed)
	  speed = speed + 15;
	 return speed;
		}
    //----------------------------------------------------------------
   // Method brake that takes no parameter. The method updates the 
   // speed by subtracting 15 to the speed if the speed will be
    //negative. The method should return the current speed.
   //----------------------------------------------------------------
    public double Brake()
	 {
	 	if (speed !=0)
	 speed = speed - 15;
	 return speed;
	
	 }

    //----------------------------------------------------------------
    //toString() method returns a string representation of a sportscar.
    //----------------------------------------------------------------
		public String toString(){
		
		return ("Make: "+ make + "," + " Model: " + model + ","+ " Year: " + year + "," + "Maximum speed:" + maxSpeed);
		} 


} 


//*********************************************************************
//  TestCars.java
//
//*******************************************************************
import java.util.Scanner;

public class TestCars
{
    //-------------------------------------------------------
// Creates a car object and then calls the accelerate method and  
// the brake method.
    //-------------------------------------------------------
    public static void main (String[] args)
    {
	String makeOfCar;
	String modelOfCar;
	int yearOfCar;
      int maxSpeed;
		
	SportsCar car1;
	car1 = new SportsCar(makeOfCar,modelOfCar,maxSpeed);

	Scanner scan = new Scanner(System.in);

	System.out.println ("Testing Cars Program\n");

	System.out.print ("Enter the make of the car: ");
	makeOfCar = scan.nextLine();
	System.out.print ("Enter the model of the car: ");
	modelOfCar = scan.nextLine();

System.out.print ("Enter the year of the car: ");
	yearOfCar = scan.nextInt();

System.out.print ("Enter the maximum speed of the car: ");
	maxSpeed = scan.nextInt();

	// Create a SportsCar object car1 with user input. 

		 car1 = new SportsCar (modelOfCar,yearOfCar,maxSpeed);	
		  
	
// Accelerate seven times and display the current speed after 
      // each call.

	 for(int i=0; i<5; i++)
      {
          
          SportsCar.Accelerate();
          System.out.println("Now the speed is " + SportsCar.getSpeed());
}
	// Brake eight times and display the current speed after 
      // each call.
	for(int i=0; i<8; i++)
      {
          
          SportsCar.Brake();
          System.out.println("Now the speed is " + SportsCar.getSpeed());	
	}


	// Print summary for the car 
	System.out.println();
	System.out.println (car1);
    }
	 }


Is This A Good Question/Topic? 0
  • +

Replies To: inheritance java help

#2 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: inheritance java help

Posted 25 April 2012 - 03:06 PM

Post the errors.
Was This Post Helpful? 0
  • +
  • -

#3 alex528   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-April 12

Re: inheritance java help

Posted 25 April 2012 - 03:46 PM

TestCars.java:21: cannot find symbol
symbol : constructor SportsCar(java.lang.String,java.lang.String,int)
location: class SportsCar
car1 = new SportsCar(makeOfCar,modelOfCar,maxSpeed);
^
TestCars.java:49: non-static method Accelerate() cannot be referenced from a static context
SportsCar.Accelerate();
^
TestCars.java:50: cannot find symbol
symbol : method getSpeed()
location: class SportsCar
System.out.println("Now the speed is " + SportsCar.getSpeed());
^
TestCars.java:57: non-static method Brake() cannot be referenced from a static context
SportsCar.Brake();
^
TestCars.java:58: cannot find symbol
symbol : method getSpeed()
location: class SportsCar
System.out.println("Now the speed is " + SportsCar.getSpeed());
^
5 errors
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: inheritance java help

Posted 25 April 2012 - 04:00 PM

The first error: there is no SportsCar() constructor that takes ( String, int, int ) parameters. Either add the appropriate constructor or fix your statement.

Second error: make the Accelerate() method static.

Third: can't find the method getSpeed(). Hmmm. You need one.

Fourth: make it static.

Fifth: can't find the method getSpeed().

Your method names should begin with lower-case letters.
Was This Post Helpful? 1
  • +
  • -

#5 alex528   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-April 12

Re: inheritance java help

Posted 25 April 2012 - 05:23 PM

Thanks but now i'm still having error on the accelerate..

the code i have in SportsCar.java for accelerate is
  public void Accelerate()
	 { 
	 if (speed != maxSpeed)
	  speed = speed + 15;

		}



is that right?

//*********************************************************************
//  SportsCar.java		
//
//  Represents a sportscar.
//*********************************************************************

public class SportsCar extends Car{

    // Instance variables
	 private String make;
    private String model;
    private int year;
	 private int speed;
	 private int maxSpeed = 0;

    //add your code here.
	 
	
	
//------------------------------------------------------------------
// A constructor that has four parameters-a String containing the
// make of the car, a String containing the model of the car, a int 
// containing the yearof the car, a int containing the maximum speed 
// of the car. The constructor should call the superclass 
// constructor to initialize make, model, and year. The constructor 
//should initialize maximum speed and set speed to 0. 

	public SportsCar (String make, String model, int year, int maxSpeed)
	{
		super(make, model,year);
		this.maxSpeed = maxSpeed;
		speed = 0;
		}

    //----------------------------------------------------------------
   // Method accelerate that takes no parameter. The method updates the 
   // speed by adding 15 to the speed if the speed will not exceed the
   // maximum speed. The method should return the current speed. 
   //----------------------------------------------------------------
    public void Accelerate()
	 { 
	 if (speed != maxSpeed)
	  speed = speed + 15;

		}
    //----------------------------------------------------------------
   // Method brake that takes no parameter. The method updates the 
   // speed by subtracting 15 to the speed if the speed will be
    //negative. The method should return the current speed.
   //----------------------------------------------------------------
    public void Brake()
	 {
	 	if (speed !=0)
	 speed = speed - 15;
	
	
	 }
	 
	 public double getSpeed()
	 {return speed;
	 }

    //----------------------------------------------------------------
    //toString() method returns a string representation of a sportscar.
    //----------------------------------------------------------------
		public String toString(){
		
		return ("Make: "+ make + "," + " Model: " + model + ","+ " Year: " + year + "," + "Maximum speed:" + maxSpeed);
		} 


} 



//*********************************************************************
//  TestCars.java
//
//*******************************************************************
import java.util.Scanner;

public class TestCars
{
    //-------------------------------------------------------
// Creates a car object and then calls the accelerate method and  
// the brake method.
    //-------------------------------------------------------
    public static void main (String[] args)
    {
	String makeOfCar;
	String modelOfCar;
	int yearOfCar;
      int maxSpeed;
		
		
	SportsCar car1;
	
	Scanner scan = new Scanner(System.in);

	System.out.println ("Testing Cars Program\n");

	System.out.print ("Enter the make of the car: ");
	makeOfCar = scan.nextLine();
	System.out.print ("Enter the model of the car: ");
	modelOfCar = scan.nextLine();

System.out.print ("Enter the year of the car: ");
	yearOfCar = scan.nextInt();

System.out.print ("Enter the maximum speed of the car: ");
	maxSpeed = scan.nextInt();

	// Create a SportsCar object car1 with user input. 

		 	car1 = new SportsCar( makeOfCar,  modelOfCar, yearOfCar, maxSpeed);


		  
	
// Accelerate seven times and display the current speed after 
      // each call.

	 for(int i=0; i<5; i++)
      {
         
           SportsCar.Accelerate();
          System.out.println("Now the speed is " + SportsCar.getSpeed());
}
	// Brake eight times and display the current speed after 
      // each call.
	for(int i=0; i<8; i++)
      {
          
          SportsCar.Brake();
          System.out.println("Now the speed is " + SportsCar.getSpeed());	
	}


	// Print summary for the car 
	System.out.println();
	System.out.println (car1);
    }
	 }

This post has been edited by Curtis Rutland: 25 April 2012 - 07:29 PM
Reason for edit:: edited to remove personal information at poster's request

Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: inheritance java help

Posted 25 April 2012 - 06:07 PM

also, you want inheritance ?
SportCar shouldn't have the variables make, model, year. It should use the ones of its father. If SportCar does not use make, model, year of its father Car, there is no reason for it to extend Car
Was This Post Helpful? 1
  • +
  • -

#7 alex528   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-April 12

Re: inheritance java help

Posted 25 April 2012 - 08:40 PM

But it says on number 2. A constructor that has four parameters—a String containing the make of the car, a String containing the model of the car, a int containing the yearof the car, a int containing the maximum speed of the car.
Was This Post Helpful? 0
  • +
  • -

#8 alex528   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-April 12

Re: inheritance java help

Posted 25 April 2012 - 09:08 PM

i have to form a class hierarchy..
Was This Post Helpful? 0
  • +
  • -

#9 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: inheritance java help

Posted 25 April 2012 - 09:21 PM

Post your updated code
Was This Post Helpful? 0
  • +
  • -

#10 alex528   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-April 12

Re: inheritance java help

Posted 25 April 2012 - 09:26 PM

It works now thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1