1 / 11

Input/Output Formats in Java: Currency, Percentages, and Interactivity

Learn how to apply currency and percentage formats to program output in Java, as well as how to create interactivity using keyboard input and dialog boxes.

tjason
Download Presentation

Input/Output Formats in Java: Currency, Percentages, and Interactivity

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3 Input/Output

  2. Apply formats (currency and percentages) to output. • Use keyboard input in Java program to create interactivity. • Use dialog box input/output in Java program to create interactivity. • Associate import statements with corresponding input/output/format classes/packages. Objectives

  3. Input data from the Java console: Note: In order to use this class, you must use the following import statement: import java.util.Scanner; Scanner Class

  4. Scanner Age Example:

  5. Scanner Addition Example:

  6. An input dialog box asks a question and uses a box for entering a response. String response = JOptionPane.showInputDialog(null, "Enter Fahrenheit"); If this data is to be used in a calculation, it will need to be converted from String data to double data or integer data with one of the following statements: double fahrenheit = Double.parseDouble(response); Note: In order to use this class, you must use the following import statement: import javax.swing.JOptionPane; Input Dialog Boxes

  7. Uses a simple window to display (output) information. JOptionPane.showMessageDialog(null, "Fahrenheit: " + fahrenheit + "\nCelsius: " + celsius); Note: In order to use this class, you must use the following import statement: import javax.swing.JOptionPane; Message Dialog Boxes

  8. JOptionPane Addition Example

  9. JOptionPane Addition Output

  10. Used to format output as currency. Currency Example:  NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance( ); System.out.println(currencyFormatter.format(20.5) ); Output: $20.50 Note: In order to use this NumberFormat class, you must use the import statement: import java.text.NumberFormat; NumberFormat Class

  11. Used to format output with a specific number of digits before and after the decimal point. double fahrenheit = 212.5; DecimalFormat patternFormatter = new DecimalFormat ("#,###.00"); System.out.println(“The temperature is “ + patternFormatter.format(fahrenheit));  Output: The temperature is 212.50 Note: In order to use this NumberFormat class, you must use the following import statement: import java.text.DecimalFormat; DecimalFormat Class

More Related