-2
import java.util.Scanner;
public class LKM {
public static void main(String[] args){
    Scanner keyboard=new Scanner(System.in);
     String startKm=keyboard.nextLine();
     String endKm=keyboard.nextLine();
     String liters=keyboard.nextLine();
}
    public void Car (double startOdo, double endOdo, double liters){
    startKm=startOdo;
    endKm=endOdo;
    liters=liters;
}
public static void LKM(String args[]){
   calculateLKM red=new Car(1,20,10);
   Car white=new Car(5,10,5);
   System.out.println((red.endKm-red.startKm)/red.liters);
   System.out.println((white.endKm-white.startKm)/white.liters);
}
}

I have to define a class that calculates fuel consumption of a car using one constructors and one method. I tried learning class and objects but it didn't work so well..I need just a few tips. Thank you.

  • 2
    as there is basicly nothing as it should be i´d recommend going through [basic tutorials](https://docs.oracle.com/javase/tutorial/getStarted/index.html) and [java language tuorials](https://docs.oracle.com/javase/tutorial/java/index.html), especially the one dealing with [classes and Objects](https://docs.oracle.com/javase/tutorial/java/javaOO/index.html) – SomeJavaGuy Nov 21 '16 at 10:28
  • where is the `Car` class , we can't drive you in right direction if we can't see whole code – Pavneet_Singh Nov 21 '16 at 10:28
  • 2
    No Car class; incorrect notation for Car constructor; poor encapsulation; unreadable code. – duffymo Nov 21 '16 at 10:31

1 Answers1

0

I think you are confusing Method and Constructor. When you do "New Car()", you try to invoke Car Construstor. So to do it, you need a class "Car" with a Constructor inside.

Create a new file named "Car.java" and insert this code inside :

public class Car {
    public Car(double startOdo, double endOdo, double liters){
      this.startOdo = startOdo;
      this.endOdo = endOdo;
      this.liters = liters;
    }
}

But if you wan't to do :

startKm=startOdo;
endKm=endOdo;
liters=liters;

You need fields inside your Car Class. So add startKm, endOdo, liters as fields in your Car class :

private double startOdo;
private double endOdo;
private double liters;

Then add some getters and setters to access your fields :

    public double getStartOdo() {
        return startOdo;
    }
    public void setStartOdo(double startOdo) {
        this.startOdo = startOdo;
    }
    public double getEndOdo() {
        return endOdo;
    }
    public void setEndOdo(double endOdo) {
        this.endOdo = endOdo;
    }
    public double getLiters() {
        return liters;
    }
    public void setLiters(double liters) {
        this.liters = liters;
    }

This would give you something like this :

public class Car {
    private double startOdo;
    private double endOdo;
    private double liters;
    public double getStartOdo() {
        return startOdo;
    }
    public void setStartOdo(double startOdo) {
        this.startOdo = startOdo;
    }
    public double getEndOdo() {
        return endOdo;
    }
    public void setEndOdo(double endOdo) {
        this.endOdo = endOdo;
    }
    public double getLiters() {
        return liters;
    }
    public void setLiters(double liters) {
        this.liters = liters;
    }
    public Car(double startOdo, double endOdo, double liters){
      this.startOdo=startOdo;
      this.endOdo=endOdo;
      this.liters=liters;
    }
}

Finally, you will be able to do what you want in your main file. You just have to call "getStartOdo()", "getEndOdo()" and "getLiters()" to retrieve your datas and display it with System.out.println() method.

Oh, and don't forget to call your "LKM" method in your Main method, otherwise nothing will happen.

Morgan
  • 124
  • 4