Uploaded by Andrew Goraichy

Car Fuel Efficiency Java Code

advertisement
Car.java:
--------
/**
*This class represents a Car that tracks fuel efficiency and fuel costs.
* It calculates kilometers per liter (KPL), determines if the car is fuel-efficient or inefficient, * and allows comparison between different cars based on their fuel efficiency.
* Bugs: none known
*
* @author Andrew Goraichy (2025)
* @version 1.0
* @see also MyProgram.java
*/
public class Car {
// declaring varuables
private int secLastKm;
private int recentKm;
private float litre;
private static float gasPrice;
private static final int EFFICIENT_KPL = 13;
private static final int INEFFICIENT_KPL = 7;
public Car(int secLastKm, int recentKm, float litre) {
this.secLastKm = secLastKm;
this.recentKm = recentKm;
this.litre = litre;
}
// setting the price of gas
public static void setgasPrice(int gasPrice){
gasPrice = gasPrice;
}
// calcualates Km/L
public float calculateKPL(){
return ((recentKm-secLastKm)/litre);
}
// determines if car is a Gas hog
public boolean gasHog(){
if ((recentKm-secLastKm)/litre < INEFFICIENT_KPL){
return (true);
}
else {
return (false);
}
}
// determines if it is an economy car
public boolean economyCar(){
if ((recentKm-secLastKm)/litre > EFFICIENT_KPL){
return (true);
}
else{
return (false);
}
}
// determines the cost of filling up the car
public double fillup (int Kms, int liters){
secLastKm = recentKm;
recentKm = Kms;
litre = liters;
return (litre*gasPrice);
}
// compares the effeccy of the cars
public int compareTo(Car other){
if (this.calculateKPL() > other.calculateKPL()){
return (1);
}
else if (this.calculateKPL() < other.calculateKPL()){
return (2);
}
else{
return(3);
}
}
// converts it to a string
public String toString(){
return (calculateKPL() + " km/litre");
}
}
MyProgram.java:
--------------
/**
* Program Name: Car Fuel Efficiency Calculator
* Author: Andrew
* Date: Febuary 25, 2025
* * Description:
* This program calculates and compares the fuel efficiency of two cars based on user input.
* It prompts the user for odometer readings and fuel consumption data, determines whether * a car is fuel-efficient or a gas hog, and allows users to update the odometer readings * after a fill-up. The program also compares the fuel efficiency of the two cars.
*/
import java.util.Scanner;
public class MyProgram
{
public static void main(String[] args)
{
int carNum;
int newred;
int newlit;
Scanner scanner = new Scanner (System.in);
System.out.println("What is the price of gas? ");
Car.setgasPrice(scanner.nextInt());
// gets information for car 1
System.out.println("Enter information for car #1:");
System.out.println("Previous odometer reading:");
int prevous1 = scanner.nextInt();
System.out.println("Most recent odometer reading:");
int recent1 = scanner.nextInt();
System.out.println("Litres of gas used between the readings:");
int lit1 = scanner.nextInt();
Car Car1 = new Car(prevous1, recent1, lit1);
// gets information for car 2
System.out.println("Enter information for car #2:");
System.out.println("Previous odometer reading:");
int prevous2 = scanner.nextInt();
System.out.println("Most recent odometer reading:");
int recent2 = scanner.nextInt();
System.out.println("Litres of gas used between the readings:");
int lit2 = scanner.nextInt();
Car Car2 = new Car(prevous2, recent2, lit2);
// fuel effecy for car1
System.out.println("Car #1");
System.out.println("Fuel efficiency: " + Car1);
if (Car1.gasHog() == true){
System.out.println("it is a gas hog");
}
else if(Car1.economyCar() == true){
System.out.println("it is an effecent car");
}
else {
System.out.println("it is nither a gas hog or an effect car");
}
// fuel effecy for car2
System.out.println("\nCar #2");
System.out.println("Fuel efficiency: " + Car2);
if (Car2.gasHog() == true){
System.out.println("it is a gas hog");
}
else if(Car2.economyCar() == true){
System.out.println("it is an effecent car");
}
else {
System.out.println("it is nither a gas hog or an effect car");
}
// compares effency
if (Car1.compareTo(Car2) == 1){
System.out.println("Car 1 is more fuel effecet with: " + Car1);
}
else if (Car1.compareTo(Car2) == 2){
System.out.println("Car2 is more fuel effecent with: " + Car1);
}
else{
System.out.println("Nither are more fuel effecent");
}
// fills up car
do {
System.out.println("Fill up for car #: ");
carNum = scanner.nextInt();
if (carNum != 1 && carNum != 2){
break;
}
else {
System.out.println("current odometer reading: ");
newred = scanner.nextInt();
System.out.println("num of l to fill up tank");
newlit = scanner.nextInt();
System.out.println("Latest fuel effency");
if (carNum == 1){
Car1.fillup(newred, newlit);
System.out.println(Car1);
}
else{
Car2.fillup(newred, newlit);
System.out.println(Car2);
}
}
}while (carNum ==1 || carNum ==2);
}
}
Download