Internet Technologies
1.0.0
1.0.0
  • Introduction
  • Contents
  • Practical List
  • HTML & CSS
    • Q1
    • Q2
    • Q3
    • Q4
    • Q5
  • JAVA Programs
    • Q6
    • Q7
    • Q8
    • Q9
    • Q10
    • Q11
    • Q12
  • JAVASCRIPT
    • Q13
    • Q14
    • Q15
  • JDBC
    • Q16
    • Q17
  • JSP
    • Side Note
    • Q18
    • Q19
    • Q20
    • Q21
    • Q22
    • Q23
  • End
Powered by GitBook
On this page
  • Code
  • Output
  1. JAVA Programs

Q8

Question 8

PreviousQ7NextQ9

Last updated 4 years ago

Write a java program that computes the area of a circle, rectangle and a Cylinder using function overloading.

Code Insights:

Created java_3.java, a new java class file and imported java.util.Scanner package to take user input, OverloadClass has 3 different implementation of same method (area) for 3 different types of possible inputs - Cylinder, Rectangle, and Circle.

In the main method Scanner class object takes the desired input bf dimensions of shape to calculate area through menu-driven approach using switch-case along with options to quit and handle invalid input from user.

Code

/**
 *Solution for Ans. 8
 * @author Jatin Kamboj 8647
 */
import java.util.Scanner;

class OverloadClass {
    void area(double x, double y)     // Cylinder
    {
        double t = (2 * 3.14 * x * y) + (2 * 3.14 * x * x);
        System.out.println("=> The area of the " +
                "Cylinder is " + t + " sq. units.\n");
    }

    void area(float x, float y) {     // Rectangle
        System.out.println("=> The area of the" +
                " Rectangle is " + x * y + " sq. units.\n");
    }

    void area(double x) {             // Circle
        double z = 3.14 * x * x;
        System.out.println("=> The area of the" +
                " Circle is " + z + " sq. units.\n");
    }
}

public class java_3 {
    public static void main(String[] args) {
        OverloadClass ob = new OverloadClass();
        Scanner sc = new Scanner(System.in);
        System.out.println("\n -- This program is" +
                " Menu-driven and calculates Area of" +
                " using Method Overloading -- \n");
        int choice = 10;
        while (choice != 0) {
            System.out.print("1.Area of Cylinder " +
                    "\n2.Area of Rectangle " +
                    "\n3.Area of Circle " +
                    "\nPress 0 to QUIT " +
                    "\n\nEnter Menu Option: ");
            choice = sc.nextInt();

            switch (choice) {
                case 1:
                    System.out.println("\nEnter dimensions" +
                            " for cylinder (Radius, " +
                            "Height): ");
                    double n1 = sc.nextDouble();
                    double n2 = sc.nextInt();
                    ob.area(n1, n2);
                    break;

                case 2:
                    System.out.println("\nEnter dimensions" +
                            " for Rectangle (Length, " +
                            "Breadth): ");
                    float n3 = sc.nextFloat();
                    float n4 = sc.nextFloat();
                    ob.area(n3, n4);
                    break;

                case 3:
                    System.out.println("\nEnter Radius " +
                            "for Circle: ");
                    double n5 = sc.nextDouble();
                    ob.area(n5);
                    break;

                case 0:
                    System.out.println("\nPROGRAM ENDED");
                    break;

                default:
                    System.out.println("\nINVALID INPUT" +
                            " TRY AGAIN!!!\n");
            }
        }
    }
}

Output

Try or Test The Corresponding Code Here

The above notebook runs on cloud, make sure to run the first cell of the notebook to set-up JDK and JRE on the cloud machine.

Browse Source Code
Google Colaboratory
Live Demo java_3.java
Logo
2KB
java_3.java
Download java_3.java
Image-1/4 output for java_3.java
Image-2/4 output for java_3.java
Image-3/4 output for java_3.java
Image-4/4 output for java_3.java