INTRODUCTION TO JAVA HIGHLIGHTING CHAPTER 9 AND CHAPTER














- Slides: 14

INTRODUCTION TO JAVA HIGHLIGHTING CHAPTER 9 AND CHAPTER 10

ABOUT CONSTRUTORS FROM CHAPTER 9 To have a good grasp of the idea behind constructors we need to look at the big picture behind it: that is OOP which stands for Object Oriented Programming is a computer programming approach in which a computer program is designed as a collection of interacting Objects which are entities having state and behavior. The idea behind OOP is to organize computer programs in ways that model how Objects are organized and interact in the real world. Real world objects talk to themselves, they have characteristics like color, weight, mass, volume, height, width and shape. Objects can inherit characteristics from each other replica of the way child objects inherit traits from parent objects in nature. An object can produce other objects of its kind like a chick lays and aches eggs. An object can make a clone of itself. Objects of the same type can be sorted using different criteria. The list is endless… All these features of OOP are the underlying mechanisms at work in OOP software. How do constructors come into the picture? This will be explained with sample classes in the next series of slides.

Classes We have looked at the object concisely. Lets see how they are represented in computer programs. People that came up with the idea of objects discovered that objects can belong to different groups that share many things in common. That is why people talk about ‘classes of Objects’. Example, a ‘Ferrari’ belongs to the ‘class of Cars’. A shark belongs to the ‘class of Fish’. Likewise designers of object oriented language decided to use the concept of ‘class’ to represent a collection of objects that share a lot of features in common. In java programming language, the ‘class’ is the starting point in building objects. It is the blueprint for creating objects. To have a real building (Object) you start with a design (class) on paper i. e. by an architect.

PROPONENTS ON THE CONCEPT OF OBJECTS q. A number of people have done research on the technology of objects. One notable person is Grady Booch. This is his definition of an object from an object technology perspective. q An object is an entity with state, behavior and a unique identity. Whereas the English dictionary puts an object as something that can be perceived with the senses.

Common examples of objects • A light bulb is a common object example. A light bulb has state i. e. the light can be on or off. the behavior is that it ‘shines light’ Unique identity comes with different serial numbers for seemingly identical objects. In the context of computer program objects, this will be different memory addresses. o A cell phone is another example. It state will be whether it is ‘on’ or ‘off’. It’s behavior will be to ‘receive call’, ‘make call’, ‘turn on’, ‘turn off’.

BULB AND PHONE EXAMPLES IN PROGRAMS One activity a java programmer will be involved in on a consistent basis is building templates for objects. That template is the class. Now lets represent the examples earlier stated as sample java programs A sample java program public class Bulb { boolean on, off; void shines. Light() { System. out. println(“Light bulb shining”); } }

THE SECOND SAMPLE PROGRAM public class Cellphone { boolean on, off; public void make. Call() { System. out. println(“My phone is making calls”); } public void receive. Call() { System. out. println(“My phone is receiving calls now”); }

Explanation of the code samples You start a java program with a class declaration. This is what we did when we wrote class Bulb { } and class Cellphone { }. You may wonder if we really wrote this. Where are the rest of the class? You may ask. What we want to establish by presenting the classes this way now is that the basic way of creating a class is to write the word ‘class’ followed by the class name, followed by an opening ‘{‘ and closing brace ‘}’. You can do this using a text editor like notepad saving the source document with the name of the class and a ‘. java’ extension. Though these class definitions are empty, they will compile successfully. To look at these classes closely, we can see declarations like ‘boolean off and on’. This is a way to express the state of the object we want to craft out of the class. Remember an object has a state. In programming circles those things that retain the state data of an object are called instance

Explanation continuation The declarations shines. Light(), make. Calls(), and receive. Calls() that are present in the classes is to express the behaviour of our would-be object. In programming circles these declarations are referred to as methods or functions though java often employs the first name: method. We’ll talk more of instance variables and methods later. Lets briefly discuss two important activities that makes the building of software possible using java

CREATING AN OBJECT Lets make objects of the classes Cell phone and Bulb. To make objects of the class templates, create them in a class that acts as an application or turn those class templates into an application. An application is a program that runs on your local desktop computer. But what really makes an application is the appearance of the main method in the program. The main method reads as ‘public static void main(String[]args)’. The syntax for creating an object from a class template is <class name> <object reference> = new <class name>();

TURNING SAMPLE CODE TO APPLICATION public class Cellphone { boolean on, off; void make. Call() { System. out. println(“My phone is making” + “calls”); } void receive. Call() { System. out. println(“My phone is” + “receiving calls now”); } public static void main(String[]args) { Cellphone cp = new Cellphone(); } }

Cellphone constructor explanation The line new Cellphone() in the previous code is an invocation of the Cellphone default constructor. To have an idea of how the constructor looks the way it does, one should remember how a basic method definition looks like. Example one of the Cellphone method is written as void make. Calls(). A basic method declaration consist of a return type ( example void) and a method name (example make. Calls) ending with opening and closing braces. . Likewise the statement Cellphone() is a kind of method call. It has a method name which is the same with the class, a pair of smooth braces and no return type. That’s what makes a constructor. It is a special method called when creating an Object in Java programming. In this instance it is called default constructor because though you typed Cellphone cp = new Cellphone(); explicitly you never defined it in code, it is the compiler that defines the code for you behind the scenes. To clarify, if you’ve written this lines in your class definition, public Cellphone() { } And later written Cellphone cp = new Cellphone(); then you have what is called No argument constructor. If you have written something like , public Cellphone(Boolean on, Boolean Off) { this. on = on; this. off = on; } Then you have a constructor with two arguments of Boolean types. The compiler doesn’t create a default constructor when you’ve give it one in your class definition. Read more on constructors and

Important points to note from Chapter 10 of Introduction to Java. q When comparing the contents of two Strings for equality, use equals() method and not equality (==) operator q If you create two String objects with the same contents such as String first = new String(“John”) and String second = new String(“John”) and try to compare them for equality using a line like if(first == second) you will get a false as result because the equality (==) operator by function does not compare the contents of objects for equality but compares their memory locations or references. When you create two objects with the same contents like “John” in this instance , their references ( first and second) are not the same. If you really intend to compare the relative contents of the objects, do something like this instead: if(first. equals(second)). q If you create two String objects like this: String first = “John” and String second = “John”. This time around if you compare for equality using the equality (==) operator, the result is true because creating String Objects of the same content without the “new” operator references the second (and subsequent instances) of the String object as the same first instance once the content is the same using a scheme called a String pool. q Relational operators are used for comparing between numeric operands both variables and constants. They always produce a Boolean result (true or false); Usual Relational operators are used for single conditions like if(age >= 18) “Live home”. Other relational operators include <=, ==, <, >, != q Conditional operators or otherwise logical operators are for testing multiple conditions. Examples of conditional operators include Logical AND (i. e. &&), Logical OR (i. e. ||) and Logical NOT (i. e. !) Example of use : if(bank. Interest < 5 && amount. To. Borrow > 100000) “Borrow”. We can see that two conditions (the interest to be paid and the amount that can be borrowed) is influencing the decision to borrow. Read more

Practice Assignment… Consider the following program segment String univer = “My School is very good”; String uni = “my school is very Good”; 1. Write a Java program to a. Get the total length of the string univer b. Compare if the two strings are equal not minding letter cases c. Print out only “University is good” from univer d. Reverse the string Uni e. Get the last index of ‘s’ from the string univer 2. Write a program in Java that reads a line of text from the keyboard and displays it on the screen one word per line as well as writing on the screen the number of words in the text. The text contains letters and blanks only.