ppt - AD Book Enterprises

Download Report

Transcript ppt - AD Book Enterprises

10/9: the switch selection structure
• looking at Interest.java
– use of postincrement
– Math.pow( 1.0 + rate, year )
– JTextArea
• switch multiple-selection structure
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++ ;
Interest.java: Math.pow
• Math.pow( 1.0 + rate, year );
– Math class method Math.pow( x , y );
y
– calculates x to the y power: x
– listed in the java.lang.Math library
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.com
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;
switch multiple selection structure
• consists of cases 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
Today’s 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.