Sections 2.7-2.8 and input
Download
Report
Transcript Sections 2.7-2.8 and input
CSCI 1100/1202
January 23, 2002
Class Methods
• Some methods can be invoked through the class
name, instead of through an object of the class
• These methods are called class methods or
static methods
• The Math class (defined in java.lang) contains
many static methods that provide mathematical
functions
temp = Math.cos(90) + Math.sqrt(delta);
• See pages 710-712 of the text for a complete
listing of available methods
Formatting Output
• The NumberFormat class has static
methods that return a formatter object
getCurrencyInstance()
getPercentInstance()
• Each formatter object has a method
called format that returns a string with
the specified information in the
appropriate format
• See Price.java
Formatting Output
• The DecimalFormat class can be used to
format a floating point value in generic ways
• For example, you can specify that the number
be printed to three decimal places
• The constructor of the DecimalFormat class
takes a string that represents a pattern for the
formatted number
• Instantiated in traditional way with new operator
• See CircleStats.java
Input
• We will not be using the Keyboard class
mentioned in the text.
• Refer to:
– Notes on input handed out on January 18th
– Back of program template sheet for sample
code.
Changes to enable input
• Need to import the classes that provide
us with services for input.
• Need to deal with the errors that may
occur during user input.
• Need to instantiate the objects necessary
to handle the flow of data from the
keyboard to the program.
Input Template
import java.io.*;
public class MyProgram
{
public static void main (String[] args) throws IOException
{
InputStreamReader reader = new InputStreamReader(System.in);
bufferedReader input = new BufferedReader(reader);
System.out.print(“Prompt for input:”);
String inputString = input.readline();
}
}
How input works
• InputStreamReader reader = new InputStreamReader(System.in);
– The object reader is an instance of the
InputStreamReader class and is bound to the
system input stream System.in
– Will convey data from the keyboard to the
program
• BufferedReader input = new BufferedReader(reader);
– The object input is an instance of the
BufferedReader class and is bound to the
reader object.
– BufferedReader offers useful services like
readLine()
How input works
• System.out.print(“Prompt for input:”);
– The user needs to know that input is
expected.
• String inputString = input.readline();
– The readline() method of BufferedReader
reads a line of input from the keyboard.
– It reads up to a \n character (does not
include the newline character in the String
object it returns).
• See TextInput.java
Converting text to a number
• The readLine() method of BufferedReader
returns a String object
• Need to convert it to an integer if integer input
is required:
Integer integerObject = new Integer(text);
int integerPrimitive = integerObject.intValue();
• Can combine into one statement:
int number = new Integer(text).intValue();
• Similar for converting to a double (use the
Double class)
• See IntegerInput.java
Program Statements
• We will now examine some other program
statements
• Chapter 3 focuses on:
– the flow of control through a method
– decision-making statements
– operators for making complex decisions
– repetition statements
– software development stages