Question
Includes some mathematical concepts Two projects build on each other Assignment 2 and 3 n0te:i need help with ass. 3 not assignment 2 because assignment
Includes some mathematical concepts
Two projects build on each other
Assignment 2 and 3
n0te:i need help with ass. 3 not assignment 2 because assignment 2 has already been solved and i will post the solution but also the question to help you understand what the classes do and how to use them in assignment 3
Assignment 2 question
In this assignment, you will create a Java program containing three classes named GasTank, Engine, and Car. These classes will be used to create Car objects which can execute methods to compute locations and fuel levels after driving given distances and directions.
- The GasTank class represents an object to keep track of the current and maximum levels of fuel. It must:
- have a private instance variable that keeps track of an integer (maximum) capacity
- have a private instance variable that keeps track of a double (current) level
- have a constructor which takes one argument, an int, which is used to set the instance variable for capacity (but if a negative value is passed in it gets set to 0), while the current level is initialized to 0
- have the following member functions implemented:
public int getCapacity() public double getLevel() public void setLevel(double levelIn)
- the ``get'' functions should return the capacity and level
- the ``set'' function should take a double argument to set the level, but if the argument is below 0 it gets set to 0, or above the capacity it gets set to the capacity
- The Engine class represents an engine object and stores a few attributes of the engine. It must:
- have a private instance variable which stores a String description of the engine (e.g. "V8")
- have a private instance variable that keeps track of the integer miles per gallon (mpg) value
- have a private instance variable that keeps track of the integer maximum speed value
- have a constructor which takes three arguments (a String and 2 integers) which are used to initialize the three instance variables. If the String argument has length 0, the description should be set to the value "Generic engine", and if either integer argument is negative, the corresponding instance variable should be set to 0.
- have public ``get'' functions to return each of the description, mpg, and max speed. The function that returns the description should return the String description as well as the mpg and max speed, clearly labeled (e.g. "V6 (MPG: 20, Max speed: 120)")
- 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''. It must:
- have a private instance variable which stores a String description of the car (e.g. "Sweet Ride")
- have private instance variables that keep track of integer values for the x-coordinate and y-coordinate (and are initialized to 0)
- have a private instance GasTank variable
- have a private instance Engine variable
- have a constructor which takes as arguments a String description, an integer for the maximum fuel capacity, and an Engine object reference (in that order). If the description is length 0, the corresponding instance variable should be set to "Generic car", and if a null reference is passed in for the Engine parameter, a new Engine should be created with an empty description and 0's for all values.
- have the following member functions implemented:
public String getDescription() public int getX() public int getY() public double getFuelLevel() public int getMPG() public void fillUp() public int getMaxSpeed() public double drive(int distance, double xRatio, double yRatio)
- getDescription() should return the description of the car as well as the description of its Engine, fuel level and capacity, and location, all clearly labeled (e.g. "Sweet Ride (engine: V6 (MPG: 20, Max speed: 120)), fuel: 0.00/15, location: (0,0)")
- fillUp() should cause the gas tank to be filled to its maximum capacity
- drive(int distance, double xRatio, double yRatio) should take the desired distance to travel as an integer, and two double values which, together, specify the direction as a ratio of horizonal to vertical distances. For example, if xRatio = 1 and yRatio = 2, for every 1 unit to the right (positive x), the car would travel 2 units up (positive y). Negative values for xRatio and yRatio correspond to moving left and down, respectively. Given the distance and direction, this method should correctly compute the ending coordinates (tuncated to integers). It should also correctly subtract the amount of fuel used. If the distance specified requires greater than the amount of fuel than the car currently has, the car should travel as far as it can until it is empty and print this text to the console: "Ran out of gas after driving x miles." (with the correct value substituted in for x). The method should return the number of miles which were actually driven.
- All double values which are printed out to the console should be formatted so that exactly 2 decimal places are displayed.
ASS. 2 SOLUTION
/////GasTank.java
import java.lang.Math;
class GasTank
{
private int capacity;
private double curr_level;
GasTank(int cap)
{
if(cap
{
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
{
this.curr_level=0;
}
else if(level > capacity){
this.curr_level = capacity;
}
else
{
this.curr_level=level;
}
}
}
/////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
{
this.mpg=0;
}
else
{
this.mpg=mpg;
}
if(speed
{
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;
}
}
/////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()
{
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;
}
}
Ass. 3 Question:
- Make a new project, with a copy of each of the classes listed in assignment 2.
- 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.
- When any number other than the x ratio or y ratio are requested, if a negative number or zero is entered, your program should loop and keep redisplaying the same input dialog until a positive number is entered.
- A Car object should be created (along with the necessary GasTank and Engine objects) and all of the information about the car and legs of the trip should be used to calculate the location of the car and fuel level after each (just as in assignment 2). Before beginning the first leg of the trip, the car's tank should be filled, but it should not be filled again for subsequent legs. All aspects of the calculations which were required for the previous assignment are required for this assignment, so make sure your code works and correct any bugs that existed in your last assignment. You should not need to modify any of the classes given as long as they were working correctly.
- Finally, a JFrame object should be created which contains an instance of a panel class that you create (which should be in DrivePanel.java and named DrivePanel and will extend JPanel similar to the example DrawPanel class in section 4.15 of the book). On the panel should be drawn line segments corresponding to each leg of the trip, with the assumption that the first leg begins at point (0,0) and that point is located at the bottom left corner. Keep in mind that graphics windows treat y=0 as the top of the window, with y-coordinates increasing downward, but we will assume (and draw) the opposite to be in line with traditional coordinate systems. Additionally, the location of the end of each leg should be written in text at the location 10 pixels to the right. (See the example-output1.jpg below for the output when a correctly working version is given the following series of input: TestCar, 50, V8, 50, 100, 3, 200, 2, 3, 300, -1, 4, 500, 2, -0.5. That is, a trip of the three legs (200, 2, 3), (300, -1, 4), (500, 2, -0.5). Additionally, see Assignment3-Example.pdf for pictures of the full set of windows in the correct sequence.)
-
//JPG EXAMPLE
- Your JFrame window must be created at size 600 x 600. To get full points for this assignment, your JFrame window should correctly show the lines and end coordinates in text even after being resized.
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