the switch selection structure

Download Report

Transcript the switch selection structure

2/25: the switch selection structure
• looking at Interest.java
– NumberFormat
– Locale
– JTextArea
– use of postincrement
– Math.pow( 1.0 + rate, year )
• switch multiple-selection structure
Interest.java (pt.1)
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
public class Interest {
public static void main (String args[] )
{
double amount, principal = 1000.0, rate = 0.05;
NumberFormat moneyFormat =
NumberFormat.getCurrencyInstance( Locale.US );
JTextArea outputTextArea = new JTextArea ();
Interest.java (pt. 2)
outputTextArea.setText
( "Year\tAmount on Deposit\n" );
for ( int year = 1; year <= 10; year++ ) {
amount = principal * Math.pow
( 1.0 + rate, year );
outputTextArea.append ( year + "\t" +
moneyFormat.format ( amount ) + "\n" );
}
JOptionPane.showMessageDialog ( null ,
outputTextArea, "Compound Interest",
JOptionPane.INFORMATION_MESSAGE );
System.exit ( 0 );
}
}
NumberFormat
• A class that helps visually format number values:
– Local settings
– U.S. would print 1,450.00 but
– France would print 1.450,00
• DecimalFormat is a subclass of NumberFormat
• getCurrencyInstance is a method of this class
to format currency values for a particular locale.
Locale
• There are many Locales possible. Ours is
– Locale.US
– Others:
• Locale.GB (Great Britain)
• Locale.VA (Vatican City)
• Locale.TH (Thailand)
• Locale.ZR (Zaire)
• Complete list available at http://userpage.chemie.fuberlin.de/diverse/doc/ISO_3166.html
Interest.java: JTextArea
• JTextArea: a type of output area.
• we create a new object ( outputTextArea ) as an
instance of the class JTextArea: instantiation.
• associated methods:
– setText – set the Text of the JTextArea to some value.
– append – means add onto the JTextArea some type of
String output, similar to saying
result = result + “\n” + x + “ dollars”; (EX)
• View more info about JTextArea at java.sun.com
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
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!" );
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 only handle integers
Today’s Program: p. 211 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.