Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS 260: App Dev I. 2 Objects and Reference Variables n Predefined Java classes you have used:  String —for defining and manipulating strings  Integer.

Similar presentations


Presentation on theme: "CIS 260: App Dev I. 2 Objects and Reference Variables n Predefined Java classes you have used:  String —for defining and manipulating strings  Integer."— Presentation transcript:

1 CIS 260: App Dev I

2 2 Objects and Reference Variables n Predefined Java classes you have used:  String —for defining and manipulating strings  Integer —for defining and manipulating integers  Double —for defining and manipulating _________. n 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 3 The Operator new Integer aNumber = new Integer(78); The operator _____ 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 aNumber is an _______ of the Integer class Integer aNumber = new Integer(50); creates a new object called aNumber, but aNumber points to a different address Java’s automatic _______ collector will remove the 78 eventually, or you can use System.gc();

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

5 5 The class String n In Java, a string is not a variable, but an _____. n 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 ________. n A string contains ___ or more characters enclosed in double quotes. n The ________ of the first character in a string is 0, of the second character is 1, and so on.

6 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 7 Input/Output n 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 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 “________”. n 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 9 Tokenizing a String A string can be broken up into parts called ________ using the class StringTokenizer, which is in the package java.util. n 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 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 11 File Input/Output n It is often more efficient to get input from and send output to a file on a disk (instead of using the keyboard or ________). n 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 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 13 Writing to a File n First, create a __________ object and associate it with the destination file. Then, create a ___________ object using the FileWriter object. n This can be done in a single statement: PrintWriter outFile = new PrintWriter(new FileWriter(“a:\\prog.out”); n You write data to the file with a statement like outFile.println(“The paycheck is: $” + pay); You _______ the file with outFile.close(); n If the output file doesn’t exist, Java will create it. n 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 }


Download ppt "CIS 260: App Dev I. 2 Objects and Reference Variables n Predefined Java classes you have used:  String —for defining and manipulating strings  Integer."

Similar presentations


Ads by Google