Sei sulla pagina 1di 18

COLLEGE OF COMPUTER STUDEIS

INFORMATION TECHNOLOGY DEPARTMENT

CCS0023L
(Object Oriented Programming)

EXERCISE

7
Dealing with Polymorphism and Inheritance

<Nicole D. Parallag>
<BSITSMBA-B21>
<September 28, 2019>
I. Objectives:

At the end of the experiment, students must be able to:

Cognitive
a) understand the effects of access modifiers in an inheritance relationship
b) understand the concept of Java abstract Classes

Psychomotor:
a) construct a program using 'extends' keyword to define an inheritance relationship
b) compile and debug the error of the program

Affective
a) appreciate the concept behind this experiment

II. BACKGROUND INFORMATION

Polymorphism is the ability of different objects to respond to the same message in different
ways. This means that different objects can have very different method implementations for
the same message.

Method Overloading is the process of declaring methods with the same name but different
parameter types.

Method Overriding allows a subclass to redefine methods of the same name from a
superclass

III. EXPERIMENTAL PROCEDURE:

1. Create an interface named Comparison whose method can be used to compare


two
Time objects. The methods will include isGreater, isLess, and isEqual.

2. 2.1. Create class Person with attributes name, age and gender.
2.2. Create class Student with attribute grade and,
2.3. Create class Teacher with attribute salary.
Note:
Class Student and Teacher must inherit from Person.
In main class, create and print two students and one teacher.
Apply inheritance, setter and getter.

Sample Output:
Student 2:
Name: Rachel
Age: 15
Gender: F
Grade: 2.50
3. Give a definition of a class named Arithmetic whose methods are three add() and multiply()
and one subtract() and divide().

 Parameter for add() and multiply() should be two, three and four int respectively.
 Parameter for subtract() and divide() should be two int.
 Applying method calling in the main(), test your program by calling the following
methods:

add(1,3)

add(1,3,4,5)

multiply(2,3,4)

multiply(1,1)

subtract(3,1)

divide(5,2)

PROGRAM #1
CODE: MAIN.Class
package main.in;
import java.util.Scanner;
public class MainIn extends comparisson{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
comparisson comp = new comparisson();
System.out.println("Enter two time variables (hh/mm/ss):");
String time1 = sc.nextLine();
String time2 = sc.nextLine();
if(time1.compareTo(time2) == 1){
comp.isGreater(time1, time2);
}
else if(time1.compareTo(time2) < 0){
comp.isLess(time1, time2);
}
else
comp.isEqual(time1, time2);
}

}
CODE: comparisson.Class
package main.in;

public class comparisson {


public void isGreater(String date1, String date2)
{
System.out.println(date1 + " is greater than " + date2);
}
public void isLess(String date1, String date2)
{
System.out.println(date1 + " is less than " + date2);
}
public void isEqual(String date1, String date2)
{
System.out.println(date1 + " is equal to " + date2);
}
}
PROGRAM #2
CODE Basic.class

package basic;
import java.util.*;
public class Basic {
public static void main(String[] args) {
System.out.println("DATABASE");
System.out.println(" ");
System.out.println("1. Teacher");
System.out.println("2. Student");

System.out.print("Choice: ");
Scanner scan = new Scanner(System.in);
int choice = scan.nextInt();

System.out.println(" ");

if(choice == 1){
teacher x;
x = new teacher();

System.out.print("Name: ");
Scanner scann = new Scanner(System.in);
x.name = scann.nextLine();
System.out.println(" ");

System.out.print("Age: ");
Scanner scan2 = new Scanner(System.in);
x.age = scan2.nextInt();
System.out.println(" ");

System.out.print("Gender: ");
Scanner scan3 = new Scanner(System.in);
x.gend = scan3.next().charAt(0);
System.out.println(" ");

System.out.print("Salary: " + x.salary);


System.out.println(" ");

else if(choice == 2){


student y;
y = new student();

System.out.print("Name: ");
Scanner scann = new Scanner(System.in);
y.name = scann.nextLine();
System.out.println(" ");

System.out.print("Age: ");
Scanner scan2 = new Scanner(System.in);
y.age = scan2.nextInt();
System.out.println(" ");

System.out.print("Gender: ");
Scanner scan3 = new Scanner(System.in);
y.gend = scan3.next().charAt(0);
System.out.println(" ");

System.out.print("Grade: " + y.grade);


System.out.println(" ");

else{
System.out.println("Invalid Input");
}

}
}
OUTPUT:
CODE:student.class

package basic;

public class student extends person {


public void setName(String Brand)
{
this.name = name;
}
public void setAge(int age)
{
this.age = age;
}
public void setGend(char gend)
{
this.gend = gend;
}
public void setGrade(double grade)
{
this.grade = grade = 2.0;
}

public String getName(String name)


{
return this.name;
}
public int getAge(int age)
{
return this.age; }
public char getGend(char gend)
{
return this.gend;
}
public double getgrade(double grade)
{
return this.grade;
}

}
Output:
CODE: teacher.class

package basic;
public class teacher extends person {
public void setName(String Brand)
{
this.name = name;
}
public void setAge(int age)
{
this.age = age;
}
public void setGend(char gend)
{
this.gend = gend;
}
public void setSalary(double salary)
{
this.salary = salary = 10000;
}
public String getName(String name)
{
return this.name;
}
public int getAge(int age)
{
return this.age;
}
public char getGend(char gend)
{
return this.gend;
}
public double getSalary(double salary)
{
return this.grade;
}
}

OUTPUT:
CODE PERSON.JAVA

package basic;
public class person {
String name;
int age;
char gend;
double grade = 2.0;
double salary = 10000;
}

FINAL CODE OUTPUT:


TEACHER:
STUDENT:

PROGRAM #3
CODE:
package lab7b;
public class Lab7b {

public static void main(String[] args) {


int a = 1;
int b = 3;
int c = 2;
int d = 4;
int e = 5;

int q = add(a, b);


int w = add(a, b, d, e);
int r = multiply(c, b, d);
int t = multiply(a);
int y = subtract(b, a);
int o = divide(e, c);

System.out.println("The sum of " + a + " and " + b + " is equals to " + q);
System.out.println(" ");

System.out.println("The sum of " + a + " and " + b + " is equals to " + w);
System.out.println(" ");

System.out.println("The product of " + a + " and " + b + " is equals to " + r);
System.out.println(" ");

System.out.println("The product of " + a + " and " + b + " is equals to " + t);
System.out.println(" ");

System.out.println("The difference of " + a + " and " + b + " is equals to " + y);
System.out.println(" ");

System.out.println("The quotient of " + a + " and " + b + " is equals to " + o);
System.out.println(" ");

public static int add(int n1, int n2) {


int sum;
sum = n1 + n2;
return sum;
}
public static int add(int n1, int n2, int n3, int n4){
int sum;
sum = n1 + n2 + n3 + n4;
return sum;
}

public static int multiply(int n2a, int n2, int n3) {


int sum;
sum = n2a * n2;
return sum;
}

public static int multiply(int n1) {


int sum;
sum = n1 * n1;
return sum;
}

public static int subtract(int n1, int n2) {


int sum;
sum = n1 - n2;
return sum;
}

public static int divide(int n1, int n2) {


int sum;
sum = n1 / n2;
return sum;
}
}

OUTPUT:
IV. QUESTION AND ANSWER:

1. Consider the following two classes:


public class ClassA {
public void methodOne(int i) {
}
public void methodTwo(int i) {
}
public static void methodThree(int i) {
}
public static void methodFour(int i) {
}
}

public class ClassB extends ClassA {


public static void methodOne(int i) {
}
public void methodTwo(int i) {
}
public void methodThree(int i) {
}
public static void methodFour(int i) {
}
}
a. Which method overrides a method in the superclass?
b. Which method hides a method in the superclass?
c. What do the other methods do?
A. The method that overrides a method in the superclass is method two
B. The Method that hides a method in the superclass is method one
C. They cause the compile-time errors. _
Topic Polymorphism and Abstraction
Lab Activity No 7a
Lab Activity Comparison
CLO 1,3
Program execution (20)
Correct output (20)
Design of output (10)
Design of logic (20)
Standards (20)
Delivery (10)
TOTAL

Topic Polymorphism and Abstraction


Lab Activity No 7a
Lab Activity Person
CLO 1,3
Program execution (20)
Correct output (20)
Design of output (10)
Design of logic (20)
Standards (20)
Delivery (10)
TOTAL

Topic Polymorphism and Abstraction


Lab Activity No 7a
Lab Activity Arithmetic Operations
CLO 1,3
Program execution (20)
Correct output (20)
Design of output (10)
Design of logic (20)
Standards (20)
Delivery (10)
TOTAL

Potrebbero piacerti anche