Arrays in Classes and Methods Facts about Arrays
Arrays in Classes and Methods
Facts about Arrays o Arrays can be used as instance variables in classes. o Both an indexed variable of an array and an entire array can be an argument to a method. o Methods can return an array value.
Example – Sales Associate import java. util. *; /** Class for sales associate records. */ public class Sales. Associate { private String name; private double sales; public Sales. Associate( ) { name = "No record"; sales = 0; } public Sales. Associate(String initial. Name, double initial. Sales) { set(initial. Name, initial. Sales); } public void set(String new. Name, double new. Sales) { name = new. Name; sales = new. Sales; }
Example – Sales Associate (cont’d) public void read. Input( ) { System. out. print("Enter name of sales associate: "); Scanner keyboard = new Scanner(System. in); name = keyboard. next. Line( ); } System. out. print("Enter associate’s sales: $"); sales = keyboard. next. Double( ); public void write. Output( ) { System. out. println("Sales associate: " + name); System. out. println("Sales: $" + sales); } public String get. Name( ) { return name; } } public double get. Sales( ) { return sales; }
Example – Sales Reporter import java. util. *; /** Program to generate sales report. */ public class Sales. Reporter { private double highest; private double average; private Sales. Associate[] record; //The array object is //created in get. Figures. private int number. Of. Associates; //Same as record. length public void get. Figures( ) { Scanner keyboard = new Scanner(System. in); System. out. println("Enter number of sales associates: "); number. Of. Associates = keyboard. next. Int( ); record = new Sales. Associate[number. Of. Associates ]; int i; for (i = 0; i < number. Of. Associates; i++) { record[i] = new Sales. Associate( ); System. out. println("Enter data for associate " + (i + 1)); record[i]. read. Input( ); System. out. println( ); } }
Example – Sales Reporter (cont’d) /** Computes the average and highest sales figures. Precondition: There is at least one sales. Associate. */ public void update( ) { int i; double next. Sales = record[0]. get. Sales( ); highest = next. Sales; double sum = next. Sales; for (i = 1; i < number. Of. Associates; i++) { next. Sales = record[i]. get. Sales( ); sum = sum + next. Sales; if (next. Sales > highest) highest = next. Sales; //highest sales figure so far. } average = sum/number. Of. Associates; }
Example – Sales Reporter (cont’d) /** Displays sales report on console screen. */ public void display. Results( ) { System. out. println("Average sales per associate is $" + average); System. out. println("The highest sales figure is $" + highest); System. out. println( ); int i; System. out. println("The following had the highest sales: "); for (i = 0; i < number. Of. Associates; i++) { double next. Sales = record[i]. get. Sales( ); if (next. Sales == highest) { record[i]. write. Output( ); System. out. println("$" + (next. Sales - average) + " above the average. "); System. out. println( ); } } System. out. println("The rest performed as follows: "); for (i = 0; i < number. Of. Associates; i++) { double next. Sales = record[i]. get. Sales( ); if (record[i]. get. Sales( ) != highest) { record[i]. write. Output( ); if (next. Sales >= average) System. out. println("$" + (next. Sales - average) + " above the average. "); else System. out. println("$" + (average - next. Sales) + " below the average. "); System. out. println( ); } } }
Example – Sales Reporter (cont’d) } public static void main(String[ ] args) { Sales. Reporter clerk = new Sales. Reporter( ); clerk. get. Figures( ); clerk. update( ); clerk. display. Results( ); }
Indexed Variables as Method Arguments o An indexed variable can be an argument to a method in exactly the same way that any other variable of the array’s base type can be an argument. o For example, possible. Average = average(first. Score, next. Score[i]);
An Example import java. util. *; /** A program to demonstrate the use of indexed variables as arguments. */ public class Argument. Demo { public static void main(String[] args) { Scanner keyboard = new Scanner(System. in); System. out. println("Enter your score on exam 1: "); int first. Score = keyboard. next. Int( ); int[] next. Score = new int[3]; int i; double possible. Average; for (i = 0; i < next. Score. length; i++) next. Score[i] = 80 + 10*i; for (i = 0; i < next. Score. length; i++) { possible. Average = average(first. Score, next. Score[i]); System. out. println("If your score on exam 2 is " + next. Score[i]); System. out. println("your average will be " + possible. Average); } } public static double average(int n 1, int n 2) { return (n 1 + n 2)/2. 0; } }
Entire Arrays as Method Arguments o An entire array can also be used a single argument to a method. o The way you specify an array parameter in a method definition is similar to the way you declare an array, e. g. , public class Sample. Class { public static void increment. Array. By 2(double[] a) { int i; for (i = 0; i < a. length; i++) a[i] = a[i] +2; } }
Entire Arrays as Method Arguments (cont’d) o When you give an entire array as an argument to a method, no square brackets are used, e. g. , Sample. Class. increment. Array. By 2(a); where a has been declared by double[] a = new double[10]; q A method can change the values in an array. q The same array parameter can be replaced with array arguments of different lengths.
Arguments for the Method main o The heading for the main method of a program is as follows: public static void main(String[] args) q The part String[] args make it look as though args is a parameter for an array with base type String. q In fact, this is exactly the case, however, a default array of strings is automatically provided as a default argument to main when you run your program.
Arguments for the Method main (cont’d) o It is possible to provide additional “string arguments” when you run a program. o This is normally done by running the program from the command line of the operating system like: java Test. Program Sally Smith q This will set args[0] to “Sally” and args[1] to “Smith”.
Arguments for the Method main (cont’d) o These two indexed variables can be used in the method main, e. g. , public class Test. Program { public static void main(String[] args) { System. out. println(“Hello “ + args[0] + “ “ + args[1]); } }
Arguments for the Method main (cont’d) o The program out would then be: Hello Sally Smith q Since the identifier args is a parameter, you can really use any name you like, however, it is traditional to use the name args for this parameter. q This argument to main is an array of strings. If you want numbers, you must convert the string representations to actual numbers.
Use of = and == with Arrays o Since arrays are objects, the assignment operator = and the equality operator == behave the same way as they do with other kinds of objects. o The assignment statement b = a; where a and b are arrays, gives the variable b the same memory address as the variable a. o That is, a and b are really just different names for the same array.
Use of = and == with Arrays (cont’d) o The statements b = a; followed by a[2] = 2001; changes the value of b[2] to 2001 as well as that of a[2]. o It is usually best not to use the assignment operator = (or the equality operator ==) with arrays. o If you want to create an array b with the same values as a, you should write: int i; for (i= 0; i< a. length; i++) b[i] = a[i];
Equality for Arrays /** This is just a demonstration program to see how equals and == work. */ public class Test. Equals { public static void main(String[] args) { int[] a = new int[3]; int[] b = new int[3]; int i; for (i = 0; i < a. length; i++) a[i] = i; for (i = 0; i < b. length; i++) b[i] = i; if (b == a) System. out. println("Equal by ==. "); else System. out. println("Not equal by ==. "); if (equals(b, a)) System. out. println("Equal by the equals method. "); else System. out. println("Not equal by the equals method. "); }
Equality for Arrays (cont’d) public static boolean equals(int[] a, int[] b) { boolean match; if (a. length != b. length) match = false; else { match = true; //tentatively int i = 0; while (match && (i < a. length)) { if (a[i] != b[i]) match = false; i++; } } return match; } }
Methods that Return Arrays o In Java, a method may return an array. o Example: Public static char[] vowels() { char[] new. Array = new char[5]; new. Array[0] = ‘a’; new. Array[1] = ‘e’; new. Array[2] = ‘i’; new. Array[3] = ‘o’; new. Array[4] = ‘u’; return new. Array; }
- Slides: 21