










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











Student.java
Activity 7-
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
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
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()); } }