AQuick Referenceof Java Programming Languagefor OOPApproach
AQuick Referenceof Java Programming Languagefor OOPApproach
Lesson Outcomes
OOP has its own specialized terminologies which are object, class and methods.
▪ Object:
- Represent entity that has a state, exhibits some well-defined behaviour and has a unique
identity.
- Is a self-contained entity which has its own private collection of properties (i.e. data) and
methods (i.e. operation) that encapsulate functionality into a reusable and dynamically loaded
structure.
▪ Class:
- Represent a set or collection of objects that share a common structure and a common
behaviour.
▪ Field:
- Attribute of the class object.
- Two types:
i. Primitive, i.e. int, long, double etc.
ii. Reference data types (ADT), i.e. String
▪ Method:
- Represent the action of object
- Two types of methods:
i. User defined methods: constructor (default, normal, copy), destructor, storer/mutator,
retriever/accessor, processor and printer.
ii. Built-in methods
B. ARRAY
- Array used to store a collection of data - more useful to think of an array as a collection of
variables of the same type.
- Declare single-dimensional array:
Syntax: elementType arrayName[] OR
ElementType[] arrayName
Example:
char[] alphabet;
int[] list;
double[] price;
[Link]();
[Link]("Value entered into array");
▪ Object
- An object represents an entity in the real world that can be distinctly identified.
- An object has a unique identity, state and behaviour:
• State – also known as its properties or attributes is represented by data fields with their
current values. Example: Circle – radius, rectangle – width & height
• Behaviour – also known as its action is defined by methods. To invoke a method on an
object is to ask the object to perform an action. Example: circle – getArea().
- Objects of the same type are defined using a common class. Object is an instance of a class.
▪ Class
- Class is a template, blueprint or contract that defines what an objects data fields and methods
will be.
- A Java class uses variables to define data fields and methods to define actions.
- Class provides methods of a special type, known as constructors, which are invoked to create
new object.
- A constructor can performs any action, but constructors are designed to perform initializing
actions, such as initializing the data fields of objects.
- There are 2 types of classes:
i. Predefined classes, i.e. Scanner, String, Character, StringBuilder/StringBuffer,Math,
Arrays, File, and etc.- classes that contained predefined method.
ii. User-defined clasess – classes defined by programmer / user
Predefined Classes
▪ Scanner Class
- Create object to read input from [Link] class ([Link] is a class refer to the standard
input device).
Example:
Program
/*This program demonstrates the use of Scanner Predefined
* Class
*/
Makes the Scanner class
package pkgCSC305; available to program
import [Link];
}
}
▪ String Class
- Reference type for variables that represent string (sequence of characters)
Constructing a String
Example:
Program Output
/*This program demonstrates the use of Hello Roslan
* String Predefined Class
*/
package pkgCSC305;
public PredefinedClass() {
}
[Link](msg + name);
} Class Method
}
String Comparison
Methods Effect
[Link](s2)→boolean Returns true if s1 equal to s2
[Link](s2)→boolean Returns true if s1 equal to s2 case insensitive
[Link](s2)→ int Returns 0 (s1 and s2 are indentical), < 0 (s1 less than s2), > 0
(s1 greater than s2)
[Link](s2)→ int Returns 0 (s1 and s2 are indentical), < 0 (s1 less than s2), and
> 0 (s1 greater than s2) case insensitive
[Link](prefix)→ boolean Returns true if s1 starts with the specified prefix
[Link](suffix) → boolean Returns true if s1 ends with the specified suffix
[Link]("s1 : Apple");
[Link]("s2 : Apple");
[Link]("s3 : aPPLe");
if ([Link](s2))
[Link]("Case sensitive : " + [Link](s2) + " : s1 and
s2 are identical");
if ([Link](s3))
[Link]("Case sensitive : " + [Link](s3) + " : s1 and
s3 are identical");
else
[Link]("Case sensitive : " + [Link](s3) + " : s1 and
s3 are not identical");
if ([Link](s3))
[Link]("Case insensitive : " + [Link](s3) +
" : s1 and s3 are identical");
Output
s1 : Apple
s2 : Apple
s3 : aPPLe
Case sensitive : true : s1 and s2 are identical
Case sensitive : false : s1 and s3 are not identical
Case insensitive : true : s1 and s3 are identical
if ([Link](s2) == 0)
[Link](s1 + " equal to " + s2);
else if ([Link](s2) > 0)
[Link](s1 + " greater than " + s2);
else
[Link](s1 + " less than " + s2);
[Link]();
if ([Link](s2) == 0)
[Link](s1 + " equal to " + s2);
else if ([Link](s2) > 0)
[Link](s1 + " greater than " + s2);
else
[Link](s1 + " less than " + s2);
Output
First Comparison : Case Sensitive
Orange less than oRaNGe
if([Link](pre))
[Link](s1 + " starts with prefix " + pre);
else
[Link](s1 + " does not starts with prefix " + pre);
if ([Link](suf))
[Link](s2 + " ends with suffix " + suf);
else
[Link](s2 + " does not ends with suffix " + suf);
Output
Apple starts with prefix App
Orange ends with suffix nge
Methods Effect
[Link]() → int Returns the number
[Link](index) → char Returns the character at the specified index from this string
[Link](s2) → String Returns a new string that concatenates this string with string s1
Example:
Java Program Segment
String s1 = "Apple";
String s2 = "Sweet ";
char c = [Link](0);
[Link]("Index 0 of " + s1 + " is character " + c);
String s3 = [Link](s1);
[Link](s2 + "+ " + s1 + " becomes " + s3);
Output
The length of string Apple is 5
Index 0 of Apple is character A
Sweet + Apple becomes Sweet Apple
Methods Effect
[Link](beginIndex) → String Returns the string’s substring that begins with the character at
the specified beginIndex and extends to the end of the string
[Link](beginIndex, endIndex) Returns the string’s substring that begins at the specified
→ String beginIndex and extends to the character at index endIndex – 1
Example:
Java Program Segment
String s1 = "Sweet Mango Cafe";
String s2 = [Link](6);
String s3 = [Link](0,11);
Obtaining substrings
Example:
Java Program Segment
Output
String [Link](6) of Sweet Mango Cafe is Mango Cafe
String [Link](0,11) of Sweet Mango Cafe is Sweet Mango
Methods Effect
[Link]() → String Returns a new string with all characters converted to lowercase
[Link]() → String Returns a new string with all characters converted to uppercase
[Link]() →String Returns a new string with blank characters trimmed on both
side
[Link](oldChar, newChar) → Returns a new string that replaces the all matching characters
String in this string with the new character
[Link](oldString, Returns a new string that replaces the first matching substring
newString) → String in this string with the new string
[Link](oldString, newString) Returns a new string that replaces all matching substring in this
→ String string with the new string
[Link](delimiter) → String[] Returns an array of strings consisting of the substrings split by
the delimiter
Example:
Java Program Segment Output
String s = "Welcome";
String s2 = " Welcome ";
String s3 = "Wel#co#me";
[Link]([Link]()); welcome
[Link]([Link]()); WELCOME
[Link]([Link]()); Welcome
[Link]([Link]('e', 'A')); WAlcomA
[Link]([Link]("e", "AB")); WABlcome
[Link]([Link]("el", "AB")); WABcome
Methods Effect
[Link](ch) → int Returns the index of the first occurrences of ch in the string,
returns -1 if not matched
s1. indexOf (ch, fromIndex) → int Returns the index of the first occurrence of ch after fromIndex
in the string, returns -1 if not matched
s1. indexOf (str) → int Returns the index of the first occurrence of string s in this
string, returns -1 if not matched
[Link] (str, fromIndex) → int Returns the index of the first occurrence of string s in this
string after fromIndex, returns -1 if not matched
[Link](ch) →int Returns the index of the last occurrence of ch in the string,
returns -1 if not matched
[Link](ch, fromIndex) → int Returns the index of the last occurrence of ch before fromIndex
in this string, returns -1 if not matched
s1. lastIndexOf(str) → int Returns the index of the last occurence of string s, returns -1 if
not matched
s1. lastIndexOf(str, fromIndex) → Returns the index of the last occurrence of string s before
int fromIndex, returns -1 if not matched
Example:
Java Program Segment Return/Output
String s1 = "Welcome to Java";
[Link]([Link]('W')); 0
[Link]([Link]('e')); 4
[Link]([Link]('o',5)); 9
[Link]([Link]("come")); 3
[Link]([Link]("Java", 5)); 11
[Link]([Link]("java", 5)); -1
[Link]([Link]('W')); 0
[Link]([Link]('e')); 9
[Link]([Link]('o',5)); 4
[Link]([Link]("come")); 3
[Link]([Link]("Java", 5)); -1
[Link]([Link]("java", 5)); 11
- String are not array but string can be converted into an array and vice versa
i. toCharArray
- to convert string to an array of characters
Example:
Java Program Segment Output
String text = "Java"; Java
[Link](text); arrayChar[0]: J
char[] arrayChar = [Link](); arrayChar[1]: a
for (int i=0; i <[Link]; i++) arrayChar[2]: v
[Link]("arrayChar[" + i + "]: " + arrayChar[3]: a
arrayChar[i]);
Example:
Java Program Segment Output
String src = "CSC305"; CSC305
char[] dst = {'I','T','C','3','0','5'};
[Link](0,3,dst,0);
[Link](dst);
Example:
Java Program Segment Output
String str = new String(new char[]{'J','a','v','a'}); Java
Java
String str2 = [Link](new char[]{'J','a','v','a'});
[Link](str);
[Link](str2);
if(isPalindrome(s))
{
[Link](s + " is a palindrome");
}
else
{
[Link](s + " is not a palindrome");
}
}
public static boolean isPalindrome(String s)
{
int low = 0;
int high = [Link]() - 1;
boolean isTrue = true;
▪ Character Class
- Enable primitive data value of character to be treated as object
- Example: Character character = new Character(‘a’);
Methods / Constructor Effect
[Link](value) Construct a character object with char value
[Link]() → char Returns the char value from this object
[Link](c2) → int Compares this character with another
[Link](c2)→ boolean Returns true if this character is equal another
[Link](c1) → boolean Returns true if the specified character is digit
[Link](c1) → boolean Returns true if the specified character is letter
[Link](c1) → boolean Returns true if the character is a letter or digit
[Link](c1) → boolean Returns true if the character is a lowercase letter
[Link](c1) → boolean Returns true if the character is an uppercase letter
[Link](c1) → char Returns the lowercase of the specified character
[Link](c1) → char Returns the uppercase of the specified character
String s = [Link]();
return counts;
}
}
▪ StringBuilder/StringBuffer Class
- An alternative to the String class.
- Can be used whenever String is used.
- More flexible than String: add, insert, append new contents into a StringBuilder or a
StringBuffer.
- StringBuffer– methods for modifying buffer in it are synchronized.
- StringBuffer – more efficient if it is accessed by single task.
- Has three constructor andmore than 30 methods.
Example:
Java Program Output
StringBuilder strBuilder = new
StringBuilder("Hello,");
[Link](" ");
[Link]("welcome");
[Link](" ");
[Link]("to");
[Link](" ");
[Link]("Java");
[Link](22,30);
[Link](22);
[Link](5, '!');
[Link]();
Java Program
package pkgCSC305;
import [Link];
[Link]("Enter a string");
String s = [Link]();
return [Link](s2);
}
return [Link]();
}
return [Link]();
}
▪ Math Class
- Contains the methods needed to perform basic mathematical function
- There are various class of methods under Math class, below are the few example:
i. Trigonometric Methods
Predefined methods Effect / Value Returned
[Link](x) Return the trigonometric sine of an angle in radians
[Link](x) Return the trigonometric cosine of an angle in radians
[Link](x) Return the trigonometric tangent of an angle in radians
[Link](x) Convert the angle in degrees to an angle in radians
[Link](x) Convert the angle in radians to an angle in degrees
[Link](x) Return the angle in radians for the inverse of sin
[Link](x) Return the angle in radians for the inverse of cos
[Link](x) Return the angle in radians for the inverse of tangent
v. random method
Predefined methods Effect / Value Returned
(int) ([Link]()*x) Return a random number integer between 0 and x-1
x + (int) ([Link]()*y) Return a random number integer between x and (x+y)-1
Example:
Java Program
package pkgCSC305;
/**main() method*/
public static void main(String[] args) {
final int NUMBER_OF_CHARS = 175;
final int CHARS_PER_LINE = 25;
▪ Arrays Class
- [Link] class contains various static methods for sorting and searching arrays,
comparing arrays, and filling array elements.
- Static methods under Arrays class are:
• [Link]()
• sort the whole array or partial array
• [Link]()
• Search for a key in an array – the array must be presorted in increasing order.
• [Link]()
• to check whether two arrays are equal
• two arrays are equal if they have the same contents.
• [Link]()
• to fill in all or part of the array
i. [Link]()
example:
Java Program Output
int[] numbers = {6,4,2,3,5}; Before sort()
6 4 2 3 5
[Link]("Before sort()"); After sort()
for (int i=0; i<[Link]; i++) 2 3 4 5 6
[Link](numbers[i]+ " ");
[Link]();
[Link](numbers);
[Link]("After sort()");
for (int i=0; i<[Link]; i++)
[Link](numbers[i] + " ");
ii. [Link]()
example:
Java Program Output
int[] numbers = {6,4,2,3,5}; Before sort()
6 4 2 3 5
[Link]("Before sort()"); After sort()
for (int i=0; i<[Link]; i++) 2 3 4 5 6
[Link](numbers[i]+ " ");
[Link]();
[Link](numbers);
[Link]("After sort()");
iii. [Link]()
example:
Java Program Output
int[] list1 = {2,3,5,7,11}; Array List1:
int[] list2 = {2,3,5,7,11}; 2 3 5 7 11
Array List2:
[Link]("Array List1: "); 2 3 5 7 11
for (int i=0; i<[Link]; i++)
[Link](" " + list1[i]); Array list1 and
list2 are equal
[Link]();
[Link]("Array List2: ");
for (int i=0; i<[Link]; i++)
[Link](" " + list2[i]);
[Link]();
[Link]();
if([Link](list1, list2))
[Link]("Array list1 and list2 are equal");
iv. [Link]()
example:
Java Program Output
int[] list1 = {1,2,3,4}; 3
3
[Link](list1, 3); 3
3
for (int i=0; i<[Link]; i++)
[Link](list1[i]);
▪ File Class
- A File object encapsulates the properties of a file or a path but does not contain the methods
for creating a file or for reading/writing data from I/O file.
- 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 File Class contains the methods for obtaining File properties and for renaming and
deleting files.
- In order to perform I/O you need to create objects using appropriate Java I/O classes. The
objects contain the methods for reading/writing data from/to a file.
- Read/write strings and numeric values from/to a text file can be performed by using Scanner
and PrintWriter classes.
- [Link] class can be used to create a file and write data to a text file.
- Create a PrintWriter object for a text file as follows:
printWriter output = new printWriter(fileName);
Example:
Java Program
package pkgCSC305;
import [Link];
if ([Link]())
{
[Link]("File already exists");
[Link](0);
}
Example:
Java Program
import [Link];
import [Link].*;
while ([Link]()){
String firstName = [Link]();
String secondName = [Link]();
String thirdName = [Link]();
String fourthName = [Link]();
String lastName = [Link]();
float score = [Link]();
[Link]();
}
}
User-Defined Classes
• [Link]()
• Invokes the getArea() method on c1. Methods are invoked as operations on objects.
• Method getArea() is referred to as instance method, because it can be invoke only on
a specific instance.
• The object on which an instance method is invoked is called a calling object.
Example 1: Defining Classess, Creating Objects And Accessing Object’s Data and Methods
Java Program
package pkgCSC305;
class Circle{
double getArea(){
return radius * radius * [Link];
}
}
- Static method:
• Same as static variables.
• Static methods can be called without creating an instance of the class.
• Definition:
static int getNumberObject() { return numberOfObject; }
- Declare constant:
• final static double PI = 3.1412;
Example:
Java Program
package pkgCSC305;
class Circle2{
double radius;
static int numberOfObjects = 0;
Circle2()
{
radius = 1.0;
numberOfObjects++;
}
Circle2(double newRadius)
{
radius = newRadius;
numberOfObjects++;
}
double getArea()
{
return radius * radius * [Link];
}
}
} }
} }
- protected: to allow subclasses to access fields or methods defined in the superclass, but not
allow non-sub classes to access the field and methods.
Example:
Java Program
package pkgCSC305;
[Link]([Link]()* 1.1);
class Circle3{
public Circle3(){numberOfObjects++;}
Example:
Java Program
package pkgCSC305;
class Circle4{
private double radius = 1; //encapsulate radius;
private static int numberOfObjects = 0; //encapsulate numberOfObjects
public Circle4(){numberOfObjects++;}
public Circle4(double newRadius){
radius = newRadius;
numberOfObjects++;
}
public double getRadius(){return radius;};
public void setRadius(double newRadius){
radius = (newRadius >= 0) ? newRadius : 0;
}
public static int getNumberOfObjects(){return numberOfObjects;}
public double getArea(){return [Link](radius, 2) * [Link];}
}
int n = 5;
printAreas(myCircle,n);
Output
Radius Area
1.0 3.141592653589793
2.0 12.566370614359172
3.0 28.274333882308138
4.0 50.26548245743669
5.0 78.53981633974483
Radius is 6.0
n is 5
▪ Array of Objects
- The following statement declares and creates array of 10 obj object.
ClassName[] obj = new ClassName[size];
Example:
Circle[] circleArray = new Circle[10];
- To initialize thecircleArray, you can use a for loop like this one:
for (int i = 0; i <[Link]; i++) {
circleArray[i] = new Circle();
}
Example:
Java Program
package pkgCSC305;
class Circle5{
private double radius = 1; //encapsulate radius;
private static int numberOfObjects = 0; //encapsulate numberOfObjects
public Circle5(){numberOfObjects++;}
public Circle5(double newRadius){
radius = newRadius;
numberOfObjects++;
}
public double getRadius(){return radius; }
public void setRadius(double newRadius){
radius = (newRadius >= 0) ? newRadius : 0;
}
public static int getNumberOfObjects(){return numberOfObjects;}
public double getArea(){return [Link](radius, 2) * [Link];}
}
return circleArray;
}
[Link]("-------------------------------------------
-------------");
[Link]("%-30s%-15s\n", "The total area of circle is
", sum(circleArray));
}
public static double sum(Circle5[] circleArray){
double sum = 0;
for (int i = 0; i < [Link]; i++)
sum += circleArray[i].getArea();
return sum;
}
}
Output
Radius Area
40.43061764438477 5135.356814405086
94.62579335893402 28129.948699229713
89.72514484194619 25291.710896656983
97.32597575533629 29758.25121328157
92.30989550748046 26769.877966284428
--------------------------------------------------------
The total area of circle is 115085.14558985777
D. THINKING IN OBJECT
Example:
Java Program
public class Student{
Example:
Java Program
public class Student{
private int id;
private String name;
public student(){ // default constructor
id = 0;
name = " ";
}
public Student (int ssn, String newName){// Normal constructor
id = ssn;
name = newName;
}
public int getID(){ return id; } // Accessor method
public String getName() { return name; }// Accessor method
public void setName(String newName) { name = newName;}// mutator
// method
public void setId (int newId) { id = newId; }// mutator method
Example:
Java Program
package pkgCSC305;
}
}
class Foo {
static int i = 0;
int j = 0;
public Foo(){}
public Foo(int a, int b){
i = a;
j = b;
}
public int getI(){return i;}
public int getJ(){return this.j;}
public int getJ2(){return j; }
void setI(int a) {i = a;}
void setJ(int b) {this.j = b;}
}
Output
i = 1
this.j = 1
i = 2
this.j = 2
i = 2
this.j = 0
2
0
• A subclass is not a subset of its superclass. Usually, a subclass contains more information and
methods that its superclass.
• Private data fields in a superclass are not accessible outside the class. Therefore they cannot be
used directly in a subclass. They can be accessed/mutated through public accessor/mutator if
defined in the superclass.
• Not all is-a relationship should be modeled using inheritance.
• Inheritance is used to model the is-a relationship. A subclass and its superclass must have the
is-a relationship.
• Java does not allow multiple inheritance (C++ allow multiple inheritance).
Example:
Java Program (Base Class)
package pkgCSC305;
//default constructor
public GeometricObject1(){ dateCreated = new [Link]();}
// normal constructor
public GeometricObject1(String color, boolean filled){
dateCreated = new [Link]();
[Link] = color;
[Link] = filled;
}
public Circle7(){}
▪ Polymorphism: Is the capability of an action or method to do different things based on the object
that is acting upon. This is the third basic principle of OOP. There are THREE types of
polymorphism:
i. Overloaded Methods: Are methods with the same name signature but either a different
number of parameters or different types in the parameter.
Example:
Java Program
public class Test {
public static void main(String[] args){
A a = new A();
a.p(10);
a.p(10.0);
}
}
class B {
public void p(double i) {
[Link](i * 2);
}
}
class A extends B {
public void p(int i) {
[Link](i);
}
}
ii. Overriden Methods: Are methods that are defined within an inherited or subclass. They
have the same signature and the subclass definition is used.
Example:
Java Program
public class Test {
public static void main(String[] args){
A a = new A();
a.p(10);
a.p(10.0);
}
}
class B {
public void p(double i) {
[Link](i * 2);
}
}
class A extends B {
public void p(double i) {
[Link](i);
}
}
iii. Dynamic method binding:Is the ability of a program to resolve references to subclass
methods at runtime.
Example:
Java Program
package pkgCSC305;
F. OBJECT COMPOSITION
- An object can contain another object. The relationship between the two is called composition.
- Composition is actually a special case of the aggregation relationship. Aggregation model has-
a relationship and represents an ownership between two objects.
- The owner is called an aggregating object and its class an aggregating class.
- The subject object is called an aggregated object and its class an aggregated class.
Example:
Java Program
package pkgCSC305;
class Engine {
public String start() {return "Engine Start";}
public String rev() {return "Engine Reverse";}
public String stop() {return "Engine Stop";}
}
class Wheel {
public int inflate(int psi) {return psi;}
}
class Window {
public String rollup() {return "Window: Rool up";}
public String rolldown() {return "Window: Roll Down";}
}
class Door {
public Window window = new Window();
public String open() {return "Door: Open";}
public String close() {return "Door: Close";}
}
Example:
Java Program (Abstract Class)
package pkgCSC305;
publicboolean isFilled(){
return filled;
}
public Circle8(){}
publicdouble getDiameter(){
return 2 * radius;
}
displayGeometricObject(geoObject);
}
public static void displayGeometricObject(GeometricObject object){
[Link]("The area is " + [Link]());
[Link]("The perimeter is " +
[Link]());
}
}
- Interface: is a classlike construct that contains only constants and abstract methods. It is
similar to abstract class, but its intent to specify common behaviour for objects.
- Example of interface:
public interface Edible {
public abstract String howToEat();
}
Example:
Java Program (Sub Class)
package pkgCSC305;
class Animal {}
interface Edible{
public abstract String howToEat();
}
Output
REFERENCES:
[Mar2012-B3]
public abstract class Article{
a. Derive two subclasses named magazine and CD. These subclasses should have getLength() and
calcPrice() methods.
Magazine: adds a page count (number of pages) and cost per page.
CD: adds a playing time (in minutes) and cost per minute.
getLength will return the number of pages or playing times (in minutes).
calcPrice will calculate the cost of book (number of pages * cost per pages) and the cost for
CD is number of minutes * cost per minute and set its price in the Article class.
getCost will return the price.
Both subclasses have their normal constructors and a toString method each.
Answer:
import [Link];
public class CD extends Article {
private int time;
private double cost;
b. Write a complete application class called ArticleApp that performs each of the following in
sequence:
• Read the total number of published article.
• Declare an array of object to hold the published materials using polymorphism.
• For each published material, read in the type of publication (Magazine or CD) and the
properties.
Answer:
import [Link].*;
import [Link].*;
[Sept2011-B3]
Given the following diagram are the class of Patient as super class and InPatient and OutPatient as
subclasses.
b) Write the definition for abstract method from both subclasses. Given for out patient the charges
will be incurred when the patient will be referred to specialist doctor. The charges are RM20. For
in patient, charges depend on the number of days warded. The charges are RM 10 per day.
Answer:
double calcCharges()
{
return getDayOfWarder * 10;
}
double calcCharges()
{
double charge = 0;
if (getRefSpecialist() == true)
charges = 20;
return charge;
}
c) Write a program fragment to determine total charges that have been collected from all patients.
Answer:
double totalCharge = 0;
for (int i=0; i<[Link]; i++)
totalCharge = totalCharge + patient[i].calcCharge();
[Link](“\nTotal charges that have been collected, RM”) <<
totalCharge.
d) Count number of patient who have been warded more than 5 days.
Answer:
int count=0;
for (int j=0; j<[Link]; j++)
{
if(patient[j] instanceOf InPatient) {
InPatient temp = (InPatient) patient[j];
if ([Link]() > 5)
count++;
}
}
e) Lis all males’s name that has been referred to the specialist doctor.
Answer:
for (int i=0; i<[Link]; i++)
{
if (patient[j] instanceOf OutPatient) {
OutPatient temp = (OutPatient) patient[j];
if (([Link]() == true) &&
([Link]().equals(“male”)))
[Link](“\nName : ” +[Link]());
}
}
TUTORIAL