Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

🎯 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:

  1. brand (String): The brand of the car.

  2. model (String): The model of the car.

  3. year (int): The production year of the car.

  4. mileage (double): The current mileage of the car.

Constructors:

You should define two constructors for the Car class:

  1. Default Constructor: This constructor does not take any arguments and initializes brand and model to empty strings, year to zero, and mileage to zero.

  2. Parameterized Constructor: This constructor takes arguments for brand, model, year, and mileage.

Methods:

The Car class should contain the following methods:

  1. Getters and Setters for brand, model, year, and mileage.

  2. 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:

  1. Prompts the user to enter the brand, model, year, and current mileage for a new car.

  2. Creates a new Car object using the parameterized constructor and the user's input.

  3. Prints out the car's brand, model, year, and current mileage.

  4. Prompts the user to enter a number of miles to drive the car. Calls the drive method with the user's input, then prints out the car's new mileage.

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:

  1. Java files - Employee.java and your driver file

  2. Screenshot of the Console with the code executing

  • No labels