COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Advertisements

1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text I/O.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Exception Handling (in a nutshell). 2 Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle the.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Chapter 10 Classes Continued
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Exception Handling in Java Course Lecture Slides 7 th July 2010 “ Admitting.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 14 Exception Handling and Text.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 12 Exception Handling and Text.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Assertions Program correctness. Assertions Java statement – enables you to assert an assumption about your program. – An assertion contains a Boolean.
Chapter 12 Inheritance and Exceptions Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 15 Exceptions and.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
1 The String Class F Constructing a String: F Obtaining String length and Retrieving Individual Characters in a string F String Concatenation (concat)
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 Chapter 4 Inheritance and Polymorphism. 2 Objectives u To develop a subclass from a superclass through inheritance. u To invoke the superclass’s constructors.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
1 Chapter 15 Exceptions and Assertions. 2 Objectives F To know what is exception and what is exception handling (§15.2). F To distinguish exception types:
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 15 Exceptions and Assertions Nothing is impossible.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
1 Chapter 2 Inheritance and Polymorphism. 2 Objectives u To develop a subclass from a superclass through inheritance. u To invoke the superclass’s constructors.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
Modern Programming Tools And Techniques-I
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
Chapter 12 Exception Handling and Text IO
Chapter 9 Inheritance and Polymorphism
Java Programming Language
Chapter 9 Strings and Text I/O
Chapter 9 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 2
Chapter 12 Exception Handling and Text IO Part 1
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 12 Exception Handling and Text IO Part 2
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi

Chapter 9: String + Files

 String is a reference type (class) String //Since strings are used frequently, Java provides a //shorthand initializer for creating a string: String message = "Welcome to Java"; //String is a class String message = new String("Welcome to Java"); //You can also create a string from an array of // characters (char) char [] charArray = {‘W’, ‘e’, ‘l’, ‘c’, ‘o’, ‘m’, ‘e’}; String message = new String(charArray);

Immutable String s = "Java"; s = "HTML";

If you use the string initializer, no new object is created, if the interned object is already created. Interned String

Difference between “==“ vs. “equals()” String Comparison String s1 = new String("Welcome"); String s2 = "Welcome"; if (s1.equals(s2)) { // s1 and s2 have the same contents } if (s1 == s2) { // s1 and s2 have the same reference }

To compare strings, do not use >=, <= String Comparison String s1 = new String("Welcome"); String s2 = "Welcome"; if (s1.compareTo(s2) > 0) { // s1 is greater than s2 // lexicographically, i.e. unicode order } else if (s1.compareTo(s2) == 0) { // s1 and s2 have the same contents } else // s1 is less than s2

x.compareTo(y) return the difference of the first distinct character x = a b c y = a b d Unicode code = 81 Unicode code = 82 x.compareTo(y) = 81 – 82 = -1 String Comparison

 The StringBuilder / StringBuffer class is an alternative to the String class.  In general, a StringBuilder/StringBuffer can be used wherever a string is used.  StringBuilder/StringBuffer is more flexible than String.  You can add, insert, or append new contents into a string buffer  whereas the value of a String object is fixed once the string is created. StringBuilder

 Command line arguments Command Line args class TestMain { public static void main(String[] args) {... }

 The File class is intended to provide an abstraction that deals with most of the machine-dependent complexities of files and path names in a machine- independent fashion.  The filename is a string.  The File class is a wrapper class for the file name and its directory path. File

 The file path might be absolute or relative  Absolute  c:\book\Welcome.java  /home/liang/book/Welcome.java  Relative ..\Welcome.java  images/123.jpg File Path

 The directory separator for Windows is a backslash (\).  The backslash is a special character in Java and should be written as \\ in a string literal  The forward slash (/) is the Java directory separator, which is the same as on Unix.  The statement new File("image/us.gif")works on Windows, Unix, and any other platform. File Path

 A File object encapsulates the properties of a file or a path.  But does not contain the methods for reading/writing data from/to a file.  In order to perform I/O, you need to create objects using appropriate Java I/O classes.  Scanner and PrintWriter classes. Text I/O

 What is the printout of the following code? Q >Welcome to Java >Welcabcme tabc Java

 Suppose that s1 and s2 are two strings. Which of the following statements or expressions are incorrect? Q

Chapter 11: Inheritance

 Suppose you want to define classes to model circles, rectangles, and triangles.  These classes have many common features.  What is the best way to design these classes so to avoid redundancy?  The answer is to use inheritance. Motivation

Superclass GeometricObject Circle4 Rectangle1 TestCircleRectangle Run

 A subclass does not inherit the private members of its parent class.  A subclass is not a subset of the superclass  Contains more information! Inheritance

 An abstract class example abstract public abstract class GraphicObject { // declare fields // declare non-abstract methods abstract void getPerimeter(); }

 A constructor may invoke  An overloaded constructor or  Its superclass’s constructor  If none of them is invoked explicitly, the compiler puts super() as the first statement in the constructor. Constructors

 Example Constructors

 The keyword super refers to the superclass of the class in which super appears.  This keyword can be used in two ways:  To call a superclass constructor  super();  To call a superclass method  super.toString(); super

Chaining

 Method overriding Overriding

 Like an instance method, a static method can be inherited.  However, a static method cannot be overridden.  If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden. Overriding

 Every class in Java is descended from the java.lang.Object class.  If no inheritance is specified when a class is defined, the superclass of the class is Object. Object Class

 Equivalent Object Class

 An object of a subtype can be used wherever its supertype value is required.  This feature is known as polymorphism. Polymorphism

 Polymorphism Code

 Accessibility summary Accessibility

 A final class cannot be extended  A final variable is a constant  A final method cannot be overridden by its subclasses. final

 What is the printout of running the class C? Q

 What problem arises in compiling the program? Q

True or false? 1. A subclass is a subset of a superclass. 2. When invoking a constructor from a subclass, its superclass’s no-arg constructor is always invoked. 3. You can override a private method defined in a superclass. 4. You can override a static method defined in a superclass. Q

What is the output? Q

Chapter 13: Exceptions

 Exception Types Exception Hierarchy

 RuntimeException, Error and their subclasses are known as unchecked exceptions.  All other exceptions are known as checked exceptions, meaning that  the compiler forces the programmer to check and deal with the exceptions. Checked

 Exception handling process 1. Declare exception 2. Throw exception 3. Catch exception Exception Handling

 Every method must state the types of checked exceptions it might throw.  This is known as declaring exceptions. Declaring Exception

 When the program detects an error, the program can create an instance of an appropriate exception type and throw it.  This is known as throwing an exception. Throwing Exception

 Example Throwing Exception /** Set a new radius */ public void setRadius(double newRadius) throws IllegalArgumentException { if (newRadius >= 0) radius = newRadius; else throw new IllegalArgumentException( "Radius cannot be negative"); }

 Example Catching Exception

 Exception handling example Catching Exception

 To deal with a checked exception, you have to write the code as shown in (a) or (b) Catching Exception

 The order in which exceptions are specified in catch blocks is important.  A compile error will result if a catch block for a superclass type appears before a catch block for a subclass type. Catching Exception

 Occasionally, you may want some code to be executed regardless of whether an exception occurs or is caught. finally

 When to use exceptions: 1. An exception occurs in a method:  If you want the exception to be processed by its caller, you should create an exception object and throw it.  If you can handle the exception in the method where it occurs, there is no need to throw it. When?

 When should you use the try-catch block in the code?  You should use it to deal with unexpected error conditions.  Do not use it to deal with simple, expected situations. When?

 Output? Q

Q

 Suppose that statement2 causes an exception in the following try-catch block: Q

Answer the following questions: ■ Will statement3 be executed? ■ If the exception is not caught, will statement4 be executed? ■ If the exception is caught in the catch block, will statement4 be executed? ■ If the exception is passed to the caller, will statement4 be executed? Q