Lec05 - CS1101Y: Programming Methodology Using Pre-Built...

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

View Full Document Right Arrow Icon
Using Pre-Built Methods Lecture 5 — September 8, 2008 CS1101Y: Programming Methodology
Background image of page 1

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

View Full DocumentRight Arrow Icon
September 9, 2008 CS1101Y--group2, Lecture 5 2 Chapter 5 - Using Pre-Built Methods The API Library API Headings Math Class Wrapper Classes for Primitive Types Lottery Example Character Class String Methods: substring indexOf lastIndexOf Formatted Output with the printf Method
Background image of page 2
September 9, 2008 CS1101Y--group2, Lecture 5 3 The API Library When working on a programming problem, you should normally check to see if there are pre-built classes that meet your program's needs. If there are such pre-built classes, then use those classes (don't "reinvent the wheel"). For example: User input is a rather complicated task. The Scanner class handles user input. Whenever you need user input in a program, use the Scanner class's input methods (rather than writing and using your own input methods). Math calculations are sometimes rather complicated. The Math class handles math calculations. Whenever you need to perform non-trivial math calculations in a program, use the Math class's methods (rather than writing and using your own math methods).
Background image of page 3

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

View Full DocumentRight Arrow Icon
September 9, 2008 CS1101Y--group2, Lecture 5 4 The API Library Java's pre-built classes are stored in its class library , which is more commonly known as the Application Programming Interface (API) library. See http://java.sun.com/j2se/1.5.0/docs/api. Java's API classes are not part of the core Java language. For a program to use an API class, the class first needs to be loaded/imported into the program. For example, to use the Scanner class, include this at the top of your program: import java.util.Scanner; The java.util thing that precedes Scanner is called a package. A package is a group of classes. The java.util package contains quite a few general-purpose util ity classes. The only one you'll need for now is the Scanner class.
Background image of page 4
September 9, 2008 CS1101Y--group2, Lecture 5 5 The API Library Some classes are considered to be so important that the Java compiler automatically imports them for you. The automatically imported classes are in the java.lang package. The Math class is one of those classes, so there's no need for you to import the Math class if you want to perform math operations. The Java compiler automatically inserts this statement at the top of every Java program: import java.lang.*; The asterisk is a wild card and it means that all classes in the java.lang package are imported, not just the Math class.
Background image of page 5

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

View Full DocumentRight Arrow Icon
September 9, 2008 CS1101Y--group2, Lecture 5 6 API Headings To use an API class, you don't need to know the internals of the class; you just need to know how to "interface" with it.
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 51

Lec05 - CS1101Y: Programming Methodology Using Pre-Built...

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

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