Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- import java.util.Scanner;
- class Shape{
- private double len1, len2;
- public String s;
- Shape(String msg) {
- System.out.println(msg + " is created");
- this.s = msg;
- }
- void area(double len1, double len2) {
- this.len1 = len1;
- this.len2 = len2;
- System.out.println("Area of " + s + " is : " + len1 * len2 + "\n\n");
- }
- void area(double constant, double len1, double len2) {
- this.len1 = len1;
- this.len2 = len2;
- System.out.println("Area of " + s + " is : " + constant * (len1 * len2) + "\n\n");
- }
- void area(double len1) {
- this.len1 = len1;
- System.out.println("Area of circle is : " + 3.1416 * len1 * len1 + "\n\n");
- }
- Scanner input = new Scanner(System.in);
- void resultOfTwo() {
- if(s.equals("Rectangle")) {
- System.out.println("Please Enter Length and Height");
- double a = input.nextDouble();
- double b = input.nextDouble();
- area(a, b);
- }
- else if(s.equals("Triangle")) {
- System.out.println("Please Enter Base and Height");
- double a = input.nextDouble();
- double b = input.nextDouble();
- area(.5, a, b);
- }
- }
- void resultOfCircle() {
- System.out.println("Please Enter The Radius");
- double a = input.nextDouble();
- area(a);
- }
- }
- public class Test {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- while(true) {
- System.out.println("Please Enter your choice : ");
- System.out.println("1. Calculate Rectangle's Area");
- System.out.println("2. Calculate Triangle's Area");
- System.out.println("3. Calculate Circle's Area");
- System.out.println("4. Exit");
- int choice = input.nextInt();
- if (choice == 1) {
- Shape ob = new Shape("Rectangle");
- ob.resultOfTwo();
- } else if (choice == 2) {
- Shape ob = new Shape("Triangle");
- ob.resultOfTwo();
- } else if (choice == 3) {
- Shape ob = new Shape("Circle");
- ob.resultOfCircle();
- } else if(choice == 4) {
- break;
- } else {
- System.out.println("Your choice is wrong. Please try again!\n\n");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement