Download break - University of Maryland

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
• 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
}
2
• We can rewrite your maximum method by using the ternary
operator:
Expr ? exprValueIfExprIsTrue : exprValueIfExprIsFalse;
• Rewriting maximum
int maximum = x > y ? x : y;
3
• 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
4
• 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" );
break;
case 9:
System.out.println( "Quit" );
break;
default:
System.out.println( "Sorry, invalid" );
break;
}
Original:
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” );
The case that is
chosen depends on
the value of “option”
The “default” case
is chosen if none of
the cases match
5
• General form:
The control-expression is
one of the following types:
char, int, short, byte
switch ( control-expression ) {
case case-label-1 :
statement-sequence-1
break;
case case-label-2 :
Each case label must be of
statement-sequence-2
a type that is compatible
break;
with the control expression.
…
case case-label-n :
You may have any number of statements,
statement-sequence-n
including nesting of if-else and loops.
break;
default :
The “break” statement jumps
default-statement-sequence
out of the switch statement.
break;
}
The “default” case is optional, and
is executed if none of the other
cases match.
6
• 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;
This is not correct!
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" );
}
Output:
Double
Quit
Sorry, invalid
This is probably not
what you intended.
7
• 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--;
break;
…
}
Note: This is a char, not a String.
This is
performed for
either ‘I’ or ‘i’
8
• 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
9
• 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?
10
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
11
• 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
FileNewPackage
12
• 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
13
• A special package containing widely used classes:
• String
• Math
• etc.
• java.lang.* is automatically imported by every Java program
14
• 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/packagesummary.html
15