Download presentation
Presentation is loading. Please wait.
1
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 29, 2005
2
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 2 Yesterday Variables and expressions Input/output Writing a whole program Any questions?
3
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 3 Today Classes and objects Graphical user interface (GUI) File input/output Formatting input and output
4
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 4 Classes In the Java programming language: ♦ a program is made up of one or more classes ♦ a class contains one or more methods ♦ a method contains program statements
5
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 5 Classes What is a class? ♦ Data ♦ Operations Classes allow creation of new data types public class Student { }
6
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 6 Classes Vs. Data Types Abstract Descriptors ♦ Data Type ♦ Class Concrete Entities ♦ Variable ♦ Object
7
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 7 Inside a class Other classes Data types Methods (operations) public class Student { private String name; private int age; public void ComputeGrade(); }
8
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 8 Primitive Variables int x = 45; x is associated with a memory location. It stores the value 45 When the computer sees x, it knows which memory location to look up the value in
9
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 9 Reference Variables Integer num; The memory location associated with num can store a memory address. The computer will read the address in num and look up an Integer object in that memory location
10
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 10 Creating Objects We use the new operator to create objects, called instantiation Integer num; num = new Integer(78); parameter
11
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 11 Changing the Reference Var num = new Integer (50); The address of the newly-created object is stored in the already-created reference variable num
12
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 12 Garbage Collection What happened to the memory space that held the value 78? If no other reference variable points to that object, Java will "throw it away"
13
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 13 System.out.println (”Hello World!”); object method information provided to the method (parameters) Using Objects System.out object ♦ represents a destination to which we can send output Example: ♦ println method dot operator
14
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 14 Questions 1.True or False. A primitive variable is a variable that stores the address of a memory space. ______ 2.The operator ____ is used to create a class object. 3.In Java, the ________ operator is used to access members of a class. It separates the class (or object) name from the method name. 4.True or False. Class objects are instances of that class. ______ new dot (.) False True
15
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 15 The class String String variables are reference variables Given String name; ♦ Equivalent Statements: name = new String(“Van Helsing”); name = “Van Helsing”;
16
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 16 name = “Van Helsing”; Van Helsing
17
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 17 The class String The String object is an instance of class string The value “Van Helsing” is instantiated The address of the value is stored in name The new operator is unnecessary when instantiating Java strings String methods are called using the dot operator
18
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 18 Common String Methods String(String str) ♦ constructor ♦ creates and initializes the object char charAt(int index) ♦ returns char at the position specified by index (starts at 0) int indexOf(char ch) ♦ returns the index of the first occurrence of ch int compareTo(String str) ♦ returns negative if this string is less than str ♦ returns 0 if this string is the same as str ♦ returns positive if this string is greater than str
19
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 19 Common String Methods boolean equals(String str) ♦ returns true if this string equals str int length() ♦ returns the length of the string String replace(char toBeReplaced, char replacedWith) ♦ returns the string in which every occurrence of toBeReplaced is replaced with replacedWith String toLowerCase() ♦ returns the string that is the the same as this string, but all lower case String toUpperCase() ♦ returns the string that is the same as this string, but all upper case
20
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 20 String Examples String str = “Van Helsing"; System.out.println (str.length()); System.out.println (str.charAt(2)); System.out.println (str.indexOf(‘s'); System.out.println (str.toLowerCase()); n 11 7 van helsing
21
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 21 Using Dialog Boxes for I/O Use a graphical user interface (GUI) class JOptionPane ♦ Contained in package javax.swing ♦ showInputDialog allows user to input a string from the keyboard ♦ showMessageDialog allows the programmer to display results Program must end with System.exit(0);
22
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 22 JOptionPane Methods showInputDialog str = JOptionPane.showInputDialog(strExpression); ♦ stores what the user enters into the String str showMessageDialog JOptionPane.showMessageDialog(parentComponent, strExpression, boxTitleString, messageType);
23
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 23 showMessageDialog parentComponent ♦ parent of the dialog box ♦ we'll use null StrExpression ♦ what you want displayed in the box boxTitleString ♦ title of the dialog box messageType ♦ what icon will be displayed
24
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 24 messageType JOptionPane.ERROR_MESSAGE ♦ error icon JOptionPane.INFORMATION_MESSAGE ♦ information icon JOptionPane.PLAIN_MESSAGE ♦ no icon JOptionPane.QUESTION_MESSAGE ♦ question mark icon JOptionPane.WARNING_MESSAGE ♦ exclamation point icon
25
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 25 JOptionPane Example JOptionPane.showMessageDialog (null, "Hello World!", "Greetings", JOptionPane.INFORMATION_MESSAGE);
26
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 26 UsingGUI.java example
27
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 27 Reading From Text Files Similar to reading from the keyboard Create a BufferedReader object, but use a FileReader object instead of InputStreamReader Create BufferedReader object inside the main method instead of outside Substitute the name of the file for System.in When finished reading from the file, we need to close the file: ♦ BufferedReader close() method
28
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 28 Reading From Text Files String file = "data.dat"; BufferedReader inFile = new BufferedReader (new FileReader (file)); String line = inFile.readLine(); inFile.close();
29
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 29 Exceptions FileNotFoundException ♦ if the file specified to open was not found IOException ♦ some other I/O exception public static void main (String[] args) throws FileNotFoundException, IOException
30
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 30 Writing To Text Files Similar to reading from text files Use FileWriter and PrintWriter instead of FileReader and BufferedReader PrintWriter ♦ methods include print() and println(), just like those we used in System.out Like reading, we need to close the file when we're done ♦ PrintWriter close() method
31
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 31 Writing To Text Files String file = "outfile.dat"; PrintWriter outFile = new PrintWriter (new FileWriter (file)); outFile.print ("Hi"); outFile.println(" There!"); outFile.close();
32
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 32 User Input BufferedReader ♦ reads everything as a string Integer.parseInt ♦ only handles one integer in the string How to handle? Enter 3 numbers: 34 15 75
33
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 33 The StringTokenizer Class tokens ♦ elements that comprise a string tokenizing ♦ process of extracting these elements delimiters ♦ characters that separate one token from another StringTokenizer class ♦ defined in the java.util package ♦ used to separate a string into tokens
34
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 34 Tokens and Delimiters “The weekend is over" "Bart:Lisa:Homer:Marge" delimiter: ' ' (space) tokens: “The’’ “weekend’’ “is’’ “over’’ delimiter: ':' tokens: "Bart" "Lisa" "Homer" "Marge"
35
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 35 The StringTokenizer Class Default delimiters: ♦ space, tab, carriage return, new line Methods ♦ StringTokenizer (String str) ♦ StringTokenizer (String str, String delimits) ♦ String nextToken() ♦ boolean hasMoreTokens() ♦ int countTokens()
36
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 36 Tokenize.java example separated by spaces separated by commas
37
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 37 Formatting the Output of Decimal Numbers float: defaults to 6 decimal places double: defaults to 15 decimal places
38
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 38 class DecimalFormat Import package java.text Create DecimalFormat object and initialize DecimalFormat fmt = new DecimalFormat (formatString); FormatString ♦ "0.00" - limit to 2 decimal places, use 0 if there's no item in that position ♦ "0.##" - limit to 2 decimal places, no trailing 0 Use method format ♦ rounds the number instead of truncating Result of using DecimalFormat is a String
39
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 39 Examples DecimalFormat twoDecimal = new DecimalFormat("0.00"); DecimalFormat fmt = new DecimalFormat("0.##"); System.out.println (twoDecimal.format(56.379));_______ System.out.println (fmt.format(56.379));_______ System.out.println (twoDecimal.format(.3451));_______ System.out.println (fmt.format(.3451));_______ System.out.println (twoDecimal.format(.3));_______ System.out.println (fmt.format(.3));_______ 56.38 0.35 0.30 56.38 0.35 0.3
40
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 40 To do Ch. 3 examples: ♦ Movie Ticket Sale ♦ Donation to Charity ♦ Student Grade Homework 2 due tomorrow. Homework 3 due next Tuesday. Read Ch. 4
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.