SlideShare a Scribd company logo
1 of 10
Download to read offline
I am stuck on parts E and F
Exercise 1: NumberListTester.java
Class NumberList implements a simple list of Integers, using an ArrayList. NumberListTester
generates 10 random, positive, 2-digit ints, and adds them to the list. It then calls method
printList which prints the items on the list.
Run the program until you understand what it does.
a. Write the body of the method printReversed, which prints the list items in reverse order. Add a
statement in main to call printReversed after the call to printList.
Check ______
b. Write the body of the method printEveryOther, which prints every other item on the list,
beginning with the first one. In main, replace the statement that calls printReversed with one
that calls printEveryOther.
Check ______
c. Write the body of the method printEvens, which prints all the even-numbered ints on the list.
In main, replace the statement that calls printEveryOther with one that calls printEvens.
Check ______
All this printing is getting a bit tedious. Let's try some more fun operations.
d. 0. Add a new ArrayList-of-integer instance variable called bigList to the NumberList class
Add a statement to the NumberList constructor that creates an empty ArrayList object pointed to
by bigList
Delete the call to printEvens from main.
Write the body of the method splitList, which copies all ints that are greater than or equal to 50
from aList to bigList.
Add a method to the NumberList class to print bigList.
Add a sequence of method calls in main to verify that the splitList method is working correctly.
I.e., after calling splitList, call again the method that prints the original list and then call the
method you wrote in step 4 to print the bigList.
Check _____
E. Add a method insert to the NumberList class that takes two parameters of type int. The first
parameter is the index at which to insert a new number into the list, and the second is the number
to be inserted. E.g., if the first param is 4 and the second is 37, then 37 will be inserted at index
4 in aList (i.e., as the new 5th list element). The number inserted does not replace the number
currently at that position, but is inserted just before it.
Hint: The ArrayList class has a method that does exactly this. Call that method from your insert
method.
Now add statements to main to allow the user to specify the number to insert and the position at
which to insert it. Then, after inserting the new number, print the updated list.
Check _____
F. Finally, add code to your insert method that will assure that an IndexOutOfBoundsException
cannot be thrown. I.e., your insert method should just print an appropriate error message if the
first parameter --- the index at which to insert the new Integer --- is illegal.
Hint: What are the legal indices for an ArrayList? What is the smallest? What is the largest?
What about the special case of inserting a new value at the end of the list? Try it!
Check _____
MY CODE SO FAR:
import java.util.ArrayList ;
import java.util.Random ;
/**
* A class to provide practice using ArrayLists
*/
class NumberList
{
// instance var's
private ArrayList aList ;// a list of Integer objects
private ArrayList bigList;
/**
* Creates a NumberList object.
*/
public NumberList()
{
aList = new ArrayList() ; // creates an empty list
bigList = new ArrayList();
}
/**
* Adds a number to the list.
* @param number the number to be added to the list
*/
public void add(int number)
{
aList.add(number) ; // calls add method of ArrayList class
}
/**
* Prints the numbers stored in aList.
*/
public void printList()
{
System.out.println( "The numbers on the list: " ) ;
// for each number on the list, from 1st to last...
for (int i = 0 ; i < aList.size() ; i++)
{
int number = aList.get(i) ; // get the number
System.out.print(number + " ") ; // print it
}
System.out.println(" ") ;
}
/**
* Prints the numbers stored in aList, in reverse order.
*/
public void printReversed()
{
System.out.println( "The numbers on the list, in reverse order: " ) ;
for (int i = aList.size()-1 ; i >= 0 ; i--)
{
int number = aList.get(i) ; // get the number
System.out.print(number + " ") ; // print it
}
System.out.println(" ") ;
}
/**
* Prints every other number stored in aList, starting with the first one.
*/
public void printEveryOther()
{
System.out.println( "Starting with the first, every other number: " ) ;
for (int i = 0 ; i < aList.size() ; i++, i++)
{
int number = aList.get(i) ; // get the number
System.out.print(number + " ") ; // print it
}
System.out.println(" ") ;
}
/**
* Prints all the even-numbered ints stored in aList.
*/
public void printEvens()
{
System.out.println( "The even numbers on the list: " ) ;
for (int i = 0 ; i < aList.size() ; i++)
{
if(aList.get(i) % 2 == 0){
int number = aList.get(i) ; // get the number
System.out.print(number + " ") ; // print it
}
}
System.out.println(" ") ;
}
/**
* Copies all ints that are 50 or greater from aList to bigList.
*/
public void splitList()
{
System.out.println( "The numbers on the big list, greater than 50: " ) ;
for (int i = 0 ; i < aList.size() ; i++)
{
if(aList.get(i) >= 50){
int c = aList.get(i);
bigList.add(c); // get the number
System.out.print(c + " ") ; // print it
}
}
System.out.println(" ") ;
}
}
public class NumberListTester
{
public static void main (String [] args)
{
Random r = new Random() ;
NumberList list = new NumberList() ;
// populate the list with 10 random 2-digit ints (10 to 99)
for (int i = 1 ; i <= 10 ; i++)
{
int next = r.nextInt(90) + 10 ;
// call the "add" method of the NumberList class
list.add( next ) ;
}
// print the aList
list.printList() ;
list.splitList();
}
}
Solution
Please see the below code which is amended in E an F part of the quation:
import java.util.ArrayList ;
import java.util.Random ;
/**
* A class to provide practice using ArrayLists
*/
class NumberList
{
// instance var's
private ArrayList aList ;// a list of Integer objects
private ArrayList bigList;
private int number,index;
/**
* Creates a NumberList object.
*/
public NumberList(int index, int number)
{
aList = new ArrayList() ; // creates an empty list
bigList = new ArrayList();
bigList.add(index,number);//it accepts index and number
}
/**
* Adds a number to the list.
* @param number the number to be added to the list
*/
public void add(int number)
{
aList.add(number) ; // calls add method of ArrayList class
}
/**
* Prints the numbers stored in aList.
*/
public void printList()
{
System.out.println( "The numbers on the list: " ) ;
// for each number on the list, from 1st to last...
for (int i = 0 ; i < aList.size() ; i++)
{
int number = aList.get(i) ; // get the number
System.out.print(number + " ") ; // print it
}
System.out.println(" ") ;
}
/**
* Prints the numbers stored in aList, in reverse order.
*/
public void printReversed()
{
System.out.println( "The numbers on the list, in reverse order: " ) ;
for (int i = aList.size()-1 ; i >= 0 ; i--)
{
int number = aList.get(i) ; // get the number
System.out.print(number + " ") ; // print it
}
System.out.println(" ") ;
}
/**
* Prints every other number stored in aList, starting with the first one.
*/
public void printEveryOther()
{
System.out.println( "Starting with the first, every other number: " ) ;
for (int i = 0 ; i < aList.size() ; i++, i++)
{
int number = aList.get(i) ; // get the number
System.out.print(number + " ") ; // print it
}
System.out.println(" ") ;
}
/**
* Prints all the even-numbered ints stored in aList.
*/
public void printEvens()
{
System.out.println( "The even numbers on the list: " ) ;
for (int i = 0 ; i < aList.size() ; i++)
{
if(aList.get(i) % 2 == 0){
int number = aList.get(i) ; // get the number
System.out.print(number + " ") ; // print it
}
}
System.out.println(" ") ;
}
public void insertNum(int index, int number)throws IndexOutOfBoundsException{
index=this.index;
number=this.number;
bigList.add(index,number);
int size=bigList.size();
if(index>size)
{
System.out.println("the index at which to insert the new Integer --- is illegal");
}else{
System.out.println("not found");
}
}
/**
* Copies all ints that are 50 or greater from aList to bigList.
*/
public void splitList()
{
System.out.println( "The numbers on the big list, greater than 50: " ) ;
for (int i = 0 ; i < aList.size() ; i++)
{
if(aList.get(i) >= 50){
int c = aList.get(i);
bigList.add(c); // get the number
System.out.print(c + " ") ; // print it
}
}
System.out.println(" ") ;
}
}
--------------------------------------------------------------------------------------------------------
import java.util.Random;
public class NumberListTester
{
public static void main (String [] args)
{
Random r = new Random() ;
NumberList list = new NumberList(4,37) ;
// populate the list with 10 random 2-digit ints (10 to 99)
for (int i = 1 ; i <= 10 ; i++)
{
int next = r.nextInt(90) + 10 ;
// call the "add" method of the NumberList class
list.add( next ) ;
}
// print the aList
list.printList() ;
list.splitList();
}
}

More Related Content

Similar to I am stuck on parts E and FExercise 1      NumberListTester.java.pdf

Need to be done in C++ Please Sorted number list implementation wit.pdf
Need to be done in C++  Please   Sorted number list implementation wit.pdfNeed to be done in C++  Please   Sorted number list implementation wit.pdf
Need to be done in C++ Please Sorted number list implementation wit.pdf
aathiauto
 
Need to be done in C Please Sorted number list implementation with.pdf
Need to be done in C  Please   Sorted number list implementation with.pdfNeed to be done in C  Please   Sorted number list implementation with.pdf
Need to be done in C Please Sorted number list implementation with.pdf
aathmaproducts
 
Illegal numbers.a. Complete the method find which accepts a collec.pdf
Illegal numbers.a. Complete the method find which accepts a collec.pdfIllegal numbers.a. Complete the method find which accepts a collec.pdf
Illegal numbers.a. Complete the method find which accepts a collec.pdf
gopalk44
 
Implementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfImplementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdf
maheshkumar12354
 
Chapter 7.1
Chapter 7.1Chapter 7.1
Chapter 7.1
sotlsoc
 
Create a menu-driven program that will accept a collection of non-ne.pdf
Create a menu-driven program that will accept a collection of non-ne.pdfCreate a menu-driven program that will accept a collection of non-ne.pdf
Create a menu-driven program that will accept a collection of non-ne.pdf
rajeshjangid1865
 
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdfLabprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
freddysarabia1
 
In the rest of this lab, you will help build such a list class. The .pdf
In the rest of this lab, you will help build such a list class. The .pdfIn the rest of this lab, you will help build such a list class. The .pdf
In the rest of this lab, you will help build such a list class. The .pdf
SANDEEPARIHANT
 
An object of class StatCalc can be used to compute several simp.pdf
 An object of class StatCalc can be used to compute several simp.pdf An object of class StatCalc can be used to compute several simp.pdf
An object of class StatCalc can be used to compute several simp.pdf
aravlitraders2012
 
I need help with this program for java.The program you are given t.pdf
I need help with this program for java.The program you are given t.pdfI need help with this program for java.The program you are given t.pdf
I need help with this program for java.The program you are given t.pdf
fonecomp
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdf
akkhan101
 
I only need help with four methods in the EmployeeManager class the .pdf
I only need help with four methods in the EmployeeManager class the .pdfI only need help with four methods in the EmployeeManager class the .pdf
I only need help with four methods in the EmployeeManager class the .pdf
arpitcomputronics
 
import java.util.LinkedList; import java.util.Random; import jav.pdf
import java.util.LinkedList; import java.util.Random; import jav.pdfimport java.util.LinkedList; import java.util.Random; import jav.pdf
import java.util.LinkedList; import java.util.Random; import jav.pdf
aquastore223
 
File LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfFile LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdf
Conint29
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
vishalateen
 
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdfAssignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
formicreation
 
ArrayTest.java import java.util.Arrays; import java.util.Scann.pdf
ArrayTest.java import java.util.Arrays; import java.util.Scann.pdfArrayTest.java import java.util.Arrays; import java.util.Scann.pdf
ArrayTest.java import java.util.Arrays; import java.util.Scann.pdf
deepua8
 

Similar to I am stuck on parts E and FExercise 1      NumberListTester.java.pdf (20)

Need to be done in C++ Please Sorted number list implementation wit.pdf
Need to be done in C++  Please   Sorted number list implementation wit.pdfNeed to be done in C++  Please   Sorted number list implementation wit.pdf
Need to be done in C++ Please Sorted number list implementation wit.pdf
 
Need to be done in C Please Sorted number list implementation with.pdf
Need to be done in C  Please   Sorted number list implementation with.pdfNeed to be done in C  Please   Sorted number list implementation with.pdf
Need to be done in C Please Sorted number list implementation with.pdf
 
Illegal numbers.a. Complete the method find which accepts a collec.pdf
Illegal numbers.a. Complete the method find which accepts a collec.pdfIllegal numbers.a. Complete the method find which accepts a collec.pdf
Illegal numbers.a. Complete the method find which accepts a collec.pdf
 
Implementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfImplementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdf
 
Chapter 7.1
Chapter 7.1Chapter 7.1
Chapter 7.1
 
I need help with the 2nd TODO comment and the other comments Ill.pdf
I need help with the 2nd TODO comment and the other comments Ill.pdfI need help with the 2nd TODO comment and the other comments Ill.pdf
I need help with the 2nd TODO comment and the other comments Ill.pdf
 
Create a menu-driven program that will accept a collection of non-ne.pdf
Create a menu-driven program that will accept a collection of non-ne.pdfCreate a menu-driven program that will accept a collection of non-ne.pdf
Create a menu-driven program that will accept a collection of non-ne.pdf
 
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdfLabprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
 
In the rest of this lab, you will help build such a list class. The .pdf
In the rest of this lab, you will help build such a list class. The .pdfIn the rest of this lab, you will help build such a list class. The .pdf
In the rest of this lab, you will help build such a list class. The .pdf
 
An object of class StatCalc can be used to compute several simp.pdf
 An object of class StatCalc can be used to compute several simp.pdf An object of class StatCalc can be used to compute several simp.pdf
An object of class StatCalc can be used to compute several simp.pdf
 
For this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdfFor this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdf
 
I need help with this program for java.The program you are given t.pdf
I need help with this program for java.The program you are given t.pdfI need help with this program for java.The program you are given t.pdf
I need help with this program for java.The program you are given t.pdf
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdf
 
I only need help with four methods in the EmployeeManager class the .pdf
I only need help with four methods in the EmployeeManager class the .pdfI only need help with four methods in the EmployeeManager class the .pdf
I only need help with four methods in the EmployeeManager class the .pdf
 
import java.util.LinkedList; import java.util.Random; import jav.pdf
import java.util.LinkedList; import java.util.Random; import jav.pdfimport java.util.LinkedList; import java.util.Random; import jav.pdf
import java.util.LinkedList; import java.util.Random; import jav.pdf
 
File LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfFile LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdf
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
 
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdfAssignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
 
ArrayTest.java import java.util.Arrays; import java.util.Scann.pdf
ArrayTest.java import java.util.Arrays; import java.util.Scann.pdfArrayTest.java import java.util.Arrays; import java.util.Scann.pdf
ArrayTest.java import java.util.Arrays; import java.util.Scann.pdf
 
Merge radix-sort-algorithm
Merge radix-sort-algorithmMerge radix-sort-algorithm
Merge radix-sort-algorithm
 

More from RAJATCHUGH12

Javaa. The mean of a list of numbers is its arithmetic average. T.pdf
Javaa. The mean of a list of numbers is its arithmetic average. T.pdfJavaa. The mean of a list of numbers is its arithmetic average. T.pdf
Javaa. The mean of a list of numbers is its arithmetic average. T.pdf
RAJATCHUGH12
 
vapour pressure equation The Clansius-Clapeyron relation relates the.pdf
vapour pressure equation The Clansius-Clapeyron relation relates the.pdfvapour pressure equation The Clansius-Clapeyron relation relates the.pdf
vapour pressure equation The Clansius-Clapeyron relation relates the.pdf
RAJATCHUGH12
 
The Vietnam War EraDiscuss the range of American responses to the .pdf
The Vietnam War EraDiscuss the range of American responses to the .pdfThe Vietnam War EraDiscuss the range of American responses to the .pdf
The Vietnam War EraDiscuss the range of American responses to the .pdf
RAJATCHUGH12
 
t hether each of the following statements are true or false T F Stok.pdf
t hether each of the following statements are true or false T F Stok.pdft hether each of the following statements are true or false T F Stok.pdf
t hether each of the following statements are true or false T F Stok.pdf
RAJATCHUGH12
 
Origins of psychologyWhen did it begin as a science 1879.pdf
Origins of psychologyWhen did it begin as a science 1879.pdfOrigins of psychologyWhen did it begin as a science 1879.pdf
Origins of psychologyWhen did it begin as a science 1879.pdf
RAJATCHUGH12
 
Listings for BinaryHeap.java and BinaryHeapTest.java are shown in th.pdf
Listings for BinaryHeap.java and BinaryHeapTest.java are shown in th.pdfListings for BinaryHeap.java and BinaryHeapTest.java are shown in th.pdf
Listings for BinaryHeap.java and BinaryHeapTest.java are shown in th.pdf
RAJATCHUGH12
 
How does differ from evolution systematic Only cladistics traces e.pdf
How does  differ from evolution systematic  Only cladistics traces e.pdfHow does  differ from evolution systematic  Only cladistics traces e.pdf
How does differ from evolution systematic Only cladistics traces e.pdf
RAJATCHUGH12
 

More from RAJATCHUGH12 (20)

Why is the T7 polymerase gene in E. coli strain BL21(DE3) under the .pdf
Why is the T7 polymerase gene in E. coli strain BL21(DE3) under the .pdfWhy is the T7 polymerase gene in E. coli strain BL21(DE3) under the .pdf
Why is the T7 polymerase gene in E. coli strain BL21(DE3) under the .pdf
 
What type of utility program is designed to automatically make dupli.pdf
What type of utility program is designed to automatically make dupli.pdfWhat type of utility program is designed to automatically make dupli.pdf
What type of utility program is designed to automatically make dupli.pdf
 
Which of the following is true of xylem conducts water up the plant .pdf
Which of the following is true of xylem conducts water up the plant .pdfWhich of the following is true of xylem conducts water up the plant .pdf
Which of the following is true of xylem conducts water up the plant .pdf
 
What are the essential elements to have in BRDR plansWhen is a d.pdf
What are the essential elements to have in BRDR plansWhen is a d.pdfWhat are the essential elements to have in BRDR plansWhen is a d.pdf
What are the essential elements to have in BRDR plansWhen is a d.pdf
 
Javaa. The mean of a list of numbers is its arithmetic average. T.pdf
Javaa. The mean of a list of numbers is its arithmetic average. T.pdfJavaa. The mean of a list of numbers is its arithmetic average. T.pdf
Javaa. The mean of a list of numbers is its arithmetic average. T.pdf
 
What is the characteristic of the ring of quaternionsSolutionQ.pdf
What is the characteristic of the ring of quaternionsSolutionQ.pdfWhat is the characteristic of the ring of quaternionsSolutionQ.pdf
What is the characteristic of the ring of quaternionsSolutionQ.pdf
 
What are the four basic categories of output A. Instructions, pictu.pdf
What are the four basic categories of output  A. Instructions, pictu.pdfWhat are the four basic categories of output  A. Instructions, pictu.pdf
What are the four basic categories of output A. Instructions, pictu.pdf
 
vapour pressure equation The Clansius-Clapeyron relation relates the.pdf
vapour pressure equation The Clansius-Clapeyron relation relates the.pdfvapour pressure equation The Clansius-Clapeyron relation relates the.pdf
vapour pressure equation The Clansius-Clapeyron relation relates the.pdf
 
What is a test cross and why is it used What technology is essential.pdf
What is a test cross and why is it used What technology is essential.pdfWhat is a test cross and why is it used What technology is essential.pdf
What is a test cross and why is it used What technology is essential.pdf
 
What are homologous traits a. Traits that are functional and adapti.pdf
What are homologous traits a. Traits that are functional and adapti.pdfWhat are homologous traits a. Traits that are functional and adapti.pdf
What are homologous traits a. Traits that are functional and adapti.pdf
 
The Vietnam War EraDiscuss the range of American responses to the .pdf
The Vietnam War EraDiscuss the range of American responses to the .pdfThe Vietnam War EraDiscuss the range of American responses to the .pdf
The Vietnam War EraDiscuss the range of American responses to the .pdf
 
The distance between two successive minimize of a transverse wave.pdf
The distance between two successive minimize of a transverse wave.pdfThe distance between two successive minimize of a transverse wave.pdf
The distance between two successive minimize of a transverse wave.pdf
 
t hether each of the following statements are true or false T F Stok.pdf
t hether each of the following statements are true or false T F Stok.pdft hether each of the following statements are true or false T F Stok.pdf
t hether each of the following statements are true or false T F Stok.pdf
 
Rewrite the expression as an algebraic expression of x Rewrite the .pdf
Rewrite the expression as an algebraic expression of x Rewrite the .pdfRewrite the expression as an algebraic expression of x Rewrite the .pdf
Rewrite the expression as an algebraic expression of x Rewrite the .pdf
 
Need help answering these. Underline the correct answer The fundame.pdf
Need help answering these. Underline the correct answer  The fundame.pdfNeed help answering these. Underline the correct answer  The fundame.pdf
Need help answering these. Underline the correct answer The fundame.pdf
 
Origins of psychologyWhen did it begin as a science 1879.pdf
Origins of psychologyWhen did it begin as a science 1879.pdfOrigins of psychologyWhen did it begin as a science 1879.pdf
Origins of psychologyWhen did it begin as a science 1879.pdf
 
Listings for BinaryHeap.java and BinaryHeapTest.java are shown in th.pdf
Listings for BinaryHeap.java and BinaryHeapTest.java are shown in th.pdfListings for BinaryHeap.java and BinaryHeapTest.java are shown in th.pdf
Listings for BinaryHeap.java and BinaryHeapTest.java are shown in th.pdf
 
In hypothesis testing, what is a Type I error Type II errorSol.pdf
In hypothesis testing, what is a Type I error Type II errorSol.pdfIn hypothesis testing, what is a Type I error Type II errorSol.pdf
In hypothesis testing, what is a Type I error Type II errorSol.pdf
 
How does differ from evolution systematic Only cladistics traces e.pdf
How does  differ from evolution systematic  Only cladistics traces e.pdfHow does  differ from evolution systematic  Only cladistics traces e.pdf
How does differ from evolution systematic Only cladistics traces e.pdf
 
Giving your lawn more nutrients than it can absorb, can cause polluti.pdf
Giving your lawn more nutrients than it can absorb, can cause polluti.pdfGiving your lawn more nutrients than it can absorb, can cause polluti.pdf
Giving your lawn more nutrients than it can absorb, can cause polluti.pdf
 

Recently uploaded

Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 

Recently uploaded (20)

How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Our Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdfOur Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 

I am stuck on parts E and FExercise 1      NumberListTester.java.pdf

  • 1. I am stuck on parts E and F Exercise 1: NumberListTester.java Class NumberList implements a simple list of Integers, using an ArrayList. NumberListTester generates 10 random, positive, 2-digit ints, and adds them to the list. It then calls method printList which prints the items on the list. Run the program until you understand what it does. a. Write the body of the method printReversed, which prints the list items in reverse order. Add a statement in main to call printReversed after the call to printList. Check ______ b. Write the body of the method printEveryOther, which prints every other item on the list, beginning with the first one. In main, replace the statement that calls printReversed with one that calls printEveryOther. Check ______ c. Write the body of the method printEvens, which prints all the even-numbered ints on the list. In main, replace the statement that calls printEveryOther with one that calls printEvens. Check ______ All this printing is getting a bit tedious. Let's try some more fun operations. d. 0. Add a new ArrayList-of-integer instance variable called bigList to the NumberList class Add a statement to the NumberList constructor that creates an empty ArrayList object pointed to by bigList Delete the call to printEvens from main. Write the body of the method splitList, which copies all ints that are greater than or equal to 50 from aList to bigList. Add a method to the NumberList class to print bigList. Add a sequence of method calls in main to verify that the splitList method is working correctly. I.e., after calling splitList, call again the method that prints the original list and then call the method you wrote in step 4 to print the bigList. Check _____ E. Add a method insert to the NumberList class that takes two parameters of type int. The first parameter is the index at which to insert a new number into the list, and the second is the number to be inserted. E.g., if the first param is 4 and the second is 37, then 37 will be inserted at index 4 in aList (i.e., as the new 5th list element). The number inserted does not replace the number currently at that position, but is inserted just before it. Hint: The ArrayList class has a method that does exactly this. Call that method from your insert method.
  • 2. Now add statements to main to allow the user to specify the number to insert and the position at which to insert it. Then, after inserting the new number, print the updated list. Check _____ F. Finally, add code to your insert method that will assure that an IndexOutOfBoundsException cannot be thrown. I.e., your insert method should just print an appropriate error message if the first parameter --- the index at which to insert the new Integer --- is illegal. Hint: What are the legal indices for an ArrayList? What is the smallest? What is the largest? What about the special case of inserting a new value at the end of the list? Try it! Check _____ MY CODE SO FAR: import java.util.ArrayList ; import java.util.Random ; /** * A class to provide practice using ArrayLists */ class NumberList { // instance var's private ArrayList aList ;// a list of Integer objects private ArrayList bigList; /** * Creates a NumberList object. */ public NumberList() { aList = new ArrayList() ; // creates an empty list bigList = new ArrayList(); } /** * Adds a number to the list. * @param number the number to be added to the list */ public void add(int number) { aList.add(number) ; // calls add method of ArrayList class
  • 3. } /** * Prints the numbers stored in aList. */ public void printList() { System.out.println( "The numbers on the list: " ) ; // for each number on the list, from 1st to last... for (int i = 0 ; i < aList.size() ; i++) { int number = aList.get(i) ; // get the number System.out.print(number + " ") ; // print it } System.out.println(" ") ; } /** * Prints the numbers stored in aList, in reverse order. */ public void printReversed() { System.out.println( "The numbers on the list, in reverse order: " ) ; for (int i = aList.size()-1 ; i >= 0 ; i--) { int number = aList.get(i) ; // get the number System.out.print(number + " ") ; // print it } System.out.println(" ") ; } /** * Prints every other number stored in aList, starting with the first one.
  • 4. */ public void printEveryOther() { System.out.println( "Starting with the first, every other number: " ) ; for (int i = 0 ; i < aList.size() ; i++, i++) { int number = aList.get(i) ; // get the number System.out.print(number + " ") ; // print it } System.out.println(" ") ; } /** * Prints all the even-numbered ints stored in aList. */ public void printEvens() { System.out.println( "The even numbers on the list: " ) ; for (int i = 0 ; i < aList.size() ; i++) { if(aList.get(i) % 2 == 0){ int number = aList.get(i) ; // get the number System.out.print(number + " ") ; // print it } } System.out.println(" ") ; } /** * Copies all ints that are 50 or greater from aList to bigList. */ public void splitList() { System.out.println( "The numbers on the big list, greater than 50: " ) ;
  • 5. for (int i = 0 ; i < aList.size() ; i++) { if(aList.get(i) >= 50){ int c = aList.get(i); bigList.add(c); // get the number System.out.print(c + " ") ; // print it } } System.out.println(" ") ; } } public class NumberListTester { public static void main (String [] args) { Random r = new Random() ; NumberList list = new NumberList() ; // populate the list with 10 random 2-digit ints (10 to 99) for (int i = 1 ; i <= 10 ; i++) { int next = r.nextInt(90) + 10 ; // call the "add" method of the NumberList class list.add( next ) ; } // print the aList list.printList() ; list.splitList(); } }
  • 6. Solution Please see the below code which is amended in E an F part of the quation: import java.util.ArrayList ; import java.util.Random ; /** * A class to provide practice using ArrayLists */ class NumberList { // instance var's private ArrayList aList ;// a list of Integer objects private ArrayList bigList; private int number,index; /** * Creates a NumberList object. */ public NumberList(int index, int number) { aList = new ArrayList() ; // creates an empty list bigList = new ArrayList(); bigList.add(index,number);//it accepts index and number } /** * Adds a number to the list. * @param number the number to be added to the list */ public void add(int number) { aList.add(number) ; // calls add method of ArrayList class } /**
  • 7. * Prints the numbers stored in aList. */ public void printList() { System.out.println( "The numbers on the list: " ) ; // for each number on the list, from 1st to last... for (int i = 0 ; i < aList.size() ; i++) { int number = aList.get(i) ; // get the number System.out.print(number + " ") ; // print it } System.out.println(" ") ; } /** * Prints the numbers stored in aList, in reverse order. */ public void printReversed() { System.out.println( "The numbers on the list, in reverse order: " ) ; for (int i = aList.size()-1 ; i >= 0 ; i--) { int number = aList.get(i) ; // get the number System.out.print(number + " ") ; // print it } System.out.println(" ") ; } /** * Prints every other number stored in aList, starting with the first one. */ public void printEveryOther() {
  • 8. System.out.println( "Starting with the first, every other number: " ) ; for (int i = 0 ; i < aList.size() ; i++, i++) { int number = aList.get(i) ; // get the number System.out.print(number + " ") ; // print it } System.out.println(" ") ; } /** * Prints all the even-numbered ints stored in aList. */ public void printEvens() { System.out.println( "The even numbers on the list: " ) ; for (int i = 0 ; i < aList.size() ; i++) { if(aList.get(i) % 2 == 0){ int number = aList.get(i) ; // get the number System.out.print(number + " ") ; // print it } } System.out.println(" ") ; } public void insertNum(int index, int number)throws IndexOutOfBoundsException{ index=this.index; number=this.number; bigList.add(index,number); int size=bigList.size(); if(index>size) { System.out.println("the index at which to insert the new Integer --- is illegal");
  • 9. }else{ System.out.println("not found"); } } /** * Copies all ints that are 50 or greater from aList to bigList. */ public void splitList() { System.out.println( "The numbers on the big list, greater than 50: " ) ; for (int i = 0 ; i < aList.size() ; i++) { if(aList.get(i) >= 50){ int c = aList.get(i); bigList.add(c); // get the number System.out.print(c + " ") ; // print it } } System.out.println(" ") ; } } -------------------------------------------------------------------------------------------------------- import java.util.Random; public class NumberListTester { public static void main (String [] args) { Random r = new Random() ; NumberList list = new NumberList(4,37) ;
  • 10. // populate the list with 10 random 2-digit ints (10 to 99) for (int i = 1 ; i <= 10 ; i++) { int next = r.nextInt(90) + 10 ; // call the "add" method of the NumberList class list.add( next ) ; } // print the aList list.printList() ; list.splitList(); } }