Question

Write a java program that creates a class Car. Class Car contains the following private member...

Write a java program that creates a class Car. Class Car contains the following private member fields: make, model, year, and miles. The class Car has a constructor that takes as arguments the car make, model, and year. In addition, the constructor sets miles to 100 as default. Class Car has accessors and mutators to get and set the make, model, year and miles on the car. The program should complete the following tasks:

  1. Ask the user to enter make, model, and year for a car creating an object.
  2. Display back the make, model, year, and default miles for the car using accessor methods.
  3. Ask the user to enter the new miles (e.g. 50,000).
  4. Display the new miles using accessor method.
0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class Car {

    private String make, model;
    private int year, miles;

    public Car(String make, String model, int year) {
        this.make = make;
        this.model = model;
        this.year = year;
        this.miles = 100;
    }

    public String getMake() {
        return make;
    }

    public void setMake(String make) {
        this.make = make;
    }

    public String getModel() {
        return model;
    }

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

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMiles() {
        return miles;
    }

    public void setMiles(int miles) {
        this.miles = miles;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter make: ");
        String make = in.nextLine();
        System.out.print("Enter model: ");
        String model = in.nextLine();
        System.out.print("Enter year: ");
        int year = in.nextInt();

        Car car = new Car(make, model, year);
        System.out.println("Make: " + car.getMake() + ", Model: " + car.getModel() + ", Year: " + car.getYear() + ", Miles: " + car.getMiles());
        System.out.print("Enter new miles(e.g. 50000): ");
        car.setMiles(in.nextInt());
        System.out.println("Make: " + car.getMake() + ", Model: " + car.getModel() + ", Year: " + car.getYear() + ", Miles: " + car.getMiles());
    }
}

Add a comment
Know the answer?
Add Answer to:
Write a java program that creates a class Car. Class Car contains the following private member...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Program Write a class named Car that has the following fields: yearModel- The yearModel field...

    Java Program Write a class named Car that has the following fields: yearModel- The yearModel field is an int that holds the car’s year model. make- The make field is a String object that holds the make of the car, such as “Ford”, “Chevrolet”, etc. speed- This speed field is an int that holds the car’s current speed. In addition, the class should have the following methods: Constructor- The constructor should accept the car’s year model and make as arguments....

  • C++ Program 1a. Purpose Practice the creation and use of a Class 1b. Procedure Write a...

    C++ Program 1a. Purpose Practice the creation and use of a Class 1b. Procedure Write a class named Car that has the following member variables: year. An int that holds the car’s model year. make. A string object that holds the make of the car. speed. An int that holds the car’s current speed. In addition, the class should have the following member functions. Constructor. The constructor should accept the car’s year and make as arguments and assign these values...

  • python Design a class named Car that has the following fields: yearModel: The yearModel field is...

    python Design a class named Car that has the following fields: yearModel: The yearModel field is an Integer that holds the car’s year model. make: The make field references a String that holds the make of the car. speed: The speed field is an Integer that holds the car’s current speed. In addition, the class should have the following constructor and other methods: Constructor: The constructor should accept the car’s year model and make as arguments. These values should be...

  • Programming Language is Java 1. Create a class to describe a car. Include fields like: make,...

    Programming Language is Java 1. Create a class to describe a car. Include fields like: make, model, year and color. Add all the accessors and mutators for these fields. Add a constructor that can initialize the fields. 2. Create a class that will enable you to handle standard mail communication with a customer. Add all accessors and mutators, as well as a constructor. Add a method named fullAddress that will return the address formatted for a standard mail. Add a...

  • java Write a class named Car that has the following fields: • yearModel: The yearModel field...

    java Write a class named Car that has the following fields: • yearModel: The yearModel field is an int that holds the car's year model. • make: The make field is a String object that holds the make of the car. • speed: The speed field is an int that holds the car's current speed. In addition, the class should have the following methods: • Constructor: The constructor should accept the car's year model and make as arguments. These values...

  • WRITE JAVA PROGRAM Problem Statement You are required to write a stock price simulator, which simulates...

    WRITE JAVA PROGRAM Problem Statement You are required to write a stock price simulator, which simulates a price change of a stock. When the program runs, it should do the following:  Create a Stock class which must include fields: name, symbol, currentPrice, nextPrice, priceChange, and priceChangePercentage.  The Stock class must have two constructor: a no-argument constructor and a constructor with four parameters that correspond to the four fields. o For the no-argument constructor, set the default value for...

  • write a program C++ about Copmputer struct Redo lab 3 and change the Computer class to...

    write a program C++ about Copmputer struct Redo lab 3 and change the Computer class to a struct. 1. Keep the default and non-default constructors 2. Your struct does not need the accessors and mutators since all variables are public by default. 3. Write a main(): - Create an instance of type Computer using the default constructor. - Set the values for year, model, and purpose using any values you'd like. - Print the values of year, model, and purpose...

  • Design and implement a C++ class called Date that has the following private member variables month...

    Design and implement a C++ class called Date that has the following private member variables month (int) day (nt) . year (int Add the following public member functions to the class. Default Constructor with all default parameters: The constructors should use the values of the month, day, and year arguments passed by the client program to set the month, day, and year member variables. The constructor should check if the values of the parameters are valid (that is day is...

  • Please help me to write a c++ program that will Implement a HotelRoom class, with private...

    Please help me to write a c++ program that will Implement a HotelRoom class, with private data members: the room number, room capacity (representing the maximum number of people the room can accommodate), the occupancy status (0 or the number of occupants in the room), the daily room rate. Member functions include: • a 4-argument constructor that initializes the four data members of the object being created (room number, room capacity, room rate, occupancy status) to the constructor's arguments. The...

  • [JAVA] Program: Design a Ship class that the following members: A field for the name of...

    [JAVA] Program: Design a Ship class that the following members: A field for the name of the ship (a string) o A field for the year the the ship was built (a string) o A constructor and appropriate accessors and mutators A toString method that displays the ship's name and the year it was built Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members: A field for the maximum number of passengers...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT