Switch, Rounding Errors, Libraries

Slides:



Advertisements
Similar presentations
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Advertisements

5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
ECE122 L8: More Conditional Statements February 7, 2007 ECE 122 Engineering Problem Solving with Java Lecture 8 More Conditional Statements.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Logical Operators and Conditional statements
Switch Statements Comparing Exact Values. 2 The Switch Statement The switch statement provides another way to decide which statement to execute next The.
Java Programming, Second Edition Chapter Four Advanced Object Concepts.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
1 Data Comparisons and Switch Data Comparisons Switch Reading for this class: L&L 5.3,
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Flow of Control Part 1: Selection
Chapter 2: Java Fundamentals
ITIP © Ron Poet Lecture 12 1 Finding Out About Objects.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
Introduction to Java Java Translation Program Structure
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Decisions. Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Control statements Mostafa Abdallah
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
John Hurley Spring 2011 Cal State LA CS 201 Lecture 5:
Primitive Types Four integer types: Two floating-point types:
Switch statement.
Conditionals, Block Statements, Style
C++ Lesson 1.
Information and Computer Sciences University of Hawaii, Manoa
Dept of Computer Science University of Maryland College Park
String Comparison, Scanner
JAVA MULTIPLE CHOICE QUESTION.
Working with Java.
COMP 14 Introduction to Programming
Dept of Computer Science University of Maryland College Park
Java Variables and Types
Week 3 - Friday CS222.
Selections Java.
Dept of Computer Science University of Maryland College Park
Yanal Alahmad Java Workshop Yanal Alahmad
Lecture 2: Data Types, Variables, Operators, and Expressions
University of Central Florida COP 3330 Object Oriented Programming
Chapter 3 Assignment Statement
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Conditionals & Boolean Expressions
Chapter 4: Making Decisions.
Primitive Types Vs. Reference Types, Strings, Enumerations
INPUT STATEMENTS GC 201.
CMSC 202 Static Methods.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Starting JavaProgramming
Java - Data Types, Variables, and Arrays
MSIS 655 Advanced Business Applications Programming
Chapter 2: Java Fundamentals
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Java Programming Review 1
The Java switch Statement
Java Statements B.Ramamurthy CS114A, CS504 4/23/2019 BR.
Using java libraries CGS3416 spring 2019.
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
The switch Statement When we want to compare a variable against several values to see which one it has, we can use the switch statement: switch (status)
Agenda Types and identifiers Practice Assignment Keywords in Java
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

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 FileNewPackage 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

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