1323Agenda3 HW int number = 716; System.out.println(“The number is “ + “number”); Valid variable name? two_shoes Flowchart symbols and clarity. “Single.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Computer Programming Lab 8.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
[ ] Review Exam [ ] Questions [ ] Lab 6 [ ] HW6 [ ] Method Block Diagram [ ] Methods, methods [ ] Looping constructs [ ] for loop [ ] ++, -- (Thursday)
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Loops – While, Do, For Repetition Statements Introduction to Arrays
COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
Topic 11 Scanner object, conditional execution Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
An Introduction to Textual Programming
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Truth and while Today 15 Minutes online time to finish the random/Swing programs. Truth tables: Ways to organize results of Boolean expressions. Note Taking:
CS 240 – Computer Programming I Lab Kalpa Gunaratna –
Catie Welsh February 2,  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2.
Programming Fundamentals I Java Programming Spring 2009 Instructor: Xuan Tung Hoang TA: Tran Minh Trung Lab 03.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
Chapter 5: Loops Tarik Booker CS 201 California State University, Los Angeles.
CompSci 230 S Programming Techniques
Chapter 5 The if Statement
Chapter 2 Basic Computation
Chapter 2 More on Math More on Input
Java for Beginners University Greenwich Computing At School DASCO
The switch Statement, and Introduction to Looping
Chapter 5: Control Structures II
Repetition-Counter control Loop
Engineering Innovation Center
SELECTION STATEMENTS (1)
Chapter 2 Basic Computation
Topic 11 Scanner object, conditional execution
Building Java Programs
Control Statement Examples
Building Java Programs
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Building Java Programs
Lec 4: while loop and do-while loop
Building Java Programs
Building Java Programs
Building Java Programs
Repetition Statements
CSC 1051 – Data Structures and Algorithms I
Unit 3: Variables in Java
Building Java Programs
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Data Types and Maths Programming Guides.
Presentation transcript:

1323Agenda3 HW int number = 716; System.out.println(“The number is “ + “number”); Valid variable name? two_shoes Flowchart symbols and clarity. “Single entry point, single exit” for I/O, computation/process Lab2 Good work – Continue to bring the kitchen sink to lab. I’m hiring 3 new people: 1 to convert code to flowchart (like last class quiz) 1 to convert flowcharts to code 1 to develop flowcharts

String objects String fname = “Jill”; How ‘long’ is fname? How many characters in fname? Sure, you just counted them – 4

String objects String fname = “Jill”; How ‘long’ is fname? How many characters in fname? Sure, you just counted them – 4 String someLets = “asfgiopawrth8[w gae[gnklasdfga[r 9awe[rgklr 890S_yS nawoguS IS GiOPGIOS{G jIG imn{ Gjio[ SGJi{SiSgj()S{ GJ9S gj[ adfg9pa GUUV(_ “; How long is someLets?

String objects String someLets = “asfgiopawrth8[w gae[gnklasdfga[r 9awe[rgklr 890S_yS nawoguS IS GiOPGIOS{G jIG imn{ Gjio[ SGJi{SiSgj()S{ GJ9S gj[ adfg9pa GUUV(_ “; How long is someLets? Good news – java gives us a tool that tells us. A method called ‘length’. How do we use it? int len = 0; len = someLets.length(); That looks something like num2Sqrt = Math.sqrt(num2); Yes, they are both ‘Method calls”.

Review To obtain user input from the terminal: 1 import java.util.Scanner; 2 Scanner console = new Scanner(System.in); 3 gpa = console.nextDouble ( ); Example: (after having 1 and 2 in your program:) double num1 = 0; System.out.println (“Please enter a number: “); num1 = console.nextDouble( ):

Sample Flowchart

Logic error, symbol error!

if – else construct if (newRate > oldRate) { cost = price * newRate; System.out.println(“New cost is “ + cost); } else { cost = price * oldRate; System.out.println(“No change to cost. Cost is “ + cost); } Note the use of the concatenation operator + What is the “TRUE Block” “FALSE Block” Note the indention of ‘subservient code’

Note that an if does not require an else. if ( code > oldCode) { System.out.println(“Code has changed. New code: “ + code); } System.out.println (“Hello Joe”); Now I’ll put some examples on the board to try to trick you

Pick a num1: if (num1 > 5) System.out.println(“Joe”); else { System.out.println(“Sarah”); System.out.println(“Alice”); num1 = num1 + 1; } System.out.println(“Henry”); // if (num1 > 5) System.out.println(“Joe”); else System.out.println(“Sarah”); System.out.println(“Alice”); num1 = num1 + 1; System.out.println(“Henry”);

Pick a num1: if (num1 > 5) System.out.println(“Joe”); System.out.println(“Alice”); else { System.out.println(“Sarah”); System.out.println(“Alice”); num1 = num1 + 1; } System.out.println(“Henry”); // if (num1 > 5) System.out.println(“Joe”); else System.out.println(“Sarah”); System.out.println(“Alice”); num1 = num1 + 1; System.out.println(“Henry”);

Subservient code. A standard practice is to indent subservient code consistently. At Dell, it is 2 spaces, Google, 4 spaces if (num1 > 5) { num1 = num1 * 7; num2 = 9; } else { num2 = 0; } Notes: { on line by itself else on line by itself

Nested if statements if (num1 > 5) { if (num2 == 4) { num1 = num1 * 7; num2 = 9; } else num1 = 5; } else { num2 = 0; }

Let’s work on lab 3 Note: Answering questions in the lab: Read the lab at least 5 times. Complete flowcharts for the program that you are working on. (it doesn’t have to be perfect, but it does have to be complete) What is a “workbench box” in a flowchart? (think chocolate chip cookies) Every method has its own flowchart. Every program and system have their own Block Diagram

lab 3 seuCalculator has one method - main seuController will have several methods, main, calculator, analyzeSentence, dieRoll and moneyBags main is a ‘controller’ method – s/he will just tell other people what to do. The calculator method will end up being your seuCalculator program logic So we will put several methods in one class?

lab 3 How will we do that? First things first – we already have a flowchart for calculator, so we just need a flowchart for seuController main method Here we go ----

lab 3 How will we do that? Did we get here on Tuesday? If so, roll on.

int num1 = 3; System.out.println(“Hello “ + num1 + “ cats”); System.out.print(“well, “); System.out.println(“well.”); System.out.println(“So, let’s go \n” + “party!”); Pop a new line - - how?, automatic?

P lease E xcuse M y D ear A unt S ally Order of operations (3*5 + 7) * 3/7 – 2 You can never have too many parentheses

Operators ==Equality operator (for primitive data types) != > >= < <= ||or &&and

Logical expression – an expression that is true or false universally Truth Table 3 > 5 || today is Monday ABA||BA&&B!AA&&!B TTTTFF TFTFFT FTTFTF FFFFTF

Random numbers in java - book page 188 (we will just use nextInt(xx) there are others) 1. import java.util.Random; 2. Random gener = new Random(); 3. int num1 = 0; Ex: num1 = gener.nextInt(7); // integer 0, 1, 2, 3, 4, 5, 6 Ex: num1 = gener.nextInt(20); // integer 0, 1, 2, …, 19 Ex: num1 = gener.nextInt(2); // ?

Ex: random integer between 4 and 13 we need rnum to be 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 so 10 numbers. Note: 13 – 4 is 9 we need = 10 How do we get 10 random numbers? nextInt(10) yes, but 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 But we want one of the 10 numbers 4, 5, 6, …, 13 Well, just add 4 to each of these numbers and …. So: rnum = gener.nextInt(10) + 4; does the job Ex: generate a random integer between -3 and 3