samedi 7 janvier 2017

How do I sort, reverse, and print an array of three random integers in a separate class?

I am fairly new to Java and for my class I am suppose to make a class called ArrayProcessor. The ArrayProcessor class contains three methods. One prints the contents of an array, another sorts the contents of an array, and the third reverses the contents. The class is meant to process arrays of three ints. Each of the three methods receives as an argument a reference variable to an array of integers. Two of the methods return a reference variable to an array object.

UML diagram for the ArrayProcessor class

I started writing the class, but I got stuck and I need some help. Here is what I have so far:

// A class that defines an object of type ArrayProcessor

public class ArrayProcessor {

// A method to sort the array
public int[] sort3IntArray(int[] anArray){
    int[] sortedArray = new int[3];

    if (anArray[0] < anArray[1]){
    if (anArray[0] < anArray[2]){

        }
    }

    return anArray; 
}

// A method to reverse the array
public int[] reverse3IntArray(int[] anArray){
    int[] reversedArray = new int[3];
    return anArray;
}

// A method to print the array
public void print3IntArray(int[] anArray){

}   

}

Then I have to make another class called ArrayProgram where I have to create an array of anArray of three random ints. I also have to do the following:

  • invoke the print3IntArray method of the ArrayProcessor class and print the anArray entries
  • invoke the sort3IntArray method of the ArrayProcessor class, and print the sorted arrayand
  • invoke the reverse3IntArray method of the ArrayProcessor class, and print the reversed array

How would I write this?




Aucun commentaire:

Enregistrer un commentaire