Question: / * APCSA Ch 8 5 ex 9 9 : ArrayList of Integer objects STU by Burkham, v . 8 a 2 9 jan 2
APCSA Ch ex: ArrayList of Integer objects STU
by Burkham, va janfebfebjanfeb
REQUIRED:
You must be fluent with everything in this program for the AP exam.
Examine and study each method carefully!
WRITE METHODS:
sumArrayList
This method must return int, which is the sum each int in D array.
You must use a 'normal' for loop counting loop
Use a wrapper Integer with number at each index.
It should take about lines of code, at most.
sumArrayListEnhancedForLoop
This method returns an int by summing each int in D array, using
and 'enhanced' for loop, not a counting loop.
Cast from Integers with int
It should take about lines of code, at most.
pctNegNumbers
This method returns an int, the percent negative numbers rounded in array.
Use an Enhanced For Loop not a counting loop!
Round the result to an integer no decimal places using int cast
of a double.
This program demonstrates:
ARRAYLIST
how values stored into an object
how objects must first convert to objects
ArrayList return
ArrayList add method
use of ArrayList size method
STRING
use of String length method
String concatenation
complex String building
use of both substring method contexts overloads
creation and use of literal String object inside then calling methods
WRAPPER
how the class called a "wrapper" class is used
LOOPING
Standard & Enhanced
CLASSES
Class used only for doing things methods 'static'
No instance anything
Pure Static: No Constructor, No PIV, No V only static methods
METHODS
how static methods are used
how static methods are called from another class
void return
int return
MATH & OTHER
modulo division to print variable quantities of numbers per line
use of Math.random to create three digit integers
use of &&
print formatting fixed length strings for columns of numbers
use of tab, t
use of new line,
TOM CLASS
STATIC METHODS used:
createArrayListOfIntegersintint Returns a D ArrayList, containing int objects
printArrayListintint Prints; Returns nothing
sumArrayListArrayList Returns int: the sum of all array numbers
sumArrayListEnhancedForLoopArrayList Returns int: sum of all array numbers
pctNegNumbersArrayList Return percent of negative number in D array
SAMPLE OUTPUT for numbers, per line:
D Array of numbers, formatted in columns:
Sum of numbers using Normal 'for' Loop:
Sum of numbers using Enhanced 'for' Loop:
DESIRED Neg Numbers
ACTUAL Neg Numbers
import java.util.ArrayList;
This runner class contains the main method to actually run the program
public class ChArrayListIntegerObjectsSTUa Runner class
public static void mainString args BEGIN JAVA PROGRAM
int numNums ; choose how many numbers to create
int numPctNeg ; target neg numbers to have #
int numsPerLine ; how many numbers to print per line
# SOURCE OF: warning: unchecked unchecked conversion
# Because ArrayList was created in a class's method, and returned as reference warnings are OK
# Java 'thinks' correctly it could cause an error in other circumstances, such as using
# Double instead of Integer in the called method createArrayListOfIntegers
ArrayList numbers Tom.createArrayListOfIntegersnumNumsnumPctNeg; #
Tom.printArrayListnumbersnumsPerLine; print D array in chosen format # per line
System.out.println
Sum of numNums numbers using Normal 'for' Loop:
Tom.sumArrayListnumbers; sum with normal for loop
System.out.printlnSum of numNums numbers using Enhanced 'for' Loo
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
