0

"HELLO WORLD"

This is my first post on the site and am I in week 5 of a programming java course. I should mention that I am a bit of a beginner still. My assignment this week is as follows:

"Create an (double) array to store the scores below. Then create a sorting class using one of the above methods. Make sure the sorting class processes double arrays (double[] arrayName;). Create a client class to call the sorting Class. Pass the array of scores to the Sorting class. Sort the array from smallest to largest and printout the sorted array."

The work I have done so far is created the first class for gathering and printing the results and it is error free. I have also created the second class but I have an error I can't figure out. I have titled this post the error I am receiving. By the way, I am working with NetBeans (not my favorite program). Anyhow, here is the code for my second class:

public class SortingClass {

    public static void SortingClass ( double[] array )
    {
        double[] array1 = new double[]
        {53.5, 60.3, 96.2, 53.3, 56.4, 52.7, 76.4, 77.5, 71.0, 78.2,
        65.2, 59.3, 80.5, 92.1, 85.7, 78.7, 66.2, 88.8, 50.2, 73.4};


        double temp;
        int max;

        for (int i = 0; i < array1.length - 1; i ++)
        {
            max = indexOfLargestElement ( array1, array1.length - i );

            temp = array1[max];
            array1[max] = array1[array1.length - i - 1];
            array1[array1.length - i - 1] = temp;
        }
    }

    public static double indexOfLargestElement ( double[] array1, int size)
    {
        int index = 0;
        for ( int i = 1; i < size; i++ )
        {
            if ( array1[i] > array1[index] )
                index = i;
        }
        return index;
    }
}

The error I am receiving is at the line:

max = indexOfLargestElement ( array1, array1.length - i );

The error message to recap is "Incompatible type: possible lossy conversion from double to int."

user207421
  • 305,947
  • 44
  • 307
  • 483
  • By the way, if my approach is incorrect, please let me know if I should consider a different route. – Joseph B Howle Oct 30 '16 at 20:06
  • `indexOfLargestElement`has a return type of `double` make it an `int` – Peter Lawrey Oct 30 '16 at 20:32
  • Thank you so much. That solved the error. Next question. – Joseph B Howle Oct 30 '16 at 21:06
  • I am going to revise my initial post include my other class because, the suggestion cleared the underline error and now I get a run error. Please see the revised initial post. Btw, the run error I get is: run: clientclass.SortingClass@15db9742 BUILD SUCCESSFUL (total time: 0 seconds) – Joseph B Howle Oct 30 '16 at 21:16
  • @JosephBHowle : Can you update your new error in the original post ? The "error" you mention in your comment is just a "build success" message. – CyprUS Oct 30 '16 at 21:21
  • As a side note, you are doing inefficient sorting technique, look at any native implementation of quicksort or merge sort, which will give you sorting in O(n * logn) time – CyprUS Oct 30 '16 at 21:24
  • Absolutely, I have adjusted my original post accordingly. Perhaps my understanding is not correct though. What I assume is supposed to happen is that the ClientClass should print what is in the array in a sorted order. Assignment gives us three sorting methods and we are supposed to pick one. For my selection, I chose the selection sort which I believe means to sort the entirety of the array. Please correct me if I am wrong. – Joseph B Howle Oct 30 '16 at 21:26
  • The example I followed for the selection sort is what is in our text book. I wish I could have done it better since my understanding low. – Joseph B Howle Oct 30 '16 at 21:28
  • Can you show is your implementation for SortingClass.toString() ? Without one you will get the output in your question. – Peter Lawrey Oct 30 '16 at 21:29
  • Nothing to do with Netbeans, sorting , or arrays. – user207421 Nov 01 '16 at 20:08

1 Answers1

0

Java will not allow you to accidentally assign a double to an int because you would be losing data. You should either have your method return an int, or cast your return value explicitly to an int.

Joe C
  • 15,324
  • 8
  • 38
  • 50
  • given what I have provided above, how do I do so. Like I mentioned, I am and student working in the beginner level. – Joseph B Howle Oct 30 '16 at 21:30
  • Well, you need to decide which of those two things to do. Once you've decided that, I might be able to help you implement your choice. – Joe C Oct 30 '16 at 22:03