Unformatted text preview:

datatype datatype identifier dataype dataype identifier 0 3 1 4 2 double scores OR double scores identifier new double size scores new double 10 double scores new double 10 1 2 0 1 2 3 4 5 6 7 8 9 5 scores 0 scores 2 3rd element scores i 1 double scores new double 10 double avg avg sum 0 0 1 2 3 4 5 6 7 8 9 for int i 0 i 10 i System out print System out print Enter Score Score scores i scores i in nextDouble in nextDouble sum scores i scores i avg sum 10 scores 2 for int i 0 i 10 i System out print Score i System out print score i System out print score i System out println score i System out println score i avg avg Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Input score score score score score score score score score score 22 24 90 88 75 95 65 80 92 69 Score Score Score Score Score Score Score Score Score Score Output 0 22 0 1 24 0 2 90 0 3 88 0 4 75 0 5 95 0 6 65 0 7 80 0 8 92 0 9 69 0 48 0 46 0 20 0 18 0 5 0 25 0 5 0 10 0 22 0 1 0 6 7 1 8 3 2 9 7 for int i 0 i scores length scores length i System out print Score i System out print score i System out print score i System out println score i System out println score i avg avg String monthName new String 12 monthName 0 Jan Jan monthName 1 Feb Feb monthName 2 Mar Mar monthName 3 Apr Apr monthName 4 May May monthName 5 Jun Jun monthName 6 Jul Jul monthName 7 Aug Aug monthName 8 Sep Sep monthName 9 Oct Oct monthName 10 Nov Nov monthName 11 Dec Dec 4 String monthName Jan Jan Feb Feb Mar Mar Apr Apr May May Jun Jun Jul Jul Aug Aug Sep Sep Oct Oct Nov Nov Dec Dec 6 9 5 int size double scores System out println Enter number of scores to enter size in nextInt in nextInt scores new double size double size 3 3 BankAccount BankAccount ba ba System out print System out print Enter number of accounts int numAccounts in nextInt in nextInt 9 ba new BankAccount numAccounts BankAccount numAccounts ba 0 deposit 500 9 1 4 0 1 2 3 4 5 6 7 8 9 numAccounts 6 1 BankAccount BankAccount ba ba System out print System out print Enter number of accounts int numAccounts in nextInt in nextInt ba new BankAccount numAccounts BankAccount numAccounts ba 0 new BankAccount BankAccount ba 0 deposit 500 0 1 2 3 4 5 6 7 8 9 4 012 1 1 1 Person person person new Person 20 1 1 person 0 setName Ms Latte Latte person 0 setAge 20 person 0 setGender F 1 9 1 Chapter 10 Sample Program Illustrate the processing of an array of Person objects File Ch10ProcessPersonArray java 0 A A A 1 class Ch10ProcessPersonArray public static void main String args Person person declare the person array person new Person 5 and then create it 5 Create person Array String name inpStr int age char gender for int i 0 i person length i read in data values System out println System out println Enter info for a person person Compute Average Age float sum 0 averageAge for int i 0 i person length i sum person i getAge averageAge sum float person length System out println Average age averageAge name in nextLine in nextLine age in nextInt in nextInt inpStr in next charAt 0 Find the youngest and oldest person create a new Person and assign values person i person i new Person Person youngest points to the youngest person oldest points to the oldest person person i setName name person i setAge age person i setGender person i setGender gender youngest oldest person 0 person 0 for int i 1 i person length i if person i getAge youngest getAge found a younger person youngest person i else if person i getAge oldest getAge found an older person oldest person i System out println Oldest System out println Oldest oldest getName is oldest getAge years old 03 1 System out println Youngest System out println Youngest youngest getName youngest getName is youngest getAge youngest getAge years old B 42 1 tms x tms x null 8 1 6 2 1 4 15 for 6 A double scores Get scores from user double data double sum 0 for int int i 0 i data length data length i double e data i data i sum sum e double avg 0 for int i 0 i scores length scores length i avg scores i scores i avg avg scores length scores length double scores Get scores from user double avg 0 for int i 0 i scores length scores length i avg scores i scores i avg avg scores length scores length int count 0 for int i 0 i scores length scores length i if scores i if scores i avg avg count double scores Get scores from file double search Get value to search from user boolean found false for int j 0 j scores length scores length j if Math abs scores j search if Math abs scores j search 1E1E 14 found true 7 7 double max scores 0 double min scores 0 for int i 1 i scores length scores length i if scores i if scores i max max scores i scores i else if scores i if scores i min min scores i scores i public class Bank public int count double atLeast atLeast int matches 0 for int int j 0 j accounts length accounts length j if accounts j getBalance atLeast accounts j getBalance atLeast matches Found a match return matches private BankAccount BankAccount accounts public class Bank public BankAccount find int accountNumber accountNumber for int int j 0 j accounts length accounts length j if accounts j getAccountNumber accounts j getAccountNumber accountNumber accountNumber Found a match return accounts j accounts j return null No match in the array list public BankAccount maximum if accounts length accounts length 0 return null BankAccount largestYet accounts 0 for int int i 1 i accounts length accounts length i BankAccount a accounts i accounts i if a getBalance largestYet getBalance a getBalance largestYet getBalance largestYet a return largestYet largestYet 06 7 3 4 5 89 int int scores scores new int numStudents numTests int numStudents numTests 8 0689 0 32 5 double payScaleTable double payScaleTable payScaleTable new double 4 5 0689 payScaleTable new double 4 5 B payScaleTable new double 4 payScaleTable 0 new double 5 payScaleTable 1 new double 5 payScaleTable 2 new double 5 4 0 2 9 0689 0689 payScaleTable 1 length payScaleTable length 8 8 B 8 5 1 5 1 1 5 C double data fill array double prices 2 new double 10 data double prices double data clone data clone Note clone returns …


View Full Document
Download Chapter 8
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 8 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 8 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?