Saturday, July 20, 2024

Java-I Assignment 1

Object Oriented Programming Using Java -I (Core Java) 

 Assignment 1: Java Tools and IDE, Simple Java Programs

Set A

a) Using javap, view the methods of the following classes from the lang package: java.lang.Object , java.lang.String and java.util.Scanner. and also Compile sample program 8. Type the following command and view the bytecodes. javap -c MyClass.

Program:- 

public class MyClass

{

int num;

public MyClass()

{

num=0;

}

public MyClass(int num)

{

this.num = num;

}

public static void main(String[] args)

{

MyClass m1 = new MyClass(); if(args.length > 0)

{

int n = Integer.parseInt(args[0]); MyClass m2 = new MyClass(n); System.out.println(m1.num); System.out.println(m2.num);

}

else

System.out.println(“Insufficient arguments”);

}

}


Command:-

javac  MyClass.java

Java MyClass

javap -c MyClass.


b) Write a program to calculate perimeter and area of rectangle.

(hint : area = length * breadth , perimeter=2*(length+breadth))

Program:-

import java.util.Scanner;

public class Rectangle 

{


    public static void main(String[] args) 

    {

        Scanner sc = new Scanner(System.in);


        System.out.println("Enter Length of Rectangle : ");

        int length = sc.nextInt();


        System.out.println("Enter breadth of Rectangle : ");

        int breadth = sc.nextInt();


        int area = length * breadth;

        System.out.println("Area of Reactangle : " + area);


        int Perimeter = 2 * (length + breadth);

        System.out.println("Perimeter of Reactangle : " + Perimeter);


        sc.close();

    }

}



c) Write a menu driven program to perform the following operations

i. Calculate the volume of cylinder. (hint : Volume: π × r² × h)

ii. Find the factorial of given number.

iii. Check the number is Armstrong or not.

iv. Exit

Program:-

 

import java.util.Scanner;


public class NumericalsMenu {

    public static void main(String[] args) {


        Scanner sc = new Scanner(System.in);


        System.out.println("\n1.Volume of Cylinder. \n2.Factorial of Number. \n3.Armstrong Number. \n4.Exit");

        System.out.println("Enter Your Choice : ");

        int choice = sc.nextInt();


        switch (choice) {

        case 1:

            System.out.println("Enter Radius:");

            Float r = sc.nextFloat();

            System.out.println("Enter Height:");

            Float h = sc.nextFloat();

            double Volume = Math.PI * r * r * h;

            System.out.printf("Volume of Cylinder: %f" ,Volume);

            break;


        case 2:

            System.out.println("Enter Number for Finding Factorial : ");

            int num = sc.nextInt();

            long fact = 1;

            for (int i = 1; i <= num; ++i) {

                fact = fact * i;

            }

            System.out.printf("Factorial of %d = %d\n", num, fact);

            break;


        case 3:

            System.out.println("Enter Number for Finding Armstrong Number : ");


            int n = sc.nextInt();


            int leng = 0;

            int t1 = n;


            while (t1 != 0) {

                t1 = t1 / 10;

                leng = leng + 1;

            }


            int t2 = n;

            int arm = 0;

            int rem;


            while (t2 != 0) {

                int mult = 1;

                rem = t2 % 10;

                for (int i = 1; i <= leng; i++) {

                    mult = mult * rem;

                }

                arm = arm + mult;

                t2 = t2 / 10;

            }

            if (arm == n) {

                System.out.println("The given number is armstrong..!");

            } else {

                System.out.println("The given number is not armstrong..!");

            }

            break;

        case 4:

            System.exit(0);

        

        default:

            break;


        }

        sc.close();


    }

}


d) Write a program to accept the array element and display in reverse order.

Program:-

import java.util.Scanner;


public class ReverseArray {

    public static void main(String[] args) {


        Scanner sc = new Scanner(System.in);


        System.out.println("Enter size of Array :");

        int n = sc.nextInt();


        int arr[] = new int[n];


        System.out.println("Enter Elements in Array");

        for (int i = 0; i < n; i++) {

            arr[i] = sc.nextInt();

        }

        

        System.out.println("Array elements:");

        for (int i = 0; i < n; i++) {

            System.out.print(arr[i] + " ");

        }


        System.out.println("\nArray elements in Reverse Order :");

        for (int i = n - 1; i >= 0; i--) {

            System.out.print(arr[i] + " ");

        }

        sc.close();

    }

}



Set B

a) Write a java program to display the system date and time in various formats shown below: 

Current date is : 31/08/2021 

Current date is : 08-31-2021

Current date is : Tuesday August 31 2021

Current date and time is : Fri August 31 15:25:59 IST 2021 

Current date and time is : 31/08/21 15:25:59 PM +0530 

Current time is : 15:25:59

Current week of year is : 35 

Current week of month : 5 

Current day of the year is : 243

Note: Use java.util.Date and java.text.SimpleDateFormat class


Program:-

import java.text.SimpleDateFormat;

import java.util.Date;


public class DateFormatter {

    public static void main(String[] args) {


        Date date = new Date();

        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

        String Str = sdf.format(date);

        System.out.println("Current date is: " + Str);


        sdf = new SimpleDateFormat("MM-dd-yyyy");

        Str = sdf.format(date);

        System.out.println("Current date is: " + Str);


        sdf = new SimpleDateFormat("EEEE MMMM dd yyyy");

        Str = sdf.format(date);

        System.out.println("Current date is: " + Str);


        sdf = new SimpleDateFormat("E MMMM dd HH:mm:ss z yyyy");

        Str = sdf.format(date);

        System.out.println("Current date and time is: " + Str);


        sdf = new SimpleDateFormat("dd/MM/yy HH:mm:ss a Z");

        Str = sdf.format(date);

        System.out.println("Current date and time is: " + Str);


        sdf = new SimpleDateFormat("hh:mm:ss");

        Str = sdf.format(date);

        System.out.println("Current time is: " + Str);


        sdf = new SimpleDateFormat("w");

        Str = sdf.format(date);

        System.out.println("Current week of year is: " + Str);


        sdf = new SimpleDateFormat("W");

        Str = sdf.format(date);

        System.out.println("Current week of the month is: " + Str);


        sdf = new SimpleDateFormat("D");

        Str = sdf.format(date);

        System.out.println("Current day of the year: " + Str);

    }

}



b) Define a class MyNumber having one private int data member. Write a default constructor to initialize it to 0 and another constructor to initialize it to a value (Use this). Write methods isNegative, isPositive, isZero, isOdd, isEven. Create an object in main. Use command line arguments to pass a value to the object

(Hint : convert string argument to integer) and perform the above tests. Provide javadoc comments for all constructors and methods and generate the html help file.)

Program:-

public class MyNumber {

    private int x;


    public MyNumber() {

        x = 0;

    }


    public MyNumber(int x) {

        this.x = x;

    }


    public boolean isNegative() {

        if (x < 0)

            return true;

        else

            return false;

    }


    public boolean isPositive() {

        if (x > 0)

            return true;

        else

            return false;

    }


    public boolean isZero() {

        if (x == 0)

            return true;

        else

            return false;

    }


    public boolean isOdd() {

        if (x % 2 != 0)

            return true;

        else

            return false;

    }


    public boolean isEven() {

        if (x % 2 == 0)

            return true;

        else

            return false;

    }


    public static void main(String[] args){

        int x = Integer.parseInt(args[0]);

        MyNumber m = new MyNumber(x);

        if (m.isNegative()) {

            System.out.println("Number is Negative");

        }

        if (m.isPositive()) {

            System.out.println("Number is Positive");

        }

        if (m.isEven()) {

            System.out.println("Number is Even");

        }

        if (m.isOdd()) {

            System.out.println("Number is Odd");

        }

        if (m.isZero()) {

            System.out.println("Number is Zero");

        }


    }


}


c) Write a menu driven program to perform the following operations on multidimensional array ie matrix :

i. Addition

ii. Multiplication

iii. Transpose of any matrix.

iv. Exit

Program:-

import java.util.Scanner;


public class MenuMatrix {

    // to calculate Addition of matrix

    public void addition() {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter size of row and column: ");

        int r = sc.nextInt();

        int c = sc.nextInt();


        int[][] m1 = new int[r][c];

        int[][] m2 = new int[r][c];


        System.out.println("\nEnter values of matrix M1:\n");

        for (int i = 0; i < r; i++) {

            for (int j = 0; j < c; j++) {

                System.out.printf("Enter element at index %d and %d :", i, j);

                m1[i][j] = sc.nextInt();

            }

        }

        System.out.println("\nEnter values of matrix M2:\n");

        for (int i = 0; i < r; i++) {

            for (int j = 0; j < c; j++) {

                System.out.printf("Enter element at index %d and %d :", i, j);

                m2[i][j] = sc.nextInt();

            }

        }

        System.out.println("\nSum of M1 and M2:");

        int[][] sum = new int[r][c];

        for (int i = 0; i < r; i++) {

            for (int j = 0; j < c; j++) {

                sum[i][j] = m1[i][j] + m2[i][j];

            }

        }

        for (int i = 0; i < r; i++) {

            for (int j = 0; j < c; j++) {

                System.out.print(sum[i][j] + " ");

            }

            System.out.println();

        }

        sc.close();


    }// Addition


    // To Multiplicate Matrices


    public void multiplication() {


        Scanner sc = new Scanner(System.in);

        System.out.println("Enter size of row and column: ");

        int r = sc.nextInt();

        int c = sc.nextInt();


        int[][] m1 = new int[r][c];

        int[][] m2 = new int[r][c];

        System.out.println("\nEnter values of matrix M1:\n");

        for (int i = 0; i < r; i++) {

            for (int j = 0; j < c; j++) {

                System.out.printf("Enter element at index %d and %d :", i, j);

                m1[i][j] = sc.nextInt();

            }

        }

        System.out.println("\nEnter values of matrix M2:\n");

        for (int i = 0; i < r; i++) {

            for (int j = 0; j < c; j++) {

                System.out.printf("Enter element at index %d and %d :", i, j);

                m2[i][j] = sc.nextInt();

            }

        }


        System.out.println("\nMultiplication of M1 and M2:\n");

        int[][] mul = new int[r][c];

        for (int i = 0; i < r; i++) {

            for (int j = 0; j < c; j++) {

                for (int k = 0; k < c; k++) {

                    mul[i][j] = mul[i][j] + m1[i][k] * m2[k][j];

                }

            }

        }

        for (int i = 0; i < r; i++) {

            for (int j = 0; j < c; j++) {

                System.out.print(mul[i][j] + " ");

            }

            System.out.println();

        }

        sc.close();

    }// Multiplication


    // Transpose of Matrix

    public void transpose() {


        Scanner sc = new Scanner(System.in);

        System.out.println("Enter size of row and column: ");

        int r = sc.nextInt();

        int c = sc.nextInt();


        int[][] m = new int[r][c];

        System.out.println("Enter values of Matrix:\n");

        for (int i = 0; i < r; i++) {

            for (int j = 0; j < c; j++) {

                System.out.printf("Enter element at index %d and %d :", i, j);

                m[i][j] = sc.nextInt();

            }

        }

        System.out.println("\nEntered Matrix...\n");

        for (int i = 0; i < r; i++) {

            for (int j = 0; j < c; j++) {

                System.out.print(m[i][j] + " ");

            }

            System.out.println();

        }

        System.out.println("\nTranspose Matrix...\n");

        for (int i = 0; i < r; i++) {

            for (int j = 0; j < c; j++) {

                System.out.print(m[j][i] + " ");

            }

            System.out.println();

        }

        sc.close();

    }// Transpose


    public static void main(String[] args) {

        MenuMatrix m = new MenuMatrix();

        Scanner sc = new Scanner(System.in);


        System.out.println("\n1.Addition of Matrix. \n2.Multiplication of Matrix. \n3.Transpose of Matrix. \n4.Exit");

        System.out.println("Enter Your Choice : ");

        int choice = sc.nextInt();


        switch (choice) {

            case 1:

                m.addition();

                break;

            case 2:

                m.multiplication();

                break;

            case 3:

                m.transpose();

                break;

        }

        sc.close();

    }

}


Set C

a) Write a program to accept n names of country and display them in descending order.

Program:- 

import java.util.*;


public class countrysort {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter total no of Countries: ");

        int n = sc.nextInt();

        String country[] = new String[n];

        sc.nextLine();

        for (int i = 0; i < n; i++) {

            System.out.println("Enter country no :" + (i + 1));

            country[i] = sc.nextLine();

        }

        System.out.println("Country names without Sorting:");

        for (String ele : country) {

            System.out.println("" + ele);

        }

        String temp;

        for (int i = 0; i < n; i++) {

            for (int j = i + 1; j < n; j++) {

                if (country[i].compareTo(country[j]) < 0) {

                    temp = country[i];

                    country[i] = country[j];

                    country[j] = temp;

                }

            }

        }

        System.out.println("Countries in Descending order:");

        for (String ele : country) {

            System.out.println("" + ele);

        }

        sc.close();

    }

}


b) Write a menu driven program to perform the following operations on 2D array:

i. Sum of diagonal elements

ii. Sum of upper diagonal elements

iii. Sum of lower diagonal elements

iv. Exit

c) Write a program to display the 1 to 15 tables.

( 1 * 1 = 1    2 * 1 = 2    …….    15 * 1 = 15

1 * 2 = 2      2 * 2 = 4                  15 * 2 = 30

1 * 3 = 3      2 * 3 = 6                    15 * 3 = 45

…..

1 * 10 = 10     2 * 10 = 20             15 * 10 = 150)

Program:-


public class table {

    public static void main(String[] args) {

        for (int i = 1; i <= 15; i++) {

            System.out.println("Table of "+(i)+" number:\n");

            for (int j = 1; j <= 10; j++) {

                

                System.out.println(i + "*" + j + "=" +i*j);

            }

            System.out.println("----------------------");

        }

    }

}


No comments:

Post a Comment