Knowee
Questions
Features
Study Tools

Complete the Car class byDeclaring the String factory instance variableDeclaring the String model instance variableDeclaring int year instance variableDeclaring string colour instance variableWriting the constructor method with input parameters for factory, model, year and colourWriting the method carAge to display the age of the car (2024 - year)Complete the main method byUsing new to create an instance of Car with the constructor: Car("BMW" , "X5" , 2021 , "Black") Printing the car detailsPrinting the return value for the carAge() method on the Car instance

Question

Complete the Car class byDeclaring the String factory instance variableDeclaring the String model instance variableDeclaring int year instance variableDeclaring string colour instance variableWriting the constructor method with input parameters for factory, model, year and colourWriting the method carAge to display the age of the car (2024 - year)Complete the main method byUsing new to create an instance of Car with the constructor: Car("BMW" , "X5" , 2021 , "Black") Printing the car detailsPrinting the return value for the carAge() method on the Car instance

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is the Java code that fulfills your requirements:

public class Car {
    // Declare instance variables
    String factory;
    String model;
    int year;
    String colour;

    // Constructor method with input parameters
    public Car(String factory, String model, int year, String colour) {
        this.factory = factory;
        this.model = model;
        this.year = year;
        this.colour = colour;
    }

    // Method to calculate the age of the car
    public int carAge() {
        return 2024 - this.year;
    }

    // Method to print car details
    public void printCarDetails() {
        System.out.println("Factory: " + this.factory);
        System.out.println("Model: " + this.model);
        System.out.println("Year: " + this.year);
        System.out.println("Colour: " + this.colour);
    }

    public static void main(String[] args) {
        // Create an instance of Car
        Car car = new Car("BMW", "X5", 2021, "Black");

        // Print car details
        car.printCarDetails();

        // Print the age of the car
        System.out.println("The age of the car is: " + car.carAge() + " years");
    }
}

This code defines a Car class with the required instance variables and methods. The main method creates an instance of Car with the specified parameters, prints the car details, and then prints the age of the car.

This problem has been solved

Similar Questions

Create two (2) classes named Car and TestCar.In the Car class, initialize two (2) public static variables: MAKE (String, final) and numCars (int). Set their values to "Toyota" and 0, respectively.Declare two (2) empty String instance variables named chassisNo and model.Create a public constructor with a single parameter named model (String).  This constructor should:Increment the value of numCars by one.Set the value of chassisNo based on the result of concatenating the String "ch" and the value of numCars. Syntax: chassisNo = ch + numCars;Use the this keyword to set the instance variable model equal to the value of the parameter model.Display a message saying "Car manufactured".Create two (2) pairs of getter and setter methods that will allow you to get and set the values of the two (2) instance variables: getModel() and setModel(), getChassisNo() and setChassisNo()Add a public toString() method that will return and display the car's make, model, and chassis number in separate lines.In the main method of the TestCar class, display the car's make and the initial number of manufactured cars by accessing them from the Cars class. Example: System.out.println("Manufacturer: " + Car.MAKE);Instantiate two (2) Car objects assigning the values "Camry" and "Veloz", respectively. Example: Car car1 = new Car("Camry");Add two (2) statements to display the data of the two (2) Car objects. Example: System.out.println(car1);Display the total number of cars manufactured by accessing the numCars method.

Defi ne a class MotorVehicle as describedbelow: Data members: (a) modelName (b) modelNumber (c) modelPrice Methods: (a) display() method to display the name,price, and model number. Defi ne another class named Car that inherits theclass MotorVehicle and has the following: Data members: (a) discountRateMethods: (a) display() method to display the Carname, Car model number, Car price, andthe discount rate. (b) discount() method to compute thediscount Create the classes MotorVehicleand Car with suitable constructors andtest it.

creata a classs vehicle withe datamenbers vehicle number name cost brand and yeat. declare year as static. create objects and display the details. create array of objects

Create an abstract class Motor Vehicle with the following details:Data Members: (a) model Name (b)model Number (c) model PriceMethods: display() to show all the details.Create a subclass of this class Car that inherits the class Motor Vehicle and add the following details:Data Members:  discount RateMethods:(a) display() method to display the Car name, model number, price and the discount rate.(b) discount() method to compute the discount.Input FormatName:Model No:Price:Discount rate:Discount value:Price after discount:Output FormatVehicleTN SA5221100000.00202000.008000.00

Develop a Java Application with ‘Vehicle’ Class with vehicle type, vehicle number, engine number, chassis number, top speed, number of passengers, number of gears. Generate a report for the vehicles. Implement the concept using Interfaces.Input:CarHRDQ245652WVC12342H2X1234535kph45

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.