Winter 2019 CMPE212 5/4/2019 CMPE212 – Reminders

Slides:



Advertisements
Similar presentations
Today Quiz in the lab, this week. Assignment 1 due Friday, this week. Continue useful classes from the java.lang package. (We did Math and wrapper classes,
Advertisements

George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Fundamental Programming Structures in Java: Strings.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Unit 3: Java Data Types Math class and String class.
Winter 2006CISC121 - Prof. McLeod1 Last Time Misc. useful classes in Java: –String –StringTokenizer –Math –System.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology- George Koutsogiannakis 1.
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Today… Style, Cont. – Naming Things! Methods and Functions Aside - Python Help System Punctuation Winter 2016CISC101 - Prof. McLeod1.
Today… Quiz 1 Topics and “Rules”. Some useful classes from the java.lang package. Includes: –static methods discussion. –The concept of mutability. Winter.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
Today Review passing by reference and pointers. null pointers. What is an Object? Winter 2016CMPE212 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Today… StringTokenizer class. Method Overloading. Catching Exceptions (and what they are!). Start Pointers and Aliasing. Winter 2016CMPE212 - Prof. McLeod1.
Today… Style hints for Assignment 1. (Nothing else you need from lecture for assn 1.) Designing and writing methods. Some useful classes from the java.lang.
Chapter 2 Basic Computation
JAVA MULTIPLE CHOICE QUESTION.
Last Time (Midterm Exam!) Before that – methods. Spring 2006
Static Members and Methods
Introduction to Computer Science / Procedural – 67130
CISC124 Labs start this week in JEFF 155: Meet your TA.
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
Winter 2018 CMPE212 9/18/2018 CMPE212 – Stuff…
Fall 2017 CISC124 9/18/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
Java Programming: From Problem Analysis to Program Design, 4e
CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week.
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
CMSC 202 Static Methods.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
CSS 161 Fundamentals of Computing Introduction to Computers & Java
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Fall 2018 CISC124 12/1/2018 CISC124 Note that the next assignment, on encapsulation, is due next Wednesday at 7pm – not Friday. The next Quiz is not until.
Classes and Objects 5th Lecture
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Java Classes and Objects 3rd Lecture
CISC124 Assignment 3 due tomorrow at 7pm.
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s will be added to the course web site by the end of the week. Labs start next week in JEFF 155:
Fall 2018 CISC124 2/17/2019 CISC124 Quiz 1 This Week. Topics and format of quiz in last Tuesday’s notes. You are ready for exercises 4 and 5, and assignment.
CISC124 Labs start this week in JEFF 155. Fall 2018
Fall 2018 CISC124 2/22/2019 CISC124 Quiz 1 This Week. Topics and format of quiz in last Tuesday’s notes. The prof. (me!) will start grading the quiz.
OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS
CISC101 Reminders All assignments are now posted.
Winter 2019 CMPE212 4/5/2019 CMPE212 – Reminders
CMPE212 – Reminders Assignment 2 sample solution is posted.
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
Classes and Objects Static Methods
CMPE212 – Reminders Assignment 3 due next Friday.
Winter 2019 CMPE212 4/17/2019 CMPE212 – Reminders
CMPE212 – Reminders Course Web Site:
Last Time Some discussion of program “efficiency”. Arrays – 1D and 2D
CISC101 Reminders All assignments are now posted.
CMPE212 – Reminders Quiz 1 marking underway. Assignment 1 due today.
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Winter 2019 CMPE212 5/10/2019 CMPE212 – Reminders
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
CMPE212 – Reminders Assignment 2 due today, 7pm.
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
CISC101 Reminders Assignment 3 due today.
CMPE212 – Reminders Assignment 2 due next Friday.
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
String Class.
Presentation transcript:

Winter 2019 CMPE212 5/4/2019 CMPE212 – Reminders First quiz in the lab next week, starting Monday. More info in Tuesday’s lecture. You are in two groups in onQ – your Lab and your Grader. Assignment 1 due next Friday. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

Today Useful Classes in the java.lang package, Cont. Math Wrapper Classes System String StringTokenizer in java.util Method Overloading (if we have time) Winter 2019 CMPE212 - Prof. McLeod

java.lang This is the only package (and all its interfaces, enums, classes, Exceptions, Errors and annotations) that is automatically imported into every Java program. If you need something that is in another package (and there are lots of them!), you will need to write a specific import statement for that class or for the entire package containing that class. Your IDE should be able to help you create the appropriate import statement – Eclipse does. Winter 2019 CMPE212 - Prof. McLeod

Fall 2013 CMPE212 Aside - static Methods Many of these java.lang class are utilitarian in nature – they contain many static methods: static attributes and methods are loaded once into memory and not garbage collected until main is finished. These methods will run faster the next time(s) they are invoked. Generally, they are utility methods that do not depend on the values of a class’ attributes. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

static Methods, Cont. static methods can be invoked without instantiation of the Object that owns them. Math.random(), for example. static methods and attributes are shared by all instances of a class – there is only one copy of these methods in memory. A static method can only invoke other static methods in its own class – you can’t have pieces of code disappearing from a static method in memory... This is all done for reasons of ease of use and efficiency. Winter 2019 CMPE212 - Prof. McLeod

Math Class As you would expect: A collection of static constants and static mathematical methods. You cannot instantiate the Math class, but why would you want to? Let’s look over the API Docs. Winter 2019 CMPE212 - Prof. McLeod

Fall 2013 CMPE212 Wrapper Classes Sometimes it is necessary for a primitive type value to be an Object, rather than just a primitive type. Some data structures only store Objects. Some Java methods only work on Objects. Wrapper classes also contain some useful constants and a few handy methods. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

Wrapper Classes - Cont. Each primitive type has an associated wrapper class: Each wrapper class Object can hold the value that would normally be contained in the primitive type variable, but now has a number of useful static methods. char Character int Integer long Long float Float double Double Winter 2019 CMPE212 - Prof. McLeod

Wrapper Classes - Cont. Integer number = new Integer(46); //”Wrapping” Integer num = new Integer(“908”); Integer.MAX_VALUE // gives maximum integer Integer.MIN_VALUE // gives minimum integer Integer.parseInt(“453”) // returns 453 Integer.toString(653) // returns “653” number.equals(num) // returns false int aNumber = number.intValue(); // aNumber is 46 Winter 2019 CMPE212 - Prof. McLeod

Wrapper Classes – Cont. The Double wrapper class has equivalent methods: Double.MAX_VALUE // gives maximum double value Double.MIN_VALUE // gives minimum double value Double.parseDouble(“0.45E-3”) // returns 0.45E-3 See the API Docs for other methods and constants dealing with NaN and -Infinity and Infinity Winter 2019 CMPE212 - Prof. McLeod

Wrapper Classes – Cont. The Character wrapper class: has methods to convert between ASCII and Unicode numeric values and characters. isDigit(character) returns true if character is a digit. isLetter(character) isLetterOrDigit(character) isUpperCase(character) isLowerCase(character) isWhitespace(character) toLowerCase() toUpperCase() … Winter 2019 CMPE212 - Prof. McLeod

System Class We have used: System.out.println() System.out.print() System.out.printf() Also: System.err.println() Winter 2019 CMPE212 - Prof. McLeod

Other Useful System Class Methods System.currentTimeMillis() Returns, as a long, the number of milliseconds elapsed since midnight Jan. 1, 1970. System.exit(0) Immediate termination of your program. System.getProperties() All kinds of system specific info - see the API. System.getProperty(string) Displays single system property. System.nanoTime() Time in nanoseconds Winter 2019 CMPE212 - Prof. McLeod

Strings, so Far String literals: “Press <enter> to continue.” Fall 2013 CMPE212 Strings, so Far String literals: “Press <enter> to continue.” String variable declaration: String testStuff; or: String testStuff = “A testing string.”; String concatenation (“addition”): String testStuff = “Hello”; System.out.println(testStuff + “ to me!”); Would print the following to the console window: Hello to me! Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

Strings - Cont. Escape sequences in Strings: These sequences can be used to put special characters into a String: \” a double quote \’ a single quote \\ a backslash \n a linefeed \r a carriage return \t a tab character Winter 2019 CMPE212 - Prof. McLeod

Strings, so Far - Cont. For example, the code: System.out.println(“Hello\nclass!”); prints the following to the screen: Hello class! Winter 2019 CMPE212 - Prof. McLeod

String Class - Cont. Since String’s are Objects they can have methods. String methods (67 of them!) include: length() equals(OtherString) equalsIgnoreCase(OtherString) toLowerCase() toUpperCase() trim() charAt(Position) substring(Start) substring(Start, End) Winter 2019 CMPE212 - Prof. McLeod

String Class - Cont. String’s do not have any attributes. indexOf(SearchString) replace(oldChar, newChar) startsWith(PrefixString) endsWith(SuffixString) valueOf(integer) String’s do not have any attributes. See the API Docs for details on all the String class methods. Winter 2019 CMPE212 - Prof. McLeod

String Class - Cont. A few examples: int i; boolean aBool; String testStuff = “A testing string.”; i = testStuff.length(); // i is 17 aBool = testStuff.equals(“a testing string.”); // aBool is false aBool = testStuff.equalsIgnoreCase(“A TESTING STRING.”); // aBool is true Winter 2019 CMPE212 - Prof. McLeod

String Class - Cont. char aChar; aChar = testStuff.charAt(2); // aChar is ‘t’ i = testStuff.indexOf(“test”); // i is 2 Winter 2019 CMPE212 - Prof. McLeod

Aside - More about String’s Is “Hello class” (a String literal) an Object? Yup, “Hello class!”.length() would return 12. Also, String’s are immutable – meaning that they cannot be altered, only re-assigned. There are no methods that can alter characters inside a string while leaving the rest alone. Arrays are mutable, in contrast – any element can be changed. Winter 2019 CMPE212 - Prof. McLeod

Aside – More Exercises Exericse 5 (Palindromes) is on strings. Exercise 6 will give you more practice with modular program design and get you thinking about Object Oriented design. Winter 2019 CMPE212 - Prof. McLeod

Other java.lang Classes Fall 2013 CMPE212 Other java.lang Classes Object is the base class for all objects in Java. We’ll need to learn about object hierarchies (Inheritance) for this to make more sense. Thread is a base class used to create threads in multi-threaded program. More about this topic near the end of the course. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

StringTokenizer Class This useful class is in the “java.util” package, so you need to have an import java.util.*; or import.java.util.StringTokenizer; statement at the top of your program. This class provides an easy way of parsing strings up into pieces, called “tokens”. Tokens are separated by “delimiters”, that you can specify, or you can accept a list of default delimiters. Winter 2019 CMPE212 - Prof. McLeod

StringTokenizer Class - Cont. The constructor method for this class is overloaded. So, when you create an Object of type StringTokenizer, you have three options: new StringTokenizer(String s) new StringTokenizer(String s, String delim) new StringTokenizer(String s, String delim, boolean returnTokens) Winter 2019 CMPE212 - Prof. McLeod

StringTokenizer Class - Cont. s is the String you want to “tokenize”. delim is a list of delimiters, by default it is: “ \t\n\r” or space, tab, line feed, carriage return. You can specify your own list of delimiters if you provide a different String for the second parameter. Winter 2019 CMPE212 - Prof. McLeod

StringTokenizer Class - Cont. If you supply a true for the final parameter, then delimiters will also be provided as tokens. The default is false - delimiters are not provided as tokens. Winter 2019 CMPE212 - Prof. McLeod

StringTokenizer Class - Cont. Here is some example code: String aString = "This is a String - Wow!"; StringTokenizer st = new StringTokenizer(aString); System.out.println("The String has " + st.countTokens() + " tokens."); System.out.println("\nThe tokens are:"); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); } // end while Winter 2019 CMPE212 - Prof. McLeod

StringTokenizer Class - Cont. Screen output: The String has 6 tokens. The tokens are: This is a String - Wow! Winter 2019 CMPE212 - Prof. McLeod

StringTokenizer Class - Cont. Note that the StringTokenizer object is emptied out as tokens are removed from it. You will need to re-create the object in order to tokenize it again. Winter 2019 CMPE212 - Prof. McLeod

Scanner Class Tokenizer The Scanner class has a tokenizer built into it. Scanner uses a regular expression or “regex” instead of the (easier to understand, but less powerful!) delimiter list. The default regex is: "\p{javaWhitespace}+" which means “any number of whitespace characters”. A whitespace character is a space, a tab, a linefeed, formfeed or a carriage return. " \t\n\f\r" in other words. Winter 2019 CMPE212 - Prof. McLeod

Tokenizing Demo See SystemPropertiesDemo.java Includes some old-fashioned string parsing code that uses String class methods only. Winter 2019 CMPE212 - Prof. McLeod

Fall 2013 CMPE212 Method Overloading A method can have the same name in many different classes (println(), for example). “Overloading” is when a method name is used more than once in method declarations within the same class. (also like println()…) The rule is that no two methods with the same name within a class can have the same number and/or types of parameters in the method declarations. (The “NOT” rule.) Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

Method Overloading - Cont. Why bother? – Convenience! Java does not have default arguments. Allows the user to call a method without requiring him to supply values for all the parameters. One method name can be used with many different types and combinations of parameters. Allows the programmer to keep an old method definition in the class for “backwards compatibility”. Winter 2019 CMPE212 - Prof. McLeod

Method Overloading - Cont. How does it work? Java looks through all methods until the parameter types match with the list of arguments supplied by the user. If none match, Java tries to cast types in order to get a match. (Only “widening” casting like int to double, however.) Winter 2019 CMPE212 - Prof. McLeod

Method Overloading - Cont. Final notes on overloading: You can have as many overloaded method definitions as you want, as long as they are differentiated by the type and/or number of the parameters listed in the definition. Do not change the return type – that is tacky! See the getInt() and getDouble() overloaded methods in the Exercise 1 IOHelper class for example. Winter 2019 CMPE212 - Prof. McLeod