Results 1 to 2 of 2
  1. #1
    javanewbie82 is offline Member
    Join Date
    Oct 2016
    Posts
    13
    Rep Power
    0

    Default accessor question and how do I test?

    I have to write a Odometer program and test it with several trips. I am totally confused about this part: create an accessor method which will track gallons of gas consumed since last reset and also test your program with several trips with different fuelEfficiencies.
    I tried looking in absolute java about accessors but I'm still confused.
    Java Code:
    public class Odometer
     { 
    		//instance variables
    	 private int mileage;
    	 private int fuelEfficiency;
    	 public Odometer() //mutator method to set mileage to 0 
    	 { 			
    		 mileage = 0; 
    		 fuelEfficiency = 20;
    	 }
    	 public Odometer(int mileage, int fuelEfficiency) //mutator method for mileage
    	 { 
    		 this.mileage = mileage; // setting instance variable mileage to the parameter
    		 setFuelEfficiency(fuelEfficiency);
    	 }
    	 
    	 public void setMileage() //mutator method to set mileage to new value
    	 {
    		 mileage = 25;
    	 }
    	private void setFuelEfficiency(int fuelEfficiency) //constructor method to create fuelEfficiency object
    	 {
    		 this.fuelEfficiency = fuelEfficiency;
    		 
    	 }
    	 //calculate gallons?
    	 private void setGallons(int gallons)
    	 {
    		 gallons = fuelEfficiency/mileage;
    	 }
    	 // accessor method to return fuel since last trip?
    	 private int getGallons()
    	 {
    		 return gallons;
    	 }
    	 
    	
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: accessor question and how do I test?

    You don't need (and don't actually have) a 'gallons' variable.
    What your getGallons should do is return the result of the calculation.

    And get rid of the setGallons.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 3
    Last Post: 05-28-2014, 08:52 AM
  2. help me with a practice test question ? there is no answer
    By stephanie904 in forum New To Java
    Replies: 2
    Last Post: 02-28-2012, 01:20 AM
  3. Accessor/Mutator Question
    By noble in forum New To Java
    Replies: 4
    Last Post: 02-02-2010, 04:21 AM
  4. A test-question on XSL-FO.
    By an24 in forum XML
    Replies: 0
    Last Post: 12-28-2009, 02:36 PM
  5. Another beginner question for AP test
    By DanK in forum New To Java
    Replies: 1
    Last Post: 04-27-2009, 05:36 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •