import java.io.*; import java.util.Scanner; public class Car { //fields declarat
ID: 3625795 • Letter: I
Question
import java.io.*;import java.util.Scanner;
public class Car
{
//fields declaration
private int yearModel;
private String make;
private int speed;
//constructor
public Car(int year, String m )
{
yearModel = year;
make = m;
speed = 0;
}
//accessor methods
public int getYear()
{
return yearModel;
}
public String getMake()
{
return make;
}
public int getSpeed()
{
return speed;
}
//increase speed by 5
public void accelerate()
{
speed+=5;
}
//decrease speed by 5
public void brake()
{
speed-=5;
}
}
import java.io.*;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class carTest
{
public static void main(String[] args)
{
//variables
String str;
int year;
int speed;
//Scanner object
Scanner keyboard = new Scanner(System.in);
//inputting model
str = JOptionPane.showInputDialog("Enter car model:");
//inputting year
str = JOptionPane.showInputDialog("Enter year:");
Car object1 = new Car(year, str);
//Accelerating car
System.out.println("Accelerate");
object1.accelerate();
System.out.println("Speed of car is" + object1.getSpeed());
object1.accelerate();
System.out.println("Speed of car is" + object1.getSpeed());
//brake car
System.out.println("Brake");
object1.brake();
System.out.println("Speed of car is" + object1.getSpeed());
object1.brake();
System.out.println("Speed of car is" + object1.getSpeed());
object1.brake();
System.out.println("Speed of car is" + object1.getSpeed());
object1.brake();
System.out.println("Speed of car is" + object1.getSpeed());
object1.brake();
System.out.println("Speed of car is" + object1.getSpeed());
System.exit(0);
}
}
Explanation / Answer
The easiest way is everywhere you have a System.out.println(stuff), replace it with JOptionPane.showMessageDialog(null,stuff). Note that you need the null first(something to do with referencing i think) and then the stuff can be whats printed out
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.