Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
ICS4U Java Syntax Name: Efficient If Statements Date: This Java code segment is correct, but terribly inefficient in terms of processing speed, memory usage, and length. How can it be improved? Scanner in = new Scanner(System.in); System.out.println(“Amount over speed limit:”); int overSpeedLimit = Integer.parseInt(in.nextLine()); int points = 0; if ( overSpeedLimit >= 50 ) { points = 6; } else if ( overSpeedLimit >= 30 ) { points = 4; } else if (overSpeedLimit >= 16 ) { points = 3; } System.out.println(points + “ demerit points!”); 07/05/2017 00:22 Page 1 of 6 ICS4U Java Syntax Name: Efficient If Statements Date: Avoid Unnecessary If Statements This code segment has four separate if statements when it actually only needs one If it is possible for more than one branch to be applicable, then more than one if statement should be used If it is possible for only one branch to be applicable, then only one if statement should be used Do Not Check Conditions More Than Necessary You learn a lot of information when an if statement condition is false. Remember this information so that you don’t unnecessarily check it again Factor Up If the first line of code inside each branch is the same, then that line of code can be factored up out of the if statement. Factoring up reduces the number of lines of code without changing the program’s functionality Factor Down If the last line of code inside each branch is the same, then that line of code can be factored down out of the if statement. Factoring down also reduces the number of lines of code without changing the program’s functionality 07/05/2017 00:22 Page 2 of 6 ICS4U Java Syntax Name: Efficient If Statements Date: Here is the Java code segment rewritten for efficiency: Scanner in = new Scanner(System.in); System.out.println(“Amount over speed limit:”); int overSpeedLimit = Integer.parseInt(in.nextLine()); int points = 0; if ( overSpeedLimit >= 50 ) { points = 6; } else if ( overSpeedLimit >= 30 ) { points = 4; } else if ( overSpeedLimit >= 16 ) { points = 3; } System.out.println(points + “ demerit points!”); 07/05/2017 00:22 Page 3 of 6 ICS4U Java Syntax Name: Efficient If Statements Date: Homework Rewrite the Java programs below so that they are more efficient: 1. Admission to the Toronto Zoo import java.util.*; public class TorontoZoo { public static void main(String[] args) { Scanner in = new Scanner(System.in); int age, price = 0; System.out.println(“How old are you?”); age = Integer.parseInt(in.nextLine()); if ( age >= 65 ) { price = 14; } else if (age >= 13 ) { price = 20; } else if (age >= 4 ) { price = 12; } System.out.println(“Ticket price $” + price); } } 07/05/2017 00:22 Page 4 of 6 ICS4U Java Syntax Name: Efficient If Statements Date: 2. Long Distance import java.util.*; public class LongDistance { public static void main(String[] args) { Scanner in = new Scanner(System.in); String country; System.out.println(“Where are you calling?”); country = in.nextLine(); if ( country.equals(“Belgium”) ) { double rate; rate = 0.02; System.out.println(“$” + rate + “ per min”); } if ( country.equals(“Cuba”) ) { double rate; rate = 1.20; System.out.println(“$” + rate + “ per min”); } if ( country.equals(“Greenland”) ) { double rate; rate = 0.67; System.out.println(“$” + rate + “ per min”); } if ( country.equals(“Peru”) ) { double rate; rate = 0.10; System.out.println(“$” + rate + “ per min”); } } } 07/05/2017 00:22 Page 5 of 6 ICS4U Java Syntax Name: Efficient If Statements Date: 3. Gender Greetings import java.util.*; public class GenderGreetings { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println(“(m)ale or (f)emale?”); String gender = in.nextLine(); System.out.println(“What is your last name?”); String lastName = in.nextLine(); if ( gender.equals(“m”) ) { System.out.println(“Hello Mr. ” + lastName); } else { System.out.println(“Hello Ms. ” + lastName); } } } 07/05/2017 00:22 Page 6 of 6