Question

In: Computer Science

This code in java: Create a class named car. A car has color, model, company, registration...

This code in java:

Create a class named car. A car has color, model, company, registration number. You can stear a car. A car can move forward. A car has a gear box. A typical gear decide weather car is moving forward or backward. A person owns a car.

Kindly add some other functionalities like refuel

Solutions

Expert Solution


CODE:

In "Car" class:

package carproperties;
public class Car
{
    //variables
    private String color, model, company, regNumber,owner;
    private char gear;
    //getters and setters
    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }
  
    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public char getGear() {
        return gear;
    }
    //setGear function also prints the status of the gearBox
    // if incorrect input returns without changing the data
    public void setGear(char gear) {
        if(gear == 'F')
            System.out.println("Car is moving in Forward direction.");
        else if (gear == 'B')
            System.out.println("Car is moving in Backward direction.");
        else if(gear == 'N')
            System.out.println("Car is not moving.");
        else
        {
            System.out.println("Incorrect Input");
            return;
        }
        this.gear = gear;
    }
    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getRegNumber() {
        return regNumber;
    }

    public void setRegNumber(String regNumber) {
        this.regNumber = regNumber;
    }
    //constructor
    public Car(String color, String model, String company, String regNumber, String owner, char gear) {
        this.color = color;
        this.model = model;
        this.company = company;
        this.regNumber = regNumber;
        this.owner = owner;
        this.gear = gear;
    }

    //This function takes in steering direction and prints it
    public void stear(String direction)
    {
        System.out.println("Car is being steared in "+direction+" direction");
    }
    //prints a message
    public void refuel()
    {
        System.out.println("Car is refueled");
    }
}


IN Driver program, "CarProperties":

package carproperties;

import java.util.Scanner;

public class CarProperties
{
    //driver code for example
    public static void main(String[] args)
    {
        //car class variable for example
        Car c = new Car("Red","Mustang", "Ford","US2345","Jeff", 'F');
        Scanner scan = new Scanner(System.in);
        // to get user option choice
        int con;
        do
        {
            // prints the choice and gets an input
            System.out.print("Choose:"+
                                "\n1.\tStear the car"+
                                "\n2.\tChange Gear of the car"+
                                "\n3.\tRefuel the car"+
                                "\n4.\tExit"+
                                "\nChoice: ");
            con = scan.nextInt();
            // using switch case on the value of "con" till con is not 4
            switch(con)
            {
                case 1 :
                    System.out.print("Enter the direction to stear(Left/Right): ");
                    String dir = scan.next();
                    c.stear(dir);
                    break;
                case 2:
                    System.out.print("Enter the update gear(F/B/N) ");
                    char ch = scan.next().charAt(0);
                    c.setGear(ch);
                    break;
                case 3:
                    c.refuel();
                    break;
                case 4:
                    break;
                default:
                    System.out.println("Incorrect Input. Please try again");
              
            }
        }while(con!=4);
    }
  
}


OUTPUT:

Choose:
1.   Stear the car
2.   Change Gear of the car
3.   Refuel the car
4.   Exit
Choice: 1
Enter the direction to stear(Left/Right): Left
Car is being steared in Left direction
Choose:
1.   Stear the car
2.   Change Gear of the car
3.   Refuel the car
4.   Exit
Choice: 2
Enter the update gear(F/B/N) B
Car is moving in Backward direction.
Choose:
1.   Stear the car
2.   Change Gear of the car
3.   Refuel the car
4.   Exit
Choice: 3
Car is refueled
Choose:
1.   Stear the car
2.   Change Gear of the car
3.   Refuel the car
4.   Exit
Choice: 4

Ask any questions if you have in comment section below.

Please rate the answer.


Related Solutions

Code in Java Create a class named DictionaryWord as: DictionaryWord - word: String                            &n
Code in Java Create a class named DictionaryWord as: DictionaryWord - word: String                                                              - meanings: String + DictionaryWord (String word, String meanings) + getWord(): String + setWord (String word): void + getMeanings(): String + setMeanings(String meanings): void Write a program with the following requirements: Creates 8 DictionaryWord objects with: Word and meanings as the table: word meanings bank robber Steals money from a bank burglar Breaks into a home to steal things forger Makes an illegal copy of something...
In java: -Create a class named Animal
In java: -Create a class named Animal
Code in Java 1. Create a class Flower with data: Name, Price, Color and properly methods....
Code in Java 1. Create a class Flower with data: Name, Price, Color and properly methods. 2. Create another class named ListFlower. This class manages a collection of Flower (may be LinkedList) named a. Implementing some methods for ListFlower: Add: add new item of Flower to a Display: display all items of a sort(): sort as descending by Price and display all items of a search(Flower f): check and return whether f is exists in a or not. delete(int pos):...
CODE IN JAVA Create a new class named Task1. In its main function, use Scanner to...
CODE IN JAVA Create a new class named Task1. In its main function, use Scanner to read integers from the keyboard with the method nextInt. Use a try catch block to capture non-integer input, and tell the user, "This is not an Integer". The program loop should end when the user enters the string "quit". (Hint: "quit" is clearly not a number and will be captured in the try / catch block.) A typical Output Dialog is shown below: Enter...
1. Create a class named Mobile 2. Add IEMICode, processor, make, model and color properties to...
1. Create a class named Mobile 2. Add IEMICode, processor, make, model and color properties to the Mobile class 3. Create a methods connectBlueTooth, sendMessage, changeColor, displayInfo in the class Mobile. 4. Write a python script that creates two instances of the class Mobile, changes their colors to Black and Pink and prints a message to the console to display the object attributes using the displayInfo method 5. Run the program and observe the message in Console
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Create a new Java project called lab1 and a class named Lab1 Create a second class...
Create a new Java project called lab1 and a class named Lab1 Create a second class called VolumeCalculator. Add a static field named PI which = 1415 Add the following static methods: double static method named sphere that receives 1 double parameter (radius) and returns the volume of a sphere. double static method named cylinder that receives 2 double parameters (radius & height) and returns the volume of a cylinder. double static method named cube that receives 1 double parameter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT