Write a class named Car that has the following fields: yearModel. The yearModel
ID: 3717632 • Letter: W
Question
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 references 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 constructor and other methods:
Constructor. The constructor should accept the car’s year model and make as arguments. These values should be assigned to the object’s yearModel and make fields. The constructor should also assign 0 to the speed field.
Accessors. Appropriate accessor methods should get the values stored in an object’s yearModel, make, and speed fields.
Create a method called accelerate. The accelerate method should add 7 to the speed each time it is called.
Create a method called brake. The brake method should subtract 5 from the speed each time it is called.
Create a GUI with the following elements:
A label and text field showing the yearModel
A label and text field showing the make
A label and text field showing the speed
A button that accelerates the car and displays the new speed
A button that brakes the car and displays the new speed
Implement the GUI in a separate class called CarView which takes a car in it's constructor.
Given each GUI widget a meaningfull name, such as yearModelLabel and yearModelField.
Also, implement the ActionListeners for the buttons either as a private nested class or as anonymous inner classes. Use setText to update the contents of the text fields.
Please add some comments to understand the actions.
Explanation / Answer
here is your answer : --------->>>>>>
CarView.java : ---------->>>>>>>>
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.util.Scanner;
public class CarView extends JFrame{
private Car car;
private JLabel ym,mk,sp;
private JTextField ytm,mtk,stp;
private JButton acc,brk;
private JPanel p1,p2,p3,p4;
public CarView(Car car){
super("Car Simulation ");
this.car = car;
setSize(400,400);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildGUI();
setVisible(true);
}
private void buildGUI(){
ym = new JLabel("Year-Model : ");
mk = new JLabel("Make : ");
sp = new JLabel("Speed : ");
ytm = new JTextField(7);
ytm.setEditable(false);
ytm.setText(car.getYearModel()+"");
mtk = new JTextField(7);
mtk.setEditable(false);
mtk.setText(car.getMake());
stp = new JTextField(7);
stp.setEditable(false);
stp.setText(car.getSpeed()+"");
acc = new JButton("Accelerate");
acc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
car.accelerate();
stp.setText(car.getSpeed()+"");
}
} );
brk = new JButton("Break");
brk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
car.brake();
stp.setText(car.getSpeed()+"");
}
} );
p1 = new JPanel();
p1.setSize(400,70);
p2 = new JPanel();
p2.setSize(400,70);
p3 = new JPanel();
p3.setSize(400,70);
p4 = new JPanel();
p4.setSize(400,70);
p2.setLayout(new FlowLayout());
p3.setLayout(new FlowLayout());
p1.setLayout(new FlowLayout());
p4.setLayout(new FlowLayout());
p1.add(ym);
p1.add(ytm);
p2.add(mk);
p2.add(mtk);
p3.add(sp);
p3.add(stp);
p4.add(brk);
p4.add(acc);
setLayout(new GridLayout(4,1));
add(p1);
add(p2);
add(p3);
add(p4);
}
public static void main(String[] args) {
String make;
int ymodel;
Scanner sc = new Scanner(System.in);
System.out.println("Enter Car Make : ");
make = sc.next();
System.out.println("Enter Car Year-Model : ");
ymodel = sc.nextInt();
Car car = new Car(ymodel,make);
CarView v = new CarView(car);
}
}
Car.java : --------->>>>>>>>>>
public class Car{
private String make;
private int speed;
private int yearModel;
public Car(int yearModel,String make){
this.yearModel = yearModel;
this.make = make;
speed = 0;
}
public int getSpeed(){
return speed;
}
public int getYearModel(){
return yearModel;
}
public String getMake(){
return make;
}
public void accelerate(){
speed = speed + 7;
}
public void brake(){
speed = speed - 5;
if(speed < 0){
speed = 0;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.