Question: You must be fluent with everything in this program for the AP exam. Examine and study each method carefully! WRITE 3 METHODS: 1. sumArrayList() This

You must be fluent with everything in this program for the AP exam. Examine and study each method carefully! WRITE 3 METHODS: 1. sumArrayList() This method must return int, which is the sum each int in 1-D array. You must use a 'normal' for loop (counting loop). Use a wrapper Integer with number at each index. It should take about 6 lines of code, at most. 2. sumArrayListEnhancedForLoop() This method returns an int by summing each int in 1-D array, using and 'enhanced' for loop, not a counting loop. Cast from Integers with (int). It should take about 3 lines of code, at most. 3. 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: -- createArrayListOfIntegers(int,int) --> Returns a 1-D ArrayList, containing int objects -- printArrayList(int,int) --> Prints; Returns nothing -- sumArrayList(ArrayList) --> Returns int: the sum of all array numbers -- sumArrayListEnhancedForLoop(ArrayList) --> Returns int: sum of all array numbers -- pctNegNumbers(ArrayList) --> Return percent of negative number in 1- D array SAMPLE OUTPUT (for 50 numbers, 10 per line): 1-D Array of 50 numbers, formatted in 10 columns: 1 2 3 4 5 6 7 8 9 10 |----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 1 | -714 232 -539 246 805 -710 -193 519 411 -728 11 | -45 -162 859 664 314 -739 21 -436 234 988 21 | 93 710 -877 -332 306 972 -453 -134 604 246 31 | 302 700 -963 -577 462 175 111 724 -562 764 41 | -613 -482 -253 218 -791 611 580 -237 -758 -460 Sum of 50 numbers using Normal 'for' Loop: 1113 Sum of 50 numbers using Enhanced 'for' Loop: 1113 50% DESIRED Neg Numbers 46% ACTUAL Neg Numbers */ import java.util.ArrayList; // =================================================================================== ===== // ====== This runner class contains the main method to actually run the program ======== // =================================================================================== ===== public class Ch85_ArrayList_IntegerObjects_STU7a // Runner class { public static void main(String[] args) // ---- BEGIN JAVA PROGRAM ---- { int numNums = 50; // choose how many numbers to create int numPctNeg = 50; // target % neg numbers to have (00-100) //# int numsPerLine = 10; // 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.createArrayListOfIntegers(numNums,numPctNeg); //# Tom.printArrayList(numbers,numsPerLine); // print 1-D array in chosen format (# per line) System.out.println(" Sum of "+numNums+" numbers using Normal 'for' Loop: " +Tom.sumArrayList(numbers)); // sum with normal for loop System.out.println("Sum of "+numNums+" numbers using Enhanced 'for' Loop: " +Tom.sumArrayListEnhancedForLoop(numbers)); // sum with enhanced for loop! System.out.println(" "+numPctNeg+"% DESIRED Neg Numbers"); System.out.println(Tom.pctNegNumbers(numbers)+"% ACTUAL Neg Numbers"); } // ---- END JAVA PROGRAM ---- } // =================================================================================== ===== // ====== This class contains the static methods to create & manipulate ArrayLists ======== // =================================================================================== ===== class Tom // This class uses only static methods { // Demonstrates use of ArrayList, etc. // ----------------------------------------------------------------------- // --- Returns an ArrayList of each int stored as object Integer --- // ----------------------------------------------------------------------- public static ArrayList createArrayListOfIntegers(int numNums,int numPctNeg) { // Local vars have same name as instance cars ArrayList numbers = new ArrayList();// create new ArrayList of Integers for (int i=0; i

// --------------------------------------------------------------------- // --- Returns nothing: prints out array of numbers row by row --- // --------------------------------------------------------------------- public static void printArrayList(ArrayList AbeLincoln,int numsPerLine) { String pad=" "; // use big leading spaces padding for prepending pad string String header; // declare the ref var, header, to be instantiated later... String header1=" "; // use leading row's spaces to format header1's start String header2=" |"; // use vertical line (pipe) for each line start String lines=""; // lines of the array are a single big string to be printed! for (int i=0; i GeorgeW)

{ /* >>>>>>>>>> INSERT CODE HERE <<<<<<<<< Question #1 */ return 0; } // --------------------------------------------------------------------------------- // --- Returns an int: sum each int in 1-D array, using 'enhanced' for loop --- // --- cast from Integers with (int) --- // --------------------------------------------------------------------------------- public static int sumArrayListEnhancedForLoop(ArrayList ThomasJ) { /* >>>>>>>>>> INSERT CODE HERE <<<<<<<<< Question #2 */ return 0; } //# -----------------------------------------------------------------------//# //# --- Returns an int: percent negative numbers (rounded) in array ---//# //# -----------------------------------------------------------------------//# public static int pctNegNumbers(ArrayList ThomasJ) //# { //# /* >>>>>>>>>> INSERT CODE HERE <<<<<<<<< Question #3 */ return 0; } //# }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!