Transcript Variables1

Agenda March 8
1. PowerPoint Presentation on Variables. For download:
http://members.shaw.ca/rzamar/IT11/Variables1.ppt
http://members.shaw.ca/rzamar/IT11/VariableNotes.doc
2. Mastery Questions (5.2) on page 38. Download the Java
source file ex52.java from:
http://members.shaw.ca/rzamar/IT11/Unit5/ex52.java
3. Mastery Questions (5.3) on page 39. Download the Java
source file ex53.java from:
http://members.shaw.ca/rzamar/IT11/Unit5/ex53.java
4. Mastery Questions (5.4) on page 40. Download the Java
source file ex54.java from:
http://members.shaw.ca/rzamar/IT11/Unit5/ex54.java
The memory of a program is lot like a
warehouse full of boxes.
A warehouse uses different boxes to
store different types of items.
If you are going to store soccer balls you need
to use cubed shaped boxes. If you are going to
store hockey sticks you need long rectangular
shaped boxes.
The type of boxes you use depend on the type
of items you need to store.
A program uses different types of
variables to store different types of
values.
If you want to store an integer value in your
program you need to store it in a variable of type
integer. If you want to store a string value in
your program you need to store it in a variable
of type string.
The boxes represent variables & the actual
items being stored are specific values.
Example of an Integer Variable
Example of a String Variable
Analogy Recap.
Variable Types (Table On Page 38)
int num1 = 5;
// can store integers
long num2 = 1332223423;
// can store large integers
double num3 = 3.14159;
// can store decimal numbers
char myChar = ‘c’;
// can only store single characters
String myString = “Hello!”;
// can store many characters
boolean myBool = false;
// can only store two values
Drawing Variables
Different ways you can draw strings on your Applet:
public void paint(Graphics screen)
{
screen.setColor(Color.blue);
screen.drawString(“I am a blue string”, 25, 35);
String s = “I am a gray string”;
int x = 50;
int y = 80;
screen.setColor(Color.gray);
screen.drawString(s + “!”, x, y);
int myIntValue = 7;
screen.setColor(Color.black);
screen.drawString(“” + myIntValue, 100, 100);
}
Drawing Variables Continued.
The output produced from previous paint method is
below. The rectangle represents the Applet window.
I am a blue string
I am a gray string!
7
(25,35)
(50,80)
(100,100)
shown
Re-Using Your Variables
// declare you variables here
String country;
public void init()
{
// initialize your variables here
country = “Canada”;
}
public void paint (Graphics screen)
{
screen.drawString (country, 30, 30); // draws the word Canada
country = “Spain”; // country now holds the value “Spain”
screen.drawString (“I live in ”+ country + “!”, 30, 50);
}
I live in Spain!
Drawing Integers
// declare my variables
int myAge;
String myName;
public void init()
{
// initialize variables
myAge = 10 * 3;
myName = “Zamar”;
}
public void paint (Graphics screen)
{
// print my variables out to the screen
screen.drawString (“My name is Mr. ” + myName + “.”, 30, 30);
screen.drawString (“I am ” + myAge + “ years old.”, 30, 50);
}
My name is Mr. Zamar.
I am 30 years old.