lect5UsingClassesAndObjects

lect5UsingClassesAndObjects - Packages CMPT 126 Lecture 5...

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

View Full Document Right Arrow Icon
CMPT 126: Lecture 5 Using Classes and Objects Tamara Smyth, [email protected] 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
Image of page 1

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

View Full Document Right Arrow Icon
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. CMPT 126: Using Classes and Objects, Lecture 5 5 Program Using Scanner import java.util.Scanner; public class RainyDays { public static void main(String[] args) { String blah; Scanner scan = new Scanner(System.in); blah = scan.nextLine(); System.out.println("You input the line: \"" + blah + "\""); int numRainyDays; System.out.println("How many consecutive days has ‘‘ + "it rained in Vancouver?"); numRainyDays = scan.nextInt();
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern