Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
***** 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. 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 ***** • • • • 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: 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 Chapter 3-1 cg 36 ***** SWTJC STEM ***** • • • 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. 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 ***** • • • 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 Manipulation Method length of string length() character at position n charAt(int index) find substring: start m and end n-1 substring(int m, int n) change to upper case toUpperCase() Chapter 3-1 cg 35 ***** SWTJC STEM ***** • String Class Ex. Example from Program “StringMutation” (See text CDROM Chapter 3) // 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 ***** • String Class Ex. Example from Program “StringMutation” (See text CDROM Chapter 3) 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 ***** • • • • • 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 Type Method String nextLine() Byte nextByte() Integer nextInteger() Double nextDouble() Chapter 3-1 cg 32 ***** SWTJC STEM ***** • • • • 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. Chapter 3-1 cg 32 ***** SWTJC STEM ***** • 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 Package Class Dot Operator Chapter 3-1 cg 32 ***** SWTJC STEM ***** • 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 + "\""); } Object Method } Chapter 3-1 cg 32 ***** SWTJC STEM ***** • • Scanner Class Ex. 2 Example 2 Program “CalculateSum” Input tokens on separate line. 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 ***** • • Scanner Class Ex. 3 Example 3 Program “CalculateSumMultiple” Input tokens on same line with white space as delimiter. 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 ***** Scanner Class Ex. 4 import java.util.Scanner; public class Average2Nums { Designates the public static void main(String[] args) { keyboard 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 ); } Casts the sum to double before dividing by 2 } Chapter 2 cg 16 ***** SWTJC STEM ***** Scanner Class Ex. 5 import java.util.Scanner; public class ConvertSeconds { public static void main(String[] args) { Why? int totalSeconds, hours, minutes, seconds; Because Scanner scan = new Scanner(System.in); hours & seconds are integer! 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"); } } Chapter 2 cg 16