Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please refactor the following code to have: One Car class implementing the car and all its features, in a separate file. One CarView class implementing

Please refactor the following code to have:

One Car class implementing the car and all its features, in a separate file.

One CarView class implementing the GUI, in a separate file.

The CarView should be started with new CarView(new Car(...));

Make text fields read-only.

Add a color field and accessors to the Car class, create a button where you can select the car's color, and show the color on a label. Hint: use a JColorChooser.

Prevent the car from going faster than 65. Hint: this is a feature of the car so implement the change in the Car class.

Prevent the car from going backwards when breaking.

Show the speed on a slider, instead or in addition to a text field.

Put images on the buttons. Feel free to create your own images or find some online. Be sure to submit images.

Create a menu with an exit item that shuts down the program.

Please edit the code below to satify the changes needed above and add comments please so i understand what was changed.

Also, Please include BOTH the Car Class and CarView Class in answer.

If anything in my code doesn't make sense feel free to adjust the code to your convenience.

Thank You!

======================================================================================================

public class Car { private String carMake; // Create car make string variable. private int carSpeed; // Creating car speed integer variable. private int carYear; // Creating car year integer variable.

// Creating an constructor to take the values of the year, make and speed. public Car(int carYear,String carMake){ this.carYear = carYear; this.carMake = carMake; this.carSpeed = 0; }

// Method that returns car year. public int getCarYear(){ return carYear; } // Method that returns car make public String getCarMake(){ return carMake; }

// Mehtod that returns car speed. public int getCarSpeed(){ return carSpeed; }

// Creating accelerate method to increases the car speed by 7. public void accelerate(){ carSpeed += 7; }

// Creating brake method to decrease car speed by 5. public void brake(){ carSpeed -= 5; } }

import javax.swing.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.*;

public class CarView extends JFrame { private Car car; private JLabel yearLabel,makeLabel,speedLabel; // Create labels for variable private JTextField yearTextField,makeTextField,speedTextField; // Create text fields for variables private JButton accelerateButton, brakeButton; // Create button for acceleration and brake variables private JPanel carYearPanel,carMakePanel,carSpeedPanel,buttonPanel; // Create panals for // Constructor what accepts the car class as an arguemnt public CarView(Car car) { super("Car View"); // calling and naming super class car view. this.car = car; setSize(500,500); // Setting the size of the Gui to 1000 in height and width. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Allows user to exit with x. buildGUI(); // Creating gui builder for application setVisible(true); // Allows the gui to be visible when true. }

// Create gui builder method holding the labels, text fields, buttons, and layout. public void buildGUI() { yearLabel = new JLabel("Car Model Year: "); // Displays label of car year. makeLabel = new JLabel("Car Model Make: "); // Displays label for car make speedLabel = new JLabel("Car Model Speed: "); // Displays lable for car speed yearTextField = new JTextField(10); // Set the size of year text field. yearTextField.setText(car.getCarYear()+""); // Displays the car year from method. makeTextField = new JTextField(10); // Set the size of the make text field. makeTextField.setText(car.getCarMake()+""); // Displays the car make from method. speedTextField = new JTextField(10); // Set the size of the speed text field. speedTextField.setText(car.getCarSpeed()+""); // Displays the car speed from method. accelerateButton = new JButton("Accelerate"); // Creating acceleration button. accelerateButton.addActionListener(new ActionListener() // Adding action listener interface to activate button // Create action method for acceleration of 7 mph. { public void actionPerformed(ActionEvent accelerate) { car.accelerate(); // Calls acceeraltion method speedTextField.setText(car.getCarSpeed()+""); // Updates the car speed after acceleration. } } ); brakeButton = new JButton("Brake"); // Creating brake button. brakeButton.addActionListener(new ActionListener() // Adding action listener interface to activate brake button. // Create action method for brake decelleration of 5 mph. { public void actionPerformed(ActionEvent brake) { car.brake(); // Calls brake method. speedTextField.setText(car.getCarSpeed()+""); // Updates car speed after deceleration. } } ); carYearPanel = new JPanel(); // Creating car year panel. carYearPanel.setSize(1800,500); // Sizing the year panel carMakePanel = new JPanel(); // Creating the car make panel. carMakePanel.setSize(1800,500); // Sizing the make panel. carSpeedPanel = new JPanel(); // Creating the car speed panel. carSpeedPanel.setSize(1800,500); // Sizing the speed panel. buttonPanel = new JPanel(); // Creating the button panel. buttonPanel.setSize(1800,500); // Sizng the button panel carYearPanel.setLayout(new FlowLayout()); // Setting year panel layout. carMakePanel.setLayout(new FlowLayout()); // Setting make panel layout. carSpeedPanel.setLayout(new FlowLayout()); // Setiing speed panel layout. buttonPanel.setLayout(new FlowLayout()); // Setting button panel layout. carYearPanel.add(yearLabel); // Adding year label to gui. carYearPanel.add(yearTextField); // Adding year tect feild to gui. carMakePanel.add(makeLabel); // Adding make label to gui. carMakePanel.add(makeTextField); // Adding make text feild to gui. carSpeedPanel.add(speedLabel); // Addiing speed label to gui. carSpeedPanel.add(speedTextField); // Adding speed text field to gui. buttonPanel.add(accelerateButton); // Adding acceleration button to gui. buttonPanel.add(brakeButton); // Adding brake button to gui. setLayout(new GridLayout(4,1)); // Sizing the grid layout of gui. add(carYearPanel); // Adding the car year panel to gui. add(carMakePanel); // Adding the car make panel to gui. add(carSpeedPanel); // Adding the car speed panel to gui. add(buttonPanel); // Adding the button panel to gui. }

// Creating main method public static void main(String[] args) { int carYear; // Create car year variable for text field. String carMake; // Create car make string variable for text field. // Instance that holds method for car view. Car car = new Car(1992,"Toyota"); // Instance that displays car gui. CarView view = new CarView(car); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

=+ Who do you think is right? Why?

Answered: 1 week ago