Results 1 to 7 of 7
Thread: Sorting Array UI
- 02-17-2011, 11:14 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
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...
}
- 02-17-2011, 11:39 PM #2
That is not practical. When you sort things the smallest is the first and the largest is the last (or vice versa).I want to sort the array list by temperature. so that list[0]= "smallest value" and list[1]="largest value"
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.
- 02-17-2011, 11:53 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
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 } } } }
- 02-17-2011, 11:55 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
Using Array.sort
i would like to use array.sort then, but not sure how to go about doing that
- 02-18-2011, 12:08 AM #5
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.
- 02-18-2011, 12:49 AM #6
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
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] }
- 02-18-2011, 01:50 AM #7
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
-
Help with sorting an object array.
By TommyLR in forum New To JavaReplies: 1Last Post: 02-03-2011, 11:43 AM -
Array List Sorting
By makpandian in forum New To JavaReplies: 5Last Post: 11-14-2010, 02:33 AM -
Sorting Array
By saqib15 in forum New To JavaReplies: 1Last Post: 02-12-2010, 03:42 AM -
Sorting an Array via Stacks
By viperlasson in forum New To JavaReplies: 0Last Post: 02-01-2010, 06:53 AM -
Sorting an array of Strings
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 07:39 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks