Presentation is loading. Please wait.

Presentation is loading. Please wait.

***** SWTJC STEM ***** Chapter 3-1 cg 36 Java Class Libraries & API’s A class library is a set of classes that supports the development of programs. Java.

Similar presentations


Presentation on theme: "***** SWTJC STEM ***** Chapter 3-1 cg 36 Java Class Libraries & API’s A class library is a set of classes that supports the development of programs. Java."— Presentation transcript:

1 ***** SWTJC STEM ***** Chapter 3-1 cg 36 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. 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.

2 ***** SWTJC STEM ***** Chapter 3-1 cg 36 Java Packages 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: PackageClassUse java.utilScanner Random Inputting from keyboard and files Generates random numbers java.langMath String Various math constants and functions Handling text java.textDecimalFormatFormatting decimal numbers

3 ***** SWTJC STEM ***** Chapter 3-1 cg 36-37 Java Class Packages 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 on 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.

4 ***** SWTJC STEM ***** Chapter 3-1 cg 36-37 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.

5 ***** SWTJC STEM ***** Chapter 3-1 cg 35 String Class 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 ManipulationMethod length of stringlength() character at position ncharAt(int index) find substring: start m and end n-1substring(int m, int n) change to upper casetoUpperCase()

6 ***** SWTJC STEM ***** Chapter 3-1 cg 35 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 Example from Program “StringMutation” (See text CDROM Chapter 3)

7 ***** SWTJC STEM ***** Chapter 3-1 cg 35 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 Example from Program “StringMutation” (See text CDROM Chapter 3)

8 ***** SWTJC STEM ***** Chapter 3-1 cg 32 Scanner Class 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 methods or appendix “M” for a complete list: Data TypeMethod StringnextLine() BytenextByte() IntegernextInteger() DoublenextDouble()

9 ***** SWTJC STEM ***** Chapter 3-1 cg 32 Scanner Class 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.

10 ***** SWTJC STEM ***** Chapter 3-1 cg 32 Scanner Class Ex. 1 Example Program “Echo” (See text CDROM Chapter 2) //****************************************************** // 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 Class Package Dot Operator

11 ***** SWTJC STEM ***** Chapter 3-1 cg 32 Scanner Class Ex. 1 Example 1 Program “Echo” 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 + "\""); } ObjectMethod

12 ***** SWTJC STEM ***** Chapter 3-1 cg 32 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 ); } Example 2 Program “CalculateSum” Input tokens on separate line.

13 ***** SWTJC STEM ***** Chapter 3-1 cg 32 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 ); } Example 3 Program “CalculateSumMultiple” Input tokens on same line with white space as delimiter.

14 ***** SWTJC STEM ***** Chapter 2 cg 16 Scanner Class Ex. 4 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 ); } Designates the keyboard Casts the sum to double before dividing by 2

15 ***** SWTJC STEM ***** Chapter 2 cg 16 Scanner Class Ex. 5 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"); } Why? Because hours & seconds are integer!


Download ppt "***** SWTJC STEM ***** Chapter 3-1 cg 36 Java Class Libraries & API’s A class library is a set of classes that supports the development of programs. Java."

Similar presentations


Ads by Google