Top Banner
CS 101 Computer Programming Statements Review++
19

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

Dec 13, 2015

Download

Documents

Barbara Bendell
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS 101 Computer Programming Statements Review++. Reviewed so far.. Variables Constants Order of Execution Boolean expressions and variables if statements,

CS 101 Computer Programming

StatementsReview++

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

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 University 2

int x = 10;

private static final int SIZE = 50;

boolean withinLimits = true;

if(x < 0 || x > 100) {withinLimits = false;

}

while(x < 100) {...while(y < 100) {

...}

}

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

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?

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

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;}

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

Exercise

CS 101 | Özyeğin University

Write a ConsoleProgram that outputs N rows of stars in the format shown belowimport acm.program.*;

public class Stars extends ConsoleProgram {

public void run() {

/* TODO */ }

}

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

Exercise

CS 101 | Özyeğin University

Write a ConsoleProgram that outputs N rows of stars in the format shown belowimport 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("");

} }

}

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

Exercise

CS 101 | Özyeğin University

Now modify the program to print out rows of stars in the format shown belowimport acm.program.*;

public class Stars extends ConsoleProgram {

public void run() {

/* TODO */ }

}

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

Exercise

CS 101 | Özyeğin University

Now modify the program to print out rows of stars in the format shown belowimport 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("");

} }

}

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

Exercise

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

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

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.

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

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; }

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

Exercise

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

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

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);}

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

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.

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

Exercise• Implement the Checkerboard example using a single

loop (instead of the nested loop).

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

Exercise• Implement a graphical Java program in which a ball

continuously scrolls from left to right.

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

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); }}