You are on page 1of 9

Example 1.

1
if (radius >= 0) { area = radius * radius * PI; System.out.println("The area for the circle of radius " + radius + "is " + area); } The flowchart as shown
Boolean Expression true Statement(s) false false

(radius >= 0)

true area = radius * radius * PI; System.out.println("The area for the circle of " + "radius " + radius + " is " + area);

(A)

(B)

Example 1.4
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'; 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';

Equivalent

Dangling-else Problem

The Java compiler always associate an else with the immediately preceding if unless told to do otherwise by the placement of braces ({}). This behavior can led to what is referred to as the dangling-else problem. For example:

1

if (i > j) if (i > k) System. To force the else clause to match the first if clause.println("A"). int k = 3. int j = 2.println("A").int i = 1.println("B").out.out. (B ) Example 1.out. int k = 3.out. (A) Equivalent int i = 1. int k = 3. else System. if (i > j) if (i > k) System. int j = 2. you must add a pair of braces: int i = 1.println("B"). 2 . if (i > j) { if (i > k) System. This statement prints B.out. } else System.println("A").out. else System.5 Nothing is printed from the preceding statement. int j = 2.println("B").

out. 3 . if (x > 0) y = 1 else y = -1.out. and if (num % 2 == 0) System.8 System.println(studentGrade >= 60 ? "Passed" : "Failed" ).out. is equivalent to System.println(num + “is odd”). also.println(num + “is even”).println( (num % 2 == 0)? num + “is even” : num + “is odd”).Example 1. else System. is equivalent to y = (x > 0) ? 1 : -1.out.

JOptionPane.Example 1.swing.2 Input". "Enter an int value.parseInt(dataString). while (data != 0) { sum += data. // Keep reading data until the input is 0 int sum = 0. \nthe program exits if the input is 0". JOptionPane. JOptionPane. "Example 1.INFORMATION_MESSAGE). } JOptionPane. The program import javax.QUESTION_MESSAGE).2 Input". \nthe program exits if the input is 0".10: Using while Loops Write a program that reads and calculates the sum of an unspecified number of integers.2 Output". "Enter an int value. "Example 1.QUESTION_MESSAGE). data = Integer. // Read the next data dataString = JOptionPane. } } The program execution: 4 . int data = Integer. The input 0 signifies the end of the input.showMessageDialog(null.showInputDialog(null. public class TestWhile { /** Main method */ public static void main(String[] args) { // Read an initial data String dataString = JOptionPane. "The sum is " + sum. JOptionPane.showInputDialog(null.parseInt(dataString). "Example 1.

"The sum is " + sum. sum += data. \nthe program exits if the input is 0". int sum = 0. } } 5 .parseInt(dataString). JOptionPane. JOptioPane.QUESTION_MESSAGE). JOptionPane."Enter an int value.10 as follows: //TestDo. // Keep reading data until the input is 0 do { // Read the next data String dataString = JOptionPane.Example 1.INFORMATION_MESSAGE).showInputDialog(null. } while (data != 0).JOptionPane.11 Rewrite example 1."TestDo".exit(0)."TestDo".java: Test the do-while loop import javax. data = Integer.showMessageDialog(null.swing. System. public class TestDo { /** Main method */ public static void main(String[] args) { int data.

0.99.0.01f) sum += i.02 + 0.showMessageDialog(null.01f.01 and ends with 1.JOptionPane.01. .01 + 0.13 Using for Loops Write a program that sums a series that starts with 0.3 Output".0f. as follows: 0.. i <= 1.02... public class TestSum { /** Main method */ public static void main(String[] args) { // Initialize sum float sum = 0. 1 to sum for (float i = 0. "The sum is " + sum.03 and so on. // Add 0. The numbers in the series will increment by 0. 0. // Display result JOptionPane. } } The output: 6 . i = i + 0. JOptionPane.01.INFORMATION_MESSAGE). "Example 1. The program: import javax.Example 1.swing.

Example 1. } System. } 7 . sum += number.17: TestContinue. while (number < 20) { number++. sum += number. int number = 0. if (sum >= 100) break.out.out. while (number < 20) { number++. if (number == 10 || number == 11) continue.java public class TestContinue { /** Main method */ public static void main(String[] args) { int sum = 0. System. int number = 0.16: TestBreak.println("The number is " + number). } } Example 1.java public class TestBreak { /** Main method */ public static void main(String[] args) { int sum = 0.println("The sum is " + sum).

Solution: Suppose you enter two integers 4 and 2. "Example 1. while (k <= n1 && k <= n2) { if (n1 % k == 0 && n2 % k == 0) gcd = k. 3. Example 1. until k is greater than n1 or n2.showInputDialog(null. int n2 = Integer.7 Output". } String output = "The greatest common divisor for " + n1 + " and " + n2 + " is " + gcd. k++. JOptionPane.QUESTION_MESSAGE). "Example 1. "Enter an integer". int gcd = 1. output. JOptionPane.} } System. JOptionPane. Suppose you enter two integers 16 and 24. } } 8 . JOptionPane.showInputDialog(null. but it may not be the greatest commons divisor. So you can check whether k (for k = 2. and so on) is a common divisor for n1 and n2. "Enter an integer".JOptionPane. You know number 1 is a common divisor.INFORMATION_MESSAGE).parseInt(s1). So.out. import javax. int k = 1.showMessageDialog(null.println("The sum is " + sum).18 Finding the Greatest Common Divisor Problem: Write a program that prompts the user to enter two positive integers and finds their greatest common divisor. their greatest common divisor is 8. int n1 = Integer.parseInt(s2).QUESTION_MESSAGE).swing. "Example 1. their greatest common divisor is 2.7 Input". public class GreatestCommonDivisor { /** Main method */ public static void main(String[] args) { // Prompt the user to enter two integers String s1 = JOptionPane. 4. String s2 = JOptionPane.7 Input". how do you find the greatest common divisor? Let the two input integers be n1 and n2.

if(num1 > num2) result = num1. } } 9 . return result.j). System.Example Methods public class testmax { public static void main(String[] args) { int i=5. } Public static int max(int num1 .out.println("The maximum between " + i + "and" +j+ "is" +k). int j=2 int k= max(i. else result = num2. int num2){ Int result.