Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
Author: Walter Savitch
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 12.1, Problem 6STQ
Program Plan Intro
“add” method in “ArrayList” class:
- • “add” method is used to add the particular element at the end of the list and increases the size of lists by one.
- • Users use the “add” method to set an element for the first time.
Example:
The example for adding string is given below:
Consider, “nameList” is an object of class “ArrayList<String>”. User can add the string “Rose” to the list “nameList” which is given below:
nameList.add("Rose");
User can also add the string for particular index on the given list. Suppose if user want to place a string at index “6” in the “nameList”. The index size starts from “0” to “7”.
nameList.add(6, "Merry");
The above code snippet is used to insert the name “Merry” at index “6”.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Given a base Plant class and a derived Flower class, complete main() to create an ArrayList called myGarden. The ArrayList should be able to store objects that belong to the Plant class or the Flower class. Create a method called printArrayList(), that uses the printInfo() methods defined in the respective classes and prints each element in myGarden. The program should read plants or flowers from input (ending with -1), add each Plant or Flower to the myGarden ArrayList, and output each element in myGarden using the printInfo() method.
Ex. If the input is:
plant Spirea 10 flower Hydrangea 30 false lilac flower Rose 6 false white plant Mint 4 -1
the output is:
Plant 1 Information: Plant name: Spirea Cost: 10
Plant 2 Information: Plant name: Hydrangea Cost: 30 Annual: false Color of flowers: lilac
Plant 3 Information: Plant name: Rose Cost: 6 Annual: false Color of flowers: white
Plant 4 Information: Plant name: Mint Cost: 4
import java.util.Scanner;import java.util.ArrayList;import…
Write all the Members of the ArrayList Class?
Write a declaration of a private field named library that can hold an ArrayList. The elements of the ArrayList are of type Book.
Chapter 12 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
Ch. 12.1 - Suppose aList is an object of the class...Ch. 12.1 - Prob. 2STQCh. 12.1 - Prob. 3STQCh. 12.1 - Prob. 4STQCh. 12.1 - Can you use the method add to insert an element at...Ch. 12.1 - Prob. 6STQCh. 12.1 - Prob. 7STQCh. 12.1 - If you create a list using the statement...Ch. 12.1 - Prob. 9STQCh. 12.1 - Prob. 11STQ
Ch. 12.1 - Prob. 12STQCh. 12.2 - Prob. 13STQCh. 12.2 - Prob. 14STQCh. 12.2 - Prob. 15STQCh. 12.2 - Prob. 16STQCh. 12.3 - Prob. 17STQCh. 12.3 - Prob. 18STQCh. 12.3 - Prob. 19STQCh. 12.3 - Write a definition of a method isEmpty for the...Ch. 12.3 - Prob. 21STQCh. 12.3 - Prob. 22STQCh. 12.3 - Prob. 23STQCh. 12.3 - Prob. 24STQCh. 12.3 - Redefine the method getDataAtCurrent in...Ch. 12.3 - Repeat Question 25 for the method...Ch. 12.3 - Repeat Question 25 for the method...Ch. 12.3 - Repeat Question 25 for the method...Ch. 12.4 - Revise the definition of the class ListNode in...Ch. 12.4 - Prob. 30STQCh. 12.5 - What is the purpose of the FXML file?Ch. 12.5 - Prob. 32STQCh. 12 - Repeat Exercise 2 in Chapter 7, but use an...Ch. 12 - Prob. 2ECh. 12 - Prob. 3ECh. 12 - Repeat Exercises 6 and 7 in Chapter 7, but use an...Ch. 12 - Write a static method removeDuplicates...Ch. 12 - Write a static method...Ch. 12 - Write a program that will read sentences from a...Ch. 12 - Repeat Exercise 12 in Chapter 7, but use an...Ch. 12 - Write a program that will read a text file that...Ch. 12 - Revise the class StringLinkedList in Listing 12.5...Ch. 12 - Prob. 12ECh. 12 - Write some code that will use an iterator to...Ch. 12 - Prob. 14ECh. 12 - Write some code that will use an iterator to...Ch. 12 - Prob. 17ECh. 12 - Revise the method selectionSort within the class...Ch. 12 - Repeat the previous practice program, but instead...Ch. 12 - Repeat Practice Program 1, but instead write a...Ch. 12 - Write a program that allows the user to enter an...Ch. 12 - Write a program that uses a HashMap to compute a...Ch. 12 - Write a program that creates Pet objects from data...Ch. 12 - Repeat the previous programming project, but sort...Ch. 12 - Repeat the previous programming project, but read...Ch. 12 - Prob. 9PPCh. 12 - Prob. 10PPCh. 12 - Prob. 11PPCh. 12 - Prob. 12PPCh. 12 - Prob. 13PPCh. 12 - Prob. 14PPCh. 12 - Prob. 15PP
Knowledge Booster
Similar questions
- What are the distinctions between an ArrayList and a Vector?Which collection class is best for data manipulation?arrow_forwardQ8. Write a declaration of a private field named department that can hold an ArrayList. The elements of the ArrayList are of type Student. Write an assignment to the department variable to create an appropriate ArrayList object. Q9. (A) If a collection stores 20 objects, what value would be returned from a call to its size method? (B) Write a method call using get to return the fifth object stored in a collection called department. Q10. Write a method call to add the object held in the variable student1 to a collection department files.arrow_forwardAdd a getUnsold method to the Auction class with the -following header: public ArrayList getUnsold() This method should iterate over the lots field, storing unsold lots in a new ArrayList local variable. What you are looking for is Lot objects whose highestBid field is null. At the end of the method, return the list of unsold lots.arrow_forward
- Fill in the blanks to create a proper mutator method for the cart attribute in the code below. All syntax must be exact and work with the pre-existing code public class OnlineStore { private ArrayList cart; l/complete the mutator method below: public 1. setCart( 2. cart) { 3. = 4.arrow_forwardTo demonstrate working with ArrayLists, we will be working with four source files: a Dessert class, two classes that extend Dessert called IceCream and Cake, and a TestDessert class. The TestDessert class contains a main() method that declares an ArrayList to hold Dessert objects. This ArrayList is referenced by the variable named "list". Examine TestDessert's main() method and notice the four comments. You are to implement code that accomplishes the tasks described in each step. For step 1, you are to populate the ArrayList with 10 IceCream and Cake objects. These objects should be inserted into the ArrayList at random. This means each run of the program should produce an ArrayList with different proportions of IceCream and Cake objects. After this operation, the ArrayList should contain 10 total objects (IceCream and Cake objects). Display the ArrayList after the operation. For step 2, if the first and last dessert in the ArrayList are different (one is IceCream and the other is…arrow_forward2. Revise the genericStack class we did in the class to implement it using an array instead of ArrayList. You need to check the array size before adding a new element. If it is full, create a new array that double the current size and copy the elements from the current to the new array and then add the new array. (2 points) 3. Use the genericStack class we did the class (not the one in question 2), implement the binary search public static <E extends Comparable<E>) int binarySearch (E[] list, E key) and a method that returns the maximum value element public static <E extends Comparable<E>) E max(E[] list (2 points)arrow_forward
- Java ArrayLists can only be filled with reference type data, so you cannot fill them with primitive type data. How can you work around this with integers? (How can you have an ArrayList filled with integers?)arrow_forwardIf a method given an ArrayList as a parameter calls a second method giving it the same ArrayList, and that second method deletes one of the elements, this will: (produce a non-deterministic result? trigger an error? have an effect? have no effect?) on the first ArrayList. Java uses: (pass by reference? pass by value?) So when an object is passed to a method, what is actually passed is just : (the name of the object? a pointer to the object? the index of the object?) . Select the correct answerarrow_forward5 and 6arrow_forward
- Complete the Course class by implementing the findStudentHighestGpa() method, which returns the Student object with the highest GPA in the course. Assume that no two students have the same highest GPA. Given classes: Class Course represents a course, which contains an ArrayList of Student objects as a course roster. (Type your code in here.) Class Student represents a classroom student, which has three private fields: first name, last name, and GPA. (Hint: GetGPA() returns a student's GPA.) Ex: If the following students and their GPA values are added to a course: Henry Nguyen with 3.5 GPABrenda Stern with 2.0 GPALynda Robison with 3.2 GPASonya King with 3.9 GPA then the findHighestStudent() method returns a student and the program output is: Top student: Sonya King (GPA: 3.9)Code:Course.java: import java.util.ArrayList; //create class Student //create class Courseclass Courses{ private ArrayList<Student> roster; // collection of Student object // class constructor…arrow_forwardStudents make the Dean's list if their GPA is 3.5 or higher. Complete the Course class by implementing the getDeansList() method, which returns an ArrayList of students with a GPA of 3.5 or higher. Given classes: Class Course represents a course, which contains an ArrayList of Student objects as a course roster. (Type your code in here.) Class Student represents a classroom student, which has three fields: first name, last name, and GPA. (Hint: getGPA() returns a student's GPA.) Ex: If the following students and their GPA values are added to a course: Henry Nguyen with 3.5 GPABrenda Stern with 2.0 GPALynda Robison with 3.2 GPASonya King with 3.9 GPA then the getDeansList() method returns an ArrayList of 2 students and the program output is: Dean's list: Henry Nguyen (GPA: 3.5) Sonya King (GPA: 3.9) Course.java import java.util.ArrayList; public class Course { private ArrayList<Student> roster; // Collection of Student objects public Course() { roster = new…arrow_forwardpublic static ArrayList findExactString(String s, ArrayList myArray) { return null; } // Use the above method, findExactString, as a helper method to implement addWithoutDuplication method. The addWithoutDuplication method has the following properties: 1- It takes two arguments, a string s and an ArrayList myArray of type string. 2- It returns an integer which represents the number of occurrences of the string s in myArray while attempting to add it. 3- If myArray is null, it returns -1 4- If adds the string s to the end of the ArrayList myArray if the string s is not already there. public static int addwithout Duplication(String s, ArrayList myArray) { return -1;arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- 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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education