Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 CSCI 1100/1202 January 23, 2002

2 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

3 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.javaPrice.java

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

5 Input We will not be using the Keyboard class mentioned in the text. Refer to: –Notes on input handed out on January 18 th –Back of program template sheet for sample code.

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

7 Input Template public class MyProgram {}{} public static void main (String[] args) {}{} import java.io.*; InputStreamReader reader = new InputStreamReader(System.in); bufferedReader input = new BufferedReader(reader); System.out.print(“Prompt for input:”); String inputString = input.readline(); throws IOException

8 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()

9 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

10 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

11 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


Download ppt "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."

Similar presentations


Ads by Google