CBOP3203 A class library is a set of classes that supports - - PowerPoint PPT Presentation

cbop3203 a class library is a set of classes that
SMART_READER_LITE
LIVE PREVIEW

CBOP3203 A class library is a set of classes that supports - - PowerPoint PPT Presentation

CBOP3203 A class library is a set of classes that supports the development of programs. Java contains an extensive library of pre- written classes you can use in your programs. These classes are divided into groups called packages


slide-1
SLIDE 1

CBOP3203

slide-2
SLIDE 2

 A

class library is a set

  • f

classes that supports the development of programs.

 Java contains an extensive library of pre-

written classes you can use in your programs. These classes are divided into groups called packages.

 Each package defines a number of classes,

interfaces, exceptions, and errors.

 Packages can be split into sub-packages. For

example, the java.lang package has a sub- package called java.lang.reflect.

slide-3
SLIDE 3

 Package

  • Groups class definitions together for privileged access,

lets other classes in the package see more details.

  • Generally a way of grouping a set of library routines.

 Example packages from Java’s standard class

library:

Package Class Use java.util Scanner Random Inputting from keyboard and files Generates random numbers java.lang Math String Various math constants and functions Handling text java.text DecimalFormat Formatting decimal numbers

slide-4
SLIDE 4

Classes and Java library packages

Classes and Java library packages

slide-5
SLIDE 5

 Using Package

  • There are two ways we can use public class in a

package:

  • 1. Using its absolute name; and
  • 2. Using the keyword import.

Using Absolute Name

  • java.util.Date DATE = new java.util.Date();

Using import Keyword

  • import java.util.*;
  • Date DATE = new Date();
slide-6
SLIDE 6

 To use a package in a program, it must first be

imported.

 Add the import declaration at the beginning of a

program, after the package statement and before any other executable code.

  • import packageName.*;

Imports all classes in packageName.

  • import packageName.className;

Imports one className from packageName

  • Example: import java.text.DecimalFormat;

Imports only the DecimalFormat class from the java.text package.

  • The java.lang package does not have to be imported.

String and Math methods are always available.

slide-7
SLIDE 7

//in the Draggable.java file package graphics; public interface Draggable { . . . } //in the Graphic.java file package graphics; public abstract class Graphic { . . . } //in the Circle.java file package graphics; public class Circle extends Graphic implements Draggable { . . . }

slide-8
SLIDE 8

package first; class classB { // most of your code } package first; class classA { classB b = … new classB(); }

Being declared in the same package will allow each class to access each other without extra work. classA using g classB classA.java classB.java

slide-9
SLIDE 9

package second; class classB { // most of your code } package first; import second; class classA { classB b = … new classB(); }

Being declared in the different package will require the use

  • f import to

access each other. classA.java classB.java

slide-10
SLIDE 10

 ja

java va.l .lan ang

  • Provides classes that are fundamental to the design
  • f the Java programming language.
  • The compiler for each Java program automatically

calls this package. It consists of the basic classes for java language like object, String and System. It also consists

  • f

wrapper classes like Integer, Characters and Float that can be used to change the data from the primitive object.

slide-11
SLIDE 11

 ja

java va.uti util

  • The package consists of general-purpose

classes to be used in a class. The general-purpose classes are general data structure, bit class, time, date, day, string manipulation, random number generator, etc.

 ja

java va.io io

  • This package provides classes that deals with input

stream and output to be read and printed into a file, stream and

  • ther

resources like network socket.

slide-12
SLIDE 12

 java.net

  • This package can support network application like URL,

TCP socket, UDP socket, IP address and also binary to text conversion.

 java.awt

  • This package provides interface characteristic like

window, dialog box, button, choices box, list, menu, scroll bar and test space. For more details of java packages visit: http://java.sun.com/javase/7/docs/api/ Or http://doc.java.sun.com/DocWeb/

slide-13
SLIDE 13

 Q1. What access level do you need to specify

in the class declaration to ensure that only classes from the same directory can access it?

 Q2. Create a java package with two classes to

display a message entered by user. You should use the Scanner class in java class library to get the message from the user.