Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Sorting and comparing errors.

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sorting and comparing errors.

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    Main File

    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

    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
    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.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Sorting and comparing errors.

    What error are you getting? If there's an error message, please post it.

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sorting and comparing errors.

    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

  4. #4
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default 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

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sorting and comparing errors.

    I still don't get this.. please help me!

  6. #6
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default 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?
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide

  7. The Following User Says Thank You to vanDarg For This Useful Post:

    JavaPF (March 9th, 2011)

Similar Threads

  1. URGENT!!!! help with array element comparing
    By Neo in forum Java Theory & Questions
    Replies: 3
    Last Post: March 3rd, 2011, 08:52 AM
  2. error when comparing 2 string objects
    By amr in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 9th, 2011, 07:36 PM
  3. Comparing Strings?
    By wandertheverse in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 4th, 2011, 10:32 PM
  4. Comparing Strings only using the .length() method - possible?
    By Ryker in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 16th, 2010, 05:52 PM
  5. [SOLVED] Java program to convert and compare integers
    By luke in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 18th, 2009, 06:26 PM