Hi, there are certain things to do in this java code:
1) One java file can contain at most 1 public
class only so if you have your classes in the same java
file then you need to remove the 'public' keyword from class
'CarHW', else you will need to first import that file containing
the other class (CarHW) and then use it in your main
class(CarHWMain).
2) I have added some new functions like Get and Set Methods and other functions which are needed in this code and i have also added code in your defined functions to work properly as per my understanding of your given code and it is now executable and is giving correct results.
3) You can check the complete code below:-
package abc1;
public class CarHWMain
{
public static void main(String[] args)
{
//Construct two new cars and one
used car
CarHW car1 = new CarHW("My New
Mazda", 24.5, 16.0);
CarHW car2 = new CarHW("My New
Ford", 20.5, 15.0);
CarHW car3 = new CarHW("My Used
Caddie", 15.5, 16.5, 5.5, 25127.6);
//Code to completely fill the
tanks
car1.topOfGas();
car2.topOfGas();
car3.topOfGas();
// Do Driving
car1.drive(130.8);
car3.drive(25.4);
car1.drive(20);
car2.drive(155.5);
car2.addGas(5);
car3.drive(35);
car1.drive(120);
car2.drive(55.2);
// Checks if any car needs more gas
and top off if necessary
if(car1.lowGasCheck())
// Car 1
{
car1.topOfGas();
}
if(car2.lowGasCheck())
// Car 2
{
car2.topOfGas();
}
if(car3.lowGasCheck())
// Car 3
{
car3.topOfGas();
}
//prints odometer and gas levels
for each car
System.out.println(car1.getName() +
": " + car1.getLevel() + " gallons; " + car1.getOdometer() + "
miles.");
System.out.println(car2.getName() +
": " + car2.getLevel() + " gallons; " + car2.getOdometer() + "
miles.");
System.out.println(car3.getName() +
": " + car3.getLevel() + " gallons; " + car3.getOdometer() + "
miles.");
}
}
class CarHW
{
private String carName; //nickname
private double mpg; //miles per
gallon
private double gasLevel; // number of gallons of gas
in tank
private double gasLimit; //capacity of gas tank
private double odometer; // total number of miles
driven
public CarHW(String name, double eff, double limit) //
new car
{
carName = name;
mpg = eff;
gasLimit = limit;
gasLevel = 0.0;
odometer = 0;
}
public CarHW(String name, double eff, double limit,
double level, double m) // used car
{
carName = name;
mpg = eff;
gasLimit = limit;
gasLevel = level;
odometer = m;
}
//Get and Set Methods
public String getName()
{
return carName;
}
public double getLevel()
{
return gasLevel;
}
public double getOdometer()
{
return odometer;
}
// Mutator Methods
public double mileageLimit() //Returns the
number of miles that can be driven with current gas level
{
return mpg * getLevel();
}
public void drive(double miles) // Affects gas level
and odometer
{
gasLevel = gasLevel -
(miles/mpg);
odometer = odometer + miles;
}
public boolean lowGasCheck() // returns true if tank
is less than 1/8 full
{
if(getLevel() <
(gasLimit/8))
{
return
true;
}
else
{
return
false;
}
}
public void addGas(double gallons)
{
gasLevel = gasLevel +
gallons;
}
public void topOfGas() // fills gas tank
to capacity
{
addGas(gasLimit -
getLevel());
}
}
// END OF THE CODE
please help me add on this java code to run public class CarHwMain public static void...
Use an accessor and mutator as follows: Remove the addFuel method from class Car Replace the call to addFuel in the main program with a call to the accessor and mutator for the fuel amount to achieve the same effect (add 5 gallons of fuel to the existing fuel in the Toyota Camry). Test your program again to make sure it works and produces the same output. Part 2: Add functions to remove duplicate code * In the main program,...
Below are the Car class and Dealership class that I created In the previous lab import java.util.ArrayList; class Car { private String make; private String model; private int year; private double transmission; private int seats; private int maxSpeed; private int wheels; private String type; public Car() { } public Car(String make, String model, int year, double transmission, int seats, int maxSpeed, int wheels, String type) { this.make = make; this.model = model; this.year = year; this.transmission = transmission; this.seats =...
import java.util.Scanner; public class StudentClient { public static void main(String[] args) { Student s1 = new Student(); Student s2 = new Student("Smith", "123-45-6789", 3.2); Student s3 = new Student("Jones", "987-65-4321", 3.7); System.out.println("The name of student #1 is "); System.out.println("The social security number of student #1 is " + s1.toString()); System.out.println("Student #2 is " + s2); System.out.println("the name of student #3 is " + s3.getName()); System.out.println("The social security number...
import java.util.Scanner; public class MPGMain { public static void main(String[] args) { Scanner input = new Scanner(System.in); Mileage mileage = new Mileage(); System.out.println("Enter your miles: "); mileage.setMiles(input.nextDouble()); System.out.println("Enter your gallons: "); mileage.setGallons(input.nextDouble()); System.out.printf("MPG : %.2f",mileage.getMPG()); } } public class Mileage { private double miles; private double gallons; public double getMiles()...
What is output? public abstract class People { protected string name; protected int age; public abstract void PrintInfo(); public void PrintInformation() { System.out.println("In Base Class People"); public class Teacher extends People { private int experience; public void PrintInfo() { System.out.println("In Child Class Teacher"); public class Principal extends Teacher { public void PrintInformation() { System.out.println("In Child Class Principal"); public static void main(String args[]) { Principal tim; tim = new Principal(); tim.PrintInfo(); In Base Class People Error: Compiler error In Child Class...
In Java. Please use the provided code Homework 5 Code: public class Hw05Source { public static void main(String[] args) { String[] equations ={"Divide 100.0 50.0", "Add 25.0 92.0", "Subtract 225.0 17.0", "Multiply 11.0 3.0"}; CalculateHelper helper= new CalculateHelper(); for (int i = 0;i { helper.process(equations[i]); helper.calculate(); System.out.println(helper); } } } //========================================== public class MathEquation { double leftValue; double rightValue; double result; char opCode='a'; private MathEquation(){ } public MathEquation(char opCode) { this(); this.opCode = opCode; } public MathEquation(char opCode,double leftVal,double rightValue){...
help with java OOP, here is the started code: package P2; public class Farm { private double availableFood; private Animal[] animals; public Farm() { setAvailableFood(1000); animals = new Animal[4]; animals[0] = new Chicken(); animals[1] = new Cow(); animals[2] = new Llama(); animals[3] = new Llama(); } public void makeNoise(){ // all animals make their sound (Moo, Cluck, etc) for(Animal...
I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student { private String name; private double gpa; private int idNumber; public Student() { this.name = ""; this.gpa = 0; this.idNumber = 0; } public Student(String name, double gpa, int idNumber) { this.name = name; this.gpa = gpa; this.idNumber = idNumber; } public Student(Student s)...
Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters and getter methods for the new variable and modify the toString method. The objectstudent program is in this weeks module, you can give it a default value of 19090. class Student { private int id; private String name; public Student() { id = 8; name = "John"; } public int getid() { return id; } public String getname() { return name; } public void...
4. Command pattern //class Stock public class Stock { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public void buy(int quantity){ System.out.println(“BOUGHT: “ + quantity + “x “ + this); } public void sell(int quantity){ System.out.println(“SOLD: “ + quantity + “x “ + this); } public String toString() { return “Product [name=” + name + “, price=” + price + “]”; } } a. Create two command classes that...