1 / 15

CIS 260: App Dev I

CIS 260: App Dev I. Chapter 3: Intro to Objects & I/O. Objects and Reference Variables. Predefined Java classes you have used: String —for defining and manipulating strings Integer —for defining and manipulating integers Double —for defining and manipulating _________.

glenda
Télécharger la présentation

CIS 260: App Dev I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CIS 260: App Dev I Chapter 3: Intro to Objects & I/O

  2. Objects and Reference Variables • Predefined Java classes you have used: • String—for defining and manipulating strings • Integer—for defining and manipulating integers • Double—for defining and manipulating _________. • Primitive integers and Integer objects • int aNumber = 78; • aNumber is a variable of the __________ data type int • aNumber is a variable (a memory location, say, 2500) that actually holds the value 78 • Integer aNumber = new Integer(78); • aNumber is a __________ variable (a memory location) that stores the memory location where the value 78 is stored • aNumber is called an _________ of the Integer class or an Integer _________

  3. The Operator new • Integer aNumber = new Integer(78); • Theoperator_____does the following: • Assigns the memory location for the integer 78 • Stores 78 in that memory location • Stores the address of that memory location in the reference variable (object) called aNumber • aNumberis an _______ of theIntegerclass • Integer aNumber = new Integer(50);creates a new object calledaNumber, but aNumber points to a different address • Java’s automatic _______ collector will remove the 78 eventually, or you can useSystem.gc();

  4. Predefined Classes and Methods • A ________ is a set of instructions designed to accomplish a specific task. • The method main() runs automatically. • Java has many _________ that contain classes that contain methods (all are predefined). • How to call the pow() method in the classMath: • result = Math.pow(2,3); • // result is 8 = 2 to the 3rd power • // the 2 and 3 are called arguments • ____ notation is used to access class members.

  5. The class String • In Java, a string is not a variable, but an _____. • The following two statements are equivalent: • String name = “Lisa Johnson”; • name = new String(“Lisa Johnson”); • name is actually a reference variable that contains the address of the String ________. • A string contains ___ or more characters enclosed in double quotes. • The ________ of the first character in a string is 0, of the second character is 1, and so on.

  6. Methods in the class String • The method substring() is a member of the class String. • Using String name = “Lisa Johnson”; : • name.substring(0,4) yields _______ • name.substring(5,12) yields “Johnson” • name.indexOf(‘J’) yields ___ • name.charAt(4) yields ‘ ’ • name.equals(“Lisa Johnson”) yields true • name.equals(“Luke Johnson”) yields ______ • name.length() yields ____ • name.replace('i','e') yields “Lesa Johnson” • The String object name has access to the String methods using the ____ operator.

  7. Input/Output • You can input data into a Java program using a simple GUI (____________ user interface). • The class JOptionPane has the method showInputDialog() and is used as follows: name = JOptionPane.showInputDialog(“Enter your name”); • A message can be displayed to the user using the showMessageDialog() method: JOptionPane.showMessageDialog(null,“Hello World!”,”Greetings”,JOptionPane.PLAIN_MESSAGE);

  8. More Input/Output • The class JOptionPane is in the package javax.swing, which must be imported. • A program that uses a GUI must end with the statement System.exit(0) to terminate the GUI “________”. • It is better to create an output string within the program, then use that string in the GUI. JOptionPane.showMessageDialog(null, outputString, guiTitle, JOptionPane.INFORMATION_MESSAGE);

  9. Tokenizing a String • A string can be broken up into parts called ________ using the class StringTokenizer, which is in the package java.util. • Example: StringTokenizer tokenizer = new StringTokenizer(“Richard Johnson”); String firstName = tokenizer.nextToken(); String lastName = tokenizer.nextToken(); • The above example will store “Richard” in firstName and “Johnson” in lastName.

  10. Formatting Output • The class DecimalFormat can format numbers to a specific number of decimal places. • The following statement creates the DecimalFormat object: DecimalFormat twoDecimal = new DecimalFormat("0.00"); • The following statement uses the DecimalFormat object: formattedNumber = twoDecimal.format(number); • If number contains 38.987 then formattedNumber contains ________ (rounding is performed)

  11. File Input/Output • It is often more efficient to get input from and send output to a file on a disk (instead of using the keyboard or ________). • A file is an area in __________ storage that holds information. • To input data from a file use the class FileReader. • To output data to a file use the classes FileWriter and PrintWriter (in the package _________).

  12. More on File Input • This statement creates a FileReader object associated with a specific file on a disk, then reads an entire line of data from the file, storing the line of data in a BufferedReader object. BufferedReader inFile = new BufferedReader(new FileReader(“a:\\prog.dat”)); • If the line contains different data segments, use StringTokenizer to break it up and store data in __________.

  13. Writing to a File • First, create a __________ object and associate it with the destination file. • Then, create a ___________ object using the FileWriter object. • This can be done in a single statement: PrintWriter outFile = new PrintWriter(new FileWriter(“a:\\prog.out”); • You write data to the file with a statement like outFile.println(“The paycheck is: $” + pay); • You _______ the file with outFile.close(); • If the output file doesn’t exist, Java will create it. • If it does exist, Java will overwrite it.

  14. /* * Chapter3Examples.java * Created by Richard Johnson * 9/15/04 * Demonstrates topics in Chapter 3 of Malik and Nair */ import javax.swing.*; // for GUIs import java.util.*; // for tokenizing import java.text.*; // for formatting import java.io.*; // for input/output public class Chapter3Examples { public static void main (String[] args) throws IOException, FileNotFoundException { System.out.println("Welcome to the Chapter 3 Examples\n\n"); // demonstrate simple GUIs String name = JOptionPane.showInputDialog("Enter your first and last name only:"); String message = "Thank you " + name; JOptionPane.showMessageDialog(null,message,"Greetings",JOptionPane.PLAIN_MESSAGE); // demonstrate tokenizing StringTokenizer tokenizer = new StringTokenizer(name); String firstName = tokenizer.nextToken(); // get first token in string String lastName = tokenizer.nextToken(); // get next token in string System.out.println("Your name is: " + firstName + " " + lastName); // demonstrate formatting double firstNumber = 38.9023; DecimalFormat twoDecimal = new DecimalFormat("0.00"); System.out.println(twoDecimal.format("$" + firstNumber));

  15. // demonstrate file output PrintWriter outFile = new PrintWriter(new FileWriter("prog.out")); outFile.println("My name is: " + lastName + ", " + firstName); outFile.close(); // close the file // demonstrate file input BufferedReader inFile = new BufferedReader(new FileReader("prog.out")); StringTokenizer aTokenizer = new StringTokenizer(inFile.readLine()); String junk1 = aTokenizer.nextToken(); // gets the text 'My' String junk2 = aTokenizer.nextToken(); // gets the text 'name' String junk3 = aTokenizer.nextToken(); // gets the text 'is:' String nameLastWithComma = aTokenizer.nextToken(); // gets name w/ comma String nameLast = nameLastWithComma.substring(0, nameLastWithComma.indexOf(",")); // gets name without comma String nameFirst = aTokenizer.nextToken(); System.out.println("My name is: " + nameFirst + " " + nameLast); System.out.println("\nEnd of program\n"); System.exit(0); // terminate GUI thread } }

More Related