 |
|
 |
 |
  | Arrays provide a way to store multiple values under a single variable name.
|
 |
 |
 |
 |
  | A given array can only store one type of variable.
|
 |
 |
 |
 |
  | To create an array named myIntArray of one hundred ints: int [] myIntArray = new int[100];
|
 |
 |
 |
 |
  | To create an array called names of 10 Strings: String [] names = new String[10];
|
 |
 |
 |
 |
  | To create an array called values of 25 doubles: double [] values = new double[25];
|
 |
 |
 |
 |
  | Every array has a length variable that identifies how many values it can store.
|
 |
 |
 |
 |
  | The following program fills an int array with the squares of integers and prints them to the screen.
|
 |
 |
 |
 |
  | /** * Demo of storing the squares of ints * in an array. * * @author Chuck Iverson * @version 10/6/09 */ public class Squares { public static void main(String [] args) { int [] squares = new int[10]; // allocate memory to hold 10 ints for (int i = 0; i < squares.length; ++i) { squares[i] = i*i; } print(squares); } public static void print(int [] myArray) { for (int i = myArray.length-1; i >= 0; --i) { System.out.println(myArray[i]); } } }
|
 |
 |
 |
 |
  | The following program asks the user for the number of test scores to enter, creates arrays for the names of the students and the values of the test scores, then prints the names and corresponding test scores and the max, min and average of the test scores.
|
 |
 |
 |
 |
  | /** * Get test scores from the user and calculate * the max, min and average test score. * * @author Chuck Iverson * @version 10/6/09 */
import java.util.Scanner;
public class TestScores { public static void main(String [] args) { Scanner keyboard = new Scanner(System.in); System.out.print("How many test scores do you want to enter? "); int count = keyboard.nextInt(); keyboard.nextLine(); // remove leftover newline from ENTER int [] scores = new int[count]; String [] names = new String[count]; for (int i = 0; i < count; ++i) { System.out.print("Enter name of student #" + (i+1) + ": "); names[i] = keyboard.nextLine(); System.out.print("Enter score #" + (i+1) + ": "); scores[i] = keyboard.nextInt(); keyboard.nextLine(); // remove leftover newline from ENTER } print(scores, names); int max = findMax(scores); int min = findMin(scores); double average = findAverage(scores); System.out.println("Max value: " + max); System.out.println("Min value: " + min); System.out.println("Average value: " + average); } public static void print(int [] a, String [] b) { for (int i = 0; i < a.length; ++i) { System.out.println(pad(b[i],-20) + pad(""+a[i],10)); } } public static String pad(String s, int width) { if (width > 0) { // align the string to the right while (s.length() < width) { s = " " + s; } } else { width = -width; while (s.length() < width) { // aligh the string to the left s = s + " "; } } return s; } public static int findMax(int [] b) { int max = b[0]; for (int i = 1; i < b.length; ++i) { if (b[i] > max) max = b[i]; } return max; }
public static int findMin(int [] b) { int min = b[0]; for (int i = 1; i < b.length; ++i) { if (b[i] < min) min = b[i]; } return min; }
public static double findAverage(int [] b) { double sum = 0; for (int i = 0; i < b.length; ++i) { sum += b[i]; } return sum/b.length; } }
/* sample run
How many test scores do you want to enter? 5 Enter name of student #1: Chuck Iverson Enter score #1: 93 Enter name of student #2: Christopher Vaughan Enter score #2: 98 Enter name of student #3: Kevin Harris Enter score #3: 92 Enter name of student #4: Sebastian Grillo Enter score #4: 99 Enter name of student #5: Brian Surber Enter score #5: 95 Chuck Iverson 93 Christopher Vaughan 98 Kevin Harris 92 Sebastian Grillo 99 Brian Surber 95 Max value: 99 Min value: 92 Average value: 95.4
*/
|
 |
 |
 |
 |
  | Create a text file with Notepad or another text editor named "deposits.txt". The file should contain the following numbers, one per line:
100.00 125.00 78.92 37.55
Next, create a text file named "withdrawals.txt". The file should contain the following numbers, one per line:
29.88 110.00 27.52 50.00 12.90
The numbers in the "deposits.txt" file are the amounts of deposits that were made to a savings account during the month, and the numbers in the "withdrawals.txt" file are the amounts of withdrawals that were made during the month. Write a program that reads these numbers into two different arrays. Assume the bank account has a starting balance of $500.00, and add all the deposits to the balance and subtract all the withdrawals from the balance. The program should then print the starting balance, the total of all the deposits, the total of all the withdrawals and the final balance.
|
 |
 |
 |
 |
  | Write a program that reads a single integer and then an array of integers from a file and then displays the last index at which the single integer value occurs in the array. The program should print a message if the value is not found. For example, in the array {74, 85, 102, 99, 101, 85, 56}, the last index of 85 is 5.
|
 |
 |
 |
 |
  | Write a program containing a method called countInRange that accepts an array of integers, a minimum value and a maximum value as parameters and returns the count of how many elements from the array fall between the minimum and maximum (inclusive). For example, in the array {14, 1, 22, 17, 36, 7, -43, 5}, there are four elements whose values fall between 4 and 17.
|
 |
 |
|


 |
 |
 |