Lecture Notes – Week 3 Lecture-1 Chapters 3 & 4
Outline Switch statements Character and string types and operations Case study To do list
switch Statements switch (switch-expression) { The switch-expression must yield a value of char, byte, short, or int data type. switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; … case valueN: statement(s)N; default: statement(s)-for-default; } The value1, ..., and valueN must have the same data type as the value of the switch-expression. The resulting statements in the case statement are executed when the value in the case statement matches the value of the switch-expression. value1, ..., and valueN can be constants only. 3 3 3 3
switch Statements The keyword break is optional, but it should be used at the end of each case in order to terminate the remainder of the switch statement. If the break statement is not present, the next case statement will be executed. switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; … case valueN: statement(s)N; default: statement(s)-for-default; } The default case, which is optional, can be used to perform actions when none of the specified cases matches the switch-expression. When the value in a case statement matches the value of the switch-expression, the statements starting from this case are executed until either a break statement or the end of the switch statement is reached. 4 4 4 4 4
Example Program with a Switch Statement Chinese Zodiac is based on a twelve-year cycle, with each year the cycle represented by an animal monkey, rooster, dog, pig, rat, ox, tiger, rabbit, dragon, snake, horse, or sheep, i.e., year % 12 determines the Zodiac sign. Write a program to find out the Chinese Zodiac sign for a given year. 5 5 5 5 5
Example Program with a Switch Statement import java.util.Scanner; public class ChineseZodiac { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a year: "); int year = input.nextInt(); switch (year % 12) { case 0: System.out.println("monkey"); break; case 1: System.out.println("rooster"); break; case 2: System.out.println("dog"); break; case 3: System.out.println("pig"); break; case 4: System.out.println("rat"); break; case 5: System.out.println("ox"); break; case 6: System.out.println("tiger"); break; case 7: System.out.println("rabbit"); break; case 8: System.out.println("dragon"); break; case 9: System.out.println("snake"); break; case 10: System.out.println("horse"); break; case 11: System.out.println("sheep"); } 6 6 6 6 6 6
Case Study In this case study, we learn how to design a solution for a given problem, and how to write a program containing a multi-way if-else statement, and how to test such a program to correctly implement the designed solution. 7
Problem Specification The problem is to design, implement and test a program that prompts the user to enter a weight in pounds and a height in inches, calculate the Body Mass Index (BMI) and displays the health interpretation of the BMI. The BMI is calculated by dividing the weight in kilograms by the square of the height in meters. 8 8 8 8
Problem Specification The health interpretation of BMI for people 20 years or older is as follows: 9 9 9 9
Top-level Design Prompt the user to enter a weight and a height. Calculate the BMI. Display the health interpretation of the BMI. 10 10 10
Design Refinement (1. Prompt the user to enter a weight in pounds and heights in inches) is refined by: 1.1. Prompt the user to enter the weight in pounds 1.2. Prompt the user to the height in inches 11 11 11 11 11
Design Refinement (2. Calculate the BMI) is refined by: 2.1. Convert the weight in pounds into weight in kilograms with weight in kilograms = weight * kilograms per pound 2.2. Convert the height in inches into height in metres with height in inches = height * metres per inch 2.2. Calculate the BMI with bmi = weight in kilos /(height in metres * height in metres) 12 12 12 12 12 12
Design Refinement (3. Display the health interpretation of the BMI) is refined by: 3.1. Display the prompt of the BMI interpretation 3.2. Display the interpretation 13 13 13 13 13 13 13
Design Refinement (3.2. Display the interpretation) is refined by 3.2.1. If bmi < 18.5 then displays "Underweight” else if bmi < 25 then "Normal” else if bmi < 30 then displays "Overweight” else "Obese” 14 14 14 14 14 14 14 14
Final Design 1.1. Prompt the user to enter the weight in pounds 1.2. Prompt the user to the height in inches 2.1. Convert the weight in pounds into weight in kilograms with weight in kilograms = weight * kilograms per pound 2.2. Convert the height in inches into height in metres with height in inches = height * metres per inch 2.2. Calculate the BMI with bmi = weight in kilos /(height in metres * height in metres) 3.1. Display the prompt of the BMI interpretation 3.2.1. If bmi < 18.5 then displays "Underweight” else if bmi < 25 then "Normal” else if bmi < 30 then displays "Overweight” else "Obese” 15 15 15 15 15 15 15 15
Implementation import java.util.Scanner; public class ComputeAndInterpretBMI { public static void main(String[] args) { Scanner input = new Scanner(System.in); //Prompt the user to enter weight in pounds System.out.print("Enter weight in pounds: "); double weight = input.nextDouble(); //Prompt the user to enter height in inches System.out.print("Enter height in inches: "); double height = input.nextDouble(); final double KILOGRAMS_PER_POUND = 0.45359237; // Constant final double METERS_PER_INCH = 0.0254; // Constant //Calculate BMI double weightInKilograms = weight * KILOGRAMS_PER_POUND; double heightInMeters = height * METERS_PER_INCH; double bmi = weightInKilograms / (heightInMeters * heightInMeters); //Display the interpretation System.out.println("BMI is " + bmi); if (bmi < 18.5) System.out.println("Underweight"); else if (bmi < 25) System.out.println("Normal"); else if (bmi < 30) System.out.println("Overweight"); else System.out.println("Obese"); } 16 16 16 16 16 16 16 16 16
To Do List Before Next Lecture Selectively read those sections in Chapters 3 and 4 that cover the topics in this lecture. Attend your practical session in Week 3, consisting of an in-class test, a tutorial, a case study and a set of programming exercises. Glance through those sections in Chapter 5 that cover the topics in Week 3’s lecture.