Levels Tought:
Elementary,Middle School,High School,College,University,PHD
Teaching Since: | Apr 2017 |
Last Sign in: | 12 Weeks Ago, 5 Days Ago |
Questions Answered: | 4870 |
Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Create a Java program that reads 10 numbers from the console (Scanner input = new Scanner(System.in);)
Store the numbers as Floats in the array.
Create static methods to perform the following actions on the array and returns the result.
Â
Â
Create a Java program that reads 10 numbers from the console
(Scanner input = new Scanner(System.in);)
Store the numbers as Floats in the array.
Create static methods to perform the following actions on the
array and returns the result.
1. Add all the items in the array and return the result. Name this method "add"
2. Subtract all the items in the array and return the result. Name
this method "sub
3. Multiply all the items in the array and return the result. Name
this method "multiply"
4. Divide all the items in the array and return the result. Name this
method "divide" import java.util.ArrayList;
import java.util.Random; public class Driver { public static void main(String args) { // Create an array variable
int arrayVar; // Current value is null // Instantiate the array
arrayVar = new int[10000]; // Fill the array with data from a random object
fillArray( arrayVar ); // Add an item to the array
arrayVar = addItemToArray( arrayVar ); // Remove an item from the array
arrayVar = removeItemFromArray( arrayVar ); printArray( arrayVar ); // Performance test between Arrays and ArrayLists
System.out.print( "\nrunning...");
long startTime = System.nanoTime();
for( int i = 0; i < 1000000; i++ ) {
addItemToArray( arrayVar );
}
long endTime = System.nanoTime();
System.out.println( "done"); System.out.println( ( endTime - startTime ) / 1000 / 1000 / 1000 + " seconds"); //
seconds (we hope) // Create an arraylist
ArrayList arrayList; // Instantiate an arraylist
arrayList = new ArrayList(); // Fill the arraylist
for( int i = 0; i < 10000; i++ ) {
arrayList.add(random.nextInt());
} // Test the performance by adding data
// Performance test between Arrays and ArrayLists System.out.print( "\nrunning..."); startTime = System.nanoTime();
for( int i = 0; i < 1000000; i++ ) {
arrayList.add(random.nextInt());
}
endTime = System.nanoTime();
System.out.println( "done"); System.out.println( ( endTime - startTime ) / 1000 / 1000 / 1000 + " seconds"); //
seconds (we hope) } private static int removeItemFromArray(int arrayVar) { int length = arrayVar.length;
int newArray = new int[length-1];
int newArrayLength = newArray.length; for( int i = 0; i < newArrayLength; i++ ) {
newArray[i] = arrayVar[i];
} return newArray;
} static Random random = new Random( 145646 ); private static int addItemToArray(int arrayVar) { // Efficient Way
/*
int newArray = new int[ arrayVar.length ];
for( int i = 0; i < arrayVar.length; i++ ) {
newArray[i] = arrayVar[i];
}
newArray[arrayVar.length] = random.nextInt();
return newArray;
*/ // This is the class way
int length = arrayVar.length; int newArray = new int[length+1]; for( int i = 0; i < length; i++ ) {
newArray[i] = arrayVar[i];
} newArray[newArray.length-1] = random.nextInt(); return newArray;
} private static void fillArray(int arrayVar) {
int length = arrayVar.length; Random random = new Random( 465454568 );
for( int i = 0; i < length; i++ ) {
arrayVar[i] = random.nextInt();
} } private static void printArray( int arrayVar ) {
int length = arrayVar.length;
for( int i = 0; i < length; i++ ) {
System.out.print( arrayVar[i] );
System.out.print(",");
} } }
-----------