Question:
Programming Homework Question?
2011-02-21 00:16:18 UTC
I tried my best to implement the array into ascending order but i'm not sure if it's correct. I needed it to read in the values from the user but also i need the values that were read to be ascending order from low to high values. Can someone please help?


Create a list of temperatures (highest) of each day, arranged in an ascending order. The list contains maximum 10 records.
This list must be implemented using an array.
The output consists of: the original order in which the items were read in and the sorted output.

import java.util.*;

public class Temperature()
{
public static void Sort()
{
int[] a;
int temp;
java.util.Arrays.sort(a);

for (int i=0; i<(a.length; i++)
{
temp = a[i];
a[i] = a[a.length - i – 1];
a[a.length – i – 1] = temp;
}

public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
Int[] temps = new int[10];
for (int i=0; i {
System.out.println(“Enter Temperature For The Day: “);
temps[i] = scan.nextint();
}
System.out.println(“The Temperature For The Start:” + temps[i] + “”);
Sort();
}
Three answers:
es
2011-02-21 01:27:08 UTC
1) Change "public class Temperature()" to "public class Temperature " (methods are declared with ()s but classes are not)



2) remove the stray ( from "i<(a.length"



3) You need to end your Sort() method before you begin main() -- you left out Sort's terminating brace }



4) "Int[] temps = new int[10];" should be either both Int or both int, depending on whether you want to use an integer Object or a primitive integer.



5) You appear to be mixing up the object-oriented way of doing things and the static way of doing things. Since your methods are static, any data your program needs should be declared in main() and passed in to any method that uses them. If you have a variable that is declared inside of a method, that variable only exists each time that method is running, and goes out of scope and "disappears" afterward.



6) It might be easier to have two arrays declared in your main method; one to hold the unsorted temperatures that are input by the user, and one to hold those temperatures in a sorted order. If you use this strategy, then you would need to pass both arrays in to Sort(). Your for-loop seems to swap the first and last elements, then the second and second-to-last, and so on until the "middle" element or elements are swapped, and then continues on and swaps the respective pairs back again. There are java methods to sort or you could look up a bubblesort and code that.



7) By convention in Java, generally classes are named beginning with a capital letter ("Temperature"), and methods (both static and class methods) are named with lower-case letters ("sort()")



8) "temps[i] = scan.nextint();" should be "temps[i] = in.nextint();" because it is using the Object created in "Scanner in = new Scanner(System.in);" (this declaration is syntactically similar to saying "Double x = new Double( 2.0 );" Just as "x" is the variable here, "in" is the variable there)



EDIT: It might be easier to input the temperatures into a single array, print out the array, and then call a sort routine to sort it, and output that (rather than messing with two arrays)
2011-02-21 08:58:49 UTC
assuming you want the opposite of Arrays.sort()



using the wrapper class of int, Integer and Arrays.sort() using a Comparator to reverse the comparison i would do something much like this:



public static void Sort(Integer[] a) {

Arrays.sort(a, new Comparator() {

public int compare(Integer a, Integer b) {

return b.compareTo(a); // Arrays.sort does a.compareTo(b) by default

}

}

}



its probably the easiest way of reversing the order of the sort function
?
2011-02-21 08:20:18 UTC
Yes I think so. Im going to to the video game college in Sydney.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...