Java code-: To Print the sum, multiply, subtract, divide and remainder of two numbers

Table of Contents

1. a. Write a Java program to find the sum (addition), multiply, subtract, divide and remainder of two numbers.

b. Write a Java program to find the area and perimeter of a circle.

C. Write a Java program that takes three numbers as input to calculate and display the average of the numbers.

d. Write a Java program to accept a number and check the number is even or not.

e. Write a Java program that accepts two integer values from the user using command line argument and find the larger number.

2. a. Write a Java program to check a given number is Armstrong number or not



d. Create a class called Counter with a static data member count and a static method incrementCounter. Implement a parameterized constructor that increments the count every time an object is created. Create objects of the class and display the final count.

e. Create a class called Calculator with static methods for basic arithmetic operations (add, subtract, multiply, divide). Implement these meth to perform the respective operations on two numbers. Demonstrate the usage of these methods in the main method.

4. a. Write a Java program to input n numbers in an array and display them.

b. Write a program in Java to reverse an array.

c. Write a Java program to calculate the sum and average of n number of elements of an array.

d. Write a Java program to find the maximum and minimum of n number of elements of an array.

e. Write a program in Java to find sum of two 3x3 matrix.

5. a. Write a program in Java to search an element in an array

b. Write a program in Java to sort elements of a given array.

c. Write a program in Java to check if the string is palindrome or not

d. Write a program in Java to find a substring in a string.

e. Write a program in Java to find sum of two 3x3 matrix.


Module II: Concepts of OOPs using Java

 

Implementation of Object oriented concepts using Java, History and Features of Java, internal How to set the path? JDK, JRE, and JVM (Java Virtual Machine). Compiling and Executing a Java Program, Variables, Constants, Keywords Data Types, Operators (Arithmetic, Logical and Bitwise) and Expressions, Comments, Doing Basic Program Output, Decision Making Constructs (conditional statements and loops) and Nesting, Java Methods (Defining, Scope, Passing and Returning Arguments, Type Conversion and Type and Checking, Built-in Java Class Methods)


What is Java?

Java is a programming language and a platform. Java is a high level, robust, object-oriented and secure programming language.

Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Before Java, its name was Oak. Since Oak was already a registered company, so James Gosling and his team changed the name from Oak to Java.



 



1. a. Write a Java program to find the sum (addition), multiply, subtract, divide and remainder of two numbers.



import java.util.*;

class Code1a {
    public static void main(String A[]) {
        Scanner av = new Scanner(System.in);
        System.out.print("Enter two numbers: ");
        int a = av.nextInt();
        int b = av.nextInt();
        System.out.print("Addition is: " + (a + b));
        System.out.print("\nMultiplication is: " + (a * b));
        System.out.print("\nSubstraction is: " + (a - b));
        System.out.print("\nDivide is: " + ((float) a / b));
        System.out.print("\nRemainder is: " + (a % b));
        av.close();
    }
}


b. Write a Java program to find the area and perimeter of a circle.



import java.util.*;

class Code1b {
    public static void main(String A[]) {
        Scanner av = new Scanner(System.in);
        System.out.print("Enter radius of the circle: ");
        int r = av.nextInt();
        float pi = 3.14F;
        System.out.println("\nThe area is: " + (pi * (r * r)));
        System.out.println("\nThe perimeter is: " + (2 * pi * r));
        av.close();
    }
}


C. Write a Java program that takes three numbers as input to calculate and display the average of the numbers.




import java.util.*;

class Code1c {
    public static void main(String A[]) {
        Scanner av = new Scanner(System.in);
        System.out.print("Enter three number: ");
        int x = av.nextInt();
        int y = av.nextInt();
        int z = av.nextInt();
        System.out.println("Average of three no is: " + ((float) (x + y + z) / 3));
        av.close();
    }
}


d. Write a Java program to accept a number and check the number is even or not.



import java.util.*;

class Code1d {
    public static void main(String A[]) {
        Scanner av = new Scanner(System.in);
        System.out.print("Enter a no: ");
        int no = av.nextInt();
        if (no % 2 == 0) {
            System.out.println("\nThe number is Even");
        } else {
            System.out.println("\nThe number is Odd");
        }
        av.close();
    }
}


e. Write a Java program that accepts two integer values from the user using command line argument and find the larger number.



import java.util.*;

class Code1e {
    public static void main(String A[]) {
        int a = Integer.parseInt(A[0]);
        int b = Integer.parseInt(A[1]);
        if (a > b) {
            System.out.println(a + " is larger than " + b);
        } else if (a == b) {
            System.out.println("Both are equal");
        } else {
            System.out.println(a + " is smaller than " + b);
        }
    }
}


2. a. Write a Java program to check a given number is Armstrong number or not



import java.util.*;
import java.lang.Math;

class Code2a {
    public static void main(String A[]) {
        Scanner av = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int in = av.nextInt();
        int temp = in;
        int length = 0;
        int finl = 0;
        while (temp > 0) {
            temp = temp / 10;
            length++;
        }
        temp = in;
        while (temp > 0) {
            finl = finl + ((int) Math.pow((temp % 10), length));
            temp = temp / 10;
        }
        if (finl == in) {
            System.out.println("It is a Amstrong Number");
        } else {
            System.out.println("It is not Amstrong Number");
        }
        av.close();
    }
}


b. Write a Java program to find factorial of a given number.




import java.util.*;

class Code2b {
    public static void main(String A[]) {
        Scanner av = new Scanner(System.in);
        System.out.print("Enter first number: ");
        int in = av.nextInt();
        int res = 1;
        for (int i = in; i > 0; i--) {
            res = res * i;
        }
        System.out.println("The factorial is: " + res);
        av.close();
    }
}



c. Write a Java program to calculate the sum and average of n given numbers.




import java.util.*;

class Code2c {
    public static void main(String A[]) {
        Scanner av = new Scanner(System.in);
        System.out.print("Enter numbers of inputs: ");
        int no = av.nextInt();
        System.out.print("Enter numbers: ");
        int sum = 0;
        for (int i = 0; i < no; i++) {
            sum = sum + (av.nextInt());
        }
        System.out.println("Sum is: " + sum);
        System.out.println("Average is: " + (sum / no));
        av.close();
    }
}


d. Write a Java program to find the maximum and minimum of given n numbers.




import java.util.*;

class Code2d {
    public static void main(String A[]) {
        Scanner av = new Scanner(System.in);
        System.out.print("Enter numbers of inputs: ");
        int no = av.nextInt();
        int temp;
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < no; i++) {
            temp = av.nextInt();

            if (temp > max) {
                max = temp;
            }
            if (temp < min) {
                min = temp;
            }
        }
        System.out.println("Maximum is: " + max);
        System.out.println("Minimum is: " + min);
        av.close();
    }
}


3. a. Create a class with a method in Java, and use an object of this class to call the method.




public class Code3a {
    void show() {
        System.out.println("Inside Method");
    }

    public static void main(String A[]) {
        Code3a av = new Code3a();
        av.show();
    }
}


b. Define a Book class with private attributes - title, author and public getters and setters methods, invoke the public setter and getter methods from main method to access the private attributes of Book class (Implementation of encapsulation).




class Book {
    private String title = "Java";
    private String author = "Someone";

    public void set(String ab) {
        author = ab;
    }

    public String get() {
        return title;
    }

    void show() {
        System.out.println(title + " written by " + author);
    }
}

public class Code3b {

    public static void main(String A[]) {
        Book av = new Book();
        av.show();
        String nw = av.get();
        System.out.println(nw);
        av.set("Balagurusamy");
        av.show();
    }
}



C. Create a Product class with attributes like productid, productName, and price. Implement three constructors: default (initializes productid to 0, productName to an empty string, and price to 0.0), parameterized (with productid and productName) setting price to 0.0, and another parameterized (taking all three attributes). Create objects using each constructor and display product details.




class Product {
    private int productId;
    private String productName;
    private float price;

    Product() {
        System.out.println("\nConstructor 1");
        System.out.println("productId: " + productId + "\nproductName: " + productName
        + "\nprice: " + price);
    }

    Product(int productId, String productName) {
        this.productId = productId;
        this.productName = productName;
        System.out.println("\n\nConstructor 2");
        System.out.println("productId: " + productId + "\nproductName: " + productName
        + "\nprice: " + price);
    }

    Product(int productId, String productName, float price) {
        this.productId = productId;
        this.productName = productName;
        this.price = price;
        System.out.println("\n\nConstructor 3");
        System.out.println("productId: " + productId + "\nproductName: " + productName
        + "\nprice: " + price);
    }
}

class Code3c {
    public static void main(String A[]) {
        new Product();
        new Product(10235, "Watch");
        new Product(35127, "Laptop", 53642.536F);
    }
}



d. Create a class called Counter with a static data member count and a static method incrementCounter. Implement a parameterized constructor that increments the count every time an object is created. Create objects of the class and display the final count.



class Counter {
    static int count = 0;

    static void incrementCounter() {
        System.out.println("Static method called.");
    }

    Counter(int abc) {
        count++;
        System.out.println("Input = " + abc);
    }

}

class Code3d {
    public static void main(String A[]) {
        new Counter(96);
        new Counter(23);
        new Counter(75);
        new Counter(15);
        System.out.println("Final count = " + Counter.count);
        Counter.incrementCounter();
    }
}


e. Create a class called Calculator with static methods for basic arithmetic operations (add, subtract, multiply, divide). Implement these methods to perform the respective operations on two numbers. Demonstrate the usage of these methods in the main method.



class Calculator {
    static int add(int a, int b) {
        return a + b;
    }

    static int sub(int a, int b) {
        return a - b;
    }

    static int mul(int a, int b) {
        return a * b;
    }

    static float div(int a, int b) {
        return ((float) a / b);
    }

}

class Code3e {
    public static void main(String A[]) {
        int x = 17, y = 9;
        System.out.println("For " + x + " and " + y + "\n\nAddition = "
                + Calculator.add(x, y) + "\nSubtraction = "
                + Calculator.sub(x, y) + "\nMultiplication =  " + Calculator.mul(x, y)
                 + "\nDivision = "
                + Calculator.div(x, y));
    }
}


4. a. Write a Java program to input n numbers in an array and display them.




import java.util.*;

class Code4a {
    public static void main(String A[]) {
        int[] arr = new int[100];
        Scanner av = new Scanner(System.in);
        System.out.print("Enter no of inputs: ");
        int in = av.nextInt();
        System.out.print("Enter elements: ");
        for (int i = 0; i < in; i++) {
            arr[i] = av.nextInt();
        }
        System.out.println("Enter are: ");
        for (int i = 0; i < in; i++) {
            System.out.print(arr[i] + " ");
        }
        av.close();
    }
}


b. Write a program in Java to reverse an array.



import java.util.*;

class Code4b {
    public static void main(String A[]) {
        int[] arr = new int[100];
        Scanner av = new Scanner(System.in);
        System.out.print("Enter no of inputs: ");
        int in = av.nextInt();
        System.out.print("Enter elements: ");

        for (int i = 0; i < in; i++) {
            arr[i] = av.nextInt();
        }

        for (int i = 0; i <= (in / 2); i++) {
            int temp;
            temp = arr[i];
            arr[i] = arr[in - i - 1];
            arr[in - i - 1] = temp;
        }

        System.out.println("Enter are: ");
        for (int i = 0; i < in; i++) {
            System.out.print(arr[i] + " ");
        }

        av.close();
    }
}


c. Write a Java program to calculate the sum and average of n number of elements of an array.



import java.util.*;

class Code4c {
    public static void main(String A[]) {
        int[] arr = new int[100];
        Scanner av = new Scanner(System.in);
        System.out.print("Enter no of inputs: ");
        int in = av.nextInt(), sum = 0;
        System.out.print("Enter elements: ");
        for (int i = 0; i < in; i++) {
            arr[i] = av.nextInt();
            sum += arr[i];
        }
        System.out.println("Sum = " + sum + " Average = " + ((float) sum / in));
        av.close();
    }
}


d. Write a Java program to find the maximum and minimum of n number of elements of an array.




import java.util.*;

class Code4d {
    public static void main(String A[]) {
        int[] arr = new int[100];
        Scanner av = new Scanner(System.in);
        System.out.print("Enter no of inputs: ");
        int in = av.nextInt();
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        System.out.print("Enter elements: ");
        for (int i = 0; i < in; i++) {
            arr[i] = av.nextInt();
            if (arr[i] > max) {
                max = arr[i];
            }
            if (arr[i] < min) {
                min = arr[i];
            }
        }
        System.out.println("Maximum = " + max + " Minimum = " + min);
        av.close();
    }
}


e. Write a program in Java to find sum of two 3x3 matrix.




import java.util.*;

class Code4e {
    public static void main(String A[]) {
        int[][] arr1 = new int[100][100];
        int[][] arr2 = new int[100][100];
        // int[][] arr3 = new int[100][100];
        int count;
        Scanner av = new Scanner(System.in);
        System.out.println("\nEnter a 3X3 matrix: ");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                arr1[i][j] = av.nextInt();
            }
        }
        System.out.println("\nEnter another 3X3 matrix values: ");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                arr2[i][j] = av.nextInt();
            }
        }
        System.out.println("\nTwo 3X3 matrix result is: ");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                count = 0;
                for (int k = 0; k < 3; k++) {
                    count += arr1[i][j] * arr2[j][i];
                }
                // arr1[i][j] = count;
                System.out.print(count + " ");
            }
            System.out.println();
        }
        av.close();
    }
}


5. a. Write a program in Java to search an element in an array


import java.util.*;

class A {
    int[] arr = new int[50];
    int no, srch;

    void input() {
        Scanner sc = new Scanner(System.in);
        System.out.print("No of elements:  ");
        no = sc.nextInt();
        System.out.println("Enter elements:  ");
        for (int i = 0; i < no; i++) {
            arr[i] = sc.nextInt();
        }
        System.out.print("Searching elements:  ");
        srch = sc.nextInt();
        sc.close();
    }

    int search() {
        for (int i = 0; i < no; i++) {
            if (arr[i] == srch) {
                return i;
            }
        }
        return -1;
    }

    void display(int pos) {
        if (pos != -1) {
            System.out.println("Found in " + (pos + 1) + " position");
        } else {
            System.out.println("Element not found");
        }
    }
}

public class Code5a {
    public static void main(String[] args) {
        A av = new A();
        av.input();
        int pos = av.search();
        av.display(pos);
    }
}


b. Write a program in Java to sort elements of a given array.


c. Write a program in Java to check if the string is palindrome or not


d. Write a program in Java to find a substring in a string.








































































Comments