Object Oriented Programming (OOP) LAB # 8

Slides:



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

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
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.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
Fundamental Programming Structures in Java: Strings.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Programming Section 11 James King 12 August 2003.
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.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
The java.lang Package chapter 8 Java Certification Study Group February 2, 1998 Seth Ladd.
Applications Development
Chapter 7: Characters, Strings, and the StringBuilder.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
OOP Basics Classes & Methods (c) IDMS/SQL News
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Design issues for Object-Oriented Languages
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Object-Oriented Concepts
OOP: Encapsulation &Abstraction
Objects as a programming concept
Object-oriented Programming in Java
Objects as a programming concept
Strings, StringBuilder, and Character
Inheritance and Polymorphism
Advanced Programming in Java
Programming Language Concepts (CIS 635)
ATS Application Programming: Java Programming
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Class vs Abstract vs Interface
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
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.
CS2102: Lecture on Abstract Classes and Inheritance
String Objects & its Methods
CSC 143 Inheritance.
Lesson 2: Building Blocks of Programming
String and StringBuilder
Chapter 7: Strings and Characters
More inheritance, Abstract Classes and Interfaces
Java Programming Language
Variables ICS2O.
String and StringBuilder
Chapter 9: Polymorphism and Inheritance
Object Oriented Programming (OOP) LAB # 5
Advanced Programming Behnam Hatami Fall 2017.
Topic 10 Abstract Classes “I prefer Agassiz in the abstract,
Inheritance Inheritance is a fundamental Object Oriented concept
Java Programming, Second Edition
Object Oriented Programming (OOP) LAB # 9
More About Inheritance & Interfaces
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
CISC/CMPE320 - Prof. McLeod
String and StringBuilder
Inheritance.
Review of Previous Lesson
Final and Abstract Classes
Topics OOP Review Inheritance Review Abstract Classes
String Objects & its Methods
Presentation transcript:

Object Oriented Programming (OOP) LAB # 8 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Working with Strings

Strings The string class is one of the most commonly used complex types in Java. When you create a string, you're creating an instance of a class. It's not a primitive variable, such as char, int, short and the others.

Declaring Strings in java We can declare String in java in two ways. First, by assigning the a literal string directly to the equals assignment operator. For example, String S1=“Welcome to java”; Notice that Because the string class is a complex class, you are actually creating an instance of the class in the memory. Second, by calling the constructor method for the string class. A constructor method is a method of a class that has the same name as the class itself, doesn’t return any value and should be public. So, we are going to pass in exactly the same value of the string to the constructor method. For example, String s2= new String (“Welcome to java”);

Comparing between two Strings If you want to compare two string values, you shouldn't use the double equals operator. It's unpredictable. If you declare one string using the shorthand syntax with the equals assignment operator and just a literal string, and the second string using the constructor method, then you can't compare the values using double equals. There are two methods in the class String in which you can compare the two Strings. First, The equals method which is case sensitive. Second, equalsIgnoreCase in which you can do a non-case sensitive comparison.

Comparing between two Strings Declaring two strings within the same way. Comparing between these strings using double equals operator. The result is true since the two strings were declared within the same syntax.

Comparing between two Strings Declaring two strings with different ways. Comparing between these strings using double equals operator. The result is False. Why?

Comparing between two Strings Using equals method to compare between the two Strings. Notice that s1 and s2 are objects of the String class. The result Now is correct!!

Comparing between two Strings Using equalIgnoreCase method. So, you can do a non-case sensitive comparison. The result is right if we ignore the cases.

Building strings with StringBuilder Let’s begin with an example. If s1=“Welcome to java” and you want to change the value by adding something to it. Let’s say that you want the text above to be altered to “Welcome to java programming”. The solution might seem easy to you from the first glance. Definitely, your answer will be as the following: s1=s1+ “ programming”; So, everything will work fine with you but certainly everything is not alright from the background. What's really happening is that when you append a value or otherwise change the value of the string variable, you're actually creating a new instance of the string class, and you're abandoning the reference to the old object. That old object is still out there in memory. That means you use more memory than what you actually need.

Building strings with StringBuilder Java provides two utility classes to solve this problem. They are called StringBuffer and StringBuilder, these two classes have the same API or programming interface, and they both implement the same methods named insert and append. The insert method can be used to insert text into a string at either the beginning or any other position in the string. The append method can be used to append text to a string. You can use either the StringBuilder or the StringBuffer in most environments, but in general, the StringBuilder is more slender, it takes less memory and resources.

Example#1 of using StringBuilder Using append method to append the new text at the end of the existing string (s1). Print the object sb which contains the two strings. Declare an object from StringBuilder class. As usual every thing is an object in java!

Example#2 of using StringBuilder Insert method takes two arguments. The first one is to indicate the position of the new string. Notice “7” here means insert the new text after 7 characters. That means after “welcome” word. The second argument is the text you want to add. The result of the program.

Parsing Strings value The string class has many methods that you can use to manipulate and parse its values An example in the next slide shows you some of the most common methods used.

Parsing The String Value Compute the length of the String s1. Notice that the spaces are taken into the consideration. Parsing The String Value If you want to know which text is located in specific index, use substring method. If you want to know the location or the position of a word in a string, use indexOf method and pass the value you want to know its index. trim method is used to delete the spaces located at the end of the string.

More on Strings We encourage you to look at the documentation for the string class. You'll find in list of methods and many tools that you can use to extract, manipulate, convert and otherwise use the string class you need to in your applications. You can access the documentation by highlighting the word you are looking for, then from the help menu choose dynamic help and you will find something related to the topic you want to find. See the next slide.

Looking for String class documentation

Inheritance One of the most important term you have to know about object oriented programming is called inheritance. Inheritance means that there's a relationship between classes in your application. An inheritance relationship lets you inherit or extend functionality from one class to another. In the world of C++ there is multiple inheritance. In which a single class can inherit functionality from multiple other classes but in Java, there is only single inheritance, each new class can extend or inherit functionality from only one other class.

Describing the Inheritance There are a number of ways to describe an inheritance relationship and they are just different vocabularies. some developers describe the relationship as a parent-child relationship, where the parent has the functionality and the child is inheriting. Another developers describe it as base and derived, where a base class has the functionality and the derived class is extending it. In the world of Java, the term that is most commonly used is Superclass and Subclass. The Superclass has the functionality and the Subclass is extending the Superclass.

Example of Inheritance Lets suppose that we have two classes. The first one called Shape and the other one called Square. We can describe the relationship between Shape and Square class as the following: “Square is a Shape”. So, the supper class or the parent is Shape class and the subclass is Square class. That means square inherits all methods and attributes from Shape class. To do that, Look at the next slides.

Attributes of the class Attributes of the class. Notice that some attributes defined as static Why? Super class “Shape” Defined two methods. One of them used to return the Area value and the other one is used to return perimeter value.

Use extends keyword which refers to an inheritance Use extends keyword which refers to an inheritance. That means Square inherits Shape. Where did we use this keyword??? sub class “Square” Assigning values to some of the variables. Notice that these variables are inherited from the super class. We didn’t define data types for them. Also, because they are static we didn’t need to call them through an object. The color attribute here inherited from the super class. How? Define an object from the sub class Square and call the methods using this object. Why? Notice that the methods are also inherited from the super class. They aren’t in the sub Class Square.

Using @Override If you want to use a method in the Super class but with different statements, you can use @ Override before the method and change the statements. You can’t change the arguments or what the function return. An example of that in the next slide.

We change some statements of getArea method that exists in the super class. Using @Override

The End !!