Advertisement
Niloy007

The Converter

Oct 20th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. package test;
  2.  
  3. import java.util.Scanner;
  4.  
  5. class Shape{
  6.     private double len1, len2;
  7.     public String s;
  8.     Shape(String msg) {
  9.         System.out.println(msg + " is created");
  10.         this.s = msg;
  11.     }
  12.  
  13.     void area(double len1, double len2) {
  14.         this.len1 = len1;
  15.         this.len2 = len2;
  16.  
  17.         System.out.println("Area of " + s + " is : " + len1 * len2 + "\n\n");
  18.     }
  19.  
  20.     void area(double constant, double len1, double len2) {
  21.         this.len1 = len1;
  22.         this.len2 = len2;
  23.  
  24.         System.out.println("Area of " + s + " is : " + constant * (len1 * len2) + "\n\n");
  25.     }
  26.  
  27.     void area(double len1) {
  28.         this.len1 = len1;
  29.  
  30.         System.out.println("Area of circle is : " + 3.1416 * len1 * len1 + "\n\n");
  31.     }
  32.  
  33.     Scanner input = new Scanner(System.in);
  34.  
  35.     void resultOfTwo() {
  36.         if(s.equals("Rectangle")) {
  37.             System.out.println("Please Enter Length and Height");
  38.             double a = input.nextDouble();
  39.             double b = input.nextDouble();
  40.             area(a, b);
  41.  
  42.         }
  43.         else if(s.equals("Triangle")) {
  44.             System.out.println("Please Enter Base and Height");
  45.             double a = input.nextDouble();
  46.             double b = input.nextDouble();
  47.             area(.5, a, b);
  48.         }
  49.  
  50.     }
  51.  
  52.     void resultOfCircle() {
  53.         System.out.println("Please Enter The Radius");
  54.         double a = input.nextDouble();
  55.         area(a);
  56.     }
  57.  
  58. }
  59.  
  60.  
  61. public class Test {
  62.     public static void main(String[] args) {
  63.         Scanner input = new Scanner(System.in);
  64.  
  65.         while(true) {
  66.  
  67.             System.out.println("Please Enter your choice : ");
  68.             System.out.println("1. Calculate Rectangle's Area");
  69.             System.out.println("2. Calculate Triangle's Area");
  70.             System.out.println("3. Calculate Circle's Area");
  71.             System.out.println("4. Exit");
  72.  
  73.             int choice = input.nextInt();
  74.  
  75.             if (choice == 1) {
  76.  
  77.                 Shape ob = new Shape("Rectangle");
  78.                 ob.resultOfTwo();
  79.  
  80.             } else if (choice == 2) {
  81.  
  82.                 Shape ob = new Shape("Triangle");
  83.                 ob.resultOfTwo();
  84.  
  85.             } else if (choice == 3) {
  86.  
  87.                 Shape ob = new Shape("Circle");
  88.                 ob.resultOfCircle();
  89.  
  90.             } else if(choice == 4) {
  91.  
  92.                 break;
  93.  
  94.             } else {
  95.                 System.out.println("Your choice is wrong. Please try again!\n\n");
  96.             }
  97.  
  98.         }
  99.  
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement