Question
For this assignment, you are asked to use your GasTank, Engine, and Car classes from assignment 2 and create a GUI which presents the user
For this assignment, you are asked to use your GasTank, Engine, and Car classes from assignment 2 and create a GUI which presents the user with dialog windows for inputting information about a car and driving trip. Once all of the information about your car has been entered, your program must display a window which contains a description of the car. When that window is closed, your program must gather the data about the trip and then display a graphics panel on which is drawn a set of lines showing each leg of the trip, and text showing the end coordinates of each. You will need to read and learn about various GUI (awt and swing) classes, such as the Graphics class provided by Java, and this is intended to give you experience in learning API's. There are also some helpful examples in the lecture slides. (Specifically, the "GUI introduction" slides.)
- Make a new project, with a copy of each of the classes from Assignment 2.
Classes
The GasTank class represents an object to keep track of the current and maximum levels of fuel
/////GasTank.java
import java.lang.Math;
class GasTank
{
private int capacity;
private double curr_level;
GasTank(int cap)
{
if(cap<0)
{
this.capacity=0;
}
else
{
this.capacity=cap;
}
}
public int getCapacity()
{
return this.capacity;
}
public double getLevel()
{
return this.curr_level;
}
public void setLevel(double level)
{
if(level<0)
{
this.curr_level=0;
}
else if(level > capacity){
this.curr_level = capacity;
}
else
{
this.curr_level=level;
}
}
}
The Engine class represents an engine object and stores a few attributes of the engine.
/////Engine.java
class Engine
{
private String description;
private int mpg;
private int max_speed;
Engine(String des, int mpg, int speed)
{
if(des.length()==0)
{
this.description="Generic Engine";
}
else
{
this.description=des;
}
if(mpg<0)
{
this.mpg=0;
}
else
{
this.mpg=mpg;
}
if(speed<0)
{
this.max_speed=0;
}
else
{
this.max_speed=speed;
}
}
public String getDescription()
{
return this.description + "(MPG: "+ this.mpg+ ", "+ "Max Speed: "+ this.max_speed+")";
}
public int get_mpg()
{
return this.mpg;
}
public int getMaxSpeed()
{
return this.max_speed;
}
}
The Car class represents a car object which contains an engine and a gas tank, and has methods that allow it to keep track of its location and fuel level while being ``driven''.
/////Car.java
public class Car
{
private String description;
private int x;
private int y;
private GasTank tank;
private Engine eng;
Car(String desc, int cap, Engine e)
{
if( desc.length()<=0)
{
this.description="Generic car";
}
else
{
this.description=desc;
}
this.tank=new GasTank(cap);
if(e==null)
{
this.eng=new Engine("",0,0);
}
else
{
this.eng=e;
}
}
public String getDescription()
{
String out="";
out=out+this.description;
out=out+" (engine: ";
out=out+eng.getDescription();
out=out+"), ";
out=out+" fuel: ";
out=out+this.tank.getLevel()+"//"+this.tank.getCapacity();
out=out+", location: ("+this.x+", "+ this.y+")";
return out;
}
public int get_x()
{
return this.x;
}
public int get_y()
{
return this.y;
}
public double get_fuel_level()
{
return this.tank.getLevel();
}
public int get_mpg()
{
return this.eng.get_mpg();
}
public void fillUp()
{
double fill=this.tank.getCapacity();
this.tank.setLevel(fill);
}
public int get_max_speed()
{
return this.eng.getMaxSpeed();
}
public double drive(int distance, double xr, double yr)
{
double cos=xr/Math.pow((xr*xr+yr*yr),0.5);
double sin=yr/Math.pow((xr*xr+yr*yr),0.5);
double x_dist;
double y_dist;
double dist_possible=this.tank.getLevel()*this.eng.get_mpg();
if (dist_possible>=distance)
{
dist_possible=distance;
x_dist=dist_possible*cos;
y_dist=dist_possible*sin;
this.x=this.x+(int)x_dist;
this.y=this.y+(int)y_dist;
this.tank.setLevel(this.tank.getLevel() -distance/this.eng.get_mpg());
}
else
{
x_dist=dist_possible*cos;
y_dist=dist_possible*sin;
this.x=this.x+(int)x_dist;
this.y=this.y+(int)y_dist;
this.tank.setLevel(0);
System.out.println("Ran out after driving "+ dist_possible+" miles");
}
return dist_possible;
}
}
/////////////////
- Create a new class named Assignment3, which will first use a series of dialogs to prompt the user for all of the necessary information for a car (in the following order):
- Car description
- Fuel tank capacity
- Engine description
- Miles per gallon
- Max speed
- (Now a dialog displays all of the car's info, before the next series of input dialogs)
- The number of legs in the trip (you can assume that this number will never be greater than 10)
- (For each leg of the trip, the following three input dialogs appear, in this order, one leg at a time, and each dialog should show the number of the leg that it is collecting info for)
- The distance of the leg
- The x ratio for the direction of the leg
- The y ratio for the direction of the leg
- When reading in the values which are integers, you should use Integer.parseInt (google it for documentation about how it works) to get the integer value of the value entered by the user. Note that Integer.parseInt throws an exception if given something other than a valid number. If such an exception occurs, you must catch the exception and display a dialog saying "Invalid data entered. Exiting." and then the program should immediately exit. When reading in values for doubles, you should find the necessary function which is analogous to Integer.parseInt but used for doubles, and non-numerical input should be handled the same way.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started