Download presentation
Presentation is loading. Please wait.
Published byWilfred Holmes Modified over 9 years ago
1
10/4: the for loop & the switch structure Primitive data types –why we mention them Return to counter-controlled repetition
2
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 ).
3
Primitive data types: numbers typebitsrange of values short 16-32,768 to +32,768 int 32-2,147,483,648 to +2,147,483,648 long 64-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
4
Primitive data types: other typebitsrange of values boolean 1true or false char 16‘\u0000’ to ‘\uFFFF’ Unicode character set byte 8-128 to +127
5
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.
6
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 { 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 } name of control variable initial value increment condition for final value
7
the for repetition structure A more efficient way of creating a repetition structure: Contains all 4 elements necessary for repetition. for ( int count = 1; count <= 10; count++ ) name of control variable initial value increment condition for final value
8
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 }
9
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 }
10
First 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.
11
Part 2: the switch selection structure looking at Interest.java –use of postincrement –Math.pow( 1.0 + rate, year ) –JTextArea switch multiple-selection structure
12
Interest.java: use of postincrement for ( int year = 1 ; year <= 10 ; year++ ) –would a change to a preincrement cause a changed output? – No, because it executes like it is the only thing happening in a statement: year++ ;
13
Interest.java: Math.pow Math.pow( 1.0 + rate, year ); –Math class method Math.pow( x, y ); –calculates x to the y power: x y –listed in the java.lang.Math libraryjava.lang.Math
14
Interest.java: JTextArea JTextArea : a type of output area. we create a new object ( outputTextArea ) as an instance of the class JTextArea : instantiation. associated method: append – means add onto the JTextArea some type of String output. –the append method works similarly to saying result = result + “\n” + x + “ dollars”; (EX) View more info about JTextArea at java.sun.comjava.sun.com
15
the switch multiple-selection structure previously selection structures: if, if/else switch ( pizzaSlice ) { case 1: System.out.print ( "Take more" ); break; case 2: System.out.print ( “Just right” ); break; default: System.out.print ( "Have some pizza, man!" ); break;
16
switch multiple selection structure consists of case s with labels, including the default case break exits the structure controlling variable’s value –compared to each case label –if no match, the default case is executed can handle integers
17
Second Program: p. 170 SwitchTest after you get it to work, modify the program: –Ask first for a color ( pick 3 colors: black, blue, cyan, darkGray, gray, lightGray, green, magenta, orange, pink, red, white, yellow ) to use in the next step. You must import the java.awt.Color class. Use the following statement example to set the color: g.setColor ( Color.yellow ); –Then ask the user for their choice of lines, hollow rectangles, filled rectangles, hollow ovals, and filled ovals. applicable methods: drawLine, drawRect, fillRect, drawOval, fillOval.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.