Building Java Programs: A Back to Basics Approach (4th Edition)
Building Java Programs: A Back to Basics Approach (4th Edition)
4th Edition
ISBN: 9780134322766
Author: Stuart Reges, Marty Stepp
Publisher: PEARSON
Expert Solution & Answer
Book Icon
Chapter 15, Problem 1E

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.
      • If the value is not in an array return the -1 value.

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Write a method that accepts two lists of integer values and checks if theyare identical or not (i.e. contain the same values). if they are not identical, the methodmust display the max value of each list.
1. Write a method countCommon that accepts two lists of integers as parameters and returns the number of unique integers that occur in both lists. Use one or more sets as storage to help you solve this problem. For example, if one list contains the values [3, 7, 3, –1, 2, 3, 7, 2, 15, 15] and the other list contains the values [–5, 15, 2, –1, 7, 15, 36], your method should return 4 because the elements –1, 2, 7, and 15 occur in both lists. Your method should return correct result with different lists as well. 2. Write a method maxLength that accepts a set of strings as a parameter and that returns the length of the longest string in the list. If your method is passed an empty set, it should return 0.In your project COSC241<Lab4>_<your username>, create Lab4_yourusername.java and declare proper lists and sets to call the two methods countCommon and maxLength. And your output should show the two methods work as expected.Make sure you have comments through your program.
Write a method called replaceAll that accepts two integer values as parameters and replaces all occurrences of the first value in the list with the second value. For example, if a variable called list stores [11, –7, 3, 42, 3, 0, 14, 3], the call of list.replaceAll(3, 999); should change the list to store [11, –7, 999, 42, 999, 0, 14, 999].
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education