Welcome to ICSE Java Answers. Ask any questions you have related to Java and we will answer them.
Java is a registered trademark of Oracle. This site is in no way related to or endorsed by Oracle.

write a program that ask user to enter shape whether circle ractangle or square after shape entered ask for furthr info of that shape to enter radius or length and width or lenghth after this find the area and perimete or circumferebce of that particular shape

0 votes
asked by Asfandy (5 points)

2 Answers

0 votes
This is a similar type of program.

 

import java.io.*;
class mohsinq
{
    public static void main()throws IOException
    {
        int x,y,z;
        BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("1.AREA 2.PERIMETER 3.EXIT");
        x=Integer.parseInt( obj.readLine());
       if(x==1)
        {
            System.out.println("ENTER LENGTH");
            y=Integer.parseInt( obj.readLine());
            System.out.println("ENTER BREDTH");
            z=Integer.parseInt( obj.readLine());
            System.out.println("AREA="+y*z);
        }
        else if(x==2)
        {
            System.out.println("ENTER LENGTH");
            y=Integer.parseInt( obj.readLine());
            System.out.println("ENTER BREDTH");
            z=Integer.parseInt( obj.readLine());
            System.out.println("PERIMETER="+(2*(y+z)));
        }
        else if(x==3)
        {
            System.exit(3);
        }
        else
        {
            System.out.println("INVALID ENTRY");
        }
    }
}
answered by Mohsin Shaikh (18 points)
0 votes

The Scanner class could be used very effectively. But, instead of an if-else if version, I feel that using a switch case would be more clear;

import java.util.Scanner;
class programshape
{
    public static void main(String args[])
    {
      int x,y,z;
      Scanner sc = new Scanner(System.in);
      System.out.println("1.AREA 2.PERIMETER 3.EXIT");
      x=sc.nextInt();
       switch(x)
        {
            case 1:
            System.out.println("ENTER LENGTH");
            y=sc.nextInt();
            System.out.println("ENTER BREDTH");
            z=sc.nextInt();
            System.out.println("AREA="+y*z);
             break;
            case 2:
            System.out.println("ENTER LENGTH");
            y=sc.nextInt();
            System.out.println("ENTER BREDTH");
            z=sc.nextInt();
            System.out.println("PERIMETER="+(2*(y+z)));
            break;
            case 3:
            break;
            default:
            System.out.println("INVALID ENTRY");
           break;
        }
    }
}

The code becomes clearer and compact and the Scanner class reduces the amount of syntax needed to take input.

answered by Soham Thakurata (333 points)
...