Results 1 to 7 of 7
  1. #1
    Brandon Seale is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default Sorting Array UI

    My project is to input weather stations and their temperatures using a user interface(command Post Temperatures) and then once thats posted enter another command (Display HighLow) which displays the station with the highest temp and the lowest temp.

    WeatherStationData.java- creates temp and id
    WeatherStationUI.java- hosts menu with commands
    WeatherStationMain.java runs ui
    WeatherStationList.java stores data in a list.

    Here's my post command on WeatherStationUI.java

    WeatherStationList Collection = new WeatherStationList(); // to store data
    public void PostTemperatures() //posts temps
    {
    String ID, Unit;
    double Temp;

    while(true){


    System.out.print("Station Name: "); //prompts for station name
    ID = Input.nextLine(); // puts next line as id

    if(ID.equals("Stop")) //breaks
    break;

    System.out.print("Station Temp: ");
    Unit = Input.nextLine();
    Temp = Double.parseDouble(Unit); //turns into double
    Collection.Add(ID, Temp);
    }

    }

    here on WeatherStationList.java i take the data inputted from the post command -- WeatherStationUI.java and store it in an array List[i].

    I want to sort the array list by temperature. so that list[0]= "smallest value" and list[1]="largest value"

    public WeatherStationData[] List = new WeatherStationData[MaxSize];

    public void Add(String ID, double Temp)
    {
    if(CurrSize < MaxSize) { //Make sure there's room...
    List[CurrSize] = new WeatherStationData(ID, Temp); //Then add it...
    CurrSize++; //Bump the count up, because there's one more...
    }
    else
    System.out.println("Sorry List Is Full"); //Whoops! no more room...
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    I want to sort the array list by temperature. so that list[0]= "smallest value" and list[1]="largest value"
    That is not practical. When you sort things the smallest is the first and the largest is the last (or vice versa).

    There are 3 ways to achieve your goal.

    Write your own sort method following a well publicised sorting algorithm

    Use Arrays.sort. Make your class (the objects in the array) extend comparable. Or create a Comparator and pass that to the sort method.

    Don't sort at all. Just keep track of the largest and smallest as the values are read in.

  3. #3
    Brandon Seale is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default Source Code

    where would you put the sort?

    Java Code:
    /*
     * title: Main.java
     * name: Brandon Seale
     * date: 2/16
     * comments: used Happy Inventory as framework for project.
     */
    public class Main {
        
        public static void main(String[] args)
        {
       	 WeatherStationUI NewList = new WeatherStationUI();
        }
    
    }
    Java Code:
    /*
     * Title:   		 WeatherStationUI.java
     * Description:   	 Supplies a frame for using the WeatherStationList class...
     */
    
    import java.util.*;
    
    public class WeatherStationUI {
        
        WeatherStationList Collection = new WeatherStationList(); // to store data
        
        Scanner Input = new Scanner(System.in); // new scanner
        
        public WeatherStationUI() { RunIT(); }
        
        public void RunIT()
        {
       	String Command;
           
       	while(true) {
       		Menu(); // menu
       	    
       		System.out.print("Command: "); //uses next line as command
       		Command = Input.nextLine();
    
       	 if(Command.equals("Quit")) // "Quit" breaks from program
       			break;
       		else if(Command.equals("Post Temperatures")) // to post temps
       			PostTemperatures();
       		else if(Command.equals("High-Low Report")) // to view high low
       			break;
       		else if(Command.equals("Daily Report")) // view all
       			Collection.Display();
       	    
       	}
        
           
        }
    
        public void Menu() //choices
        {
       	 System.out.println("Choices.......................................");
       	 System.out.println("\tPost Temperatures...allows the user to post new stations");
       	 System.out.println("\tHigh-Low Report...allow the user to view high to low.");
       	 System.out.println("\tDaily Report.....displays the entire list.");
       	 System.out.println("\tQuit");
       	 System.out.println("----------------------------------------------");
        }
        
        public void PostTemperatures() //posts temps
        {
       	 String ID, Unit;
       	 double Temp;
       	 
       	 while(true){
       			 
       			 
       			 System.out.print("Station Name: "); //prompts for station name
       			 ID = Input.nextLine(); // puts next line as id
       			 
       			 if(ID.equals("Stop")) //breaks
       				 break;
       			 
       			 System.out.print("Station Temp: ");
       			 Unit = Input.nextLine();
       			 Temp = Double.parseDouble(Unit); //turns into double
       			 Collection.Add(ID, Temp); // sends to collection
               	 
           	 
       		 
       		 
       		 
       		 
       		 
       	 }
       	 
        }
        
        
        
    }

    Java Code:
    /*
     * Title:   		 WeatherStationData.java
     * Description:   	 This represent a single Weather Station
     *
     */
    
    public class WeatherStationData {
        private String StationID;  //Each Station must have a unique identifier...
        private double StationTemp;
        
        
        public WeatherStationData() //Define a default constructor...
        {
       	 StationID = "";   	 
       	 StationTemp = 0.0;
       	 
        }
        
        public WeatherStationData(String ID, double Temp) //...intializer
        {
       	 StationID = ID;
       	 StationTemp = Temp;
       	 
       	 
       	 
        }
        
        //Define some setter methods...
        
        public void SetID(String ID)  { StationID = ID; }
        public void SetTemp(double Temp) { StationTemp = Temp; }
        
        
        //Define some getter methods...
        
        public String GetID()  { return StationID; }
        public double GetTemp()  { return StationTemp; }
        
        
    }
    Java Code:
    /*
     * Title:   	 WeatherStationList.java
     * Description: this implements and manages a list of inventory items...
     */
    import java.text.DecimalFormat;//in order to display in decimal format
    import java.text.NumberFormat;//same
    
    public class WeatherStationList {
        
        private int MaxSize = 25;  //The value here is the List's absolute size...
        private int CurrSize = 0;  //The number of item currently list...
        
        private WeatherStationData[] List = new WeatherStationData[MaxSize];
        
        
        //Define the core methods for the list...
        
        //First a way to add items to the list...
        
        public void Add(String ID, double Temp)
        {
       	 if(CurrSize < MaxSize) {  //Make sure there's room...
       		 List[CurrSize] = new WeatherStationData(ID, Temp); //Then add it...
       		 CurrSize++; //Bump the count up, because there's one more...
       	 }
       	 else
       		 System.out.println("Sorry List Is Full"); //Whoops! no more room...
        }
    public void HL2(String ID, double Temp)
    {
    }
        public void Display()
        {
       	 int K;
       	 
       	 for(K = 0 ; K < CurrSize ; K++)  {
       		 if(List[K] != null) {
       			 NumberFormat Rounder = new DecimalFormat("#0.00"); //displays in 0.00 decimal format
       			 
       			 System.out.println("Weather Station:   " + List[K].GetID()); //gets station id
       			 System.out.println("Temperature: " + List[K].GetTemp() + "	" + Rounder.format(((5*(List[K].GetTemp()-32))/9)));//shows temp in F and Celcius
       			 
       		 }
       	 }
       	 
        }
        
        
        
        
        
        
    
    }

  4. #4
    Brandon Seale is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default Using Array.sort

    i would like to use array.sort then, but not sure how to go about doing that

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    You have already done Post Temperatures. How about working on HighLow Report. Then you would sort the array just before you display to high and low values.

  6. #6
    Brandon Seale is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default Wrote High Low

    Java Code:
    /*
     * Title:			WeatherStationUI.java
     * Description:		Supplies a frame for using the WeatherStationList class...
     */
    
    
    import java.util.*;
    
    
    public class WeatherStationUI {
    	
    	WeatherStationList Collection = new WeatherStationList(); // to store data
    	Scanner Input = new Scanner(System.in); // new scanner
    	
    	
    	
    	
    	public WeatherStationUI() { RunIT(); } 
    	
    	public void RunIT()
    	{
    		
    	   String Command;
    	   
    	   while(true) {
    		   Menu(); // menu
    		   
    		   System.out.print("Command: "); //uses next line as command
    		   Command = Input.nextLine();
    
    		if(Command.equals("Quit")) // "Quit" breaks from program
    			   break;
    		   else if(Command.equals("Post Temperatures"))	 
    			   PostTemperatures();	
    [B]	   
    		   else if(Command.equals("High-Low Report")) // to view high low			   
    			   	Collection.Display2();[/B]
    		   else if(Command.equals("Daily Report")) // view all
    			   Collection.Display();
    		   
    	   }
    	
    	   
    	}
    
    	
    	
    	public void Menu() //choices
    	{
    		System.out.println("Choices.......................................");
    		System.out.println("\tPost Temperatures...allows the user to post new stations");
    		System.out.println("\tHigh-Low Report...allow the user to view high to low.");
    		System.out.println("\tDaily Report.....displays the entire list.");
    		System.out.println("\tQuit");
    		System.out.println("----------------------------------------------");
    	}
    	
    	public void PostTemperatures() //posts temps
    	{
    		String ID, Unit;
    		double Temp;
    		
    		while(true){
    				
    				
    				System.out.print("Station Name: "); //prompts for station name
    				ID = Input.nextLine(); // puts next line as id
    				
    				if(ID.equals("Stop")) //breaks
    					break;
    				
    				System.out.print("Station Temp: ");
    				Unit = Input.nextLine(); 
    				Temp = Double.parseDouble(Unit); //turns into double
    				Collection.Add(ID, Temp); 
    	            
                
    			
    			
    			
    			
    			
    		}
    		
    		
    	}
    	
    	
    	
    	
    	
    	
    	
    }
    Java Code:
    /*
     * Title:		WeatherStationList.java
     * Description: this implements and manages a list of inventory items...
     */
    import java.text.DecimalFormat;//in order to display in decimal format
    import java.text.NumberFormat;//same
    import java.util.*;
    
    
    public class WeatherStationList {
    	
    	private int MaxSize = 25;  //The value here is the List's absolute size...
    	private int CurrSize = 0;  //The number of item currently list...
    	
    	public WeatherStationData[] List = new WeatherStationData[MaxSize];
    	
    	
    	
    	//Define the core methods for the list...
    	
    	//First a way to add items to the list...
    	
    	public void Add(String ID, double Temp)
    	{
    		if(CurrSize < MaxSize) {  //Make sure there's room...
    			List[CurrSize] = new WeatherStationData(ID, Temp); //Then add it...
    			CurrSize++; //Bump the count up, because there's one more...
    		}
    		else
    			System.out.println("Sorry List Is Full"); //Whoops! no more room...
    	}
    	
    	
    	
    	
    	
    	public void Display()
    	{
    		int K;
    		
    		for(K = 0 ; K < CurrSize ; K++)  {
    			if(List[K] != null) {
    				NumberFormat Rounder = new DecimalFormat("#0.00"); //displays in 0.00 decimal format
    				
    				System.out.println("Weather Station:   " + List[K].GetID()); //gets station id
    				System.out.println("Temperature: " + List[K].GetTemp() + "    " + Rounder.format(((5*(List[K].GetTemp()-32))/9)));//shows temp in F and Celcius
    				
    			}
    		}
    		
    	
    		
    	}
    	
    	
    	[B]public void Display2()
    	{
    
                            What Code Do i enter here to sort the list by the value of the Double Temp?
    			 
    
    
    			System.out.println("Highest Temperature: " + List[0].GetID() + List[0].GetTemp());
    			System.out.println("Lowest Temperature: " + List[CurrSize].GetID() + List[CurrSize].GetTemp());
    		
    	}[/B]
    		
    	
    	
    	
    	
    	
    	
    
    }

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    Quote Originally Posted by Brandon Seale View Post
    What Code Do i enter here to sort the list by the value of the Double Temp?
    I gave you three options to do this. Choose one and implement the code. If you want more specific help then ask a more specific question. At the moment your question is akin to "I want to build a space shuttle. How do I do that?"

    On the other hand if you are just waiting for someone to post code then I won't do that.

Similar Threads

  1. Help with sorting an object array.
    By TommyLR in forum New To Java
    Replies: 1
    Last Post: 02-03-2011, 12:43 PM
  2. Array List Sorting
    By makpandian in forum New To Java
    Replies: 5
    Last Post: 11-14-2010, 03:33 AM
  3. Sorting Array
    By saqib15 in forum New To Java
    Replies: 1
    Last Post: 02-12-2010, 04:42 AM
  4. Sorting an Array via Stacks
    By viperlasson in forum New To Java
    Replies: 0
    Last Post: 02-01-2010, 07:53 AM
  5. Sorting an array of Strings
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:39 PM

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
  •