Switch, Rounding Errors, Libraries CMSC 131 Object-Oriented Programming I Switch, Rounding Errors, Libraries Dept of Computer Science University of Maryland College Park This material is based on material provided by Ben Bederson, Bonnie Dorr, Fawzi Emad, David Mount, Jan Plane
Accessing Private Instance Variables IMPORTANT: Remember that methods of a class can access private elements of parameters that belong to the same class Assume we have a Student class with a private instance variable called name and a method called checking public boolean checking(Student p) { // We can access p.name here even though is private // We don’t need a get method. The method checking // can access its own private instance variables and // also those of objects that belong to the same class }
Ternary Operator We can rewrite your maximum method by using the ternary operator: Expr ? exprValueIfExprIsTrue : exprValueIfExprIsFalse; Rewriting maximum int maximum = x > y ? x : y;
Switch Statement You can use a switch statement instead of a cascaded if statement if the expression you are testing is an integer, string or enum type (to be seen later on) Example: SwitchExample.java
The Switch Statement Example: Switch Statement: is a convenient (and often more efficient) way to perform a multi-way conditional based on a single control value. Example: switch ( option ) { case 1: System.out.println( "Read image" ); break; case 2: System.out.println( "Double" ); case 9: System.out.println( "Quit" ); default: System.out.println( "Sorry, invalid" ); } if ( option == 1 ) System.out.println( “Read image” ); else if (option == 2 ) System.out.println( “Double” ); else if ( option == 9 ) System.out.println( “Quit” ); else System.out.println( “Sorry, invalid” ); Original: The case that is chosen depends on the value of “option” The “default” case is chosen if none of the cases match
The Switch Statement General form: switch ( control-expression ) { The control-expression is one of the following types: char, int, short, byte General form: switch ( control-expression ) { case case-label-1 : statement-sequence-1 break; case case-label-2 : statement-sequence-2 … case case-label-n : statement-sequence-n default : default-statement-sequence } Each case label must be of a type that is compatible with the control expression. You may have any number of statements, including nesting of if-else and loops. The “break” statement jumps out of the switch statement. The “default” case is optional, and is executed if none of the other cases match.
The Switch Statement not float or double, not boolean or long The control expression can be of one of the following types: char, int, short, byte, String, enum not float or double, not boolean or long The “break” statement jumps out of the switch statement. Otherwise control flow just “falls through” into the next case int option = 2; switch ( option ) { case 1: System.out.println( "Read image" ); case 2: System.out.println( "Double" ); case 9: System.out.println( "Quit" ); default: System.out.println( "Sorry, invalid" ); } This is not correct! Double Quit Sorry, invalid Output: This is probably not what you intended.
The Switch Statement The falling though behavior is handy, because it allows you to combine cases. Example: Allowing either upper-case or lower-case for characters: char command = 'D'; switch ( command ) { case 'i': case 'I': MyUtility.insert( ); numberOfItems++; break; case 'd': case 'D': MyUtility.delete( ); numberOfItems--; … } Note: This is a char, not a String. This is performed for either ‘I’ or ‘i’
About Naming Constants If a constant is static then use uppercase letters final static int MAX = 10; If a constant is not static then do not use uppercase letters Use camel case final int maxPressure = 50; Example: ScienceExperiment.java
Floating Point Calculations What is the output of the following code: double difference = 3.9 - 3.8; Example: FloatCalculations.java Floating point numbers in Java are stored in binary representation, and frequently numbers that are easily represented in base 10 cannot be represented precisely in base 2 What can we do?
Floating Point Calculations Two important rules: You can never use == to compare floating point values. Instead, check if two numbers are within a certain tolerance of each other: Math.abs((3.9 - 3.8) - 0.1) < EPSILON Never use floating point values to represent money, e.g., 3.52 to represent $3.52. Instead, use integer 352 to represent 352 pennies
Libraries in Java FileNewPackage Library Implementation of useful routines shared by different programs Java mechanism for creating libraries: packages Package: group of related classes Example: java.util (contains Scanner class) To create a package in Eclipse use FileNewPackage
Libraries in Java To use a class from a package you can use a fully qualified name Fully qualified name package name + class name java.util.Scanner s = new java.util.Scanner(System.in); You can also import the class in the beginning of the file import java.util.Scanner; To import class in a package: import java.util.*; Imports Scanner as well as other classes in package
Package java.lang A special package containing widely used classes: String Math etc. java.lang.* is automatically imported by every Java program
package <name of package>; Package Management A class can be added to a package by including in the source file (usually very first line): package <name of package>; The variables/methods provided by a class/package are often called its API (= Application Programmers Interface) APIs should be documented java.lang documentation: http://docs.oracle.com/javase/8/docs/api/java/lang/package- summary.html