Download presentation
Presentation is loading. Please wait.
1
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 14
2
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); }
3
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); }
4
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); }
5
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); }
6
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 }
7
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 );
9
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) ;
10
Variables x, y, z i, j, k TotalCountOfNewEmployees Geekish_Designations_0123 They Stand for Something
11
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
12
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
13
“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
14
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
15
Illegal int Y; Y = 14.001 String Q; Q = 10; double z; z = “hello”;
16
Illegal int x; double y; String z; x = 12; y = 16.001 z = x + y;
17
3 categories of ILLEGAL 1.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)
18
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?)
20
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 ) {.....
21
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
22
Operators + add - subtract *multiply /divide ||OR &&AND
23
examples if ( ( x == 0 ) || ( y >= 100 ) ) // either has to be true if ( ( z < 6 ) && ( q == 40 ) ) // both have to be true
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.