ppt - AD Book Enterprises

Download Report

Transcript ppt - AD Book Enterprises

10/5: Primitives, the for loop
• Primitive data types
– why we mention them
• Return to counter-controlled repetition
Primitives: why we mention them
• recall Average2.java (p. 132) program:
average = ( double ) total / gradeCounter ;
• cast operators are used to explicitly promote (or
change) one primitive type to another.
• total and gradeCounter are both of type int. To
return a non-int result from the equation, we need
to specify a different type (ex: double).
Primitive data types: numbers
type
bits
16
32
64
range of values
• short
-32,768 to +32,768
• int
-2,147,483,648 to +2,147,483,648
• long
-9,223,372,036,854,775,808 to
+9,223,372,036,854,775,808
• float
32 -3.40292347E+38 to
+3.40292347E+38
• double 64 -1.79769313486231570E+308 to
+1.79769313486231570E+308
Primitive data types: other
type
• boolean
• char
• byte
bits range of values
1 true or false
16 ‘\u0000’ to ‘\uFFFF’
Unicode character set
8 -128 to +127
Counter-controlled repetition: theory
• Counter-controlled repetition requires:
1. the name of a control variable (loop counter);
2. the initial value of the control variable;
3. the increment (or decrement) by which the control
variable is modified each pass through the loop.
4. the condition that tests for the final value of the
control variable.
Example: draw ten rectangles.
//a while loop using counter-controlled repetition.
import javax.swing.JApplet;
//import JApplet class
import java.awt.Graphics;
//import Graphics class
public class WhileCounter extends JApplet { //JApplet is superclass
public void paint ( Graphics g )
//method for drawing
name of control variable
{
int count = 1;
//counting repetitions
initial value
int place = 25;
//starting location for rectangle
while ( count <= 10 ) {
condition for
g.drawRect ( place , place , 40 , 40 ); final value
++count;
//increment count
place += 15; //bump up place by 15
}
}
}
increment
the for repetition structure
• A more efficient way of creating a repetition
structure:
• Contains all 4 elements necessary for repetition.
condition for final value
for ( int count = 1; count <= 10; count++ )
name of
control
variable
increment
initial value
What will be replaced.
//a while loop using counter-controlled repetition.
import javax.swing.JApplet;
//import JApplet class
import java.awt.Graphics;
//import Graphics class
public class WhileCounter extends JApplet { //JApplet is superclass
public void paint ( Graphics g )
//method for drawing
{
int count = 1;
//counting repetitions
int place = 25;
//starting location for rectangle
while ( count <= 10 ) {
g.drawRect ( place , place , 40 , 40 );
++count;
//increment count
place += 15; //bump up place by 15
}
}
}
the ‘for’ version
//a ‘for’ loop alternative for counter-controlled repetition.
import javax.swing.JApplet;
//import JApplet class
import java.awt.Graphics;
//import Graphics class
public class ForCounter extends JApplet { //JApplet is superclass
public void paint ( Graphics g )
//method for drawing
{
int place = 25;
//starting location for rectangle
for ( int count = 1 ; count <= 10 ; ++count ) {
g.drawRect ( place , place , 40 , 40 );
place += 15; //bump up place by 15
}
}
}
Using for loops for animation: pt. 1
//a for loop as an alternative for counter-controlled repetition.
import javax.swing.JApplet;
import java.awt.Graphics;
import java.awt.Color;
//import JApplet class
//import Graphics class
//import Color class
public class ForCounter3 extends JApplet { //JApplet is superclass
public void paint ( Graphics g )
//method for drawing
{
int xPos = 25;
//starting x location for rectangle
int yPos = 25;
//starting y location for rectangle
boolean reverseX = false; //"which way to go" trigger
boolean reverseY = false; //"which way to go" trigger
get java file: ForCounter3
Using for loops for animation: pt. 2
for ( int count = 1 ; count <= 200 ; ++count ) {
g.setColor(Color.white);
g.drawRect ( xPos , yPos , 40 , 40 );
if ( xPos < 300 && reverseX == false )
xPos += 15;
//bump up xPos by 15 if xPos < 300
else {
xPos -= 5;
//reduce xPos by 12 if xPos >= 300
reverseX = true ;
if ( xPos < 10 )
reverseX = false ;
}
get java file: ForCounter3
Using for loops for animation: pt. 3
if ( yPos < 250 && reverseY == false )
yPos += 10; //bump up yPos by 10 if yPos < 250
else {
yPos -= 7;
//reduce yPos by 7 if yPos >= 250
reverseY = true;
if ( yPos < 10 )
reverseY = false;
}
g.setColor(Color.blue);
g.drawRect ( xPos , yPos , 40 , 40 );
//just to slow it down and look like animation
for ( byte i = 1 ; i < 100 ; i++ )
repaint();
}
get java file: ForCounter3
Program of the day
• pg. 167 Interest.java
• After you successfully run the program, alter it to
use a “while” loop instead of a “for” loop.