
Concept explainers
Java Exercise:
Create a class called OurArrayList and implement the following methods. Do NOT create
any auxiliary memory.
Include a main method and test all your methods with appropriate examples.
Assume the following classes have been defined:
a. Write a method called scaleByK() that takes an ArrayList of integers as a
parameter and replaces every integer of value K with K copies of itself. For
example, if the list stores the values (4, 1 , 2, 0 ,3) before the method is called, it
should store the values (4, 4, 4, 4, 1, 2, 2, 3, 3, 3) after the method finishes
executing. Zeroes and negative numbers should be removed from the list by this
method.
b. Write 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.

Trending nowThis is a popular solution!
Step by stepSolved in 3 steps with 4 images

- Please help me solve this with java 1-method called printArray that takes an array of double values and prints the arrayelements on the same line with one space between each two elements, then prints a new line.2. A method called getRandomElement that takes an array of double numbers and returns a random element from the array.3. A method called shiftArray that takes a double value and an array of double values. The method must shift the array in its place to the right, and insert the double value at index 0. Note that this method must not create a new array, it must rotate the array in its place.For example, if the passed array is 6.5, 2.1, 5.8, 1.5, 8.9 and the passed double value is 3.3, after invoking the method the array must become:3.3, 6.5, 2.1, 5.8, 1.54. A method called occurrences that takes a double value and an array of doubles as parameters, and returns the number of occurrences of the double value in the array.For example, if the double number 4.1 and the array {1.5,…arrow_forwardThe class PetShop has a single field as defined below: private ArrayList<Pet> petList; Define a method to print out the details of all cats in petList in PetShop.arrow_forwardJavaarrow_forward
- Define and implement a class named WordList similar to the MyArray class in the lectures thepast 2 weeks. Your WordList class should have the following data members and methods:data members:String[] list : array of n words (assume 1 ≤ n ≤ 20)int n : number of words in listmethods:1. WordList() - constructor for WordList class2. read() – read a sentence consisting of English words using a .nextLine() statement,break each word off the sentence, and save the words in array with one word in eacharray element keeping the relative order of the words unchanged.3. print() – use the array contents to print the word list4. sort() – sort the words in the array in alphabetical order using the insertion sortmethod.For example, if "To be or not to be, that is the question." is entered for the initial sentence:1. read() will build array {"To", "be", "or", "not", "to", "be", "that", "is", "the","question"}2. print() will use the array contents to print:To be or not to be that is the question3. sort()…arrow_forwardMain.java, show output of the given cases.arrow_forwardWrite program in javaarrow_forward
- 4. Say we wanted to get an iterator for an ArrayList and use it to loop over all items and print them to the console. What would the code look like for this? 5. Write a method signature for a method called foo that takes an array as an argument. The return type is void. 6. What is the difference between remove and clear in ArrayLists.arrow_forwardProvide a different implementation of ChoiceQuestion. Instead of storing the choices in an array list, the addChoice method should add the choice to the question text. For this purpose, an addLine method has been added to the Question class. Use the following files: Question.java /** A question with a text and an answer.*/public class Question{ private String text; private String answer; /** Constructs a question with empty text and empty answer. */ public Question() { text = ""; answer = ""; } /** Sets the answer for this question. @param correctResponse the answer */ public void setAnswer(String correctResponse) { answer = correctResponse; } /** Checks a given response for correctness. @param response the response to check @return true if the response was correct, false otherwise */ public boolean checkAnswer(String response) { return response.equals(answer); } /** Add a line of text to…arrow_forwardhow would you do this? this is a non graded practice labarrow_forward
- java Create a static method that: is called repeatAll returns ArrayList of Booleans takes in a single parameter - an ArrayList of Booleans This method should modify its ArrayList parameter by repeating its ArrayList values. For example, if the parameter is (true, false, false) The modified ArrayList should be (true, false, false, true, false, false) public static void main(String[] args) { Scanner in = new Scanner(System.in); int size = in.nextInt(); ArrayList<Boolean> list = new ArrayList<>(); for(int i=0; i < size; i++) { list.add(in.nextBoolean()); } System.out.println(repeatAll(list)); } }arrow_forward4. Now examine the ArrayList methods in the above table, a) Which method retrieves elements from the ArrayList? b) Which method replaces the value of an element that already exists in the ArrayList? c) Which two methods initializes the value of an element? d) How do the two methods in (c) differ? Which method(s) would be appropriate in the above Java program (after we convert it to work with ArrayLists)?arrow_forwardFirst, implement a python class called Student. This class should store 2 variables, name and grade. Later, implement a class called Classroom. The constructor of this class should input the list of students in this classrom. This class should also contain 3 methods, add_student, remove_student and calculate_stats. As the names suggest, add_student method should input a Student object and add that to its' already existing student list, remove_student must input a full name(string) and delete the student with the provided name(you can assume no 2 students have the same full name). Finally, calculate_stats method must calculate and print the following statistics of the grades: 1. The name of the student with highest grade and the grade itself. 2. The name of the student with lowest grade and the grade itself. 3. The average gradearrow_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





