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.

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.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
Fundamental Programming Structures in Java: Strings.
The Class String. String Constants and Variables  There is no primitive type for strings in Java.  There is a class called String that can be used to.
Characters and Strings. Characters In Java, a char is a primitive type that can hold one single character A character can be: –A letter or digit –A punctuation.
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:
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.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Objectives ► Become familiar with the class String ► ► Learn how to use input and output dialog boxes in a program ► ► Learn how to tokenize the input.
Java Programming Java Basics. Data Types Java has two main categories of data types: –Primitive data types Built in data types Many very similar to C++
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.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
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.
17-Mar-16 Characters and Strings. 2 Characters In Java, a char is a primitive type that can hold one single character A character can be: A letter or.
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.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Summer 2007CISC121 - Prof. McLeod1 CISC121 – Lecture 3 Last time: –Lots of fundamental Java: Console I/O Conditionals Loops For reading: –File I/O –Exceptions.
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.
Last Time Reviewed method overloading. A few useful Java classes:
Chapter 2 Basic Computation
Last Time (Midterm Exam!) Before that – methods. Spring 2006
Static Members and Methods
Yanal Alahmad Java Workshop Yanal Alahmad
Exercise on Java Basics
CISC124 Labs start this week in JEFF 155: Meet your TA.
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
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
OUTPUT STATEMENTS GC 201.
Advanced Programming Behnam Hatami Fall 2017.
CMSC 202 Static Methods.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC124 Quiz 1 marking nears completion!
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
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 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:
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
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
Classes and Objects Static Methods
Winter 2019 CMPE212 4/17/2019 CMPE212 – Reminders
CMPE212 – Reminders Course Web Site:
Last Time Some discussion of program “efficiency”. Arrays – 1D and 2D
CMPE212 – Reminders Quiz 1 marking underway. Assignment 1 due today.
Winter 2019 CMPE212 5/4/2019 CMPE212 – Reminders
Outline Creating Objects The String Class The Random and Math Classes
Unit 3: Variables in Java
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.
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:

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 2. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

What’s Next? Some Odds ‘n Ends before we can start OOP: Classes in java.lang, including the String class. StringTokenizer class. Method overloading. Exceptions and catching them. Arrays in memory. Aliasing objects. Passing objects by reference. Fall 2018 CISC124 - Prof. McLeod

Today Useful Classes in the java.lang package. (All classes in java.lang are imported automatically in every program.) Use the Java API docs as we go! Fall 2018 CISC124 - Prof. McLeod

Some Useful Java Classes The classes defined in the java.lang package are automatically imported for you, since they are used quite often. They include: The Wrapper classes Math Object String System Thread (How do I know all this?...) Fall 2018 CISC124 - Prof. McLeod

Fall 2013 CISC124 Aside - 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 second time (and later) they are invoked. Generally, they are utility methods that do not depend on the values of a class’ attributes. static methods can be invoked without instantiation of the Object that owns them. Math.random(), for example. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

static Methods, Cont. 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. Fall 2018 CISC124 - 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 just look over the API. Fall 2018 CISC124 - Prof. McLeod

Fall 2013 CISC124 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. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

Wrapper Classes - Cont. char Character int Integer long Long float 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 Fall 2018 CISC124 - 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 Fall 2018 CISC124 - 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 for other methods and constants dealing with NaN and -Infinity and Infinity Fall 2018 CISC124 - 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() … Fall 2018 CISC124 - Prof. McLeod

System Class We have used: System.out.println() System.out.print() System.out.printf() Also: System.err.println() Fall 2018 CISC124 - 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 Fall 2018 CISC124 - Prof. McLeod

Strings, so Far String literals: “Press <enter> to continue.” Fall 2013 CISC124 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! Fall 2018 CISC124 - 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 Fall 2018 CISC124 - Prof. McLeod

Strings, so Far - Cont. For example, the code: System.out.println(“Hello\nclass!”); prints the following to the screen: Hello class! Fall 2018 CISC124 - 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) Fall 2018 CISC124 - 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. Fall 2018 CISC124 - 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 Fall 2018 CISC124 - Prof. McLeod

String Class - Cont. char aChar; aChar = testStuff.charAt(2); // aChar is ‘t’ i = testStuff.indexOf(“test”); // i is 2 Fall 2018 CISC124 - 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. Fall 2018 CISC124 - Prof. McLeod

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

Other java.lang Classes Fall 2013 CISC124 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. Fall 2018 CISC124 - 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. Fall 2018 CISC124 - 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) Fall 2018 CISC124 - 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. Fall 2018 CISC124 - 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. Fall 2018 CISC124 - 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 Fall 2018 CISC124 - Prof. McLeod

StringTokenizer Class - Cont. Screen output: The String has 6 tokens. The tokens are: This is a String - Wow! Fall 2018 CISC124 - 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. Fall 2018 CISC124 - 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. Fall 2018 CISC124 - Prof. McLeod

Tokenizing Demo See SystemPropertiesDemo.java Includes some old-fashioned string parsing code that uses String class methods only. Fall 2018 CISC124 - Prof. McLeod