OOP in java Practical Exercise

 Hello Divya,

    Here is your practicals.

import java.util.Scanner;

public class Sample1 {
    /*
    // For example 1: Program to take input from user and print positive negative or zero.

    public static void main(String[] args) {
        try (Scanner sc= new Scanner(System.in)) {
            System.out.print("Enter a number: ");
            int a = sc.nextInt();

            if (a > 0) {
                System.out.println("POSITIVE");
            } 
            else if (a == 0){
                System.out.println("ZERO");
            }
            else {
                System.out.println("NEGETIVE");
            }
        }
    }
    */

    /*
     * Example 2: find maximum from given 3 numbers
     
    public static void main(String[] args) {
        try(Scanner sc = new Scanner(System.in)){
            System.out.println("Enter three numbers: ");

            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();

            int max = a;
            if (max < b) {
                max = b;
            }
            if (max < c) {
                max = c;
            }
            System.out.println("Maximum number of three: " + max);
        }
    }
    */

    /*
     * Example 3: take 5 subject marks from student and give them division according following...
     * 60+ : 1st
     * 50+ : 2nd
     * 40+ : 3rd
     * 40- : fail 

    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            System.out.println("Enter marks of 5 subject(out of 100): ");
            float [] marks = new float[5];
            float avg = 0;
            for (int i = 0; i < marks.length; i++) {
                marks[i] = sc.nextFloat();
                if (marks[i] > 100 || marks[i] < 0) {
                    System.out.print("Enter valid marks: ");
                    if (i == 0) {
                        marks[i] = sc.nextFloat();
                    }
                    else {
                        i-- ;
                    }
                }
            }
            for (int j = 0; j < marks.length; j++) {
                avg = (marks[j] + avg);
            }
            avg = avg / 5;

            if (avg >= 60) {
                System.out.println("First Division");
            } 
            else if (avg >= 50) {
                System.out.println("Second Division");
            }
            else if (avg >= 40) {
                System.out.println("Third Division");
            }
            else {
                System.out.println("Fail");
            }
        }
    }
    */

    /*
     * Example 4: take number input and print day accordingly.
     */
    /*
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            System.out.println("Enter number between 1-7: ");
            int a = sc.nextInt();

            switch (a){
                case 1:
                    System.out.println("Sunday"); 
                    break;

                case 2:
                    System.out.println("Monday");
                    break;

                case 3:
                    System.out.println("Tuesday");
                    break;

                case 4:
                    System.out.println("Wednesday"); 
                    break;

                case 5:
                    System.out.println("Thursday");
                    break;

                case 6:
                    System.out.println("Friday");
                    break;

                case 7:
                    System.out.println("Saturday");
                    break;

                default:
                    System.out.println("Enter valid number...");
            }
        }
    }
    */

    /*
     * Example 1 while: print multiplication table 
    

    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            System.out.print("Enter the number you want multiplication table: ");    
            int a = sc.nextInt();

            int i = 1;
            while (i < 11) {
                // System.out.println("n X i = ni");
                System.out.println(a + " X " + i + " = " + (a*i));
                i++ ;
            }
        }
    }
     */

    /*
     * Example 2 while: Sum of even integer from 1 to 10
    

    public static void main(String[] args) {
        int i = 0;
        int sum = 0;

        while (i <= 10) {
            sum = sum + i;
            i = i + 2;
        }
        System.out.println(sum);
    }
    */

    /*
     * Example 1 For: avg of n numbers
     */

    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            System.out.println("Enter the number: ");
            int a = sc.nextInt();
            double avg = 0;
            for (int i = 1; i <= a ; i++) {
                avg = avg + i;
            }
            avg = (avg / a);
            System.out.println(avg);
        }
    }
}
Thank You.

Post a Comment

0 Comments