Question

In: Computer Science

Creating Enumerations In this section, you create two enumerations that hold colors and car model types....

Creating Enumerations In this section, you create two enumerations that hold colors and car model types. You will use them as field types in a Car class and write a demonstration program that shows how the enumerations are used.

1. Open a new file in your text editor, and type the following Color enumeration: enum Color {BLACK, BLUE, GREEN, RED, WHITE, YELLOW};

2. Save the file as Color.java.

3. Open a new file in your text editor, and create the following Model enumeration: enum Model {SEDAN, CONVERTIBLE, MINIVAN};

4. Save the file as Model.java. Next, open a new file in your text editor, and start to define a Car class that holds three fields: a year, a model, and a color.

public class Car {

private int year;

private Model model;

private Color color;

5. Add a constructor for the Car class that accepts parameters that hold the values for year, model, and color as follows:

public Car(int yr, Model m, Color c) {

year = yr;

model = m;

color = c;

}

6. Add a display() method that displays a Car object’s data, then add a closing curly brace for the class.

public void display() {

System.out.println("Car is a " + year + " " + color + " " + model);

}

}

7. Save the file as Car.java.

8. Open a new file in your text editor, and write a short demonstration program that instantiates two Car objects and assigns values to them using enumeration values for the models and colors.

public class CarDemo {

public static void main(String[] args) {

Car firstCar = new Car(2014, Model.MINIVAN, Color.BLUE);

Car secondcar = new Car(2017, Model.CONVERTIBLE, Color.RED);

firstCar.display();

secondcar.display();

}

}

9. Save the file as CarDemo.java, and then compile and execute it.

the output should be something like

"Car is a 2014 BLUE MINIVAN

Car is a 2017 RED CONVERTIBLE"

in Java

Solutions

Expert Solution

Note: Make sure all the files are in same folder and has name as mentioned(Don't change any file name) there has to be total 4 java files which are given below.

CarDemo.java Code:

public class CarDemo
{
        public static void main(String[] args) {
        Car firstCar = new Car(2014, Model.MINIVAN, Color.BLUE);
    Car secondcar = new Car(2017, Model.CONVERTIBLE, Color.RED);
    firstCar.display();
    secondcar.display();
    
        }
}

Car.java Code:

public class Car{
    int year;
    Model model;
    Color color;
    public Car(int yr, Model m, Color c) {
    year = yr;
    model = m;
    color = c;
}
public void display() {

    System.out.println("Car is a " + year + " " + color + " " + model);

    }
}

Model.java Code:

enum Model {SEDAN, CONVERTIBLE, MINIVAN};

Color.java Code:

enum Color {BLACK, BLUE, GREEN, RED, WHITE, YELLOW};

Output:

Car is a 2014 BLUE MINIVAN
Car is a 2017 RED CONVERTIBLE


Related Solutions

Create two new images by applying a threshold first to various colors in the image. In...
Create two new images by applying a threshold first to various colors in the image. In practice, applying a threshold means that you zero out (set to zero) any value above or below a specified limit. For the first image (im1mod1), zero out all pixels that are below the value 128. For the second image (im1mod2), zero out all red pixels entirely, zero out all green pixels ABOVE 128, and zero out all blue pixels BELOW 128. Use the command...
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
Use the advertised prices for a used car of a particular model to create a linear...
Use the advertised prices for a used car of a particular model to create a linear model for the relationship between a​ car's Year and its Price. Complete parts a through e. Year   Price ($) 1994   18,194 1994   17,945 1994   16,887 1994   14,447 1995   19,282 1995   19,017 1995   20,849 1995   18,621 1996   22,665 1996   23,081 1997   24,899 1997   25,492 1997   23,703 1997   24,413 1997   23,934 1) Find the equation of the regression line. ​2) Explain the meaning of the slope...
Use the advertised prices for a used car of a particular model to create a linear...
Use the advertised prices for a used car of a particular model to create a linear model for the relationship between a​ car's Year and its Price. Complete parts a through e. Year   Price ($) 1994   11,538 1994   12,333 1994   10,436 1994   11,995 1995   14,567 1995   14,658 1995   13,656 1995   15,199 1996   16,192 1996   17,039 1996   19,074 1997   19,823 1997   19,249 1997   19,242 1997   20,577 ​a) Find the equation of the regression line. Price=___ + _____ year b) Explain the...
Car Phones, Inc., sells two models of car telephones: model X and model Y. Records show...
Car Phones, Inc., sells two models of car telephones: model X and model Y. Records show that 3 hours of sales time are used for each model X telephone that is sold and 5 hours of sales time for each model Y telephone. A total of 600 hours of sales time is available for the next 4 week period. In addition, management planning policies call for minimum sales goals of 25 units for both model X and model Y. The...
What are the two main types of investment securities that companies hold? What are the key...
What are the two main types of investment securities that companies hold? What are the key issue in accounting for these investment securities, including cost and fair value accounting? Discuss accounting for investment in debt securities, including held-to-maturity, trading, and available-for-sale securities, at initial recognition and subsequent to acquisition. Discuss in detail accounting for investment in equity securities when the investor has no significant influence and the securities are held as trading securities or available-for-sale securities at initial recognition and...
Think of this question in the context of the two-period model. Households hold stocks as part...
Think of this question in the context of the two-period model. Households hold stocks as part of their financial wealth. Assume that we recently observe stock prices rising. Also assume that consumers believe that changes in stock prices are temporary. What affect would this have on the correlation between stock price indices and consumption? Explain.
Create a program that asks the user for the names of two car dealerships and the...
Create a program that asks the user for the names of two car dealerships and the # of cars sold in each one. Then output that data in two columns as shown below. The "Store location" column has a width of 25, while the "Cars sold" column has a width of 9. Also, notice the alignment of the second column. The program should end with the "Press Enter to end this program" prompt. OUTPUT Enter the location for the first...
Amortization Schedules This project requires you to create an amortization schedule for two types of loans,...
Amortization Schedules This project requires you to create an amortization schedule for two types of loans, a fully amortizing constant payment mortgage (CPM) loan and a constant amortizing (CAM) loan. In your report, compare the amortization schedule of the CPM and CAM loans (How are they similar? How are they different? Which would you prefer and why?) Part 1: Monthly Payment Consider a $10,000 loan made at a 12 percent annual (nominal) rate of interest for 3 years. A) Calculate...
Suppose you want to buy a new car and trying to choose between two models: Model...
Suppose you want to buy a new car and trying to choose between two models: Model A: costs $17,000 and its gas mileage is 20 miles per gallon and its insurance is $200 per year. Model B: costs $25,000 and its gas mileage is 35 miles per gallon and its insurance is $400 per year. If you drive approximately 40,000 miles per year and the gas costs $3 per gallon: Find a formula for the total cost of owning Model...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT