Presentation is loading. Please wait.

Presentation is loading. Please wait.

2/25: the switch selection structure looking at Interest.java –NumberFormat –Locale –JTextArea –use of postincrement –Math.pow( 1.0 + rate, year ) switch.

Similar presentations


Presentation on theme: "2/25: the switch selection structure looking at Interest.java –NumberFormat –Locale –JTextArea –use of postincrement –Math.pow( 1.0 + rate, year ) switch."— Presentation transcript:

1 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

2 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 ();

3 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 ); }

4 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.

5 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.fu- berlin.de/diverse/doc/ISO_3166.htmlhttp://userpage.chemie.fu- berlin.de/diverse/doc/ISO_3166.html

6 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.comjava.sun.com

7 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++ ;

8 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

9 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; }

10 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 only handle integers

11 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.


Download ppt "2/25: the switch selection structure looking at Interest.java –NumberFormat –Locale –JTextArea –use of postincrement –Math.pow( 1.0 + rate, year ) switch."

Similar presentations


Ads by Google