Explanation of Solution
Adding the “lastIndexOf” methods in “ArrayIntList” class:
//definition of "ArrayIntList" class
public class ArrayIntList
{
//declare the required variables
private int[] elementData;
private int size;
//refer the remaining methods in the textbook
//definition of "lastIndexOf" method
public int lastIndexOf(int value)
{
/*iterate "i" until it reaches the size of an array*/
for (int i = size - 1; i >= 0; i--)
{
//check the condition
if (elementData[i] == value)
{
//return the "i" value
return i;
}
}
//return the value
return -1;
}
}
Explanation:
In the above program, the “ArrayIntList” is the class name,
- Inside the “ArrayIntList” class, declare the “elementData”, and “size” variables as a private integer datatype.
- The “lastIndexOf” method is defined inside the “ArrayIntList” class with the integer value as the parameter.
- The “for” loop is used to iterate the value until it reaches size of an array.
- The “if” loop is used to check whether the array of element value is equal to passed value.
- The “ith” position of an array value is returned.
- The “if” loop is used to check whether the array of element value is equal to passed value.
- If the value is not in an array return the -1 value.
- The “for” loop is used to iterate the value until it reaches size of an array.
- The “lastIndexOf” method is defined inside the “ArrayIntList” class with the integer value as the parameter.
Want to see more full solutions like this?
Chapter 15 Solutions
Building Java Programs: A Back To Basics Approach, Student Value Edition (4th Edition)
- Write a method called clump that accepts an ArrayList of strings as a parameter and replaces each pair of strings with a single string that consists of the two original strings in parentheses separated by a space. If the list is of odd length, the final element is unchanged. For example, suppose that a list contains ["four", "score", "and", "seven", "years", "ago", "our"]. Your method should change the list to store [" (four score)", ("and seven"), ("years ago"), "our"].arrow_forwardWrite a static method called "flip4" that takes an ArrayList of integer values as a parameter and that rewrites successive sequences of four values in the list. For each sequence of four values, the method will swap the first element and the fourth element and then swap the second element with the third element. For example, suppose that a variable called list stores the following sequence of values: [3, 8, 19, 42, 7, 26, 19, -8, 193, 204, 6, -4] and we make the following call: flip4(list); Afterwards the list should store the following sequence of values: [42, 19, 8, 3, -8, 19, 26, 7, -4, 6, 204, 193] Each sequence of three elements will be rotated so that the 3rd value becomes the 1st value and the other two are moved to the next higher index; ie. the 1st becomes the 2nd and the 2nd becomes the 3rd. If the list has extra values that are not part of a sequence of three, those values are unchanged. For example, if the list had instead stored: [3, 8, 19, 42, 7, 26, 19,…arrow_forwardWrite methods called min and max that return the smallest and largest values in the list respectively. For example, if a variable called list stores [11, –7, 3, 42, 0, 14] , the call of list.min() should return –7 and the call of list.max() should return 42 . If the list is empty, the methods should throw an IllegalStateExceptionarrow_forward
- Create a method called doubleList that doubles the size of a list by appending a copy of the original sequence to the end of the list. Example, if a variable list stores the values [1, 3, 2, 7] and we call of list.doubleList(); it should store [1, 3, 2, 7, 1, 3, 2, 7].arrow_forwardImplement a lastIndex method for the LinkedIntList class you worked on for HW4. This method takes an int argument and returns the largest index such that the int argument appears at that index in the list. If the int argument does not appear in the list, then -1 is returned. For example if the list example contained the numbers [4, -1, 2, 7, -1, 3] in that order, then example.lastIndex(-1) should return 4 and example.lastIndex(5) should return -1. In the text area below, write the definition of the lastIndex method. (In other words, write the code that should replace the //TODO comment.) Your code cannot call any methods. public class LinkedIntList { private class Node { private int item; private Node next; public Node() {} public Node(int number, Node nextNode) { item = number; next = nextNode; } } private Node first; // first node of the list public LinkedIntList() { first = null;…arrow_forwardWrite a method manyStrings that takes an ArrayList of Strings and an integer n as parameters and that replaces every String in the original list with n of that String. For example, suppose that an ArrayList called "list" contains the following values: ("squid", "octopus") And you make the following call: manyStrings(list, 2); Then list should store the following values after the call: ("squid", "squid", "octopus", "octopus") As another example, suppose that list contains the following: ("a", "a", "b", "c") and you make the following call: manyStrings(list, 3); Then list should store the following values after the call: ("a", "a", "a", "a", "a", "a", "b", "b", "b", "c", "c", "c") You may assume that the ArrayList you are passed contains only Strings and that the integer n is greater than 0.arrow_forward
- Write a Method Called makePalindrome() that will return a linked list with a palindrome of the calling list. Example [1,2,3] must return [1,2,3,3,2,1] and [8,9,1,4] must return [8,9,1,4,4,1,9,8].arrow_forwardWrite a method markLength4 that takes an ArrayList of Strings as a parameter and that places a String of four asterisks ("****") in front of every String of length 4. For example, suppose that an ArrayList called "list" contains the following values: (this, is, lots, of, fun, for, every, Java, programmer) And you make the following call: markLength4(list); Then list should store the following values after the call: (****, this, is, ****, lots, of, fun, for, every, ****, Java, programmer) Notice that you leave the original Strings in the list (this, lots, Java) but include the four-asterisk String in front of each to mark it. You may assume that the ArrayList contains only String values, but it might be empty.arrow_forward/*** Write the method named nOdds().* * Given an ArrayList of positive Integers, return a new list * of length "count" containing the first odd numbers from * the original list. The original will contain at least * "count" odd numbers.* * Examples:* nOdds([3, 2, 4, 5, 8], 2) returns [3, 5]* nOdds([3, 2, 4, 5, 8, 9], 3) returns [3, 5, 9]* nOdds([3, 1, 2, 4, 5, 8], 3) returns [3, 1, 5]* * @param list the ArrayList of Integers to search.* @param count the number of odd numbers to copy.* @return the first "count" odd numbers from the list.*/// TODO - Write the nOdds method here.arrow_forward
- Write the method named nOdds().* * Given an ArrayList of positive Integers, return a new list * of length "count" containing the first odd numbers from * the original list. The original will contain at least * "count" odd numbers.* * Examples:* nOdds([3, 2, 4, 5, 8], 2) returns [3, 5]* nOdds([3, 2, 4, 5, 8, 9], 3) returns [3, 5, 9]* nOdds([3, 1, 2, 4, 5, 8], 3) returns [3, 1, 5]arrow_forwardIn a demo class, write a public static method called removeLastOccurenceTen that receives a reference parameter named list of type LLNode of integer values. Your method should remove the node that contains the last occurrence of the number 10 in the list, if exist.arrow_forwardSometimes a number is the same when you read it from front to back and from back to front. Two easy examples are 123321 and 891232198. Write a Method Called makePalindrome() that will return a linked list with a palindrome of the calling list. Example [1,2,3] must return [1,2,3,3,2,1] and [8,9,1,4] must return [8,9,1,4,4,1,9,8]. In Javaarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education