YEAH 2: Simple Java! Avery Wang Jared Bitz 7/6/2018

Similar documents
YEAH Hours. January , 7-8 PM Jared Wolens

Assignment #2: Intro to Java Due: 11AM PST on Wednesday, July 12

CS 106A, Lecture 5 Booleans and Control Flow

Simple Java YEAH Hours. Brahm Capoor and Vrinda Vasavada

Variables Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

Chapter Goals. Contents LOOPS

CP122 CS I. Iteration

Programming Lecture 4

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Introduction. C provides two styles of flow control:

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

Ex: If you use a program to record sales, you will want to remember data:

Building Java Programs

Full file at

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Exam 1. CSC 121 Spring Lecturer: Howard Rosenthal. March 1, 2017

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Programming Lecture 4

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Repetition CSC 121 Fall 2014 Howard Rosenthal

CS 106A Midterm Review. Rishi Bedi, adapted from slides by Kate Rydberg and Nick Troccoli Summer 2017

CIS 110: Introduction to Computer Programming

b. Suppose you enter input from the console, when you run the program. What is the output?

CS111: PROGRAMMING LANGUAGE II

Expressions and Control Statements

Chapter 2: Data and Expressions

Programming Lecture 4

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

Homework Assignment 2: Java Console and Graphics

Assignment 2.4: Loops

Object-Oriented Programming

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

MODULE 02: BASIC COMPUTATION IN JAVA

Midterm Review Session

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

CS111: PROGRAMMING LANGUAGE II

Hangman YEAH Hours. Thursday, February 14, 7:30 9:00PM Andrew Tierno

Computers Programming Course 6. Iulian Năstac

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS 112 Introduction to Programming

Flow of Control of Program Statements CS 112 Introduction to Programming. Basic if Conditional Statement Basic Test: Relational Operators

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

More on control structures

LECTURE 5 Control Structures Part 2

Lecture 10: for, do, and switch

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

(Refer Slide Time: 00:26)

C++ DATA TYPES BASIC CONTROL FLOW

Midterms Save the Dates!

printf( Please enter another number: ); scanf( %d, &num2);

Introduction to Programming Using Java (98-388)

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

REPETITION CONTROL STRUCTURE LOGO

COMP Primitive and Class Types. Yi Hong May 14, 2015

Darrell Bethea May 25, 2011

AP Computer Science Unit 1. Programs

(Refer Slide Time: 02:59)

CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, Name:

106A assignment review #4. 13 Feb :30p-6:30p.! Miles Seiver

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Lecture 1. Types, Expressions, & Variables

CSCI 131, Midterm Exam 1 Review Questions This sheet is intended to help you prepare for the first exam in this course. The following topics have

Lecture 9. Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 1

COMP 111. Introduction to Computer Science and Object-Oriented Programming

Introduction to Computer Science Midterm 3 Fall, Points

Arithmetic and IO. 25 August 2017

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

CS 115 Lecture 4. More Python; testing software. Neil Moore

ENGR 101 Engineering Design Workshop

Language Reference Manual

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

CSCE 206: Structured Programming in C++

Programming Lecture 4

ECE 122. Engineering Problem Solving with Java

Chapter 2: Data and Expressions

CS110D: PROGRAMMING LANGUAGE I

CS 106A, Lecture 7 Parameters and Return

Lecture 3 Tao Wang 1

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

CS1100 Introduction to Programming

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

These are reserved words of the C language. For example int, float, if, else, for, while etc.

C: How to Program. Week /Mar/05

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

Chapter 2: Using Data

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

CS1004: Intro to CS in Java, Spring 2005

CMSC 104 -Lecture 6 John Y. Park, adapted by C Grasso

Transcription:

YEAH 2: Simple Java! Avery Wang Jared Bitz 7/6/2018

What are YEAH Hours? Your Early Assignment Help Only for some assignments Review + Tips for an assignment Lectures are recorded, slides are posted on website. 2

Bye Karel! 3

Variables int integer int date = 7; (From Lecture 4) double char boolean real values letters true/false double height = 5.8; char letter = A ; boolean lovescs106a = true; 4

Variables Good vs. Bad names i (From Lecture 4) something sum numdays CONSTANT double 5

Variables Good vs. Bad names (From Lecture 4) i (unless it is a loop counter) something sum numdays CONSTANT double 6

Constants Variables whose value doesn t change. (From Lecture 5) private static final double CIRCLE_RADIUS = 5.5; type name value 7

Arithmetic Operators (From Lecture 5) + - * / % Evaluates as you d expect. Careful when dividing ints truncates decimals! mod operator 8

Arithmetic Operators (From Lecture 5) a % b What s the remainder when you divide a by b? 17 % 2 evaluates to 1 52 % 2 evaluates to 0 100 % 3 evaluates to 1 9

Logical Operators (From Lecture 5)!p p && q p q NOT AND OR evaluates to true if p is false. evaluates to true if both p and q are true evaluates to true if either p or q is true 10

Relational Operators (From Lecture 5) a == b a!= b a > b a >= b a < b a <= b evaluates to true if a is equal to b. evaluates to true if a is not equal to b. evaluates to true or false as you d expect. 11

Relational Operators (From Lecture 5) a == b checks if a is equal to b. if(a == b){ println( equal! ); } a = b assigns a to the value of b. int b = 3; int a = 2; a = b; // now a is 3 12

Control Flow for (init; test; step){ statements } init while (test) { statements } (From Lecture 5) We know how many times to iterate. We don t know how many times to iterate. 13

Control Flow while (true) { // get input if (input == SENTINEL){ break; } // rest of body } (From Lecture 5) // get input fencepost while (input!= SENTINEL){ // rest of body // get input } 14

Scope (From Lecture 6) A variable s lifetime starts at initialization until end of code block public void run(){ for (int i = 0; i < 3; i++){ if (i == 0){ int j = 0; j++; } i--; } } Scope of i Scope of j 15

Forbidden Java Features (For Assignment 2) parameters return Strings instance variables (more on this later) concepts from Chapter 5 and beyond 16

Practice: FizzBuzz Write a program that prints all of the numbers in a range, separated by spaces For multiples of three print "Fizz" instead of the number For the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz". Get the upper limit from the user For a limit of 100, the output would be: 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 BuzzFizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 FizzBuzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 5859 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 7677 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 BuzzFizz 97 98 Fizz Buzz 17

public void run() { } 18

public void run() { int limit = readint( Limit? ); } 19

public void run() { int limit = readint( Limit? ); for (int i = 1; i <= limit; i++){ } } 20

public void run() { int limit = readint( Limit? ); for (int i = 1; i <= limit; i++){ if (i % 3 == 0 && i % 5 == 0){ print( FizzBuzz ); } } } 21

public void run() { int limit = readint( Limit? ); for (int i = 1; i <= limit; i++){ if (i % 3 == 0 && i % 5 == 0){ print( FizzBuzz ); } else if (i % 3 == 0){ print( Fizz ); } } } 22

public void run() { int limit = readint( Limit? ); for (int i = 1; i <= limit; i++){ if (i % 3 == 0 && i % 5 == 0){ print( FizzBuzz ); } else if (i % 3 == 0){ print( Fizz ); } else if (i % 5 == 0){ print( Buzz ); } } } 23

public void run() { int limit = readint( Limit? ); for (int i = 1; i <= limit; i++){ if (i % 3 == 0 && i % 5 == 0){ print( FizzBuzz ); } else if (i % 3 == 0){ print( Fizz ); } else if (i % 5 == 0){ print( Buzz ); } else { print(i + ); } } } 24

Assignment 2: Intro to Java! Due Date: Wed, Jul. 11, 2018 at 11 AM.

Assignment 2 Consists of 4 console programs Applies concepts from lectures 4-6 (up to Tuesday s lecture) and section 2. Done individually. 26

1. Quadratic Formula x = b ± b2 4ac 2a

Quadratic Formula readint(prompt) a (assume nonzero) b c x = b ± b2 4ac 2a Root(s) (do not round!) println(message)

Discriminant Δ = b 2 4ac Δ > 0 Δ = 0 Δ < 0

Discriminant Δ = b 2 4ac Δ > 0 Two real roots Δ = 0 One root Δ < 0 No real roots

Quadratic Formula Assume a 0. Assume a, b, and c are integers. Do not round your answer(s). double y = Math.sqrt(x); Useful Concepts Conditionals Math with int and double. Reading input. 31

2. Weather Accuweather forecast for CA 94305

Weather Prompt until SENTINEL. Print the following: Highest temperature Lowest temperature Average temperature Cold days (50 degrees or less)

Weather SENTINEL has value 1 (value you should set as default).

Weather SENTINEL has value 42 (one of many values you should test).

Weather SENTINEL has value 1 If only one temperature: Highest, lowest, and average temperature are equal.

Weather SENTINEL has value 1 If no temperatures: Print error message.

Weather SENTINEL must be a constant. Assume inputs are integers. Do not round your answer(s). Useful Concepts Fencepost. Scope. Sentinel loops. Output should match exactly. 38

3. Hailstone Sequence

Hailstone Sequence Pick some positive integer and call it n. Do the following until n is equal to 1: If n is odd, multiply it by three and add one. If n is even, divide it by two. 17

Hailstone Sequence Pick some positive integer and call it n. Do the following until n is equal to 1: If n is odd, multiply it by three and add one. If n is even, divide it by two. make 3n + 1 17 52

Hailstone Sequence Pick some positive integer and call it n. Do the following until n is equal to 1: If n is odd, multiply it by three and add one. If n is even, divide it by two. make 3n + 1 17 52 take half 26

Hailstone Sequence Pick some positive integer and call it n. Do the following until n is equal to 1: If n is odd, multiply it by three and add one. If n is even, divide it by two. make 3n + 1 take half take half make 3n + 1 take half take half 17 52 26 13 40 20 take half make 3n + 1 take half 10 5 16 8 4 2 1 take half take half take half

Hailstone Sequence Pick some positive integer and call it n. Do the following until n is equal to 1: If n is odd, multiply it by three and add one. If n is even, divide it by two. make 3n + 1 take half take half make 3n + 1 take half take half 17 52 26 13 40 20 take half make 3n + 1 take half 10 5 16 8 4 2 1 take half take half It took 12 steps to reach 1. take half

Must have a method to output a single Hailstone sequence. Hailstone Sequence

Hailstone Sequence Assume input is an integer. Output should match exactly (including all spaces on the console). Useful Concepts Fencepost. Scope & loops. Binary Operators. Ask the user whether to play directly inside the while loop: while (readboolean( Run again?, y, n )) { 46

4. Rocket

Rocket Program is non-interactive. SIZE must be a constant. Assume SIZE is 2 or greater. Must use a nested for loop.

Rocket SIZE has value 5 (value you should set as default).

Rocket SIZE has value 3 (one of many values you should test).

Rocket Decompose each part of the rocket. No println() inside run() Output should match exactly Helpful Tips: Make a table. Solve the default size (5) before using constant. Useful Concepts Nested for loop. Constants. Decomposition. 51

Example from Tuesday 52

Note about Instance Variables public class Example { private static final int SIZE = 5; // constant private int num = 0; // instance variable bad! } public void run() { int sum = 0; // local variable } For this assignment, don t use non-constant variables declared outside of methods to get avoid having to deal with scope issues!

Output Comparison Tool Output should match exactly.

Other Advice Read spec very carefully about requirements. Use constant, but no instance variables. Read the Assignment 2 style guide. Fix a bug, before moving on. Make sure output matches exactly (Output Comparison Tool). Test your programs extensively. Visit the LaIR if you get stuck. Incorporate feedback from Assignment 1! 55

Questions? 56

Have fun! 57