-3

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.

  1. One prints the contents of an array
  2. another sorts the contents of an array
  3. 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?

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140

2 Answers2

2

I think you need something like this:

The main that follow your process

public static void main(String[] args) {
    int[] anArray = {11, 6, 9};
    anArray = sort3IntArray(anArray);
    anArray = reverse3IntArray(anArray);
    print3IntArray(anArray);
}

To sort your array, there are Arrays.sort in java so, thank you for java.

// A method to sort the array
public static int[] sort3IntArray(int[] anArray) {
    Arrays.sort(anArray);
    return anArray;
}

Revert your array

// A method to reverse the array
public static int[] reverse3IntArray(int[] anArray) {
    for (int i = 0; i < anArray.length / 2; i++) {
        int temp = anArray[i];
        anArray[i] = anArray[anArray.length - i - 1];
        anArray[anArray.length - i - 1] = temp;
    }
    return anArray;
}

Print your array

// A method to print the array
public static void print3IntArray(int[] anArray) {
    for (int i = 0; i < anArray.length; i++) {
        System.out.println(anArray[i]);
    }
}

Stack-overflow have all what you need, you can find here :

Java: Sort an array

How do I reverse an int array in Java?

Good luck

Community
  • 1
  • 1
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
0

Here is the code!

import java.util.*;
class ArrayProcessor {
// A method to sort the array
public int[] sort3IntArray(int[] anArray){           
    Arrays.sort(anArray);
    System.out.println("Sorted Array is");
    for (int i = 0; i < 3; i++) {
        System.out.print(anArray[i]+" ");
    }
    return anArray; 
}
// A method to reverse the array
public int[] reverse3IntArray(int[] anArray){
    System.out.println("\nReversing Array");
    for (int i = 0; i < anArray.length / 2; i++) {
       int temp = anArray[i];
       anArray[i] = anArray[anArray.length - i - 1];
       anArray[anArray.length - i - 1] = temp;
    }
}
return anArray;
    return anArray;
}
// A method to print the array
public void print3IntArray(int[] anArray){
    System.out.println("\nArray is");
    for (int i = 0; i < 3; i++) {
        System.out.print(anArray[i]+" ");
    }
}}

Main class function is here:

public class ArrayProgram  {
public static void main(String[] args){
    int anArray[] = {10,30,20};//new int[3];
    ArrayProcessor ap = new ArrayProcessor();
    ap.sort3IntArray(anArray);
    ap.reverse3IntArray(anArray);
    ap.print3IntArray(anArray);
}}
Jay Patel
  • 2,341
  • 2
  • 22
  • 43