Assignment 06 – Ten Array Methods
You must work in alone on this assignment. Do not use any Java
language features we have not cover so far in this course.
Assignment Objectives
After completing this assignment the student should be able to:
Declare and instantiate arrays
Access array elements by index
Use loops and decisions to manipulate arrays and array elements
Write methods that manipulate arrays
Write methods that take array arguments
Write methods that return array references
Assignment Requirements
For this assignment you are given the following files:
Assignment06.java (you must complete this file)
Problem Description and Given Info Within the Assignment06.java
file, you must define the following methods. In the main method,
you may write any code that wish to test the methods you have been
asked to define.
1) Write a public static method named printArray, that takes two
arguments. The first argument is an Array of int and the second
argument is a String. The method should print out a list of the
values in the array, each separated by the value of the second
argument.
For example, given the following Array declaration and
instantiation: int[] myArray = {1, 22, 333, 400, 5005, 9};
printArray(myArray, ", ") will print out 1, 22, 333, 400, 5005, 9
printArray(myArray, " - ") will print out 1 - 22 - 333 - 400 - 5005
- 9
2) Write a public static method named getFirst, that takes an Array
of int as an argument and returns the value of the first element of
the array.
For example, given the following Array declaration and
instantiation: int[] myArray = {1, 22, 333, 400, 5005, 9};
getFirst(myArray) will return 1
3) Write a public static method named getLast, that takes an Array
of int as an argument and returns the value of the last element of
the array.
For example, given the following Array declaration and
instantiation: int[] myArray = {1, 22, 333, 400, 5005, 9};
getLast(myArray) will return 9
4) Write a public static method named getAllButFirst, that takes
an Array of int as an argument and creates and returns a new array
with all of the values in the argument array except the first
value.
For example, given the following Array declaration and
instantiation: int[] myArray = {1, 22, 333, 400, 5005, 9};
getAllButFirst(myArray) will return an Array of int with these
values {22, 333, 400, 5005, 9}
5) Write a public static method named getIndexOfMin, that takes an
Array of int as an argument and returns the index of the least
value in the array.
For example, given the following Array declaration and
instantiation: int[] myArray = {333, 22, 1, 400, 5005, 9};
getIndexOfMin(myArray) will return 2
6) Write a public static method named getIndexOfMax, that takes an
Array of int as an argument and returns the index of the largest
value in the array.
For example, given the following Array declaration and
instantiation: int[] myArray = {1, 22, 333, 400, 5005, 9};
getIndexOfMax(myArray) will return 4
7) Write a public static method named swapByIndex, that takes three
arguments. The first argument is an Array of int, and the second
and third arguments are int indexes. This method will swap the
values at the two given index arguments in the array, and return a
reference to the array.
For example, given the following Array declaration and
instantiation: int[] myArray = {1, 22, 333, 400, 5005, 9};
swapByIndex(myArray, 1, 4) will return the Array with these values
{1, 5005, 333, 400, 22, 9}
8) Write a public static method named removeAtIndex, that takes two
arguments. The first argument is an Array of int, and the second
argument is an int index. This method create and return a new array
with all of the values in the argument array except the value at
the argument index.
For example, given the following Array declaration and
instantiation: int[] myArray = {1, 22, 333, 400, 5005, 9};
removeAtIndex(myArray, 3) will return an Array with these values
{1, 22, 333, 5005, 9}
9) Write a public static method named insertAtIndex, that takes
three arguments. The first argument is an Array of int, the second
argument is an int index, and the third argument is an int value.
This method create and return a new array with all of the values in
the argument array and including the third argument value inserted
at the index specified by the second argument value.
For example, given the following Array declaration and
instantiation: int[] myArray = {1, 22, 333, 400, 9};
insertAtIndex(myArray, 2, 777) will return an Array with these
values {1, 22, 777, 333, 400, 9}
10) Write a public static method named isSorted, that takes an
Array of int as an argument. This method should return the boolean
value true if all the element values in the array are in ascending
order; otherwise the method should return the boolean value
false.
For example, given the following Array declaration and
instantiation: int[] myArray = {22, 5005, 400, 333, 1, 9};
isSorted(myArray) will return false
Method Template Here is a template that you may use or refer to
when defining your methods. All of your ten methods will follow
this template; you must provide the components designated by the
angle brackets (< >)
public static <returnType>
<methodName>(<parameters>) { <methodBody> }
code;
public class Main {
public static void main(String[] args) {
int[] myArray=new int[]{1, 22, 333,
400, 5005, 9};
String x="-";
System.out.println("printing array
elements\n");
printArray(myArray,x);
System.out.println("\n");
System.out.println("first
element:"+getFirst(myArray));
System.out.println("\n");
System.out.println("last
element:"+getLast(myArray));
System.out.println("\n");
int[]
array1=getAllButFirst(myArray);
System.out.println("printing
elelements after removing first\n");
for(int
i=0;i<array1.length;i++)
{
System.out.print(array1[i]+ " ");
}
System.out.println("\n");
System.out.println("minimum index
value:"+getIndexOfMin(myArray));
System.out.println("maximum index
value:"+getIndexOfMax(myArray));
int index1=1,index2=4;
int[]
array2=swapByIndex(myArray,index1,index2);
System.out.println("after
swaping\n");
for(int
i=0;i<array2.length;i++)
{
System.out.print(array2[i]+" ");
}
System.out.println("\n");
int index3=2; //to remove index
value
System.out.println("after remove
index value");
int[]
array3=removeAtIndex(myArray,index3);
for(int
i=0;i<array3.length;i++)
{
System.out.print(array3[i]+" ");
}
System.out.println("\n");
int index4=2; //to add at index
value
int value=7777; //value to be
added
System.out.println("after inserting
new value\n");
int []
array4=insertAtIndex(myArray,index4,value);
for(int
i=0;i<array4.length;i++)
{
System.out.print(array4[i]+" ");
}
System.out.println("\n");
System.out.println(isSorted(myArray));
}
public static void printArray(int[] myArray,String
x)
{
for(int
i=0;i<myArray.length;i++)
{
System.out.print(myArray[i]+""+x+" "); //printing
}
}
public static int getFirst(int[] myArray)
{
int a;
a=myArray[0];
return a; //returning n value
}
public static int getLast(int[] myArray)
{
int a;
a=myArray[myArray.length-1];
return a; //retur last value
}
public static int[] getAllButFirst(int[]
myArray)
{
int j=0;
int[] a=new
int[myArray.length-1];
for(int
i=1;i<myArray.length;i++)
{
a[j++]=myArray[i];
}
return a;
}
public static int getIndexOfMin(int[] myArray)
{
int minind=0;
int min=myArray[0];
for(int
i=0;i<myArray.length;i++)
{
if(myArray[i]<min)
{
min=myArray[i];
minind=i;
}
}
return minind;
}
public static int getIndexOfMax(int[] myArray)
{
int maxind=0;
int max=myArray[0];
for(int
i=0;i<myArray.length;i++)
{
if(myArray[i]>max)
{
max=myArray[i];
maxind=i;
}
}
return maxind;
}
public static int[] swapByIndex(int[]myArray,int
i1,int i2)
{
int[]a=new
int[myArray.length];
int temp=myArray[i1];
myArray[i1]=myArray[i2];
myArray[i2]=temp;
for(int
i=0;i<a.length;i++){
a[i]=myArray[i];
}
return a;
}
public static int[] removeAtIndex(int[] myArray,int
i3)
{
int[] a=new
int[myArray.length-1];
int j=0;
for(int
i=0;i<myArray.length;i++)
{
if(myArray[i]!=myArray[i3])
{
a[j++]=myArray[i];
}
}
return a;
}
public static int[] insertAtIndex(int[]myArray,int
i4,int value)
{
int[] a=new
int[myArray.length+1];
int j=0;
for(int i=0;i<i4;i++)
{
a[j++]=myArray[i];
}
a[j++]=value;
for(int
i=i4;i<myArray.length;i++)
{
a[j++]=myArray[i];
}
return a;
}
public static boolean isSorted(int[] myArray)
{
boolean a=true;
for(int
i=0;i<myArray.length;i++)
{
if(myArray[i]>myArray[i+1])
{
a=false;
break;
}
}
return a;
}
}
OUTPUT:
printing array elements
1- 22- 333- 400- 5005- 9-
first element:1
last element:9
printing elelements after removing first
22 333 400 5005 9
minimum index value:0
maximum index value:4
after swaping
1 5005 333 400 22 9
after remove index value
1 5005 400 22 9
after inserting new value
1 5005 7777 333 400 22 9
false
screenshot:
Assignment 06 – Ten Array Methods You must work in alone on this assignment. Do not...
You must create a Java class named TenArrayMethods in a file named TenArrayMethods.java. This class must include all the following described methods. Each of these methods should be public and static.Write a method named getFirst, that takes an Array of int as an argument and returns the value of the first element of the array. NO array lists. Write a method named getLast, that takes an Array of int as an argument and returns the value of the last element...
Write a static method named sumOf that takes an array of int values as an argument (you may safely assume that the array contains at least 1 element). This method should return the sum of all values in the array. Examples: Given the following array declaration and definition. int[] theArray = {0, 0, 0, 0, 0, 0}; sumOf(theArray) will return 0 Given the following array declaration and definition. int[] theArray = {1, 1, 1, 1, 1, 1}; sumOf(theArray) will return...
JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...
Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations recursively. Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page. No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies Note that the public methods of ArrayRecursion – contains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods...
Write a static method named sumOf that takes an array of int values as an argument (you may safely assume that the array contains at least 1 element). This method should return the sum of all values in the array. Examples: Given the following array declaration and definition. int[] theArray = {0, 0, 0, 0, 0, 0}; sumOf(theArray) will return 0 Given the following array declaration and definition. int[] theArray = {1, 1, 1,...
Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method, but for now leave it empty. Then write a Java method getAverage which takes an array of integers as it’s argument. The method calculate the average of all the elements in the array, and returns that average. The method should work for an array of integers of any length greater than or equal to one. The method signature is shown below: public static double getAverage(...
Game Development: Uno For this assignment you will be creating the game of Uno (See the accompanying pdf for the instructions of the game). Your version will adhere to all the rules except that only the next player can issue a challenge against the previous player in regards to penalties, and your games must have at least three (3) players and at most nine (9) players. To begin, you must create the class Card which contains: Private string field named...
I need help making this work correctly. I'm trying to do an array but it is drawing from a safeInput class that I am supposed to use from a previous lab. The safeInput class is located at the bottom of this question I'm stuck and it is not printing the output correctly. The three parts I think I am having most trouble with are in Bold below. Thanks in advance. Here are the parameters: Create a netbeans project called ArrayStuff...
5. (40 Points) Write a complete Java class named ArrayMethods that implements the methods listed below. All the methods accept the following parameters: Parameters: intar An array of int values. int firstIndex The index of the first element to include in the sum. int lastIndex The index of the last element to include in the sum. Please note that all methods must verify the validity of first Index and lastIndex. public static int sum(int[] arr, int first Index, int lastIndex)...
Java Programming Assignment Write a class named 2DArrayOperations with the following static methods: getTotal . This method should accept a two-dimensional array as its argument and return the total of all the values in the array. Write overloaded versions of this method that work with int , double , and long arrays. (A) getAverage . This method should accept a two-dimensional array as its argument and return the average of all the values in the array. Write overloaded versions of...