Home > Net >  Can someone please explain this code to me? I have having trouble understanding how to create a main
Can someone please explain this code to me? I have having trouble understanding how to create a main

Time:12-11

 public class array{
    public boolean isSorted (int[] a){
       int temp = a[0];
       for (int i=1; i < a.length; i  ) {
           if (temp < a[i])
           temp = a[i];
       else
           return false;
       }
       return true;
  }
}

To call the code I tried doing something like this

    public class first {
    public static void main(String[] args){
        WhileLoop is = new WhileLoop();
        is.isSorted();
    }




if someone could explain how the code with the array works, and then also how to call it properly as i'm not sure what to put in the parameter for isSorted that would be so helpful! thanks

CodePudding user response:

To be fair, you'll need to look up data types, function returns, array sort algorithms (bubblesort as an example).

To explain it simply: the function returns true if the array that is passed is sorted, false otherwise. The parameter it needs is an array. An array is a sort of a "list" of items (some languages call it a "vector" which is not the same as the "vector" in physics or 2D/3D math space). Having said that, you need to initialize an array, then pass it along the function.

An example is

public static void main(String[] args){
    array arraySorter = new array();
    int[] myArray = new int[] { 1, 3, 5, 6, 7 };
    bool isSorted = arraySorter.isSorted(myArray);
    System.out.println( "Is it sorted? "   isSorted.toString() );
}

I am curios what this WhileLoop is = new WhileLoop(); is. Where did you copy it from and why do you call is sorted on it?

But as I said, without the basics, it will be hard for you to understand the code. Learning programming isn't just blindly copy-pasting code and "seeing what it does". You must first study a language, it's paradigms (believe it or not, they are important), its syntax, its data types, its function types (yes, some languages have more than one function type and their difference isn't simply in the params they have or return type), whether it supports OOP, polymorphism, whether it's interpreted, compiled, low-level and so much more.

Now, regarding Stack Overflow, you should also mark the language it represents. Check the rules of the site please. You also need to properly ask the question as your code seems to have a lot of issues. Asking us to arrange code that not even you know what it does is not constructive.

CodePudding user response:

I'm not entirely sure what programming language you're using, providing details like that can help guide answers a lot.

The way you have that code laid out, isSorted is method that can be executed on an instance of a class of type array.

Incidentally, it has no bearing on the actual instance, which makes the code a little bizarre, or perhaps you're using a language where all methods can be addressed as static methods.

Without altering the array code, you'd probably want to do something like this:

public static void main(String[] args){
  array myArrayInstance = new array();
  int[] theActualIntArray = new int[] { 1, 3, 5, 6, 7 };
  bool isSortedResult = myArrayInstance.isSorted(theActualIntArray);
}

If you can change the array class to have isSorted be a static method, you can simplify the code as follows:

public static void main(String[] args) {
  int[] theActualIntArray = new int[] { 1, 3, 5, 6, 7 };
  bool isSortedResult = array.isSorted(theActualIntArray);
}

since static methods are members of the class itself and not an instance, you wouldn't actually need an instance of array new'd up to use the method.

  • Related