In the last lesson we discussed about: Boolean data type Shorthand's for arithmetic operations
description
Transcript of In the last lesson we discussed about: Boolean data type Shorthand's for arithmetic operations
![Page 1: In the last lesson we discussed about: Boolean data type Shorthand's for arithmetic operations](https://reader035.fdocuments.in/reader035/viewer/2022070403/56813a25550346895da20399/html5/thumbnails/1.jpg)
• In the last lesson we discussed about:
• Boolean data type• Shorthand's for arithmetic operations• Constants
![Page 2: In the last lesson we discussed about: Boolean data type Shorthand's for arithmetic operations](https://reader035.fdocuments.in/reader035/viewer/2022070403/56813a25550346895da20399/html5/thumbnails/2.jpg)
• We will learn about:
• Using Boolean with conditions• While loop VS For loop• Drawing on the canvas using GLabel• Nested for loops
![Page 3: In the last lesson we discussed about: Boolean data type Shorthand's for arithmetic operations](https://reader035.fdocuments.in/reader035/viewer/2022070403/56813a25550346895da20399/html5/thumbnails/3.jpg)
• It is a indefinite loop.• There are times when you don’t know how
many times you’re going to do something.So you want to use a do while loop.
• It is not infinite!
![Page 4: In the last lesson we discussed about: Boolean data type Shorthand's for arithmetic operations](https://reader035.fdocuments.in/reader035/viewer/2022070403/56813a25550346895da20399/html5/thumbnails/4.jpg)
General form: While (condition) { statements;}
Example:
int x=15;
while (x>1) {
x/=2;
println(x);
}
• 0 is not displayed
731
![Page 5: In the last lesson we discussed about: Boolean data type Shorthand's for arithmetic operations](https://reader035.fdocuments.in/reader035/viewer/2022070403/56813a25550346895da20399/html5/thumbnails/5.jpg)
public class Add extends ConsoleProgram {
private static final int SENTINEL = 0;
public void run() {
int total=0;
int val = readInt(“Enter value:”);
while (val != SENTINEL) {
total+=val; // short hand
val = readInt(“Enter value:”);
}
println(“The total is:” + total + “.”);
}
}
Not a good idea to duplicate code!
![Page 6: In the last lesson we discussed about: Boolean data type Shorthand's for arithmetic operations](https://reader035.fdocuments.in/reader035/viewer/2022070403/56813a25550346895da20399/html5/thumbnails/6.jpg)
public class Add extends ConsoleProgram {
private static final int SENTINAL = 0;
public void run() {
int total=0;
while (true) {
int val = readInt(“Enter value:”);
if (val ==SENTINEL) break;
total+=val;
}
println(“The total is:” + total + “.”);
}
}
![Page 7: In the last lesson we discussed about: Boolean data type Shorthand's for arithmetic operations](https://reader035.fdocuments.in/reader035/viewer/2022070403/56813a25550346895da20399/html5/thumbnails/7.jpg)
![Page 8: In the last lesson we discussed about: Boolean data type Shorthand's for arithmetic operations](https://reader035.fdocuments.in/reader035/viewer/2022070403/56813a25550346895da20399/html5/thumbnails/8.jpg)
General form: if (condition) {
statements
}
if ( ( num % 2 ) ==0 ) {
println(“num is even”); }
else {
println(“number is odd”);
println(“and so are you!”);
}
Its always a good idea to use braces even if there is only one statement
![Page 9: In the last lesson we discussed about: Boolean data type Shorthand's for arithmetic operations](https://reader035.fdocuments.in/reader035/viewer/2022070403/56813a25550346895da20399/html5/thumbnails/9.jpg)
if (score>=90) {
println(“A”);
} else if (score>=80) {
println(“B”);
} else if (score>=70) {
println(“C”);
} else {
println (“Bad times!”);
}
![Page 10: In the last lesson we discussed about: Boolean data type Shorthand's for arithmetic operations](https://reader035.fdocuments.in/reader035/viewer/2022070403/56813a25550346895da20399/html5/thumbnails/10.jpg)
int day = readInt(“Enter Day of the week as an integer:”);
switch (day) {
case 0:
println(“Sunday”);
break;
case 6:
println(“Saturday”);
break;
default:
println(“Weekday”);
break; }
![Page 11: In the last lesson we discussed about: Boolean data type Shorthand's for arithmetic operations](https://reader035.fdocuments.in/reader035/viewer/2022070403/56813a25550346895da20399/html5/thumbnails/11.jpg)
General form:
for ( init; condition; step) {
statements;
}
Init: Done once at the start of the loop (initialization)
Condition: checked before every iteration through loop
Statements get executed if condition is true
![Page 12: In the last lesson we discussed about: Boolean data type Shorthand's for arithmetic operations](https://reader035.fdocuments.in/reader035/viewer/2022070403/56813a25550346895da20399/html5/thumbnails/12.jpg)
Example:
for (int i=0; i<5; i++) {
println(i);
}
• As computer scientists we count from 0• The variable ‘i’ dies after the closing brace
01234
![Page 13: In the last lesson we discussed about: Boolean data type Shorthand's for arithmetic operations](https://reader035.fdocuments.in/reader035/viewer/2022070403/56813a25550346895da20399/html5/thumbnails/13.jpg)
Example:
for (int i=6; i>0; i=-2) {
println(i);
}
• 0 is not displayed
642
![Page 14: In the last lesson we discussed about: Boolean data type Shorthand's for arithmetic operations](https://reader035.fdocuments.in/reader035/viewer/2022070403/56813a25550346895da20399/html5/thumbnails/14.jpg)
• Before beginning to use ACM library you need to download it from:
• http://www-cs-faculty.stanford.edu/~eroberts/jtf/acm.jar
![Page 15: In the last lesson we discussed about: Boolean data type Shorthand's for arithmetic operations](https://reader035.fdocuments.in/reader035/viewer/2022070403/56813a25550346895da20399/html5/thumbnails/15.jpg)
How to setup the acm.jar library
![Page 16: In the last lesson we discussed about: Boolean data type Shorthand's for arithmetic operations](https://reader035.fdocuments.in/reader035/viewer/2022070403/56813a25550346895da20399/html5/thumbnails/16.jpg)
Create a new Project. In this case we call it
“MyGraphics”
Click Next after entering the new
project name
![Page 17: In the last lesson we discussed about: Boolean data type Shorthand's for arithmetic operations](https://reader035.fdocuments.in/reader035/viewer/2022070403/56813a25550346895da20399/html5/thumbnails/17.jpg)
Add External Jar file by browsing to the
location of ACM.JAR
![Page 18: In the last lesson we discussed about: Boolean data type Shorthand's for arithmetic operations](https://reader035.fdocuments.in/reader035/viewer/2022070403/56813a25550346895da20399/html5/thumbnails/18.jpg)
Using acm.program library to display text on Canvas using Glabel class
import acm.program.*;import acm.graphics.*;import java.awt.*;
public class acm extends GraphicsProgram {
public void run() {
GLabel label = new GLabel("hello world!",100,75);label.setColor(Color.RED);label.setFont(“Arial-24”);add(label);}
}