0

Since this is a re-attack on my first question, I will begin by restating that I am a java newbie. I am also trying to work through a college level java assignment for this week. To caveat this, let me say I barely know what I am doing. The assignment this week after getting clarification; is simply this:

You need to write an actual ClassClient for this project that:

  1. declared and initializes your data array
  2. creates a SortingClass object,
  3. prints the array of data (unsorted)
  4. sends the array into the SortingClass's constructor (which sorts the contents of the array)
  5. prints the same array of data (now sorted)

As of now, I am working through it set step by step. Currently, the only error NetBeans is giving me is that in my second class, everywhere array1 is used is underlined and I assume it means they are not linking to the first class where the array list is placed. MY first question is, I'm obviously not linking correctly; what am I missing? I am also using the selection sort method.

My first class called ClientClass

public class ClientClass {

    public static void main( double [] array ) {

        double array1[] =
        {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};

        //Create a SortingClass variable with data gievn in the array
        SortingClass g = new SortingClass();

        // Print array unsorted
        for (double number : array1) 
        {
        System.out.println("Number = " + number);
        }

        String outPutString = g.toString();
        System.out.println(outPutString);
    }

}

and I have written the second class

public class SortingClass {

    // Selection Sort
    public static void ClientClass (double [] array )
    {
        double temp;
        int max;

        // Selection Sort Method
        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 int indexOfLargestElement ( double[] array1, int size)
    {
        int index = 0;
        for ( int i = 1; i < size; i++ )
        {
            if ( array1[i] > array1[index] )
                index = i;
        }
        return index;
    }
}
Andy Turner
  • 122,430
  • 10
  • 138
  • 216

4 Answers4

1

Your very first problem is that this

sends the array into the SortingClass's constructor (which sorts the contents of the array)

translates to: you can't be using static all over the place!

You need something like

public class SortingClass {
  private final double data[];
  public SortingClass(double data[])  {
    this.data = data;
  }
  public void sort() {
    ... would sort on this.data

Meaning: you create an instance of that class, and you pass a reference to that array into that class.

As a starter, you would want to study stuff like this.

Community
  • 1
  • 1
GhostCat
  • 127,190
  • 21
  • 146
  • 218
  • I'm going to look into that. Although, I do wish that when placing an example that you would use my variable naming convention. Since I am new to all of this, it would make your example easier to follow. – Joseph B Howle Nov 01 '16 at 20:51
  • Sorry, but this is not programming school were we teach you the super-basics. I gave you a link to study. You should simply keep reading that as often as it takes to understand that. Or maybe, if you need some other explanations, try here: http://docs.oracle.com/javase/tutorial/java/javaOO/index.html ... but seriously: do **not** expect us to write down something for you that was written down a zillion times before! – GhostCat Nov 02 '16 at 07:27
0

public static void ClientClass (double [] array ) {} this method is taking an array of doubles called "array" as an argument. When you try and reference "array1" inside this method, it has no idea what you're talking about because there is no such variable created in the scope.

If you change this public static void ClientClass (double [] array ) {} to this public static void ClientClass (double [] array1 ) {} the method will then understand that every time you reference "array1", you are talking about the array you passed in as the method parameter.

Trevor Bye
  • 595
  • 5
  • 23
  • Awesome, taking your suggestion has cleared all the underlines. Now, when I try to run, NetBeans pops up this is an error window: When I close the error window, I find that this line in my first class is highlighted in blue: public static void main ( double [] array) { . What I believe is happening is that because I am saying double [] array that the class is trying to create that but nothing is using it. I did try to change double [] array to double [] array1 and I get an underline error stating: that variable array 1 is already defined in method main. – Joseph B Howle Nov 01 '16 at 20:46
  • the Param of the main method must be String array. Usually declared as "String[] args". – Heri Nov 01 '16 at 21:03
  • @Joseph B Howle Change your main method as Heri has described. Fwiw most IDEs will generate a psvm for you with code generation. – Trevor Bye Nov 02 '16 at 03:50
  • This is technically working, but conceptually, you are pointing him in the wrong direction. First of all method names go camelCase() in Java ... and that is for a reason. A **static** method ClientClass is **not** a constructor of that other class; and that is what the assignment asks for. – GhostCat Nov 02 '16 at 07:26
  • @GhostCat he specified that his question was: "MY first question is, I'm obviously not linking correctly; what am I missing?". So that's what I was answering. You are correct that he doesn't have a constructor and thus isn't satisfying his requirement #4, but that isn't what he was asking for here. But yes, he will at some point need the answer that you posted below. – Trevor Bye Nov 02 '16 at 14:58
0

As long as the methods in SortingClass stay static, its enough to just call it from the main class. There is not need to instantiate SortingClass by a "new":

SortingClass.ClientClass( array1);

BTW1: Good naming preserves you and others from headache. E.g. methods ("ClientClass") first start with lower letter, and second, the name suggests that it is a class, but it is a (void) method which performs a sorting. You better think that methods are verbs, whereas classes/instances are nouns.

BTW2: Your main method - if you want to start this as a java application - should take a String array and not a double array.

Heri
  • 3,796
  • 1
  • 25
  • 45
  • I'm learning more and more about better naming the more I read. Clearly I'm lacking in that department. For your suggestion "SortingClass.ClientClass( array1);" where do I place this? Or do I replace something with that? Oh, forgot to mention that we are supposed to use a double array; assuming because of decimal values. – Joseph B Howle Nov 01 '16 at 20:56
  • You place it instead the line "SortingClass g = new SortingClass();" (just replace it). And do not forget to rename the parameter "array" to "array1" in the ClientClass method, as J.West already has pointed out – Heri Nov 01 '16 at 21:02
  • Further examination of my code and I see where you are asking me to make the change. When I Input that, I get an error further down on my first class at line: String outPutString = g.toString(); with the "g" being underlined. NetBeans error say "can't find symbol" – Joseph B Howle Nov 01 '16 at 21:04
  • Oh also, renameing the parameter "array" to "array1" in the ClientClass method gives me a NetBeans underline error at line: double array1[] = that reads:array 1 is already defined in method main(double[]) – Joseph B Howle Nov 01 '16 at 21:08
  • Ive sorted out the first class and it works as it should; i.e. prints to screen the unsorted array. Next problem is the SortingClient; which is not giving me NetBeans errors but only concludes with a build successful message while not actually spitting out sorted numbers; or any error. Obviously I will need to keep relooking at this. – Joseph B Howle Nov 01 '16 at 21:42
  • There is **very** much a need to instantiate that second class, because that is what his assignment is asking for. – GhostCat Nov 02 '16 at 07:24
0

Thanks everyone. The initial error question has been resolved and my first class is working. For my second class (SortingClass), It is not however and I believe that I need to completely redo it. I am going to get to work on it right now. Thanks again.