lect5UsingClassesAndObjects

lect5UsingClassesAndObjects - CMPT 126: Lecture 5 Using...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CMPT 126: Lecture 5 Using Classes and Objects Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University October 4, 2007 1 Packages Java is supported by a standard class library, one that we can build upon rather than doing everything from scratch. A class library provides useful support when developing programs. The classes of the Java standard class library are grouped into packages . The String class and the System class are part of the java.lang package. Clusters of related classes are referred to as APIs, application programming interfaces . Examples: Java Database API: Java Swing API: graphical components for a GUI The groups of classes that make up a given API might cross packages. CMPT 126: Using Classes and Objects, Lecture 5 2 The import Declaration To use classes from packages, use the import declaration at the top of the .java file to specifies the packages and classes that will be used in a program. It is not necessary to explicitly import the classes of the java.lang package as they are made available automatically. To use the Scanner class we must include the following statement at the top of the .java file. import java.util.Scanner If we want to include an entire package, we use the asterix: import java.util.*; If two classes with the same name, but from different packages, are used in a program, they must be fully qualified with the package name, or the compiler will complain. CMPT 126: Using Classes and Objects, Lecture 5 3 The Scanner Class You will likely design programs that require reading input interactively from the user while the program is running. The Scanner class provides methods for reading input of various types from various sources, eg, data typed on the keyboard, or stored in a file. Scanner scan = new Scanner(System.in); Scanner fscan = new Scanner(new File(textfile.txt); Note: using the class File requires an explicit import of the java.io.* package. The first declaration creates an object named scan , of type Scanner , with the source of input begin System.in , the standard input stream , which by default is the keyboard. The Scanner object assumes that white space characters (space, tabs, and new lines), called delimiters , are used to separate elements of the input, called tokens . CMPT 126: Using Classes and Objects, Lecture 5 4 Some methods of the Scanner next() : read next input token, until a space is reached nextLine() : read next input token, until an end-of-line is reached. nextInt() : read next input token as type int . nextDouble() : read next input token as type double . useDelimiter(String pattern) : set delimiting pattern....
View Full Document

This note was uploaded on 12/07/2010 for the course CMPT 125 taught by Professor Diana during the Spring '08 term at Simon Fraser.

Page1 / 9

lect5UsingClassesAndObjects - CMPT 126: Lecture 5 Using...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online