Question

Exercise 2: Write array methods that carry out the following tasks for an array of integers by creating and completing the “A

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class ArrayMethods {
private int[] values;
  
public ArrayMethods(int[] initialValues)
{
values = initialValues;
}
  
public void shiftRight()
{
// first we need to store the last element of the array so that we can place it at 1st index of the array, later on
int lastElement = values[values.length - 1];
// and we are setting -1 to the last element of the array
values[values.length - 1] = -1;
// loop counter variable
int i;
// we will be running a for-loop over the array, from last to second element
for(i = values.length - 1; i > 0; i--)
{
// and we are replacing the current element with the previous element
values[i] = values[i - 1];
}
// lastly, we are placing the last element (previously stored) at index 0 of the array
values[0] = lastElement;
}
  
public Boolean adjacentDuplicate()
{
// a Boolean variable for the result
Boolean found = false;
// we are running a for-loop over the array from first to the second-last element
for(int i = 0; i < values.length - 1; i++)
{
// now, we are checking whether the current element and the element just after it are same or not
// if they are same, we are storing a true in the found variable and breaking out from the loop
if(values[i] == values[i + 1])
{
found = true;
break;
}
}
// lastly, we are returning the Boolean result
return found;
}
  
// (Optional): This method displays the contents of the array
public void display()
{
for(int i = 0; i < values.length; i++)
System.out.print(values[i] + " ");
System.out.println();
}
}

********************************************************* SCREENSHOT *******************************************************

run: D Displaying the initial contents of the array values.. 33 56 40 95 32 32 69 59 39 87 87 Displaying the initial conten

Add a comment
Know the answer?
Add Answer to:
Exercise 2: Write array methods that carry out the following tasks for an array of integers...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write array methods that carry out the following tasks for an array of integers by completing...

    Write array methods that carry out the following tasks for an array of integers by completing the ArrayMethods class below. For each method, provide a test program. public class ArrayMethods { private int[] values; public ArrayMethods(int[] initialValues) { values = initialValues; } public void swapFirstAndLast() { . . . } public void shiftRight() { . . . } .. . } a. Swap the first and last elements in the array. b. Shift all elements to the right by one...

  • Please answer all the questions 2. Design two programs named BaseClass and SubClass. In BaseClass, define...

    Please answer all the questions 2. Design two programs named BaseClass and SubClass. In BaseClass, define a variable xVar (type: char, value: 65), and a method myPrint to print xVar. SubClass is a subclass of BaseClass. In SubClass, define a variable yVar (type: int, value: 16) and another variable strVar (type: String, value: "java program!"). There is also a method myPrint to print the value of yVar and strVar in SubClass, as well as an additional method printAll, in which...

  • Write a program that instantiates an array of integers named scores. Let the size of the...

    Write a program that instantiates an array of integers named scores. Let the size of the array be 10. The program then first invokes randomFill to fill the scores array with random numbers in the range of 0 -100. Once the array is filled with data, methods below are called such that the output resembles the expected output given. The program keeps prompting the user to see if they want to evaluate a new set of random data. Find below...

  • Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations...

    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...

  • LAB: How many negative numbers Write a program that generates a list of integers, and outputs...

    LAB: How many negative numbers Write a program that generates a list of integers, and outputs those integers, and then prints the number of negative elements. Create a method (static void fillArray(int [] array)) that uses Random to generate 50 values (Random.nextInt()), put each value in the array (make sure to pass the array in to this method as an argument). Write another method (static int printAndCountNeg(int [] array)) that prints each element’s value and count the number of negative...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    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...

  • Assignment: Write a program with each of the following methods. You can assume this program is...

    Assignment: Write a program with each of the following methods. You can assume this program is saved in a file called multiArrays.java. A sample main method and sample output is provided at the end of this section. All arrays created are of integer-type. Write a method to declare, initialize, and populate a two-dimensional array. Using the following header:                         public static int[ ][ ] declare2D(Scanner scnr) The user will indicate the size of the array. Write a method to declare,...

  • Assignment 06 – Ten Array Methods You must work in alone on this assignment. Do not...

    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...

  • section Four (20 marks) Assume methods to be defined from Question 1 to Question 3 in...

    section Four (20 marks) Assume methods to be defined from Question 1 to Question 3 in this section are public class ArrayMethods. defined in the 1. (4 marks) Write a private method called switchTwoArrays that accepts two integer arrays (assuming they are of same sizes) as parameters and switches the contents of the array by switching elements in the arrays. For example, it the program starts with first array: (14213]; second array: [112131: After invocation of switchTwoArrays, first array: (51412...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT