Unit 7 - Short-Circuit Evaluation - De Morgan’s Laws - The switch statement - Type-casting in Java - Using Math.random()

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Control Structures.
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Craps. /* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
L5:CSC © Dr. Basheer M. Nasef Lecture #5 By Dr. Basheer M. Nasef.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.
ECE122 L8: More Conditional Statements February 7, 2007 ECE 122 Engineering Problem Solving with Java Lecture 8 More Conditional Statements.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Switch Statements Comparing Exact Values. 2 The Switch Statement The switch statement provides another way to decide which statement to execute next The.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
CPS120: Introduction to Computer Science
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.
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
Flow of Control Part 1: Selection
Java Simple Types CSIS 3701: Advanced Object Oriented Programming.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
& Decision Making Algorithms BY: Aakash Indurkhya.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Boolean Expressions and if-else Statements Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.
Chapter 5.  Conditionals  Booleans  Relational and Logical Operators  Switch statements.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology- George Koutsogiannakis 1.
1 CS1001 Lecture Overview Java Programming Java Programming Arrays Arrays.
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,
© A+ Computer Science - A reference variable stores the memory address of an object. Monster fred = new Monster(); Monster sally.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Control statements Mostafa Abdallah
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 How did your team meetings go? A list of three possible “adoptable” projects is linked near the bottom.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
5.02B Decision Making Structure (part 2). Compound Boolean Expressions.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
CS 121 – Intro to Programming:Java - Lecture 4 Announcements Course home page: Owl due soon; another.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You.
Relational Operator and Operations
Chapter 5 The if Statement
Boolean Expressions and if-else Statements
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Chapter 2.
Building Java Programs Chapter 2
null, true, and false are also reserved.
Selection CSCE 121 J. Michael Moore.
OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Selection—Making Decisions
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
The boolean type and boolean operators
Presentation transcript:

Unit 7 - Short-Circuit Evaluation - De Morgan’s Laws - The switch statement - Type-casting in Java - Using Math.random()

Short-Circuit Evaluation if (condition1 && condition2) ... If condition1 is false, then condition2 is not evaluated (the result is false anyway) if (condition1 || condition2) ... If condition1 is true, then condition2 is not evaluated (the result is true anyway)

De Morgan’s Laws ! (p && q) evaluates to ( !p || !q ) One could think of it as the “Distributive Property” for Logic ! (p && q) evaluates to ( !p || !q ) ! (p || q) evaluates to ( !p && !q )

The switch Statement switch (expression) { case value1: ... break; default: } The switch Statement switch case default break Don’t forget breaks! Reserved words Only works with: int char enum

The switch Statement (cont’d) The same case can have two or more labels. For example: switch (num) { case 1: case 2: System.out.println(“Buckle my shoe"); break; case 3: case 4: System.out.println(“Shut the door"); …

The Cast Operator Java allows a programmer to “cast” a piece of data to a different type when needed. Can be useful if working with int values and a decimal result is needed. For example, one might want the decimal equivalent of a fraction even though the numerator and denominator are integers. Example: decAnswer = (double)myDenominator / (double)myNumerator;

Math.Random() returns a double value 0 ≤ x < 1 Can then be scaled to fit the needs of your application. For example, if you needed a random number from 1 to 12, you would do something like this: randNum = (int) (12 * Math.Random() ) + 1;

The Craps Project The purpose of this case study is both to practice with if-else and boolean expressions and to discuss design topics: OO design, team development, and software reusability.

The Craps Project (cont’d) Your job Your assignment is placed into the context of a larger team effort. All you need to know is the public interfaces for your classes Die and CrapsGame, that is, their constructors and public methods.

The Craps Project (cont’d) Step 1: Test your CrapsGame class separately using the class CrapsTest1 provided to you. CrapsTest1 It would take a long time to test your class in the real Craps program, with random numbers of points coming up on the dice. You want a more controlled test, in which you can specify the outcome of a “dice roll,” and you need to run through all relevant sequences of “roll” outcomes. CrapsGame

The Craps Project (cont’d) Step 2: Use your Die and CrapsGame classes in the CrapsStats application. CrapsStats Note how the CrapsGame class is used in the Craps program, and also in the CrapsStats program. This is a simple example of reusability. CrapsGame Die

The Craps Project (cont’d) Step 3: Finish the RollingDie class (the drawDots method). Integrate CrapsGame and RollingDie into the Craps program. Step 3 is here to let you code a switch.