Week 4 of OOP in JAVA

1. Design a Java program to demonstrate inheritance and method overriding using a base class Media and two derived classes Book and Tape.
Create a superclass Media that stores common information about different types of media items.
The class should contain the following data members:
• String title
• double price
Include the following member functions in the Media class:
• A constructor to initialize the title and price.
• A method display() to display the details of the media item.
Create two subclasses:
1. Book Class
The Book class should inherit from Media and contain an additional data member:
• int pages
Override the display() method to show the title, price, and number of pages of the book.
2. Tape Class
The Tape class should inherit from Media and contain an additional data member:
• double duration (in minutes)
Override the display() method to show the title, price, and duration of the tape.


class Media{
    String title;
    double price;

    Media(String t,double p){
        title=t;
        price=p;
    }

    void display(){
        System.out.println("Title: "+title);
        System.out.println("Price: "+price);
    }
}

class Book extends Media{
    int pages;

    Book(String t,double p,int pg){
        super(t,p);
        pages=pg;
    }

    void display(){
        System.out.println("Book Details");
        super.display();
        System.out.println("Pages: "+pages);
    }
}

class Tape extends Media{
    double duration;

    Tape(String t,double p,double d){
        super(t,p);
        duration=d;
    }

    void display(){
        System.out.println("Tape Details");
        super.display();
        System.out.println("Duration: "+duration+" minutes");
    }
}

public class MediaDemo{
    public static void main(String[] args){
        Book b=new Book("Java Programming",450,320);
        Tape t=new Tape("Music Album",200,60);

        b.display();
        System.out.println();
        t.display();
    }
}


Create a class Shape with a method displayArea().
Create two derived classes Circle and Rectangle that override the displayArea() method.
• In Circle, calculate area using radius.
• In Rectangle, calculate area using length and breadth.
• Demonstrate method overriding by calling the method from objects of derived classes.


class Shape{
    void displayArea(){
        System.out.println("Area");
    }
}

class Circle extends Shape{
    double radius;

    Circle(double r){
        radius=r;
    }

    void displayArea(){
        double area=3.14*radius*radius;
        System.out.println("Area of Circle = "+area);
    }
}

class Rectangle extends Shape{
    double length,breadth;

    Rectangle(double l,double b){
        length=l;
        breadth=b;
    }

    void displayArea(){
        double area=length*breadth;
        System.out.println("Area of Rectangle = "+area);
    }
}

public class ShapeDemo{
    public static void main(String[] args){
        Circle c=new Circle(5);
        Rectangle r=new Rectangle(4,6);

        c.displayArea();
        r.displayArea();
    }
}


Create an abstract class Student with:
• an abstract method getPercentage()
Create two subclasses:
• ScienceStudent
• ArtsStudent
Task:
• ScienceStudent should calculate percentage from 3 subjects.
• ArtsStudent should calculate percentage from 4 subjects.
• Display the percentage using overridden methods.


abstract class Student{
    abstract void getPercentage();
}

class ScienceStudent extends Student{
    int s1,s2,s3;

    ScienceStudent(int a,int b,int c){
        s1=a;
        s2=b;
        s3=c;
    }

    void getPercentage(){
        double per=(s1+s2+s3)/3.0;
        System.out.println("Science Percentage = "+per);
    }
}

class ArtsStudent extends Student{
    int s1,s2,s3,s4;

    ArtsStudent(int a,int b,int c,int d){
        s1=a;
        s2=b;
        s3=c;
        s4=d;
    }

    void getPercentage(){
        double per=(s1+s2+s3+s4)/4.0;
        System.out.println("Arts Percentage = "+per);
    }
}

public class StudentDemo{
    public static void main(String[] args){
        ScienceStudent s=new ScienceStudent(80,75,90);
        ArtsStudent a=new ArtsStudent(70,65,80,75);

        s.getPercentage();
        a.getPercentage();
    }
}


Write a Java program that copies a portion of an array from the nth position to the mth position entered by the user into another array. Use appropriate methods from the java.util.Arrays class to perform the copying and display both arrays.


import java.util.*;

public class ArrayCopy{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);

    int[] arr = {10,20,30,40,50,60,70,80};

    System.out.print("Enter starting position (n): ");
    int n = sc.nextInt();

    System.out.print("Enter ending position (m): ");
    int m = sc.nextInt();

    int[] newArr = Arrays.copyOfRange(arr,n,m+1);

    System.out.println("Original Array: " + Arrays.toString(arr));
    System.out.println("Copied Array: " + Arrays.toString(newArr));
  }
}


Write a Java program to check whether an array is a palindrome.


public class ArrayPalindrome{
  public static void main(String[] args){
    int[] arr = {1,2,3,2,1};

    boolean isPalindrome = true;

    for(int i=0;i<arr.length/2;i++){
      if(arr[i] != arr[arr.length-1-i]){
        isPalindrome = false;
        break;
      }
    }

    if(isPalindrome)
      System.out.println("Array is Palindrome");
    else
      System.out.println("Array is not Palindrome");
  }
}


Write a Java program to sort an array in ascending order. Using a)Bubble sort b) Selection sort c)Quick sort. Don’t use Arrays.sort().


class Sorting{

  static void bubbleSort(int arr[]){
    for(int i=0;i<arr.length-1;i++){
      for(int j=0;j<arr.length-i-1;j++){
        if(arr[j] > arr[j+1]){
          int temp = arr[j];
          arr[j] = arr[j+1];
          arr[j+1] = temp;
        }
      }
    }
  }

  static void selectionSort(int arr[]){
    for(int i=0;i<arr.length-1;i++){
      int min = i;
      for(int j=i+1;j<arr.length;j++){
        if(arr[j] < arr[min])
          min = j;
      }
      int temp = arr[min];
      arr[min] = arr[i];
      arr[i] = temp;
    }
  }

  static void quickSort(int arr[],int low,int high){
    if(low < high){
      int pi = partition(arr,low,high);
      quickSort(arr,low,pi-1);
      quickSort(arr,pi+1,high);
    }
  }

  static int partition(int arr[],int low,int high){
    int pivot = arr[high];
    int i = low - 1;

    for(int j=low;j<high;j++){
      if(arr[j] < pivot){
        i++;
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
      }
    }

    int temp = arr[i+1];
    arr[i+1] = arr[high];
    arr[high] = temp;

    return i+1;
  }

  static void printArray(int arr[]){
    for(int i:arr)
      System.out.print(i+" ");
    System.out.println();
  }

  public static void main(String[] args){
    int arr1[] = {5,2,9,1,3};
    int arr2[] = {5,2,9,1,3};
    int arr3[] = {5,2,9,1,3};

    bubbleSort(arr1);
    System.out.print("Bubble Sort: ");
    printArray(arr1);

    selectionSort(arr2);
    System.out.print("Selection Sort: ");
    printArray(arr2);

    quickSort(arr3,0,arr3.length-1);
    System.out.print("Quick Sort: ");
    printArray(arr3);
  }
}


Write a Java program to find second largest and second smallest element.


public class SecondLargestSmallest{
  public static void main(String[] args){
    int[] arr = {8,3,5,1,9,6,2};

    int largest = Integer.MIN_VALUE;
    int secondLargest = Integer.MIN_VALUE;

    int smallest = Integer.MAX_VALUE;
    int secondSmallest = Integer.MAX_VALUE;

    for(int i=0;i largest){
        secondLargest = largest;
        largest = arr[i];
      }
      else if(arr[i] > secondLargest && arr[i] != largest){
        secondLargest = arr[i];
      }

      if(arr[i] < smallest){
        secondSmallest = smallest;
        smallest = arr[i];
      }
      else if(arr[i] < secondSmallest && arr[i] != smallest){
        secondSmallest = arr[i];
      }
    }

    System.out.println("Second Largest = " + secondLargest);
    System.out.println("Second Smallest = " + secondSmallest);
  }
}


Write a Java program to remove duplicate elements from an array.


import java.util.*;

public class RemoveDuplicates{
  public static void main(String[] args){
    int[] arr = {1,2,2,3,4,4,5,1};

    Set set = new LinkedHashSet<>();

    for(int i:arr){
      set.add(i);
    }

    int[] result = new int[set.size()];
    int index = 0;

    for(int i:set){
      result[index++] = i;
    }

    System.out.println("Array after removing duplicates:");
    for(int i:result)
      System.out.print(i + " ");
  }
}


Write a Java program where an array contains n-1 numbers from 1 to n with one number missing.


public class MissingNumber{
  public static void main(String[] args){
    int[] arr = {1,2,3,5,6};
    int n = 6;

    int expectedSum = n*(n+1)/2;
    int actualSum = 0;

    for(int i:arr){
      actualSum += i;
    }

    int missing = expectedSum - actualSum;

    System.out.println("Missing number = " + missing);
  }
}


Write a Java program that stores n numbers in an array and finds the median value.


import java.util.*;

public class MedianArray{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);

    System.out.print("Enter number of elements: ");
    int n = sc.nextInt();

    int[] arr = new int[n];

    System.out.println("Enter elements:");
    for(int i=0;i<n;i++){
      arr[i] = sc.nextInt();
    }

    Arrays.sort(arr);

    double median;

    if(n % 2 == 0){
      median = (arr[n/2 - 1] + arr[n/2]) / 2.0;
    }
    else{
      median = arr[n/2];
    }

    System.out.println("Median = " + median);
  }
}


Comments

Popular Posts