Question

In: Computer Science

Java Programming II Homework 2-1 In this assignment you are being asked to write some methods...

Java Programming II Homework 2-1 In this assignment you are being asked to write some methods that operate on an array of int values. You will code all the methods and use your main method to test your methods. Your class should be named Array Your class will have the following methods (click on the method signatures for the Javadoc description of the methods): [ https://bit.ly/2GZXGWK ] public static int sum(int[] arr) public static int sum(int[] arr, int firstIndex, int lastIndex) public static double average(int[] arr) public static double average(int[] arr, int firstIndex, int lastIndex) public static int maxValue(int[] arr) public static int maxValue(int[] arr, int firstIndex, int lastIndex) public static int indexOfFirstMaxValue(int[] arr) public static int indexOfFirstMaxValue(int[] arr, int firstIndex, int lastIndex) public static int numberOfBelowAverageElements(int[] arr) public static int numberOfBelowAverageElements(int[] arr, int firstIndex, int lastIndex) public static void rotateElements(int[] arr) public static void rotateElements(int[] arr, int rotationCount) public static void reverseArray(int[] arr) For example, given the following array: myArray = {45, 22, 18, 89, 82, 79, 15, 69, 100, 55, 48, 72, 16, 98, 57, 75, 44, 32, 21, 14, 7, 16, 49, 58, 72} Your methods will return the following values: 1. Sum of whole array = 1253 2. Sum of elements 12-18 = 343 3. Average of whole array = 50.12 4. Average of elements 12-18 = 49.0 5. Max of whole array = 100 6. Max of elements 12-18 = 98 7. Index of first Max of whole array = 8 8. Index of first Max of elements 12-18 = 13 9. Count of elements below average of whole array = 13 10. Count of elements below average of elements 12-18 = 4 11. Rotating once myArray = {72, 45, 22, 18, 89, 82, 79, 15, 69, 100, 55, 48, 72, 16, 98, 57, 75, 44, 32, 21, 14, 7, 16, 49, 58} 12. Rotating 5 more times myArray = {14, 7, 16, 49, 58, 72, 45, 22, 18, 89, 82, 79, 15, 69, 100, 55, 48, 72, 16, 98, 57, 75, 44, 32, 21} 13. Reversing the array myArray = {21, 32, 44, 75, 57, 98, 16, 72, 48, 55, 100, 69, 15, 79, 82, 89, 18, 22, 45, 72, 58, 49, 16, 7, 14}

Solutions

Expert Solution

Program:

public class MyClass {
public static void main(String args[]) {
int myArray[] = {45, 22, 18, 89, 82, 79, 15, 69, 100, 55, 48, 72, 16, 98, 57, 75, 44, 32, 21, 14, 7, 16, 49, 58, 72};
System.out.println("Sum of whole array: "+sum(myArray));
System.out.println("Sum of elements 12 - 18: "+sum(myArray,12,18));
System.out.println("Average of whole array: "+average(myArray));
System.out.println("Average of elements 12 - 18: "+average(myArray,12,18));
System.out.println("Max of whole array: "+maxValue(myArray));
System.out.println("Max of elements 12 - 18: "+maxValue(myArray,12,18));
System.out.println("Index of first max of whole array: "+indexOfFirstMaxValue(myArray));
System.out.println("Index of first max of elements 12 - 18"+indexOfFirstMaxValue(myArray,12,18));
System.out.println("Number of below average elements in whole array: "+numberOfBelowAverageElements(myArray));
System.out.println("Number of below average elements in 12-18: "+numberOfBelowAverageElements(myArray,12,18));
reverseArray(myArray);
}
public static int sum(int[] arr)
{
int sum = 0;
for(int i=0;i<arr.length;i++)
sum = sum + arr[i];
return sum;
}
public static int sum(int[] arr, int firstIndex, int lastIndex)
{
int sum = 0;
for(int i=firstIndex;i<=lastIndex;i++)
sum = sum + arr[i];
return sum;
}
public static double average(int[] arr)
{
double avg = 0;
int i=0;
for(i=0;i<arr.length;i++)
avg = avg + arr[i];
return avg/i;
}
public static double average(int[] arr, int firstIndex, int lastIndex)
{
double avg = 0;
int i=0,j=0;
for(i=firstIndex;i<=lastIndex;i++)
{
avg = avg + arr[i];
j++;
}

return avg/j;
}
public static int maxValue(int[] arr)
{
int max = 0;
for(int i=0;i<arr.length;i++)
{
if(arr[i]>max)
max = arr[i];
}
return max;
}
public static int maxValue(int[] arr, int firstIndex, int lastIndex)
{
int max = 0;
for(int i=firstIndex;i<=lastIndex;i++)
{
if(arr[i]>max)
max = arr[i];
}
return max;
}
public static int indexOfFirstMaxValue(int[] arr)
{
int max = 0,j=0;
for(int i=0;i<arr.length;i++)
{
if(arr[i]>max)
{
j=i;
max = arr[i];
}
}
return j;
}
public static int indexOfFirstMaxValue(int[] arr, int firstIndex, int lastIndex)
{
int max = 0,j=0;
for(int i=firstIndex;i<=lastIndex;i++)
{
if(arr[i]>max)
{
j=i;
max = arr[i];
}
}
return j;
}
public static int numberOfBelowAverageElements(int[] arr)
{
double avg = average(arr);
int i=0,j=0;
for(i=0;i<arr.length;i++)
{
if(arr[i]<avg)
j++;
}
return j;

}
public static int numberOfBelowAverageElements(int[] arr, int firstIndex, int lastIndex)
{
double avg = average(arr, firstIndex, lastIndex);
int i=0,j=0;
for(i=firstIndex;i<=lastIndex;i++)
{
if(arr[i]<avg)
j++;
}
return j;
}
public static void reverseArray(int[] arr)
{
int[] b = new int[arr.length];
int j = arr.length;
for (int i = 0; i < arr.length; i++) {
b[j - 1] = arr[i];
j = j - 1;
}
  
/*printing the reversed array*/
System.out.println("Reversed array is: \n");
for (int k = 0; k < arr.length; k++) {
System.out.print(b[k] + " ");
}
}
}

Output:


Related Solutions

Programming II: C++ - Programming Assignment Vector Overloads Overview In this assignment, the student will write...
Programming II: C++ - Programming Assignment Vector Overloads Overview In this assignment, the student will write a C++ program that overloads the arithmetic operators for a pre-defined Vector object. When completing this assignment, the student should demonstrate mastery of the following concepts: · Object-oriented Paradigm · Operator Overloading - Internal · Operator Overloading - External · Mathematical Modeling Assignment In this assignment, the student will implement the overloaded operators on a pre-defined object that represents a Vector. Use the following...
COP 2800, Java Programming Assignment 6-1 Using Java create a program using the “Methods” we covered...
COP 2800, Java Programming Assignment 6-1 Using Java create a program using the “Methods” we covered this week. come up with a “problem scenario” for which you can create a “solution”. Utilize any/all of the examples from the book and class that we discussed. Your program should be interactive and you should give a detailed statement about what the “description of the program – purpose and how to use it”. You should also use a “good bye” message. Remember to...
Java Programming Assignment 6-1 We have covered the “Methods” this week. Apply the concepts that we...
Java Programming Assignment 6-1 We have covered the “Methods” this week. Apply the concepts that we have learnt in class and come up with a “problem scenario” for which you can create a “solution”. Utilize any/all of the examples from the book and class that we discussed. You program should be interactive and you should give a detailed statement about what the “description of the program – purpose and how to use it”. You should also use a “good bye”...
JAVA programming- answer prompts as apart of one java assignment Static static variables static methods constants...
JAVA programming- answer prompts as apart of one java assignment Static static variables static methods constants Create a class Die representing a die to roll randomly. ☑ Give Die a public final static int FACES, representing how many faces all dice will have for this run of the program. ☑ In a static block, choose a value for FACES that is a randomly chosen from the options 4, 6, 8, 10, 12, and 20. ☑ Give Die an instance variable...
Programming II: C++ - Programming Assignment Fraction Object with Operator Overloads Overview In this assignment, the...
Programming II: C++ - Programming Assignment Fraction Object with Operator Overloads Overview In this assignment, the student will write a C++ program that implements a “fraction” object. When writing the object, the student will demonstrate mastery of implementing overloaded operators in a meaningful way for the object. When completing this assignment, the student should demonstrate mastery of the following concepts: · Mathematical Modeling - Fractions · Operator Overloading – Binary Operators (Internal Overload) · Operator Overloading – Binary Operator (External...
This programming assignment involves learning about some of the common exceptions that occur in Java programs.
This programming assignment involves learning about some of the common exceptions that occur in Java programs. Consider the following exception types: NullPointerException ArrayIndexOutOfBoundsException ClassCastException IllegalArgumentException Research what each exception type means and the conditions under which each occurs (i.e., is thrown). Write programs that demonstrate each type of exception being thrown (one program per exception) and provide a screen capture of the output. You should write your code so that each exception type is forced to occur. Name your programs...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a CPU scheduler. The number of CPU’s and the list of processes and their info will be read from a text file. The output, of your simulator will display the execution of the processes on the different available CPU’s. The simulator should also display: -   The given info of each process -   CPU utilization - The average wait time - Turnaround time for each process...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method. Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers. This can be done by looping until there are no more swaps being made, or using a...
JAVA programming - please answer all prompts as apart of 1 java assignment. Part A Create...
JAVA programming - please answer all prompts as apart of 1 java assignment. Part A Create a java class InventoryItem which has a String description a double price an int howMany Provide a copy constructor in addition to other constructors. The copy constructor should copy description and price but not howMany, which defaults to 1 instead. In all inheriting classes, also provide copy constructors which chain to this one. Write a clone method that uses the copy constructor to create...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT