Module 2 - Lab - Car
Objective
Your task is to create a Java class called Car
and a corresponding driver program to interact with it.
Class Attributes:
The Car
class should contain the following instance variables:
brand
(String): The brand of the car.model
(String): The model of the car.year
(int): The production year of the car.mileage
(double): The current mileage of the car.
Constructors:
You should define two constructors for the Car
class:
Default Constructor: This constructor does not take any arguments and initializes
brand
andmodel
to empty strings,year
to zero, andmileage
to zero.Parameterized Constructor: This constructor takes arguments for
brand
,model
,year
, andmileage
.
Methods:
The Car
class should contain the following methods:
Getters and Setters for
brand
,model
,year
, andmileage
.A method
drive(double miles)
that adds the given number of miles to the current mileage.
Driver Program:
Create a main program (driver program) that does the following:
Prompts the user to enter the brand, model, year, and current mileage for a new car.
Creates a new
Car
object using the parameterized constructor and the user's input.Prints out the car's brand, model, year, and current mileage (format output and make sure you label the data)
Prompts the user to enter the number of miles to drive the car. Calls the
drive
method with the user's input, then prints out the car's new mileage using an appropriate label.
This assignment will allow you to practice using classes, constructors, methods, and simple user interactions in Java. The Car
class is a simple model of a real-world entity.
Deliverables
Upload the following files in a zip folder that you have created:
Java files - Car.java and your driver file
Screenshot of the Console with the code executing
COSC-1437 / ITSE-2457 Computer Science Dept. - Author: Dr. Kevin Roark