CSCI S-1 Section 8. Coming Soon Problem Set Four (72 + 5/15 points) – Tuesday, July 21, 17:00 EST.

Slides:



Advertisements
Similar presentations
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Advertisements

CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
Loops Chapter 4. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
Warm-Up: April 21 Write a loop (type of your choosing) that prints every 3 rd number between 10 and 50.
COMP More About Classes Yi Hong May 22, 2015.
 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.
Lecture 2: Classes and Objects, using Scanner and String.
CSCI S-1 Section 6. Coming Soon Homework Part A – Friday, July 10, 17:00 EST Homework Part B – Tuesday, July 14, 17:00 EST Mid-Term Quiz Review – Friday,
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
F27SA1 Software Development 1 3. Java Programming 2 Greg Michaelson.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Random numbers. 2 The Random class A Random object generates pseudo-random numbers. –Class Random is found in the java.util package. import java.util.*;
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Chapter 4: Control Structures II
Chapter 5: Control Structures II
CSC1401 Strings (text). Learning Goals Working with Strings as a data type (a class) Input and output of Strings String operations.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Lecture 6: Midterm Review Tami Meredith. Programming Process How do we fill in the yellow box? Text Editor Compiler (javac) Interpreter (JVM: java) User.
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:
Advanced Arithmetic, Conditionals, and Loops INFSY 535.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CSCI S-1 Section 4. Deadlines for Homework 2 Problems 1-8 in Parts C and D – Friday, July 3, 17:00 EST Parts E and F – Tuesday, July 7, 17:00 EST.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Lecture 4b Repeating With Loops
Chapter 2 Clarifications
Building Java Programs
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 5: Control Structures II
Primitive Data, Variables, Loops (Maybe)
Repetition-Sentinel,Flag Loop/Do_While
Something about Java Introduction to Problem Solving and Programming 1.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs Chapter 7
Building Java Programs
Building Java Programs
Truth tables: Ways to organize results of Boolean expressions.
Truth tables: Ways to organize results of Boolean expressions.
Building Java Programs
class PrintOnetoTen { public static void main(String args[]) {
Building Java Programs
Truth tables: Ways to organize results of Boolean expressions.
Scope of variables class scopeofvars {
Building Java Programs
Building Java Programs
Random Numbers while loop
Building Java Programs
Methods/Functions.
Presentation transcript:

CSCI S-1 Section 8

Coming Soon Problem Set Four (72 + 5/15 points) – Tuesday, July 21, 17:00 EST

Why Isn’t This Nice? public static void notSoNiceDice{ System.out.println("Your turn!"); int roll1 = (int) (Math.random() * 6) + 1; System.out.println("You rolled a " + roll1 + "."); int roll2 = (int) (Math.random() * 6) + 1; System.out.println("Then you rolled a " + roll2 + "!"); System.out.println("The product of your rolls was " + (roll1 * roll2)); System.out.println("My turn!"); int roll3 = (int) (Math.random() * 6) + 1; System.out.println("I rolled a " + roll3 + "."); int roll4 = (int) (Math.random() * 6) + 1; System.out.println("Then I rolled a " + roll4 + "!"); System.out.println("The sum of my rolls was " + (roll3 + roll4)); }

It’s Repetitive, Repetitive, Repetitive!!! Four times we evaluate (int) (Math.random() * 6) + 1. Why might we want to make this a separate rollDie() method? Easier to read: if method calls rollDie(), you know what's going on Easier to change: modify and test a single method Easier to reuse : call rollDie() from many board games!

The Familiar Void //void methods have “side effects” and can help modularize Public static void main (String[] args) { methodTwo() } public static void methodTwo() { methodOne(); } public static void methodOne() { System.out.println("Inside method one"); }

Non-Void Methods “Return” Values public static void what? To get results back from rollDie(), have it return a value Why can’t we just use global/class variables? – anything can access or change them – it can be hard to determine which method made the change The semantics of ‘return’ are not mysterious – int n = 3+4//+ returns 7 – int n = sum(3,4)//sum returns 7

int rollDie() public static int rollDie() {//return an int in the range 1-6 incl. }

int rollDie() public static int rollDie() {//return an int in the range 1-6 incl. int i = (int) (Math.random()*6 + 1); return i; } public static int rollDie() {//same, but in one line }

int rollDie() public static int rollDie() {//return an int in the range 1-6 incl. int i = (int) (Math.random()*6 + 1); return i; } public static int rollDie() {//same, but in one line return (int) (Math.random()*6 + 1); }

Going to Town //some exciting things you can do with return values public static void main (String[] args) { System.out.println(rollDie());//print it int answer = rollDie();//assign it int sumOfTwoRolls = rollDie() + rollDie();//evaluate it double rollsOverFive = rollDie() / 5.0;//slice and dice it }

Parameter Passing public static boolean isGeneralissimoFrancoStillDead() { return true; } what methods don’t always return the same value? depend on user/keyboard input depend on random expressions depend on passed parameters

Parameter Passing //method to determine if a character is a digit Public static boolean isDigit(char ch) { //code to calculate and return goes here } //invoking method supplies value for ch //what does boolean mean here? boolean b = isDigit(‘2);//sample call

isDigit /** * Determines if the given character is a digit (betw `0' and `9'). ch character to be tested whether the character is a digit */ public static boolean isDigit(char ch) { }

isDigit /** * Determines if the given character is a digit (betw `0' and `9'). ch character to be tested whether the character is a digit */ public static boolean isDigit(char ch) { if ((ch >= `0') && (ch <= `9')) return true; elsereturn false; }

isDigit concise //repeating the code from the last slide… public static boolean isDigit(char ch) { if ((ch >= `0') && (ch <= `9')) return true; elsereturn false; } public static boolean isDigit(char ch) { }

isDigit concise //repeating the code from the last slide… public static boolean isDigit(char ch) { if ((ch >= `0') && (ch <= `9')) return true; elsereturn false; } public static boolean isDigit(char ch) { return ((ch >= `0') && (ch <= `9')); }

isEvenDigit /** * Determines if the given character is an even digit ch character to be tested whether the character is an even digit */ public static boolean isEvenDigit(char ch) { }

isEvenDigit /** * Determines if the given character is an even digit ch character to be tested whether the character is an even digit */ public static boolean isEvenDigit(char ch) { return ((ch == `0') || (ch == `2') || (ch == `4') || (ch == `6') || (ch == `8')); }

isEvenDigit with indexOf /** * Determines if the given character is an even digit ch character to be tested whether the character is an even digit */ public static boolean isEvenDigit(char ch) { // string method indexOf returns -1 when char not in string }

isEvenDigit with indexOf /** * Determines if the given character is an even digit ch character to be tested whether the character is an even digit */ public static boolean isEvenDigit(char ch) { // string method indexOf returns -1 when char not in string final String EVENDIGITS = "02468"; return (EVENDIGITS.indexOf(ch) != -1); }

isOddDigit //determines if the given character is an odd digit //define in terms of isDigit and isEvenDigit public static boolean isOddDigit(char ch) { }

isOddDigit //determines if the given character is an odd digit //define in terms of isDigit and isEvenDigit public static boolean isOddDigit(char ch) { return (isDigit(ch) && !isEvenDigit(ch)); }

Example //read String from keyboard and print out even digits in reverse public static void main(String[] args) { }

Example //read String from keyboard and print out even digits in reverse import java.util.*; public static void main(String[] args) { Scanner kbd = new Scanner(System.in); String input = kbd.nextLine(); for (int i = input.length() - 1; i >= 0; i--) if (isEvenDigit(input.charAt(i)) System.out.print(input.charAt(i)); System.out.println(); }

Summary Familiar method calls with parameters System.out print( ); charAt( ); Declare a method that takes parameters by putting the data type and variable name inside the parentheses public static double fahrenheitToKelvin(double fahr) {} Call a method that takes parameters by putting something with the right data type into the parentheses d = fahrenheitToKelvin(32.0);