0% found this document useful (0 votes)
191 views2 pages

Class: Write A Java To Represent A Vehicle Type

The document describes how to create a Vehicle class in Java with fields for yearModel, make, speed, and VINNumber. It includes different constructor methods to initialize the fields, getter and setter methods to access the fields, and accelerate and brake methods to modify the speed field. It also describes creating a RaceTrack class with a main method that creates a Vehicle object, displays its initial status, calls accelerate multiple times to increase speed and displays the updated speed, and calls brake multiple times to decrease speed and displays the updated speed each time.

Uploaded by

SUNDAS FATIMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
191 views2 pages

Class: Write A Java To Represent A Vehicle Type

The document describes how to create a Vehicle class in Java with fields for yearModel, make, speed, and VINNumber. It includes different constructor methods to initialize the fields, getter and setter methods to access the fields, and accelerate and brake methods to modify the speed field. It also describes creating a RaceTrack class with a main method that creates a Vehicle object, displays its initial status, calls accelerate multiple times to increase speed and displays the updated speed, and calls brake multiple times to decrease speed and displays the updated speed each time.

Uploaded by

SUNDAS FATIMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Write a Java class to represent a Vehicle type

1. Create a new Java class named Vehicle that has the following fields
o yearModel- The yearModel field is an int that holds a Vehicle's
yearModel model (e.g. 2010)
o make - The make field is a String object that holds the make of
the Vehicle (e.g. "Porsche")
o speed - The speed field is an double that holds a Vehicle's
current speed (e.g. 25.0)
o VINNumber- The VINNumber field is a string object that holds a
Vehicle's VINNumber (17 digit number)
2. In addition, the Vehicle class should have the following methods.

Constructor :

o Constructor - The constructor should accept the


Vehicle's yearModel, make, VINNumber, and
beginning speed as arguments
 These values should be used to initialize the Vehicle's
yearModel, make, VINNumber and speed fields
o Constructor - The constructor should accept the
Vehicle's yearModel, VINNumber and make as arguments
 These values should be used to initialize the Vehicle's
yearModel, VINNumber and make. Initial speed for object
should be 0.
o Constructor - The constructor should be parameter less.
 Vehicle's yearModel, make, and speed fields should be
initialized with default values.
o Getter Methods - Write four accessor (getter) methods to get the
values stored in an object's fields
 getYearModel(), getMake(), getSpeed(),
getVINNumber()
o Setter Methods - Write three setter methods to set the values
stored in an object's fields
 setYearModel(), setMake(), setSpeed(),setVINNumber()
o accelerate - Write an accelerate method that has no arguments
(parameters) passed to it and adds 5 to the speed field each time
it is called
 For example: if the Vehicle was going 3 mph, accelerate
would set the speed to 8 mph
o accelerate - Write another accelerate method that takes int
parameter and add that number to speed field each time it is
called
o accelerate - Write another accelerate method that takes
Boolean parameter IsExponential and doubles the speed field id
IsExponential is true each time it is called
 For example: if the Vehicle was going 3 mph, accelerate
would set the speed to 6 mph
 (Hint: use math class object to double the value)
o Brake. The brake method should subtract 5 from the speed field
each time it is called.

NB. VINNumber’s length must be 17, Setter and constructor should


validate it.

Write a main method that uses your Vehicle class


1. Write a separate Java class RaceTrack in file RaceTrack.java with
a main method that uses your Vehicle class
2. Inside RaceTrack class' main method
o Create a new Vehicle object (using the Vehicle constructor
method), passing in the yearModel, make, VINNumber and speed
o Display the current status of the Vehicle object using
the getter methods getYearModel(), getMake(),
getVINNumber(), and getSpeed()
o Call the accelerate method five times. After each call to the
accelerate method, get the current speed of the Vehicle and display
it. Then call the brake method five times. After each call to the brake
method, get the current speed of the Vehicle and display it.

You might also like