Page 396 - Programming Challenge #2 ( Class) that has the following fields: · ma
ID: 3767540 • Letter: P
Question
Page 396 - Programming Challenge #2 ( Class)
that has the following fields:
· make. The make field references a String object that holds the make of the car.
· In addition, the class should have the following constructor and other methods.
· Accessors. Appropriate accessor methods should get the values stored in an object's yearModel, make, and speed fields.
· brake. The break method should subtract 5 from the speed field each time it is called.
Include your name and the program's purpose in a comment that appears before the class declaration.
Use the correct comment type for the Method, including @parameters and @return values
You must ATTACH both .java files before submitting the assignment.
Explanation / Answer
import javax.swing.JOptionPane;
public class Car
{
private int Speed = 0;
private String Make = "Chevy";
private int yearModel = 2000;
public Car (String n, int Model)
{
Make = n;
yearModel = Model;
}
public int getyearModel ()
{
return yearModel;
}
public String getMake ()
{
return Make;
}
public int getSpeed ()
{
return Speed;
}
public void accelerate ()
{
if (Speed !=90)
Speed = Speed +5;
}
public void brake ()
{
if (Speed !=0)
Speed = Speed -5;
}
}
*****
public class Homewrk
{
public static void main (String [] str)
{
Car car1 = new Car("Chevy", 2000);
for (int i = 1; i <= 5; i++)
{
car1.accelerate();
System.out.println("Current speed is: " + car1.getSpeed());
}
for (int j = 1; j <= 5; j++)
{
car1.brake();
System.out.println("Current speed is: " + car1.getSpeed());
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.