PEDRIGA /* * To change this license header, choose License Headers in Project Properties. String [] deptName = {"Accounting", "Human Resources", "Sales"}; String dept; * To change this template file, choose Tools | Templates * and open the template in the editor. */ package searchlist; int x; boolean deptWasFound = false; dept = JOptionPane.showInputDialog(null, "Enter a department name"); for (x=0; x<deptName.length; ++x) if(dept.equals(deptName[x])) /** deptWasFound = true; * if(deptWasFound) * @author Geneveve JOptionPane.showMessageDialog(null, dept + "was found in the list"); */ import javax.swing.*; else public class SearchList JOptionPane.showMessageDialog(null, dept + "was not found in the list"); { } /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here } //Output of SearchList //Source Code of Figure 8-5 The FindPrice application that accesses information in parallel arrays /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package findprice; boolean validItem = false; strItem = JOptionPane.showInputDialog(null, "Enter the item number you want to order"); itemOrdered = Integer.parseInt(strItem); for(int x=0; x<NUMBER_OF_ITEMS; ++x) /** { * * @author Geneveve if(itemOrdered==validValues[x]) */ { import javax.swing.*; public class FindPrice validItem = true; { itemPrice = prices[x]; } /** * @param args the command line arguments } */ if(validItem) public static void main(String[] args) { // TODO code application logic here JOptionPane.showMessageDialog(null, "The price for item" + itemOrdered + " is $" + itemPrice); else final int NUMBER_OF_ITEMS = 10; JOptionPane.showMessageDialog(null, "Sorry invalid item entered"); int[] validValues = {101, 108, 201, 213, 266, 304, 311, 409, 411, 412}; double[] prices = {0.29, 1.23, 3.50, 0.69, 6.79, 3.19, 0.99, 0.89, 1.26, 8.00}; String strItem; int itemOrdered; double itemPrice = 0.0; //Output of FindPrice } } //Source Code of Figure 8-9 The FindDiscount Class final int NUM_RANGES = 5; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates int[] discountRangeLimit = { 1, 13, 50, 100, 200}; double[] discountRate = {0.00, 0.10, 0.14, 0.18, 0.20}; double customerDiscount; * and open the template in the editor. String strNumOrdered; */ int numOrdered; package finddiscount; int sub = NUM_RANGES - 1; strNumOrdered = JOptionPane.showInputDialog(null, "How many items are ordered?"); /** * * @author Geneveve numOrdered = Integer.parseInt(strNumOrdered); while(sub >= 0 && numOrdered < discountRangeLimit[sub]) */ --sub; import javax.swing.*; customerDiscount = discountRate[sub]; public class FindDiscount JOptionPane.showMessageDialog(null, "Discount rate for " + numOrdered + " item is " + customerDiscount ); { /** } * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here } //Output of FindDiscount //Source Code of Figure 8-13 The PassArray Class System.out.print(" " + someNums[x] ); /* System.out.println(); * To change this license header, choose License Headers in Project Properties. methodGetsArray(someNums); System.out.print("At end of main: "); * To change this template file, choose Tools | Templates for(x = 0; x < NUM_ELEMENTS; ++x) System.out.print(" " + someNums[x]); * and open the template in the editor. System.out.println(); */ } package passarray; public static void methodGetsArray(int[] arr) { /** int x; * * @author Geneveve System.out.print("At start of method arr holds: "); */ for(x = 0; x< arr.length; ++x) public class PassArray System.out.print(" " + arr[x] ); { System.out.println(); for(x = 0; x < arr.length; ++x) /** * @param args the command line arguments */ arr[x] = 888; System.out.print(" and at end of method arr holds: "); public static void main(String[] args) for(x = 0; x < arr.length; ++x) { System.out.print(" " + arr[x] ); // TODO code application logic here System.out.println(); final int NUM_ELEMENTS = 4; } int[] someNums = {5, 10, 15, 20}; int x; System.out.print("At start of main: "); for(x = 0; x < NUM_ELEMENTS; ++x) } //Output of PassArray