Sorting and comparing errors.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
Main File
Code Java:
public class Main {
public static void main(String[] args){
LibraryLab5 t = new LibraryLab5();
LibraryLab5 p = new LibraryLab5();
System.out.println("T: " + t.toString());
System.out.println("t2: " + t.toString());
System.out.println("T3: " + t.toString());
LibraryLab5 []list = Time.getRandomArrayofTimes();
Time.printArrayofTimes(list);
System.out.println();
LibraryLab5 [] tArray = Time.getRandomArrayofTimes();
Time.displayArrayToScreen(tArray);
Time.doFillArray();
LibraryLab5.quickSort();
Time.printArrayofTimes(tArray);
System.out.println();
if(list.compareTo(tArray) > 0)
System.out.println("t is Greater");
else if(list.compareTo(tArray) < 0)
System.out.println("t is smaller");
else
System.out.println("They are the same");
}
}
Time File
Code Java:
import java.util.*;
public class Time{
public LibraryLab5 pink;
static int tArray;
public static LibraryLab5 [] getRandomArrayofTimes(){
LibraryLab5 [] tArray = new LibraryLab5 [10];
Random rand = new Random(System.currentTimeMillis());
for(int i = 0; i < 10; i++){
int k = rand.nextInt(999999999);
LibraryLab5 pink = new LibraryLab5(k);
tArray[i] = pink;
}
return tArray;
}
public static int[] doFillArray() {
int[] list = new int [12];
Random rand = new Random(System.currentTimeMillis());
for (int i = 0; i < 12; ++i) {
int k = rand.nextInt(999999999);
}
return list;
}
public static <E> void displayArrayToScreen(E [] displayArray){
System.out.println("The array contents are: ");
for(int i = 0; i < displayArray.length; ++i ){
System.out.println(displayArray[i]);
}
}
public static void printArrayofTimes(LibraryLab5[] Array){
for(int i = 0; i < Array.length; i++){
System.out.println("Random Array: " + i + Array[i]);
}
}
}
My Library
Code Java:
import java.util.Random;
import java.util.*;
public class LibraryLab5{ //implements Comparable
private int minute;
private int second;
private int hour;
public LibraryLab5(){
this(System.currentTimeMillis());
}
public LibraryLab5(long elaspedTime)
{
long totalSeconds = elaspedTime / 1000L;
this.second = (int)(totalSeconds % 60L);
long totalMinutes = totalSeconds / 60L;
this.minute = (int)(totalMinutes % 60L);
int totalHours = (int)(totalMinutes/ 60L);
this.hour = (totalHours % 24);
}
//muators
public long getSeconds(){
return second;
}
public long getMinutes(){
return minute;
}
public long getHours(){
return hour;
}
public static void quickSort(int [] Array){
quickSort(Array, 0, Array.length - 1);
}
private static void quickSort(int [] Array, int first, int last){
if(last > first){
int pivotIndex = partition(Array, first, last);
quickSort(Array, first, pivotIndex - 1);
quickSort(Array, pivotIndex + 1, last );
}
}
private static int partition(int [] list, int first, int last){
int pivot = list[first];
int low = first + 1;
int high = last;
while(high > low){
while(low <= high && list[low] <= pivot)
low++;
while(low <= high && list[high] > pivot)
high--;
if(high > low){
int temp = list[high];
list[high] = list[low];
list[low] = temp;
}
}
while(high > first && list[high]>= pivot)
high--;
if(pivot > list[high]){
list[first] = list[high];
list[high] = pivot;
return high;
}
else{
return first;
}
}
public void displayTime(long totalHours, long totalMinutes, long totalSeconds){
for(int i = 0; i < totalHours; i++){
for(int j = 0; j< totalMinutes; j++){
for(int k = 0; k < totalSeconds; k++){
}
System.out.print(hour + minute + second);
}
System.out.println();
}
}
public double sumOfSquares(){
double sum = 0;
for(int i = 0; i < hour ; i++){
for(int j = 0; j < second; j++){
for(int g = 0; g < minute; g++) {
sum += hour * minute * second;
}
}
}
return sum;
}
public int compareTo(Object M){
//read compareTo method in text
if(sumOfSquares() > ((LibraryLab5)M).sumOfSquares())
return 1;
else if(sumOfSquares() < ((LibraryLab5)M).sumOfSquares())
return -1;
else
return 0;
}
public String toString(){
String s;
s = hour + " : " + minute + ": " + second + " GMT";
return s;
}
}
What I am trying to do is compare the random array of times, and sort it... but it's giving me errors, please help me.
Re: Sorting and comparing errors.
What error are you getting? If there's an error message, please post it.
Re: Sorting and comparing errors.
Code :
run:
T: 0 : 46: 3 GMT
t2: 0 : 46: 3 GMT
T3: 0 : 46: 3 GMT
Random Array: 09 : 38: 16 GMT
Random Array: 12 : 59: 39 GMT
Random Array: 211 : 42: 1 GMT
Random Array: 312 : 4: 32 GMT
Random Array: 423 : 6: 4 GMT
Random Array: 510 : 17: 37 GMT
Random Array: 614 : 5: 25 GMT
Random Array: 70 : 34: 8 GMT
Random Array: 80 : 37: 56 GMT
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
Random Array: 914 : 24: 42 GMT
The array contents are:
9 : 38: 16 GMT
2 : 59: 39 GMT
11 : 42: 1 GMT
12 : 4: 32 GMT
23 : 6: 4 GMT
10 : 17: 37 GMT
14 : 5: 25 GMT
0 : 34: 8 GMT
0 : 37: 56 GMT
14 : 24: 42 GMT
at Main.main(Main.java:24)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
That the error i get, and I have to sort these
Re: Sorting and comparing errors.
Can't get the given code to compile.
Issues with:
LibraryLab5.quickSort()
because the method requires a parameter, did you intend for the previous line to be used as a parameter?
list.compareTo(tArray)
list is an array... don't think you can call a class method on an array like this. the comment next to the class definition suggests that it should implement comparable, and to take a look at how to use it in your textbook.
For further reading/examples on comparable: Object Ordering - Oracle Java Tutorials
Hope this helps
Re: Sorting and comparing errors.
I still don't get this.. please help me!
Re: Sorting and comparing errors.
What don't you get? What logic are you having trouble conceptualizing? Simply posting your code and asking somebody the explain it is not the way it works. What are your errors? Where are the errors? What IDE (Integrated Development Environment) are you using? Explain what you are trying to do and what steps you have taken to complete them. What specific steps are you having trouble with?