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
Slide 2
Slide 2 text
Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 3
Program
MyProgram
Java API
(Application Programming Interface)
Classes inside Packages = Files inside folders
String
Math
System.out
java.lang java.util java.net javax.swing
Slide 3
Slide 3 text
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
Slide 4
Slide 4 text
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.*;
Slide 5
Slide 5 text
Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 6
java.swing.JOptionPane
Slide 6
Slide 6 text
Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 7
java.swing.JOptionPane
Slide 7
Slide 7 text
Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 8
Parsing
import javax.swing.JOptionPane;
public class Lecture06 {
public static void main(String [] args) {
String test1= JOptionPane.showInputDialog(”write here:");
int value1 = Integer.parseInt(test1);
double value2 = Double.parseDouble(test1);
System.out.println(value1);
System.out.println(value2);
}
}
Slide 8
Slide 8 text
Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 9
Integer.parseInt
Slide 9
Slide 9 text
Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 10
Integer.parseInt
Slide 10
Slide 10 text
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
Slide 11
Slide 11 text
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
Slide 12
Slide 12 text
Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 13
Example
String str = "apples";
int num = 5;
double price = 3.477;
System.out.printf("%d %s cost $%.2f\n", num, str, price);
//The above prints:
//5 apples cost $3.48
Slide 13
Slide 13 text
Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 14
Reference
Textbook – Pages 57 and 328
Slide 14
Slide 14 text
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.