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 called stutter that replaces every value with two of that value. For example, if the list initially stores [42, 7, 0, –3, 15], after the call it should store [42, 42, 7, 7, 0, 0, –3, –3, 15, 15].
Write a method called listFilter() that takes in a list of strings, and returns a list containing only the strings that have an even length or that end in "ing". You can either modify the provided list or create a new one.
Write a method called compress that replaces every pair of elements in the list with a single element equal to the sum of the pair. If the list is of odd size, leave the last element unchanged. For example, if the list stores [1, 7, 3, 9, 4, 6, 5] , your method should change it to store [8, 12, 10, 5] (1+7, then 3+9, then 4+6, then 5).
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