Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a class named Numbers. The class should have a private integer array as a

ID: 3580889 • Letter: C

Question

Create a class named Numbers. The class should have a private integer array as an instance variable.

The Numbers constructor should take an integer argument -- that will become the length (i.e. the number of elements) in the integer array.

Give the class a method called "display". This method will print a list of all the numbers in the integer array.

Give the class "setValue" and "getValue" methods.

The "setValue" method takes two integer arguments: one of them is an index number for an element of the array. The other is a value that will be assigned to that index position of the array. The setValue method should only allow values between 0 and 100 to be assigned to the array. If a value less than zero is to be assigned, then assign zero instead. If a value greater than 100 is to be assigned, then assign 100 instead.

The "getValue" method takes an integer argument and uses it as an index number to retrieve the value from that index position of the array.

Both methods should have a try-catch structure to deal with the possibility of an index number that is outside the range of the array length.

Include a method called getLength. It returns the number of elements in the array.

********************

Then create a class named NumbersDriver. It contains your main method. In the main method, do the following:

Instantiate a Numbers object that contains a 10 element array.Call the display method of the Numbers object.

Use the setValue method to assign values to the elements of the array that is encapsulated within the Numbers object.

Use the getValue method to return and display the first and last numbers in the array.

Use the getLength method to return and display the length (the number of elements) of the array.Call the display method to the Numbers object once again.

Clearly describe what you are doing when you call your main methods. Do not have anonymous data simple appear with no clue as to what it is.

Explanation / Answer

NumbersDriver.java


public class NumbersDriver {

  
   public static void main(String[] args) {
      
       Numbers obj = new Numbers (10);
       obj.display();
       for(int i=0; i<10; i++){
           obj.setValue(i, i);
       }
       System.out.println("First value: "+obj.getValue(0));
       System.out.println("Last value: "+obj.getValue(9));
       System.out.println("Length is "+obj.getLength());
       obj.display();
   }

}

Numbers.java

public class Numbers {
   private int array[];
   public Numbers(int length){
       array = new int[length];
   }
   public void display(){
       for(int i=0; i<array.length; i++){
           System.out.println(array[i]);
       }
   }
   public void setValue(int index, int value){
       try{
       if(value < 0 ){
           value = 0;
       }
       else if(value > 100){
           value = 100;
       }
       array[index] =value;
       }
       catch(ArrayIndexOutOfBoundsException e){
           System.out.println("Invalid index position.");
       }
   }
   public int getValue(int index){
       try{
           return array[index];
       }
       catch(ArrayIndexOutOfBoundsException e){
           System.out.println("Invalid index position.");
           return -1;
       }
      
   }
   public int getLength(){
       return array.length;
   }
}

Output:

0
0
0
0
0
0
0
0
0
0
First value: 0
Last value: 9
Length is 10
0
1
2
3
4
5
6
7
8
9

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote