Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Intro to Java Lab 7: ArrayLists Background We are going to create a program that stores every integer that the user tells us. We will

image text in transcribedimage text in transcribed

Intro to Java Lab 7: ArrayLists Background We are going to create a program that stores every integer that the user tells us. We will store the integers in an ArrayList. The catch is, we need to store all of the even ints in the beginning, and all of the odd ints at the end. After each insertion of an integer, we must print the sorted array! There is no starter code for this lab. You must create Lab7.java yourself! Example: 1. We first create an empty arraylist: [] 2. The user enters the int 3. Our ArrayList now looks like this: [3] 3. The user enters the int 5. Since 5 is odd, our ArrayList is now: [3, 5] 4. The user enters the int 2. Since 2 is even, our ArrayList is now: [2, 3, 5] 5. The user enters the int 4. Since 4 is even, our ArrayList is now: [4, 2, 3, 5] - - Assume the user will never enter the same integer twice. Assume the user is done when they enter the int "O". After they enter "O", add it to the ArrayList and print out the final result. Consider 0 to be an EVEN number. - Hint: the .add() method takes either one or two two arguments, if you supply two args, the first is the index of where to insert the value, and the second is the value itself. If you only supply one arg, it will insert that value at the end: myArrayList.add(3, 5); // inserts 5 at index 3 of myArrayList and shifts everything after it over. myArrayList.add(10); // adds 10 to the end of myArrayList. Stephen anthony ellis@cloudshell:-/code/lab2 (earnest-episode-102718) s javac Lab7.java stephen anthony ellis@cloudshell:-/code/lab2 (earnest-episode-102718) s java Lab7 Please enter an int: 2 ArrayList: [2] Please enter an int: 3 ArrayList: [2, 3] Please enter an int: 1 ArrayList: [2, 3, 1] Please enter an int: 4 ArrayList: [4, 2, 3, 1) Please enter an int: 9 ArrayList: [4, 2, 3, 1, 9] Please enter an int: 2 ArrayList: [2, 4, 2, 3, 1, 9] Please enter an int: 0 ArrayList: [0, 2, 4, 2, 3, 1, 9) stephen_anthony_ellis@cloudshell:-/code/lab2 (earnest-episode-102718) $ 1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions