CSE110 Principles of Programming with Java Lecture 07: Input and Formatted Output Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 4 Java API Packages • The classes of the Java standard class library are organized into packages • java.lang • java.net java.util • java.awt • javax.swing • When you want to use a class from a package, you can import one class or all classes from the package into your program
Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 5 Java API Packages • All classes of the java.lang package are imported automatically into all programs • Other classes should be explicitly imported. Examples: import javax.swing.JOptionPane; import javax.swing.*;
Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 11 printf Method printf() method in System.out can be used to specify how many digits for a floating-point number should be display. Ex: double number = 1.0/3.0; System.out.printf("%.2f\n", number); //0.33 System.out.printf("%7.3f\n", 89.123456789); //89.123
Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 12 printf Method • %d à to print an integer • %5d à to print an integer with width 5 • %7.2f à to print a floating point number with 2 digits after the decimal point and with width 7 (including the decimal point) • %s à to print a string
CSE110 - Principles of Programming Javier Gonzalez-Sanchez [email protected] Summer 2022 Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.