DOC PREVIEW
UNC-Chapel Hill COMP 14 - LECTURE NOTES

This preview shows page 1-2-3-19-20-38-39-40 out of 40 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

COMP 14 Introduction to Programming Adrian Ilie June 29 2005 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Yesterday Variables and expressions Input output Writing a whole program Any questions 2 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Today Classes and objects Graphical user interface GUI File input output Formatting input and output 3 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 4 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Classes What is a class Data Operations Classes allow creation of new data types public class Student 5 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Classes Vs Data Types Abstract Descriptors 6 Concrete Entities Data Type Variable Class Object Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Inside a class Other classes Data types Methods operations public class Student private String name private int age public void ComputeGrade 7 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 8 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 9 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Creating Objects We use the new operator to create objects called instantiation parameter Integer num num new Integer 78 10 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Changing the Reference Var num new Integer 50 The address of the newly created object is stored in the alreadycreated reference variable num 11 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 12 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Using Objects System out object represents a destination to which we can send output Example println method System out println Hello World information provided to the method method parameters dot operator object 13 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Questions 1 True or False A primitive variable is a variable that stores the address of a memory space False 2 The operator new is used to create a class object 3 In Java the operator is used to access dot 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 True 14 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL The class String String variables are reference variables Given String name Equivalent Statements name new String Van Helsing name Van Helsing 15 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL name Van Helsing Van Helsing Van Helsing Van Helsing 16 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 17 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 18 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 19 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL String Examples String str Van Helsing System out println str length 11 System out println str charAt 2 n System out println str indexOf s System out println str toLowerCase 20 Adrian Ilie 7 van helsing The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 21 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL JOptionPane Methods showInputDialog str JOptionPane showInputDialog strExpression stores what the user enters into the String str showMessageDialog JOptionPane showMessageDialog parentComponent strExpression boxTitleString messageType 22 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 23 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 24 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL JOptionPane Example JOptionPane showMessageDialog null Hello World Greetings JOptionPane INFORMATION MESSAGE 25 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL UsingGUI java example 26 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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


View Full Document

UNC-Chapel Hill COMP 14 - LECTURE NOTES

Download LECTURE NOTES
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view LECTURE NOTES and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view LECTURE NOTES and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?