ash gholami
Posts: 3
Nickname: ashgx47
Registered: Oct, 2006
|
|
Re: I am new and need help with objects and classes
|
Posted: Oct 13, 2006 11:20 PM
|
|
- Ask user in terminal window to input the model and year for two seperate cars - Apply accelerate method 3 times for the first car and 2 times for the second - Display the current speed of both cars.. should be 15mph and 10mph - Apply brake method 2 times for each of the cars. - Display speed of both cars.. should be 5mph and 0mph
hope that makes it more clear, once again i appreciate all the help and i am learning ! :)
public class Car
{
//*************Instance Variables*************************************
public int yearModel;
public String make;
public int speed;
//************Constructors*******************************************
/* Instantiates Car and makes all variables zero*/
public Car()
{
yearModel=0;
speed=0;
make="";
}
//***********Explicit value constructor*****************************
//********Instantiates new Car and initializes make, model and speed*
public Car(int yearModel, int speed, String make)
{
this.yearModel=yearModel;
this.make= make;
}
//*************Accessor Methods***************************************
//*The getYear method returns the year for the car********************
public int getYear()
{
return yearModel;
}
//*The getSpeed method returns the speed for the car******************
public int getSpeed()
{
return speed;
}
//*The getMake method returns the make of the car*********************
public String getMake()
{
return make;
}
//*the accelerate method adss 5 to the current speed every time it is called
public int accelerate()
{
speed=speed+5;
return speed;
}
//*the brake method subtracts 5 to the current speed every time it is called
public int brake()
{
speed=speed-5;
return speed;
}
//*************Mutator Methods****************************************
//*The setYear sets the year of the car to the value passed in the year parameter
public void setYear(int yearValue)
{
this.yearModel=yearValue;
}
//*The setMake sets the make of the car to the value passed in the make paramtere
public void setMake(String make)
{
this.make=make;
}
//*************Input Output Methods***********************************
//*The toString method returns a string representation of the car info
public String toString()
{
return "The "+yearModel+" "+make+" is currently going "+speed+" MPH.";
} // end method toString
//************* MAIN METHOD ******************************************
public static void main(String[]args)
{
Car car1= new Car(yearModel,make,speed);
}
} // end class Car
|
|