Summary of last week / Lab6A int x = 14; GreenfootImage myImage = new GreenfootImage();...
-
date post
22-Dec-2015 -
Category
Documents
-
view
212 -
download
0
Transcript of Summary of last week / Lab6A int x = 14; GreenfootImage myImage = new GreenfootImage();...
summary of last week / Lab6A
int x = 14;GreenfootImage myImage = new GreenfootImage();this.setImage( myImage );this.setLocation( 100, 100 );myImage.setColor( Color.YELLOW );myImage.fill( );myImage.setColor( Color.BLACK );myImage.drawString( String.valueOf( x ), 8, 20 );
100
100 14
public void writeToScreen( String text, int x, int y ) { GreenfootImage myImage = new GreenfootImage(100,
50);
setImage( myImage ); setLocation( x, y );
myImage.setColor(Color.BLACK); myImage.fill();
myImage.setColor(Color.WHITE); myImage.drawString( text, 8, 20); }
public void writeToScreen( int num, int x, int y ) { GreenfootImage myImage = new GreenfootImage(100, 50);
setImage( myImage ); setLocation( x, y );
myImage.setColor(Color.BLACK); myImage.fill();
myImage.setColor(Color.WHITE); myImage.drawString( String.valueOf(num), 8,
20); }
public void writeToScreen( boolean var, int x, int y ) { GreenfootImage myImage = new GreenfootImage(100,
50);
setImage( myImage ); setLocation( x, y );
myImage.setColor(Color.BLACK); myImage.fill();
myImage.setColor(Color.WHITE); myImage.drawString( String.valueOf(var), 8, 20); }
public void writeToScreen( double num, int x, int y ) { GreenfootImage myImage = new GreenfootImage(100, 50);
setImage( myImage ); setLocation( x, y );
myImage.setColor(Color.BLACK); myImage.fill();
myImage.setColor(Color.WHITE); myImage.drawString( String.valueOf(num), 8,
20); }
import java.awt.*;import greenfoot.*;public class Utilities{public void writeToScreen( int num, int x, int y ) { ...... code here }public void writeToScreen( String text, int x, int y ) { ...... code here } public void writeToScreen( double num, int x, int y ) { ...... code here } public void writeToScreen( boolean var, int x, int y ) { ...... code here }}
Utilities myScreen = new Utilities( );
myScreen.writeToScreen( "hello", 100, 100) myScreen.writeToScreen( 10, 200, 200 );myScreen.writeToScreen( 98.6, 300, 300 );myScreen.writeToScreen( true, 400, 400 );
Concatenation (Sequencing)
Computers perform one small step at a time, in a stated order.
z = 0;x = 15;y = 12;z = x + y;image.drawString( String.valueOf( z ), 10, 10) ;
Variables
• x, y, z• i, j, k• TotalCountOfNewEmployees• Geekish_Designations_0123
They Stand for Something
The ESSENCE of a variable
a = 15;b = 12;c = a+b; What is c?
b = 1288;c = a+b; What is c now?a, b, and c hold values and are
CHANGEABLE
x = 14;
• Could mean 14 (not likely)• Could mean 13.999995• Could mean 14.000001• Could mean 20 (honestly), might not be
base-10, more later• Could mean “Fourteen”, which has no
meaning other than to be read by humans
“Typing” a variable
int X = 0;X = 14;Therefore, 14 means 14
double X = 0.0; X = 14.0000;Therefore, X has meaning, 14 to 4 places
Variable Types (primitive, common)
• int : Integers – meant to be a whole number (used for counting)
• double : Floating Pt. – meant to be a detailed number (used for measuring) – called a DOUBLE
• boolean : true or false, meant to hold a state of something
• char : a single Character – has no meaning other than to be read by a human
• String : Strings of Characters – have no meaning other than to be read by a human
3 categories of ILLEGAL1. Variables set to a value wrong for its type:
int x;x = true;
2. Variables of different types used in the same operation:int x, y;String z;z = x * y; (* = multiply)
3. Operation wrong for variable type:boolean Q, R, S;Q = R / S; (/ = divide)
boolean types• boolean – true or false (really means
presence or absence), used to signify a condition, or “state” of being.
boolean X, Y, Z;X = true; (means “X is present”)Y = false; (means “Y is NOT present”)Z = X OR Y (not code / what is Z?, i.e. does
Z signify some presence?)Z = X AND Y (not code / what is Z?)
comparing
int x = 0; int y = 0;int z = 0;if (x < 15 ) { .....if (y >= 25)
if ( z = = 0 ) // note TWO equal signs
boolean IsOK = false;
if ( IsOK = = true ) { .....
Operators=assignment
if ( ) or while ( ) operators:
= = comparison if equal
> comparison greater than
>= comparison greater than or equal to
< comparison less than
<= comparison less than or equal to