Docsity

Polymorphism and Abstract Classes in Java: Understanding Concepts with Code Examples, Exercises of Programming Languages

Toggle sidebar
bg1
PRE-ASSESSMENT 6
1. In your own words define Polymorphism
Polymorphism is an important concept of object-oriented programming. It simply means more
than one form.
2. Describe Polymorphism by creating a scenario in the real world
A person at the same time can have different characteristic. Like a man at the same time is a
father, a husband, an employee. So the same person possess different behavior in different situations.
This is called polymorphism.
3. In your own opinion why is Polymorphism important in Java OOP approach? Explain.
Polymorphism is considered one of the important features of Object-Oriented Programming.
Polymorphism allows us to perform a single action in different ways. In other words, polymorphism
allows you to define one interface and have multiple implementations. The word “poly” means many
and “morphs” means forms, so it means many forms.
POST-ASSESSMENT 6
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Polymorphism and Abstract Classes in Java: Understanding Concepts with Code Examples and more Exercises Programming Languages in PDF only on Docsity!

PRE-ASSESSMENT 6

  1. In your own words define Polymorphism Polymorphism is an important concept of object-oriented programming. It simply means more than one form.
  2. Describe Polymorphism by creating a scenario in the real world A person at the same time can have different characteristic. Like a man at the same time is a father, a husband, an employee. So the same person possess different behavior in different situations. This is called polymorphism.
  3. In your own opinion why is Polymorphism important in Java OOP approach? Explain. Polymorphism is considered one of the important features of Object-Oriented Programming. Polymorphism allows us to perform a single action in different ways. In other words, polymorphism allows you to define one interface and have multiple implementations. The word “poly” means many and “morphs” means forms, so it means many forms. POST-ASSESSMENT 6
  1. Given the program below. Show the output and identify the polymorphism part in this code. (15 pts) Animal.java public class Animal{ public void sound(){ System.out.println("Animal is making a sound"); } } Horse.java class Horse extends Animal{ @Override public void sound(){ System.out.println("Neigh"); } public static void main(String args[]){ Animal obj = new Horse(); obj.sound(); } } OUTPUT: Neigh
  1. Create a program based on item 1. Create a new java files named it as Human.java and Student.java. Show the output of your program. (20 pts) Human.java

Student.java

Activity 7-

  1. import java.util.Arrays; import java.util.Collections; class CompareSorts { public static void main(String[] args) { int size = 0; try { size = Integer.parseInt(args[0]); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Usage: java CompareSorts n"); System.out.println("where n is the size of the array"); System.exit(1); } MyNumber[] nums = new MyNumber[size]; for (int i = 0; i < nums.length; i++) { nums[i] = new MyNumber(100000); } // make copies MyNumber[] nums2 = new MyNumber[size]; MyNumber[] nums3 = new MyNumber[size]; MyNumber[] nums4 = new MyNumber[size]; for (int i = 0; i < nums.length; i++) { nums2[i] = nums[i]; nums3[i] = nums[i]; nums4[i] = nums[i]; } // Selection sort nums Stopwatch timer = new Stopwatch(); selectSort(nums2); System.out.println("Selection sort completed:"); System.out.println("elapsed time: " + timer.elapsedTime()); System.out.println("array sorted: " + isSorted(nums2)); //print(nums2); // Insertion sort nums timer = new Stopwatch(); insertionSort(nums3); System.out.println("\nInsertion sort completed:"); System.out.println("elapsed time: " + timer.elapsedTime()); System.out.println("array sorted: " + isSorted(nums3)); // Arrays.sort(nums4) timer = new Stopwatch(); Arrays.sort(nums4,Collections.reverseOrder());

System.out.println("\nArrays.sort() completed:"); System.out.println("elapsed time: " + timer.elapsedTime()); System.out.println("array sorted: " + isSorted(nums4)); // sort sorted arrays timer = new Stopwatch(); selectSort(nums2); System.out.println("\nSelection sort on sorted array:"); System.out.println("elapsed time: " + timer.elapsedTime()); System.out.println("array sorted: " + isSorted(nums2)); timer = new Stopwatch(); insertionSort(nums3); System.out.println("\nInsertion sort on sorted array:"); System.out.println("elapsed time: " + timer.elapsedTime()); System.out.println("array sorted: " + isSorted(nums3)); timer = new Stopwatch(); Arrays.sort(nums4,Collections.reverseOrder()); System.out.println("\nArrays.sort() on sorted array:"); System.out.println("elapsed time: " + timer.elapsedTime()); System.out.println("array sorted: " + isSorted(nums4)); } // main ends private static void selectSort(MyNumber[] a) { for (int pos = 0; pos < a.length-1; pos++) { MyNumber max = a[pos]; int maxpos = pos; for (int j = pos+1; j < a.length; j++) { if (a[j].compareTo(max) > 0) { max = a[j]; maxpos = j; } } a[maxpos] = a[pos]; a[pos] = max; } } private static void insertionSort(MyNumber[] a) { for (int start = 1; start < a.length; start++) { int pos = start; MyNumber temp = a[start]; while (pos > 0 && temp.compareTo(a[pos-1]) > 0) { a[pos] = a[pos-1]; pos--; } a[pos] = temp; }

// Sort even number in ascending order Arrays.sort(arr, k, n); } // Driver Method public static void main(String[] args) { Integer arr[] = { 2, 4, 6, 8, 10, 12 }; twoWaySort(arr, arr.length); System.out.println(Arrays.toString(arr)); } } Output: 2 4 6 8 10 12

PRE-ASSESSMENT 7-

  1. Binary search is an efficient algorithm for finding an item from a sorted list of items. It works by repeatedly dividing in half the portion of the list that could contain the item, until you've narrowed down the possible locations to just one.
  2. A greedy algorithm is a mathematical process that looks for simple, easy-to-implement solutions to complex, multi-step problems by deciding which next step will provide the most obvious benefit.

class Monster { public void Monster() { System.out.println("Aswang"); } } class Tikbalang extends Monster { public void Monster() { System.out.println("Tikbalang: It is a tall, bony humanoid creature with the head and hooves of a horse "); } } class Kapre extends Monster { public void Monster() { System.out.println("Kapre: being a tall, dark-coloured, hairy, and muscular creature."); } } class Tyanak extends Monster { public void Monster() { System.out.println("Tyanak: form of a newborn baby "); } } class Main { public static void main(String[] args) { Monster aswang = new Monster(); Monster Tik = new Tikbalang(); Monster Kap = new Kapre(); Monster Tyan = new Tyanak(); aswang.Monster(); Tik.Monster(); Kap.Monster(); Tyan.Monster(); } }

class Calculator { int add(int a , int b) { return a + b; } int sub(int a , int b) { return a - b; } } class AdvancedCalculator extends Calculator { int mult(int a , int b) { return a * b; } int div(int a , int b) { return a / b; } public static void main(String args[]) { AdvancedCalculator cal = new AdvancedCalculator(); System.out.println( "addition : "+cal.add(1, 2) ); System.out.println( "subtraction : "+cal.sub(1, 2) ); System.out.println( "multiplication : "+cal.mult(1, 2) ); System.out.println( "division : "+cal.div(1, 2) ); } } OUTPUT: addition : 3 subtraction : - multiplication : 2 division : 0

POST-ASSESSMENT 9-

EncapTest.java public class EncapTest { Encapsulation private double side; public double getSide() { return side; } public void setSide(double newSide) { side = newSide; } } RunEncap.java public class RunEncap extends EncapTest { public static void main(String[] args) { EncapTest encap = new EncapTest(); encap.setSide(4.5); double area = encap.getSide()*encap.getSide(); System.out.println("Area of Square is: "+area); } }  declare class variables/attributes as private  provide public get and set methods to access and update the value of a private variable  Instead, we use the getName() and setName() methods to acccess and update the variable:

abstract class Grade { public abstract double getGrades(); ABSTRACT // Regular method private double prelim; private double midterm; private double finals; public double getPrelim() { return prelim; } public double getMidterm() { return midterm; } public double getFinals() { return finals; } public void setGrade(double newPrelim,double newMidterm,double newFinals) { prelim = newPrelim; midterm = newMidterm; finals = newFinals; } } class Average extends Grade { public double getGrades() { return (getPrelim() + getMidterm() + getFinals() )/3 ; } } class Main { public static void main(String[] args) { Grade grade = new Average(); grade.setGrade(84,85,86); System.out.println("The average is: "+grade.getGrades()); } }