SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Defining Classes I
Exercise:
Programming Projects
Question:3 | ISBN:9780132830317 | Edition: 5

Question

Define a class called Odometer that will be used to track fuel and mileage for an automobile. The class should have instance variables to track the miles driven and the fuel efficiency of the vehicle in miles per gallon. Include a mutator method to reset the odometer to zero miles, a mutator method to set the fuel efficiency, a mutator method that accepts 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 since the odometer was last reset.

Use your class with a test program that creates several trips with different fuel efficiencies. You should decide which variables should be public, if any.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer


//Odometer
package pkg3;


public class Odometer {
     private double oldmiles ,tmilesdriven, tmilage, gallons;
    
    public Odometer(double miles,double average){
        oldmiles = miles;
        tmilesdriven = miles;
        tmilage = average;
        
    }
    
    public double addmiles (double newmiles){
        tmilesdriven = 0;
        tmilesdriven = newmiles;
        return setaverage(tmilesdriven);
    }
    
    public double reset(){
        
        tmilesdriven = 0;
        return addmiles(tmilesdriven);
    }
    
    public double setaverage(double newtrip){
        
        gallons = (newtrip / tmilage);
        return gallons;
    }
    
    public String toString(){
        
        return "NUMBER OF GALLONS USED ON " + tmilesdriven + " miles trip = " + gallons + "\nTOTAL MILES AFTER TRIP: " + (oldmiles+tmilesdriven);
    }
    
}

//MAIN class

package pkg3;


public class Main {

    
    public static void main(String[] args) {
        Odometer o1 = new Odometer(53.7, 32.1);
        Odometer o2 = new Odometer(42.9, 15.4);
        Odometer o3 = new Odometer(220, 27);
        Odometer o4 = new Odometer(672.78, 36);
        Odometer o5 = new Odometer(430.9, 27);
        
        o1.addmiles(61.7);
        o2.addmiles(404.45);
        o3.addmiles(3);
        o4.addmiles(224);
        o5.addmiles(78);
        
        System.out.println(o1);
        System.out.println(o2);
        System.out.println(o3);
        System.out.println(o4);
        System.out.println(o5);
        
    }   
}

 

0 0

Discussions

Post the discussion to improve the above solution.