330 likes | 1.18k Views
***** SWTJC STEM *****. Java Class Libraries & API’s. A class library is a set of classes that supports the development of programs. Java comes with a standard class library , but libraries can be obtained separately from third party vendors.
E N D
***** SWTJC STEM ***** Java Class Libraries & API’s • A class libraryis a set of classes that supports the development of programs. • Java comes with a standard class library, but libraries can be obtained separately from third party vendors. • Classes in class libraries contain methods that are valuable to the programmer. • API’s (Application Programming Interfaces) are clusters of related classes. • Java Database API contains classes that help programmers use databases • Java Swing API contains classes that help programmers use graphical components. Chapter 3-1 cg 36
***** SWTJC STEM ***** • Classes are also grouped in packages. • Packages are more fundamental and language based than API’s. • See text appendix “M” for classes and packages. • Example packages from Java’s standard class library: Java Packages Chapter 3-1 cg 36
***** SWTJC STEM ***** Java Class Packages • To use a package in a program, it must first beimported. • 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 inpackageName. • import packageName.classNameImports on className from packageName • Example: import java.text.DecimalFormat;Imports only the DecimalFormatclass from the java.text package. • The java.lang package does not have to be imported. • String and Mathmethods are always available. Chapter 3-1 cg 36-37
***** SWTJC STEM ***** Declaring An Object • To declare an object, use this statement:ClassName objectName = new ClassName (parameters); • Where: • ClassName is the name of the class of the object. • objectName is your choice of name for the variable object. • “=“ is the assignment operator. • new is a Java reserved word. • ( parameters) is a set of data values use to initialize or setup the object. • Except for Math class objects, all others must be declared before they are used. Chapter 3-1 cg 36-37
***** SWTJC STEM ***** • The String class provides a convenient way to manage and manipulate text. • The String class is in the java.lang package and is automatically imported. • See the chart below for a partial summary of available methods or appendix “M” for a complete list: String Class Chapter 3-1 cg 35
***** SWTJC STEM ***** • Example from Program “StringMutation” (See text CDROM Chapter 3) String Class Ex. • // Variable object declaration • String phrase = "Change is inevitable"; • String mutation1, mutation2, mutation3, mutation4; • System.out.println ("Original string: \"" + phrase + "\""); • System.out.println ("Length of string: " + phrase.length());... Outputs • Original string: "Change is inevitable" • Length of string: 20 Chapter 3-1 cg 35
***** SWTJC STEM ***** • Example from Program “StringMutation” (See text CDROM Chapter 3) String Class Ex. mutation1 = phrase.concat (", except from vending machines."); mutation2 = mutation1.toUpperCase(); mutation3 = mutation2.replace ('E', 'X'); mutation4 = mutation3.substring (3, 30); ...Outputs Mutation #1: Change is inevitable, except from vending machines. Mutation #2: CHANGE IS INEVITABLE, EXCEPT FROM VENDING MACHINES. Mutation #3: CHANGX IS INXVITABLX, XXCXPT FROM VXNDING MACHINXS. Mutation #4: NGX IS INXVITABLX, XXCXPT F Chapter 3-1 cg 35
***** SWTJC STEM ***** • The Scanner class provides a convenient way to read input values of various types. • The Scanner class accepts input from various sources including the keyboard, a file, or a string. • Must be imported from java.util package • For keyboard, use “System.in” in the constructor. • See the chart below for a partial summary of available methodsor appendix “M” for a complete list: Scanner Class Chapter 3-1 cg 32
***** SWTJC STEM ***** • An object declaration looks like this: Scanner scan = new Scanner (System.in); • Declares a “new” object variable named “scan” (your choice of object variable name). • The Scanner class has a special constructor method that constructs or creates this new object. • The parameter “System.in” tells the constructor to use the PC’s keyboard to accept data entry. • The input stream may contain more than one data item, called a token. • Tokens are separated by white space, the default delimiter. • To change the delimiter use useDelimiter(String pattern). • Example: scan.useDelimiter(“,”); changes the delimiter to a comma. Scanner Class Chapter 3-1 cg 32
***** SWTJC STEM ***** • Example Program “Echo” (See text CDROM Chapter 2) Scanner Class Ex. 1 //****************************************************** // Echo.java Author: Lewis/Loftus // // Demonstrates the use of the nextLine method of the Scanner class // to read a string of text from the user. //****************************************************** import java.util.Scanner; // First import the Scanner class Package Class Dot Operator Chapter 3-1 cg 32
***** SWTJC STEM ***** • Example 1 Program “Echo” Scanner Class Ex. 1 public class Echo { //----------------------------------------------------------------- // Reads a character string from the user and prints it. //----------------------------------------------------------------- public static void main (String[] args) { String message; Scanner scan = new Scanner (System.in); // Class objectName = new Class (constructor parameters); System.out.println ("Enter a line of text:"); message = scan.nextLine(); System.out.println ("You entered: \"" + message + "\""); } } Object Method Chapter 3-1 cg 32
***** SWTJC STEM ***** • Example 2 Program “CalculateSum” • Input tokens on separate line. Scanner Class Ex. 2 import java.util.Scanner; public class CalculateSum { public static void main(String[] args) { double firstNum, secondNum, sum; Scanner scan = new Scanner(System.in); System.out.println("Enter first number to add:"); firstNum = scan.nextDouble(); System.out.println("Enter second number to add:"); secondNum = scan.nextDouble(); sum = firstNum + secondNum; System.out.println(firstNum + " + " + secondNum + " = " + sum ); } } Chapter 3-1 cg 32
***** SWTJC STEM ***** • Example 3 Program “CalculateSumMultiple” • Input tokens on same line with white space as delimiter. Scanner Class Ex. 3 import java.util.Scanner; public class CalculateSumMultiple { public static void main(String[] args) { double firstNum, secondNum, sum; Scanner scan = new Scanner(System.in); System.out.println("Enter first and second number to add:"); firstNum = scan.nextDouble(); secondNum = scan.nextDouble(); sum = firstNum + secondNum; System.out.println(firstNum + " + " + secondNum + " = " + sum ); } } Chapter 3-1 cg 32
***** SWTJC STEM ***** import java.util.Scanner; public class Average2Nums { public static void main(String[] args) { int firstNum, secondNum; double average; Scanner scan = new Scanner(System.in); System.out.println("Enter first number to average:"); firstNum = scan.nextInt(); System.out.println("Enter second number to average:"); secondNum = scan.nextInt(); average = (double) (firstNum + secondNum) / 2; System.out.println("Average of " + firstNum + " and " + secondNum + " is " + average ); } } Scanner Class Ex. 4 Designates the keyboard Casts the sum to double before dividing by 2 Chapter 2 cg 16
***** SWTJC STEM ***** import java.util.Scanner; public class ConvertSeconds { public static void main(String[] args) { int totalSeconds, hours, minutes, seconds; Scanner scan = new Scanner(System.in); totalSeconds = scan.nextInt(); seconds = totalSeconds; hours = seconds / 3600; // Integer division seconds = seconds % 3600; //modulo minutes = seconds / 60; seconds = seconds % 60; System.out.println(totalSeconds + " sec is " + hours + " hours, " + minutes + " minutes, and " + seconds + " seconds"); } } Scanner Class Ex. 5 Why?Becausehours & seconds are integer! Chapter 2 cg 16