1 / 70

Chapter 11 Slides

Exposure Java. Chapter 11 Slides. Control Structures II. PowerPoint Presentation created by: Mr. John L. M. Schram. From Materials Created by Mr. Leon Schram. // Java1101.java // This program demonstrates using multiple variables in a <for> loop. public class Java1101 {

etoile
Download Presentation

Chapter 11 Slides

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exposure Java Chapter 11 Slides Control Structures II PowerPoint Presentation created by: Mr. John L. M. Schram From Materials Created by Mr. Leon Schram

  2. // Java1101.java // This program demonstrates using multiple variables in a <for> loop. public class Java1101 { public static void main(String args[]) { int p, q; for (p=1, q=5; p <= 5; p++,q--) System.out.println("p = " + p + " q = " + q); } } Java1101.java Output p = 1 q = 5 p = 2 q = 4 p = 3 q = 3 p = 4 q = 2 p = 5 q = 1

  3. // Java1102.java // This program demonstrates a practical example of using multiple // variables in a <for> loop. This program counts the number of // quarters, as well as the value of the quarters. public class Java1102 { public static void main(String args[]) { int count; double value; for (count = 1, value = 0.25; count <= 20; count++, value+=0.25) System.out.println(count + " Quarter(s) equals " + value); } } Java1102.java Output 1 Quarter(s) equals 0.25 2 Quarter(s) equals 0.5 3 Quarter(s) equals 0.75 4 Quarter(s) equals 1.0 5 Quarter(s) equals 1.25 6 Quarter(s) equals 1.5 7 Quarter(s) equals 1.75 8 Quarter(s) equals 2.0 9 Quarter(s) equals 2.25 10 Quarter(s) equals 2.5 11 Quarter(s) equals 2.75 12 Quarter(s) equals 3.0 13 Quarter(s) equals 3.25 14 Quarter(s) equals 3.5 15 Quarter(s) equals 3.75 16 Quarter(s) equals 4.0 17 Quarter(s) equals 4.25 18 Quarter(s) equals 4.5 19 Quarter(s) equals 4.75 20 Quarter(s) equals 5.0

  4. // Java1103.java // This program introduces text window input using the // InputStreamReader class found in the java.io package. import java.io.*; // Line 1 public class Java1103 { public static void main (String args[]) throws IOException // Line 2 { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); // Line 3 String name; int age; double gpa; System.out.print("Enter Name ===>> "); // Line 4 name = input.readLine(); // Line 5 System.out.print("Enter Age ===>> "); age = Integer.parseInt(input.readLine()); // Line 6 System.out.print("Enter GPA ===>> "); gpa = Double.parseDouble(input.readLine()); // Line 7 System.out.println(); System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("GPA: " + gpa); } } Java1103.java Output Enter Name ===>> Isolde Echle Enter Age ===>> 29 Enter GPA ===>> 3.785 Name: Isolde Echle Age: 29 GPA: 3.785

  5. Keyboard Text Window Input import java.io.*; // Line 1 public class Java1103 { public static void main (String args[]) throws IOException // Line 2 { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); // Line 3 String name; int age; double gpa; System.out.print("Enter Name ===>> "); // Line 4 name = input.readLine(); // Line 5 System.out.print("Enter Age ===>> "); age = Integer.parseInt(input.readLine()); // Line 6 System.out.print("Enter GPA ===>> "); gpa = Double.parseDouble(input.readLine()); // Line 7

  6. Keyboard Text Input Line 1 import java.io.*; This import statement gives access to the input/output classes of the java.io package.

  7. Keyboard Text Input Line 2 public static void main (String args[ ]) throws IOException The statement throws IOException alerts Java of possible errors. Specifically errors during input and output, which need to be ignored or thrown away.

  8. Keyboard Text Input Line 3 BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); This statement constructs the object input of the BufferedReader class. input will be in charge of handling the data that users enter at the keyboard during program execution.

  9. Keyboard Text Input Line 4 System.out.print("Enter Name ===>> "); This statement provides a prompt for the program user prior to the statement that expects user keyboard input.

  10. Keyboard Text Input Line 5 Name = input.readLine(); input is an object of the BufferedReader class. readLine is one of its methods. This statement stops program execution and waits for user input from the keyboard.

  11. Keyboard Text Input Line 6 System.out.print("Enter Age ===>> "); age = Integer.parseInt(input.readLine()); The input.readLine statement is used to stop program execution and wait for user input. This time the statement is more complex. Numeric input is requested and readLine can only digest string input. Recall that Integer.parseInt can convert string data to numeric Integer data.

  12. Keyboard Text Input Line 7 System.out.print("Enter GPA ===>> "); gpa = Double.parseDouble(input.readLine()); Line 7 presents nothing new. This is now a repetition of the keyboard prompt followed by keyboard input that is converted. In this case the string input is converted to a Double type.

  13. Keyboard Text Input Summary #1 Start by adding the Input/Output Java package: Import java.io.*; #2 Alter the main method method by using public static void main (String args[]) throws IOException #3 Define a BufferedReader object for program input BufferedReader input = new BufferedReader(newInputStreamReader(System.in)); #4 Provide a user prompt like System.out.print("Enter Age ===>> "); #5 Get user input and convert string input if necessary: age = Integer.parseInt(input.readLine());

  14. When to Use the <for> Loop The for loop is ideal for loop structures that repeat some process a known - or fixed - number of times.

  15. When to Use the <while> Loop The while loop structure needs to be used when a condition must be checked before even a single loop body is executed.

  16. When to Use the <do..while> Loop The do..while loop structure needs to be used when the loop body must be executed at least once.

  17. Initialize Your Accumulators Program loop structures frequently use variables that sum-up entered values. Such a variable is called an accumulator. Make sure that the accumulator is initialized before it is used inside the loop structure. This is one of many reasons why OOP adds reliability to programming. The use of classes with constructors can insure that all data fields in an object are properly initialized when a new object of a class is instantiated.

  18. // Java1104.java // This program computes the average for a fixed set of grades using a for loop. import java.io.*; public class Java1104 { public static void main (String args[]) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); int k; // loop counter int count; // quantity of entered grades int grade; // individually entered grade int sum = 0; // sum of entered grades double mean; // average of entered grades System.out.print("Enter grade count ===>> "); count = Integer.parseInt(input.readLine()); for (k = 1; k <= count; k++) { System.out.print("Enter grade ===>> "); grade = Integer.parseInt(input.readLine()); sum += grade; } mean = (double) sum / count; System.out.println("Average Grade: " + mean); } } Java1104.java Output How many grades will be averaged ===>> 5 Enter grade ===>> 60 Enter grade ===>> 70 Enter grade ===>> 80 Enter grade ===>> 90 Enter grade ===>> 100 Grade average: 80

  19. // Java1105.java // This program computes the average for an unknown quantity of // grades using a while loop. This example produces a logic error, which // is best seen by entering the same set of numbers. import java.io.*; public class Java1105 { public static void main (String args[]) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); int count = 0; // quantity of entered grades int grade = 0; // individually entered grade int sum = 0; // sum of entered grades double mean; // average of entered grades while (grade != -999) { System.out.print("Enter grade ===>> "); grade = Integer.parseInt(input.readLine()); count++; sum += grade; } System.out.println(); mean = (double) sum / count; System.out.println("Average Grade: " + mean); } } Java1105.java Output Enter grade ===>> 100 Enter grade ===>> 100 Enter grade ===>> 100 Enter grade ===>> 100 Enter grade ===>> 100 Enter grade ===>> -999 Average Grade: -83.16666666666667

  20. // Java1106.java // This program computes the average for an unknown quantity of grades using a while loop, and fixes the // logic problem of the previous program example with a conditional statement. import java.io.*; public class Java1106 { public static void main (String args[]) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); int count = 0; // quantity of entered grades int grade = 0; // individually entered grade int sum = 0; // sum of entered grades double mean; // average of entered grades while (grade != -999) { System.out.print("Enter grade ===>> "); grade = Integer.parseInt(input.readLine()); if (grade != -999) { count++; // counter sum += grade; // accumulator } } System.out.println(); mean = (double) sum / count; System.out.println("Average Grade: " + mean); } } Java1106.java Output Enter grade ===>> 100 Enter grade ===>> 100 Enter grade ===>> 100 Enter grade ===>> 100 Enter grade ===>> 100 Enter grade ===>> -999 Average Grade: 100.0

  21. // Java1107.java // This program computes the average with a do..while loop. import java.io.*; public class Java1107 { public static void main (String args[]) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); int count = 0; // quantity of entered grades int grade = 0; // individually entered grade int sum = 0; // sum of entered grades double mean; // average of entered grades do { System.out.print("Enter grade ===>> "); grade = Integer.parseInt(input.readLine()); if (grade != -999) { count++; // counter sum += grade; // accumulator } } while (grade != -999); System.out.println(); mean = (double) sum / count; System.out.println("Average Grade: " + mean); } } Java1107.java Output Enter grade ===>> 60 Enter grade ===>> 70 Enter grade ===>> 80 Enter grade ===>> 90 Enter grade ===>> 100 Enter grade ===>> -999 Grade average: 80

  22. <while> and <do..while> Need a Flag The while loop and do...while loop are ideally used for repetition that depends on a certain condition to be true. This is not necessarily a counting condition. The loop may exit when a certain value is entered and the loop condition becomes true. Flag is the term used in computer science to detect if a certain value is entered or altered to change the loop exit condition. The value that is entered or altered is called the Flag.

  23. // Java1108.java // This program demonstrates nested one-way selection. import java.io.*; public class Java1108 { public static void main (String args[]) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); double bonus = 500; // year-end employee bonus double sales; // yearly sales int years; // years worked with company System.out.print("Enter yearly sales ===>> "); sales = Double.parseDouble(input.readLine()); if (sales >= 250000) { System.out.println("Your bonus will be increased by $1000.00"); bonus += 1000; System.out.print("Enter years worked with the company ===>> "); years = Integer.parseInt(input.readLine()); if (years > 10) { System.out.println("Your bonus will be increased by $250.00"); bonus += 250; } } System.out.println(); System.out.println("Your year-end bonus will be: $" + bonus); } } Java1108.java Output #1 Enter your yearly sales ===>> 300000 Your bonus will be increased by $1000.00 Enter years worked with the company ===>> 15 Your bonus will be increased by $250.00 Your year-end bonus will be: 1750 Java1108.java Output #2 Enter your yearly sales ===>> 500000 Your bonus will be increased by $1000.00 Enter years worked with the company ===>> 5 Your year-end bonus will be: 1500 Java1108.java Output #3 Enter your yearly sales ===>> 100000 Your year-end bonus will be: 500

  24. // Java1109.java // This program displays an admission message based on an entered // SAT score. It also determines financial need with a nested if...else structure. import java.io.*; public class Java1109 { public static void main (String args[]) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); int sat; double income; System.out.print("Enter your SAT score ===>> "); sat = Integer.parseInt(input.readLine()); if (sat >= 1100) { System.out.println("You are admitted"); System.out.print("Enter your family income ===>> "); income = Double.parseDouble(input.readLine()); if (income <= 20000) System.out.println("You will receive financial aid"); else System.out.println("You will not receive financial aid"); } else { System.out.println("You are not admitted"); } } } Java1109.java Output #1 Enter your SAT score ===>> 1500 You are admitted Enter your family income ===>> 10000 You will receive financial aid Java1109.java Output #2 Enter your SAT score ===>> 1200 You are admitted Enter your family income ===>> 75000 You will not receive financial aid Java1108.java Output #3 Enter your SAT score ===>> 800 You are not admitted

  25. // Java1110.java // This program assigns grades 'A'..'F' based on numerical scores using multiple nested if..else statements. import java.io.*; public class Java1110 { public static void main (String args[]) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); double score; char grade; System.out.print("Enter your numerical score ===>> "); score = Double.parseDouble(input.readLine()); if (score >= 90.0) grade = 'A'; else if (score >= 80.0) grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = 'F'; System.out.println("Your grade will be: " + grade); } } Java1110.java Output #1 Enter your numerical score ===>> 100 Your grade will be: A Java1110.java Output #2 Enter your numerical score ===>> 90.5 Your grade will be: A Java1110.java Output #3 Enter your numerical score ===>> 83.756 Your grade will be: B Java1110.java Output #4 Enter your numerical score ===>> 79.999 Your grade will be: C Java1110.java Output #5 Enter your numerical score ===>> 60 Your grade will be: D Java1110.java Output #6 Enter your numerical score ===>> 59.999 Your grade will be: F

  26. Output for Java1111.java, Java1112.java and Java1113.java 1 * 11 = 11 2 * 11 = 22 3 * 11 = 33 4 * 11 = 44 5 * 11 = 55 1 * 12 = 12 2 * 12 = 24 3 * 12 = 36 4 * 12 = 48 5 * 12 = 60 1 * 13 = 13 2 * 13 = 26 3 * 13 = 39 4 * 13 = 52 5 * 13 = 65

  27. // Java1111.java // This program displays several multiplication tables using // nested <for> loop structures. public class Java1111 { public static void main(String args[]) { for (int table = 11; table <= 13; table++) { for (int k = 1; k <= 5; k++) { System.out.println(k + " * " + table + " = " + k * table); } System.out.println(); } } }

  28. // Java1112.java // This program displays several multiplication tables using // nested pre-condition <while> loop structures. public class Java1112 { public static void main(String args[]) { int k = 1; int table = 11; while (table <= 13) { while (k <= 5) { System.out.println(k + " * " + table + " = " + k * table); k++; } System.out.println(); k = 1; table++; } } }

  29. // Java1113.java // This program displays several multiplication tables using // nested post-condition <do..while> loop structures. public class Java1113 { public static void main(String args[]) { int k = 1; int table = 11; do { do { System.out.println(k + " * " + table + " = " + k * table); k++; } while (k <= 5); System.out.println(); k = 1; table++; } while (table <= 13); } }

  30. Control Structure Style The program style of control structures varies considerably. Two different styles will be shown here with <do...while>, which shows a general pattern of choosing more clarity or choosing less space.

  31. Style 1 do { do { System.out.println(k + " * " + table + " = " + k * table); k++; } while (k <= 5); System.out.println(); k = 1; table++; } while (table <= 13);

  32. Style 2 do { do { System.out.println(k + " * " + table + " = " + k * table); k++; } while (k <= 5); System.out.println(); k = 1; table++; } while (table <= 13);

  33. // Java1114.java // This program demonstrates compound decisions with the logical or ( || ) operator. import java.io.*; public class Java1114 { public static void main (String args[]) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); int education; // years of education int experience; // years of work experience System.out.print("Enter years of education ===>> "); education = Integer.parseInt(input.readLine()); System.out.print("Enter years of experience ===>> "); experience = Integer.parseInt(input.readLine()); if ((education >= 16) || (experience >= 5)) System.out.println("You are hired"); else System.out.println("You are not qualified"); } } Java1114.java Output #1 Enter years of education ===>> 17 Enter years of experience ===>> 8 You are hired Java1114.java Output #2 Enter years of education ===>> 17 Enter years of experience ===>> 2 You are hired Java1114.java Output #3 Enter years of education ===>> 13 Enter years of experience ===>> 8 You are hired Java1114.java Output #4 Enter years of education ===>> 13 Enter years of experience ===>> 2 You are not qualified

  34. // Java1115.java // This program demonstrates compound decisions with the logical and ( && ) operator. import java.io.*; public class Java1115 { public static void main (String args[]) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); int education; // years of education int experience; // years of work experience System.out.print("Enter years of education ===>> "); education = Integer.parseInt(input.readLine()); System.out.print("Enter years of experience ===>> "); experience = Integer.parseInt(input.readLine()); if ((education >= 16) && (experience >= 5)) System.out.println("You are hired"); else System.out.println("You are not qualified"); } } Java1115.java Output #1 Enter years of education ===>> 17 Enter years of experience ===>> 8 You are hired Java1115.java Output #2 Enter years of education ===>> 17 Enter years of experience ===>> 2 You are not qualified Java1115.java Output #3 Enter years of education ===>> 13 Enter years of experience ===>> 8 You are not qualified Java1115.java Output #4 Enter years of education ===>> 13 Enter years of experience ===>> 2 You are not qualified

  35. Java Logical Operators Java uses || to indicate a logical or. Java uses && to indicate a logical and. Java uses ! to indicate a logical not.

  36. // Java1116.java // This program demonstrates compound decision with a do...while loop. // The program checks for proper data entry. The <equals> String method // is used to compare the string values. import java.io.*; public class Java1116 { public static void main (String args[]) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); String gender; do { System.out.print("Enter your Gender [M/F] ===>> "); gender = input.readLine(); } while (!( gender.equals("M") || gender.equals("F") )); System.out.println("Your gender is " + gender); } } Java1116.java Output Enter your Gender [M/F] ===>> Q Enter your Gender [M/F] ===>> t Enter your Gender [M/F] ===>> 1 Enter your Gender [M/F] ===>> f Enter your Gender [M/F] ===>> m Enter your Gender [M/F] ===>> F Your gender is F

  37. // Java1117.java // This program demonstrates compound decision with a do...while loop. // The program does not work properly because of misplaced parentheses. import java.io.*; public class Java1117 { public static void main (String args[]) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); String gender; do { System.out.print("Enter your Gender [M/F] ===>> "); gender = input.readLine(); } while (!(gender.equals("M")) || gender.equals("F") ); System.out.println("Your gender is " + gender); } } Java1117.java Output Enter your Gender [M/F] ===>> D Enter your Gender [M/F] ===>> Q Enter your Gender [M/F] ===>> F Enter your Gender [M/F] ===>> M Your gender is M

  38. Watch Your Parentheses Correct while ( !( Gender.equals("M") || Gender.equals("F") ) ); Incorrect while ( !(Gender.equals("M") ) || Gender.equals("F") );

  39. // Java1118.java // This program demonstrates incorrect use of negative compound // decision structure. There will never be correct input. import java.io.*; public class Java1118 { public static void main (String args[]) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); String gender; do { System.out.print("Enter your Gender [M/F] ===>> "); gender = input.readLine(); } while (!(gender.equals("M")) || !(gender.equals("F")) ); System.out.println("Your gender is " + gender); } } Java1118.java Output Enter your gender [M/F] ===>> D Enter your gender [M/F] ===>> Q Enter your gender [M/F] ===>> F Enter your gender [M/F] ===>> M Enter your gender [M/F] ===>> f Enter your gender [M/F] ===>> m ........ Loop never exits. No value satisfies the compound condition.

  40. De Morgan’s Law not(A or B) is equivalent to not(A) and not(B) not(A and B) is equivalent to not(A) or not(B)

  41. // Java1119.java // This program demonstrates correct use of negative compound // decision structure using DeMorgan's Law. import java.io.*; public class Java1119 { public static void main (String args[]) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); String gender; do { System.out.print("Enter your Gender [M/F] ===>> "); gender = input.readLine(); } while (!(gender.equals("M")) && !(gender.equals("F")) ); System.out.println("Your gender is " + gender); } } Java1119.java Output #1 Enter your Gender [M/F] ===>> Q Enter your Gender [M/F] ===>> W Enter your Gender [M/F] ===>> M Your Gender is M Java1119.java Output #2 Enter your Gender [M/F] ===>> F Your Gender is F

  42. // Java1120.java // This program demonstrates using a boolean data type // with an input protection loop to add readability to a program. import java.io.*; public class Java1120 { public static void main (String args[]) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); String gender; boolean correct; do { System.out.print("Enter your Gender [M/F] ===>> "); gender = input.readLine(); correct = (gender.equals("M")) || (gender.equals("F")); if (!correct) System.out.println("Incorrect input; please re-enter"); } while (!correct); System.out.println(); System.out.println("Your gender is " + gender); } } Java1120.java Output #1 Enter your Gender [M/F] ===>> q Incorrect input; please re-enter Enter your Gender [M/F] ===>> Q Incorrect input; please re-enter Enter your Gender [M/F] ===>> 1 Incorrect input; please re-enter Enter your Gender [M/F] ===>> m Incorrect input; please re-enter Enter your Gender [M/F] ===>> M Your gender is M Java1120.java Output #2 Enter your Gender [M/F] ===>> F Your Gender is F

  43. // Java1121.java // This program accepts upper-case as well as lower-case. // Gender input for [M/F] by using multiple conditional statements. import java.io.*; public class Java1121 { public static void main (String args[]) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); String gender; boolean correct; do { System.out.print("Enter your Gender [M/F] ===>> "); gender = input.readLine(); correct = (gender.equals("M")) || (gender.equals("F")) || (gender.equals("m")) || (gender.equals("f")); if (!correct) System.out.println("Incorrect input; please re-enter"); } while (!correct); System.out.println(); System.out.println("Your gender is " + gender); } } Java1121.java Output #1 Enter your Gender [M/F] ===>> q Incorrect input; please re-enter Enter your Gender [M/F] ===>> Q Incorrect input; please re-enter Enter your Gender [M/F] ===>> 1 Incorrect input; please re-enter Enter your Gender [M/F] ===>> M Your gender is M Java1121.java Output #2 Enter your Gender [M/F] ===>> F Your gender is F Java1121.java Output #3 Enter your Gender [M/F] ===>> m Your gender is m Java1121.java Output #4 Enter your Gender [M/F] ===>> f Your gender is f

  44. // Java1122.java // This program shows the need for a practical compound condition // used with an input protection loop. // The program requests the user PIN, but rejects access after three tries. import java.io.*; public class Java1122 { public static void main (String args[]) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); String pin; int tries = 0; do { System.out.print("Enter your PIN ===>> "); pin = input.readLine(); tries++; } while (!pin.equals("8423") && (tries <= 3)); if (tries > 4) System.out.println("You have exceeded your PIN entries"); else System.out.println("Your PIN is accepted"); } } Java1122.java Output #1 Please enter your PIN ===>> 4325 Please enter your PIN ===>> 4326 Please enter your PIN ===>> 4327 Please enter your PIN ===>> 4328 You have exceeded your PIN entries Java1122.java Output #2 Please enter your PIN ===>> 4325 Please enter your PIN ===>> 4326 Please enter your PIN ===>> 8423 Your PIN is accepted

  45. do…while and Input Protection You will see <do..while> used frequently for input protectionloops. The post-condition loop makes sense for checking erroneous input because you want the program to enter the loop body at least one time.

  46. Short-Circuiting with and A and ( (A or B) and (B or C) and (A or C) or ((B and C) or (A and B))) This statement is false whenever the first A is false. In such a situation it is not necessary to check the remainder of the statement.

  47. Short-Circuiting with or A or ( (A or B) and (B or C) and (A or C) or ((B and C) or (A and B))) This statement is true whenever the first A is true. In such a situation it is not necessary to check the remainder of the statement.

  48. // Java1123.java // This program uses "short circuiting" but it is not noticeable at all. import java.io.*; public class Java1123 { public static void main (String args[]) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter number 1 ===>> "); int n1 = Integer.parseInt(input.readLine()); System.out.print("Enter number 2 ===>> "); int n2 = Integer.parseInt(input.readLine()); if (n1 % 2 == 0 && n2 % 2 == 0) System.out.println("Both numbers are even"); else System.out.println("Both numbers are not even"); } } Java1123.java Output #1 Enter number 1 ===>> 12 Enter number 2 ===>> 24 Both numbers are even Java1123.java Output #2 Enter number 1 ===>> 12 Enter number 2 ===>> 15 Both numbers are not even Java1123.java Output #3 Enter number 1 ===>> 15 Enter number 2 ===>> 31 Both numbers are not even

  49. The isEven Method You have seen return methods that return integers and real numbers. Method isEven returns true or false, since it is a boolean return method. The purpose of the method is to return true if the method argument number is even and false if number is odd. It also generates output so we can see that the method was called. The purpose of this method is to help explain short circuiting in the next couple program examples. public static boolean isEven(int Number) { System.out.println(); System.out.println("Calling isEven Method"); System.out.println(); if (Number % 2 == 0) return true; else return false; }

  50. // Java1124.java // This program uses "short circuiting" and uses the isEven // method to demonstrate short circuiting with logical or. import java.io.*; public class Java1124 { public static void main (String args[]) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter number 1 ===>> "); int n1 = Integer.parseInt(input.readLine()); System.out.print("Enter number 2 ===>> "); int n2 = Integer.parseInt(input.readLine()); if (isEven(n1) || isEven(n2)) System.out.println("One or more numbers are even"); else System.out.println("Neither numbers are even"); } public static boolean isEven(int number) { System.out.println(); System.out.println("Calling isEven Method"); System.out.println(); if (number % 2 == 0) return true; else return false; } } Java1124.java Output #1 Enter number 1 ===>> 12 Enter number 2 ===>> 24 Calling IsEven Method One or more numbers are even Java1124.java Output #2 Enter number 1 ===>> 12 Enter number 2 ===>> 15 Calling IsEven Method One or more numbers are even Java1124.java Output #3 Enter number 1 ===>> 15 Enter number 2 ===>> 31 Calling isEven Method Calling isEven Method Neither numbers are even

More Related