chapter5 - Chapter5UsingPreBuiltMethods 1 1 TheAPILibrary...

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

View Full Document Right Arrow Icon
1 Chapter 5 - Using Pre-Built Methods The API Library API Headings Math  Class Wrapper Classes for Primitive Types Lottery Example String  Methods: substring indexOf lastIndexOf Formatted Output with  the  printf  Method 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 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). 1 2
Background image of page 2
3 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/javase/6/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. 1
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 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. 1 2
Background image of page 4
5 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. To interface with a class, you need to know how to use 
Background image of page 5

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

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

Page1 / 39

chapter5 - Chapter5UsingPreBuiltMethods 1 1 TheAPILibrary...

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

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