CS 101 Computer Programming

advertisement
CS 101 Computer Programming
Statements
Review++
Reviewed so far..
•
•
•
•
•
•
•
•
int x = 10;
Variables
private static final int SIZE = 50;
Constants
Order of Execution
boolean withinLimits = true;
Boolean expressions and variables
if statements, if-else statements
nested if statements
loops: while statements, for statements
while(x < 100) {
Nested loops
...
if(x < 0 || x > 100) {
withinLimits = false;
CS 101 | Özyeğin University
}
}
while(y < 100) {
...
}
2
Exercise
What is the order of execution? What is the value of x at the end?
import acm.program.*;
public class ExecutionOrder extends ConsoleProgram {
public void run() {
int x = 10;
while(x < LIMIT) {
if(x%2 != 0) {
x += 3;
} else {
x *= 10;
}
}
}
/*
/*
/*
/*
1
2
3
4
*/
*/
*/
*/
/* 5 */
private static final int LIMIT = 100; /* 6 */
}
CS 101 | Özyeğin University
Nested Loops
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;
}
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 –
CS 101 | Özyeğin University
Exercise
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 */
}
}
CS 101 | Özyeğin University
Exercise
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("");
}
}
}
CS 101 | Özyeğin University
Exercise
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 */
}
}
CS 101 | Özyeğin University
Exercise
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("");
}
}
}
CS 101 | Özyeğin University
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 < 5 && temperature >= 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);
}
}
Download