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

  • Please help me with this code. Thank you Implement the following Java class: Vehicle Class should...

    Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...

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

  • Java Object Array With 2 Elements In 1 Object

    1. Create a UML diagram to help design the class described in Q2 below. Do this exercise before you attempt to code the solution. Think about what instance variables will be required to describe a Person class object; should they be private or public? Determine what class methods are required; should they be private or public?2. Write Java code for a Person class. A Person has a name of type String and an age of type integer.Supply two constructors: one will be...

  • 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
ADVERTISEMENT