Results 1 to 3 of 3
  1. #1
    Jamison5213 is offline Member
    Join Date
    Dec 2009
    Posts
    22
    Rep Power
    0

    Default Calculating min and max in an array list

    I have everything done except finding maximum and minimum values. In this (tester) file, i can't figure out where to do that maximum and minimum for,if loop. Its bolded where it is right now but let me know where it should be. Thank you very much!







    Java Code:
    /**
     * This program uses classes and objects between different java files. 
     *
     *
     * Jamison Hyman
     * 1/15/10
     */
    
    
    public class AnnualFuelUseTester {
    
     public static void main(String[] args)
        
        {
        	
     
     int max = Integer.MIN_VALUE;
     int min = Integer.MAX_VALUE;
    
        	
        	
        	
        	    AnnualFuelUse car1 = new AnnualFuelUse(1, 81823, 82243, 15.3, 2.61);	// car object
        	    	
        	         AnnualFuelUse[] AnnualFuelUse = {new AnnualFuelUse(1, 81823, 82243, 15.3, 2.61),
        	         						 new AnnualFuelUse(6, 82243, 82713, 16.0, 2.55),
        	         						 new AnnualFuelUse(13, 82713, 83341, 14.7, 2.64),
        	         						 new AnnualFuelUse(19, 83341, 83920, 15.1, 2.60)};   	
        
                for(int index = 0; index < AnnualFuelUse.length; index++)
            {
                AnnualFuelUse[index].getmyStartMiles();
                AnnualFuelUse[index].getmyEndMiles();
                AnnualFuelUse[index].getmyGallons();
                AnnualFuelUse[index].getmyDistance();
                AnnualFuelUse[index].getmyMpg();
                AnnualFuelUse[index].getmypricePerG();
                AnnualFuelUse[index].getmyTotalPrice();
                AnnualFuelUse[index].getmyGallonsPerMile();
                AnnualFuelUse[index].getmyDay();
                
    [B]            if (AnnualFuelUse[index].getmyDistance < min){
                 min = AnnualFuelUse[index].getmyDistance;[/B]
                }
            }
              	    	
        	    	
        System.out.printf("%55s\n\n","Gas Mileage Calculations");
        System.out.printf("%s%8s%13s%12s%12s%15s%10s%13s%11s\n","Fill Up","Days","Start Miles","End Miles","Distance","Gallons Used","MPG","Price","Cost");
        
        int count = 1;
        
        for(int index = 0; index < AnnualFuelUse.length; index++)
            {
                System.out.printf("%s%12s%13s%13s%12.3s%13.5s%15.5s%12.4s%11.5s\n",count,
                          AnnualFuelUse[index].getmyDay(),AnnualFuelUse[index].getmyStartMiles(), 
                          AnnualFuelUse[index].getmyEndMiles(), AnnualFuelUse[index].getmyDistance(),
                          AnnualFuelUse[index].getmyGallons(), AnnualFuelUse[index].getmyMpg(),
                          AnnualFuelUse[index].getmypricePerG(),AnnualFuelUse[index].getmyTotalPrice());
                          
                count++;          
            }
    }
        
    }

    and the other file

    Java Code:
    /**
     * This program calculates and displays gas mileage calculations
     * using methods and Tester files.
     *
     *
     * Jamison Hyman 
     * 1/7/10
     */
    
    public class AnnualFuelUse {
    	
    	private double myStartMiles1;
    	private double myEndMiles1;
    	private double myGallons1;
    	private String myMake1;
    	private double myDistance1;			// variable names to be assigned later
    	private double myMpg1;
    	private double mypricePerG1;
    	private double myTotalPrice1;
    	private double myGallonsPerMile1;
    	private int myDay;
    
    	AnnualFuelUse(){	// default
    	}
    	
    	public AnnualFuelUse(int day ,double startMiles, double endMiles, double gallons,double pricePerG){
    		
    		myStartMiles1 = startMiles;
    		myEndMiles1 = endMiles;
    		myGallons1 = gallons;
    		myDistance1 = myEndMiles1 - myStartMiles1;		// variables assigned
    		myMpg1 = myDistance1/myGallons1;
    		mypricePerG1 = pricePerG;
    		myTotalPrice1 = pricePerG * myGallons1;
    		myGallonsPerMile1 = myGallons1/myDistance1;
    		myDay = day;
    	}
    	    
    
    	public double getmyStartMiles(){
    		return myStartMiles1;
    	}
    
    	public double getmyEndMiles(){
    		return myEndMiles1;
    	}
    
    	public double getmyGallons(){
    		return myGallons1;
    	}
    	
    	public double getmyDistance(){
    		return myDistance1;
    	}										// methods to return the values for printing later
    
    	public double getmyMpg(){
    		return myMpg1;
    	}
    	
    	public double getmypricePerG(){
    		return mypricePerG1;
    	}
    	
    	public double getmyTotalPrice(){
    		return myTotalPrice1;
    	}
    	
    	public double getmyGallonsPerMile(){
    		return myGallonsPerMile1;
    	}
    	
    	public int getmyDay(){
    		return myDay;
    	}
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Sort it and take the first and last values.

  3. #3
    Jamison5213 is offline Member
    Join Date
    Dec 2009
    Posts
    22
    Rep Power
    0

Similar Threads

  1. Array List
    By mashyum in forum Advanced Java
    Replies: 2
    Last Post: 07-28-2009, 06:47 AM
  2. Array List
    By mprentice84 in forum New To Java
    Replies: 2
    Last Post: 02-20-2009, 02:12 AM
  3. PROBLEM - calculating with array elements
    By ella in forum New To Java
    Replies: 13
    Last Post: 12-04-2008, 12:36 AM
  4. array list help
    By dorno83 in forum New To Java
    Replies: 9
    Last Post: 11-07-2008, 09:56 AM
  5. using an Array list
    By toad in forum New To Java
    Replies: 1
    Last Post: 11-18-2007, 09:08 PM

Posting Permissions

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