CS 101 Computer Programming Statements Review++. Reviewed so far.. Variables Constants Order of Execution Boolean expressions and variables if statements,

Slides:



Advertisements
Similar presentations
Overloading Having more than one method with the same name is known as overloading. Overloading is legal in Java as long as each version takes different.
Advertisements

Escape Sequences \n newline \t tab \b backspace \r carriage return
Java Control Statements
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.
Chapter 4 - Control Statements
Iterations for loop. Outcome Introduction to for loop The use of for loop instead of while loop Nested for loops.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
22 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming Java Lab 5: Boolean Operations 8 February JavaLab5 lecture slides.ppt Ping Brennan
Introduction to Programming
Variables Conditionals Boolean Expressions Conditional Statements How a program produces different results based on varying circumstances if, else if,
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Selection Victor Norman CS104 Calvin College. Reading Quiz Counts toward your grade.
Flow of Control Usually the order of statement execution through a method is linear: one after another flow of control: the order statements are executed.
CSCI 160 Midterm Review Rasanjalee DM.
Discussion1 Quiz. Q1 Which of the following are invalid Java identifiers (variable names)? a) if b) n4m3 c) Java d) e) DEFAULT_VALUE f) bad-choice.
Control Structures. Decision Making Structures The if and if…else are all selection control structures that introduce decision-making ability into a program.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Lecture 11. Today’s topic Conditional statement –Relational operators –if statement –if-else statement –If-elseif statement.
Loops Chapter 4. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures.
Chapter 4—Statement Forms The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Statement Forms C H A P T E R 4 The statements.
Chapter 4 Statement Forms. Statement types 1.Simple statements expression; println(“The total is “ + total + “.”); (method call) 2. Compound statements.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
Java Programming: From the Ground Up
Chapter 5 Methods. An overview In Add2Integers: println(“This program adds two integers.”); int n1 = readInt(“Enter n1: “); method name: println argument.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
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.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Repetition Statements while and do while loops
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Chapter 4—Statement Forms The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Statement Forms C H A P T E R 4 The statements.
Conditionals-part21 Conditionals – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
In the last lesson we discussed about: Casting Precedence The “=“ used as an assignment operator Made a calculate average program.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
Lesson thirteen Conditional Statement "if- else" ©
Chapter 4 Statement Forms. Statement types 1.Simple statements expression; println(“The total is “ + total + “.”); (method call) 2. Compound statements.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Five-Minute Review 1.What are classes and objects? What is a class hierarchy? 2.What is an expression? A term? 3.What is a variable declaration? 4.What.
Programming – Lecture 15 Going Beyond the ACM Library.
Five-Minute Review What are classes and objects? What is a class hierarchy? What is an expression? A term? What is a variable declaration? What is an assignment?
What’s cheating and what is not?
Chapter 6 More Conditionals and Loops
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
CS 106A, Lecture 7 Parameters and Return
CSS161: Fundamentals of Computing
If statement.
Fencepost loops reading: 4.1.
Lecture Notes – Week 3 Lecture-2
Types, Truth, and Expressions (Part 2)
Practice with loops! What is the output of each function below?
Selection Statements.
Repetition Statements
Types, Truth, and Expressions
Loops CGS3416 Spring 2019 Lecture 7.
CIS 110: Introduction to Computer Programming
Looping and Repetition
Chapter 4—Statement Forms
Presentation transcript:

CS 101 Computer Programming Statements Review++

Reviewed so far.. Variables Constants Order of Execution Boolean expressions and variables if statements, if-else statements nested if statements loops: while statements, for statements Nested loops CS 101 | Özyeğin University2 int x = 10; private static final int SIZE = 50; boolean withinLimits = true; if(x 100) { withinLimits = false; } while(x < 100) {... while(y < 100) {... }

Exercise CS 101 | Özyeğin University import acm.program.*; public class ExecutionOrder extends ConsoleProgram { public void run() { int x = 10; /* 1 */ while(x < LIMIT) {/* 2 */ if(x%2 != 0) {/* 3 */ x += 3;/* 4 */ } else { x *= 10;/* 5 */ } private static final int LIMIT = 100; /* 6 */ } What is the order of execution? What is the value of x at the end?

Nested Loops CS 101 | Özyeğin University i:0,j:3 – i:0,j:2 – i:0,j:1 – i:1,j:3 – i:1,j:2 – i:1,j:1 – i:2,j:3 – i:2,j:2 – i:2,j:1 – import acm.program.*; public class Stars extends ConsoleProgram { public void run() { for(int i = 0; i < N; i++){ for(int j = N; j > 0; j--){ print("i:" + i + ",j:" + j + " - "); } private static final int N = 3; }

Exercise CS 101 | Özyeğin University Write a ConsoleProgram that outputs N rows of stars in the format shown below import acm.program.*; public class Stars extends ConsoleProgram { public void run() { /* TODO */ }

Exercise CS 101 | Özyeğin University Write a ConsoleProgram that outputs N rows of stars in the format shown below import acm.program.*; public class Stars extends ConsoleProgram { public void run() { for (int i = 0; i < 10; i++) { for (int j = 0; j <= i; j++) { print("*"); } println(""); }

Exercise CS 101 | Özyeğin University Now modify the program to print out rows of stars in the format shown below import acm.program.*; public class Stars extends ConsoleProgram { public void run() { /* TODO */ }

Exercise CS 101 | Özyeğin University Now modify the program to print out rows of stars in the format shown below import acm.program.*; public class Stars extends ConsoleProgram { public void run() { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10-i ; j++) { print("*"); } println(""); }

Exercise Write a Java program that reads N positive integers and prints the maximum of those.

public class MaxOfNIntegers extends ConsoleProgram { public void run() { println("This program reads " + N + " *positive* integers,"); println("and prints the maximum."); int maximum = 0; for(int i=0; i < N; i++) { int value = readInt("Enter > "); if(value <= 0) { println("Please enter a POSITIVE integer!"); i--; // Decrement i so that we read the number again. } else if(value > maximum) { maximum = value; } println("Maximum is " + maximum); } private static final int N = 5; }

Exercise Write a Java program that reads integers from the users as long as they are positive, and finally prints the maximum of the numbers.

public class MaxOfIntegerList extends ConsoleProgram { public void run() { println("This program reads positive integers,"); println("and prints the maximum."); println("The program exits when the user enters a non-positive value."); int maximum = 0; while(true) { int value = readInt("Enter > "); if(value <= 0) break; if(value > maximum) { maximum = value; } if(maximum == 0) { println("You did not enter any positive integers."); } else { println("Maximum is " + maximum); } private static final int N = 5; }

Exercise Write a Java program that reads N positive integers and prints the maximum two of those.

public void run() { println("This program reads " + N + " *positive* integers,"); println("and prints the *two* maximum."); int maximum = 0; int secondMaximum = 0; for(int i=0; i < N; i++) { int value = readInt("Enter > "); if(value <= 0) { println("Please enter a POSITIVE integer!"); i--; // Decrement i so that we read the number again. } else if(value > maximum) { secondMaximum = maximum; maximum = value; } else if(value > secondMaximum) { secondMaximum = value; } println("Maximum is " + maximum); println("Second maximum is " + secondMaximum); }

Exercise Write a Java program that helps me decide how to travel. – If I'll travel for less than 1 km, I'll walk in any condition. – If I'll travel for less than 5 km, biking is my priority. – If I'll travel for more than 500 km, I'll fly in any condition. – I can ride my bike if the temperature is between 8 and 28 degrees. – I'll drive my car otherwise. Try to use as few boolean conditions as possible, and with exactly 4 println()’s.

public class TravelAssistant extends ConsoleProgram { public void run() { println("This programs helps the programmer"); println("decide on the means of transportation."); int distance = readInt("How many km will you travel? "); int temperature = readInt("What's the temperature? "); if(distance < 1) { println("Walk."); } else if(distance > 500) { println("Fly."); } else if(distance = 8 && temperature <= 28) { println("Bike."); } else { println("Drive."); } }

Exercise Implement the Checkerboard example using a single loop (instead of the nested loop). public class CheckerboardSingleLoop extends GraphicsProgram { public void run() { double sqSize = (double) getHeight() / N_ROWS; for (int n = 0; n < N_ROWS * N_COLUMNS; n++) { double i = n / N_COLUMNS; double j = n % N_COLUMNS; GRect sq = new GRect(j * sqSize, i * sqSize, sqSize, sqSize); add(sq); sq.setFilled((i+j) % 2 == 0); } private static final int N_ROWS = 10; private static final int N_COLUMNS = 8; }

Exercise Implement a graphical Java program in which a ball continuously scrolls from left to right.

public class ScrollingBall extends GraphicsProgram{ // Named constants can be defined at the top or the bottom of the class. private static final int BALL_SIZE = 50; private static final int PAUSE_TIME = 50; public void run() { GOval ball = new GOval((getWidth()-BALL_SIZE)/2, (getHeight()-BALL_SIZE)/2, BALL_SIZE, BALL_SIZE); ball.setFilled(true); ball.setFillColor(Color.RED); add(ball); int dx = 10; while(true) { // forever running! ball.move(dx, 0); if(ball.getX() > getWidth()) ball.setLocation(0, ball.getY()); pause(PAUSE_TIME); } public void init() { setSize(500,300); }