I'm creating an odometer program.

"The problem to solve is define a class Odometer that will be used to track fueland mileage for an automobile. The class should have member variables to track the miles driven and the fuel efficiency of the vehicle in miles per gallon. Include a mutator function to reset the odometer to zero miles, and mutator function to set the fuel efficieny, a mutator function that acepts miles driven for a trip and adds it to the odometer's total, and an accessor method that returns the number of gallons of gasoline that the vehicle has consumed sinced the odometer was last reset"

Here is what I have so far.

import java.util.Scanner;

public class Odometer
{

	public static void main(String args[])
	{
   
   	Odometer first = new Odometer();
   	Scanner keyboard = new Scanner(System.in);
   
   	System.out.println("How many miles did you drive?");
   	double Miles = keyboard.nextDouble();
   	first.getMiles();
  
   	System.out.println("What is the MPG?");
   	double MPG = keyboard.nextDouble();
   	first.getMPG();
   	
   	public int Fuel, Miles, MPG;
   	{
   	Fuel = 0;
   	Miles = 0;
   	MPG = 0;
   	}
   	public double getFuel()
   	{
   	Fuel = 0;
   	return Fuel;
   	}
   	public double getMiles()
   	{
   	Miles = 0;
   	return Miles;
   	}
   	public double getMPG()
   	{
   	MPG = 0;
   	return MPG;
   	}
 }
}

I get an error at lines 20 and 42 which are illegal start of expression and class/interface/enum expected. I have a feeling this is due to my braces and their placements but I've tried so many different braces in areas that just result in more errors. If I try to add a text line to output a value it gives me an identifier error.

When you open a brace or a parenthesis close it immediately! Then write code inside it:
Also you don't declare methods inside other methods. main is a method: public static void main(String args[]) And you did this:

public static void main(String args[]) {


public double getMPG()
   	{
   	MPG = 0;
   	return MPG;
   	}

}

Each method must have its own body. Then you can call methods inside other methods:

import java.util.Scanner;

public class Odometer
{

	public static void main(String args[])
	{
   
   	Odometer first = new Odometer();
   	Scanner keyboard = new Scanner(System.in);
   
   	System.out.println("How many miles did you drive?");
   	double Miles = keyboard.nextDouble();
   	first.getMiles();
  
   	System.out.println("What is the MPG?");
   	double MPG = keyboard.nextDouble();
   	first.getMPG();
        } 

   	public double getFuel()
   	{
   	Fuel = 0;
   	return Fuel;
   	}

   	public double getMiles()
   	{
   	Miles = 0;
   	return Miles;
   	}

   	public double getMPG()
   	{
   	MPG = 0;
   	return MPG;
   	}
}

Also the methods that you declare do nothing. They simply return 0. The global variables that you declared are not the same with the ones inside the main:

public int Fuel=0;
public static void main(String args[])
{
   int Fuel = 5;
}

Those 2 variables are not the same. Try this and see for yourself:

public int Fuel=10;
public static void main(String args[])
{
   int Fuel = 5;
   Odometer first = new Odometer();
   System.out.println("Local Fuel="+Fuel);
   System.out.println("Global Fuel="+first.Fuel);
}

I would advice somthing like this:

import java.util.Scanner;

public class Odometer
{

   	private double Fuel = 0;
        public double Miles = 0;
        public double MPG = 0;

        public double getFuel() {
           // TODO
           // calculate Fuel based on the class'es global variables: Miles, MPG 
           return Fuel;
        }
 
	public static void main(String args[])
	{
   
   	Odometer first = new Odometer();
   	Scanner keyboard = new Scanner(System.in);
   
   	System.out.println("How many miles did you drive?");
   	first.Miles = keyboard.nextDouble();
  
   	System.out.println("What is the MPG?");
   	first.MPG = keyboard.nextDouble();

       System.out.println("Fuel= "+first.getFuel());
        
 }
}

You don't want Fuel to be public because anyone could have access to that variable and change it: first.Fuel=111111111; You need it to be calculated only based on the MPG and Miles. So it needs to be private and change it only in the method getFuel intenrally in the class

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.