Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Interview-Questions Please find me a solution that

Have Programming Question? Ask it here!
 
 
 


ali
Please find me a solution that
1 Answer(s)      2 years and 3 months ago
Posted in : Java Interview Questions


Write a java program that calculates the area of rectangular, square,

triangular, and circle.

Your program must have the following:

  1. Two classes named "Area" and "AreaTest".

  2. Class "Area" has the following:

a. Void Square Method with one parameter which is length of

side.

b. Void Rectangle Method has two parameters which are

width and height.

c. Void Triangle Method has two parameters which are base

and vertical height.

d. Void Circle Method has one parameter which is radius.

You must use Math.PI and Math.pow() function for your

calculation.

e. One Void Display Method to print out the calculation result

for all methods (Square, Rectangle, Triangle, and Circle). For

example, if I want to print the area of rectangle, you have to

pass the result as a parameter to Display Method to print it

out. There is one System.out.printf() in this method and

must be as following format:

System.out.printf("The Area of %s is %s" ,str,result);

str = any of these words: Square, Rectangle, Triangle ,or

Circle.

Result = the result of the method calculation.

f. Do not use Instance Variable for your program.

  1. Class "AreaTest" has the following:

a. Switch statement that maintains accessing each of the

method in (a),(b), (c), and (d) stated above in class "Area"

b. 'For statement loop' to keep the user running your program

until he enters number '5' to exit the program.


Advertisement
View Answers

May 16, 2012 at 4:14 PM


The given code calculates the area of square, rectangle, triangle and circle. It allow the user to enter their choice. It then takes the user request and display the result accordingly.

import java.util.*;

class Area 
{
public static double square(double side){
    double area=side*side;
    return area;
}
public static double rectangle(double width,double height){
    double area=width*height;
    return area;
}
public static double triangle(double base,double height){
    double area=(base*height)/2;
    return area;
}
public static double circle(double radius){
    double area=(Math.PI)*(Math.pow(radius,2));
    return area;
}
public static void display(String str,double result){
    System.out.println("The Area of "+str+" is: "+result);
}
}

class AreaTest{
    public static void main(String[] args) 
    {
        boolean exit=false;
        String str=" ";
        Scanner input=new Scanner(System.in);
        do{
        System.out.println("1 Square");
        System.out.println("2 Rectangle");
        System.out.println("3 Triangle");
        System.out.println("4 Circle");
        System.out.println("5 Exit");

        System.out.print("Enter your choice: ");
        int choice=input.nextInt();
        switch(choice){
            case 1: 
              System.out.print("Enter side: ");
              double s=input.nextDouble();
              str="Square";
              Area.display(str,Area.square(s));
              break;

            case 2: 
              System.out.print("Enter width: ");
              double w=input.nextDouble();
              System.out.print("Enter height: ");
              double h=input.nextDouble();
              str="Rectangle";
              Area.display(str,Area.rectangle(w,h));
              break;

            case 3:
              System.out.print("Enter base: ");
              double b=input.nextDouble();
              System.out.print("Enter height: ");
              double ht=input.nextDouble();
              str="Triangle";
              Area.display(str,Area.triangle(b,ht));
              break;

            case 4:
              System.out.print("Enter radius: ");
              double r=input.nextDouble();
              str="Circle";
              Area.display(str,Area.circle(r));
              break;

            case 5:
              exit=true;
              System.exit(0);
           }
        }
            while(!exit);

    }
}


Related Tutorials/Questions & Answers:
Please find me a solution that
Please find me a solution that   Write a java program that calculates the area of rectangular, square, triangular, and circle. Your program must have the following: Two classes named "Area" and "AreaTest". Class "Area
error please send me the solution
error please send me the solution  HTTP Status 500 - type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception
Advertisements
error please send me the solution
error please send me the solution  HTTP Status 500 - type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception
Please tell me where I can find tutorials on spring hibernate?
Please tell me where I can find tutorials on spring hibernate?  Hello, Please tell me where I can find tutorials on spring hibernate? Thanks..   Dear Friend, You can find some good tutorials on Spring Hibernate here
please please i wanna insertion sort program find calendar to array value (1000,5000,10000), please help me
Insertion sort program find calendar to array value 1000,5000,10000  program find calendar for insertion sort in jfram to array 1000
Please tel me .In my System Oracle9i and J2sdk1.6 are Installed.Why this problem occures and what is the solution?
Please tel me .In my System Oracle9i and J2sdk1.6 are Installed.Why this problem occures and what is the solution?  Exception in thread "main" java.lang.UnsupportedClassVersionError: select (Unsupporte d major.minor version 50.0
please give me solution how to display next page after 20 records ? - JSP-Servlet
please give me solution how to display next page after 20 records ?  Java Servlet Paging control example  here i have attached one example code. function validate() { for(j=0;j<30;j
Can find a solution or code for this - XML
Can find a solution or code for this   Develop a java based tool which can be used to build a GUI structure based on the configurations given in the tool. Input XML Folders/Files E.g:Folder for Main Panel Files for required
urgent...pleAse help me.....please!
urgent...pleAse help me.....please!  please help me urgent! how can i do dictionary with the use of array code in java, where i will type the word then the corresponding meaning for that word will appear...thanks
please tell me
(); ^ 1 error tell me the resolution please....   import...please tell me  import java.lang.Thread; class Current{ public static... error as Current.java:7: cannot find symbol symbol : method CurrentThread
please help me.
please help me.  Please send me a code of template in opencms and its procedure.so i can implement the code. Thanks trinath
please help me.
please help me.  How to read a properties file in java with a suitable example. Please send me. Thanks Trinath   Please visit the following link: Java read properties file
please help me.
please help me.  Please send me the validation of this below link. the link is http://www.roseindia.net/answers/viewqa/JSP-Servlet/9584-JSP-Servlet-Search-and-Edit.html Thanks Trinath
please tell me
)); ^ 1 error please tell me the resolution of this   Hi...please tell me  import java.io.*; class SString{ public static void...: cannot find symbol symbol : class Bufferedreader location: class SString
please tell me
(10.6,20.0); ^ 1 error, please tell me the resolution for this...   ...please tell me  class Sample{ private double num1,num2; void sample...\oops>Javac Methods.java Methods.java:14: cannot find symbol symbol
please tell me
please tell me  class Producer extends Thread { StringBuffer sb... got error as Inner class connot have static declarations pls tell me the solution for this   class Producer extends Thread { StringBuffer sb
please tell me
please tell me  i have created one table,when i close and again login, table name will be there, but its content not displayed, showing as no rows selected, please tell me the reason
please help me.
please help me.  How to move the edits.jsp in below link? http://www.roseindia.net/answers/viewqa/JSP-Servlet/9584-JSP-Servlet-Search-and-Edit.html
help me please
help me please   Hello I want helping for this question , Please Write a program that reads some friendsââ?¬â?¢ names, stores them in an array, and then prints out on the screen all friends who start by a particular letter
please help me.
please help me.  I have a jsp page under that i add a list box under i get the countries through my database. so how can i do
Please give me the answer.
"int a=08 or 09" its giving compile time error why   "int a=08 or 09" its giving compile time error why ? can any one give me the answer of this please
please guide me - EJB
please guide me  Hi I am Pradeep singh ,done SCJP 5.0 and SCWCD 5.0 .Now i want to learn further .So please tell me whether should i learn EJB 3.0... advice me .At present i am jobless
please help me to this problem..
please help me to this problem..  i wrote a program like keyboard... inside the JTextField) but i want to make this program to let me write where i clicked (i.e allows me to write any thing at the field where i clicked
Please help me.
Please help me.  Hi i am trinath in below there is a url.In that url there is a code of edit a jsp page.I understand that code but only one thing i not get it i.e; What is the work of "id".and what is the data type of id? http
please help me.
please help me.  I have three table in mysql,and i hava create a excel sheet and add this sheet.but my question is in every sheet i can display one one table result.how can i do
please help me...
please help me...  write an application that print number in the following order using a FOR-Loop 1 2 3 4 5 2 4 6 8 10 3 6 9 1215 4 8 121620 5 10152025
please help me
before. This name list should get from the database. Please help me. By the way, I'm...please help me  Dear sir, I have a problem. How to write JSP coding, if a user select a value from drop down list for example department, the another
please tell me
, but its compiled and run successfully, please tell me the reason...please tell me  class Person{ int age; String name; void Person1(String g,int a){ name=g; age=a; } Person(String s,int i){ name=s; age=i; } void
please tell me
: '}' expected ^ 2 errors, i got this error....... please tell me the resolution...please tell me  class Person{ string name; int age; void talk(){ System.out.println("my name is"+name); System.out.println
Please help me urgent...........
Please help me urgent...........  For what kind of a problem would use a Tree Set and not a Tree Map For what kind of a problem would use a Tree Map and not a Tree Set   Hello Friend, If you want to display the list
Please give me coding for this..
Please give me coding for this..  Write an application that inputs one number consisting of five digits from the user.separates the number ibto its individual digits and prints the digits separated from one another by three
Please help me
Please help me  Hi Sir, please send me the code for the following progrems... 1) all sets are integer type: input: set1={10,20,30,40} set2={15,25,35} output: union={10,15,20,25,30,35,40} 2) input: "Hi what
please tell me
please tell me  why we are using http protocol in servlets
please help me?
please help me?  Define a class named Circle with the following properties: List item An integer data field named radius with protected access modifier, and a String data field named colour with private access modifier. Both
Please help me??
Please help me??  Question_1: Using one-dimension array of primitive type elements. Objective: The purpose of this lab exercise is to practice how to declare, create and manipulate a one-dimension array of primitive type
Please help me out
Please help me out    Description* You are hired to develop a laptop inventory information system for Sheridan College in order to keep track of the information about the laptops lent to the students. Users can add or remove data
please tell me
; ^ please tell me the resolution   class Reserve...please tell me  class Reserve extends Thread{ int available=1; int wanted; Reserve(int i){ wanted = i; } public static void
please help me here
please help me here  please show me how can this show the right output that i need please continue doing this program using only if else and do while please please" here is the problem Automatic Teller Machine [B] Balance [D
please help me here
please help me here  please show me how can this show the right output that i need please continue doing this program using only if else and do while please please" here is the problem Automatic Teller Machine [B] Balance [D
please tell me
please tell me  what are the topics in core and advaced java....   Hi Friend, Please visit the following links: http://www.roseindia.net/ http://www.roseindia.net/java/ Thanks   Hi Friend, Please visit
please tell me
please tell me  why we use public static main(String ar){} in java instead of main
please tell me
please tell me  which cmd we use to clear the screen in sql prompt
please tell me
please tell me  select * from emp order by 5 desc; in the above what is the meaning of 5, and what its functionality
Please help me
Please help me  program for when a user enter his card number, it has to create default security pin in the database
please help me
please help me  interface Test1 { String toString(); } public class Test { public static void main(String[] args) { System.out.println(new Test1() { public String toString() { return "test
please tell me
please tell me  Actually i am working on a Project tiitle is JavaMail System, 1)How to configure java mail API and a Demo Program for Sending mail and Receiving using JSP and Servlet
please answer me
please answer me  iam using html in select tag.in this one option i selected and that option data will be display in table.And use servlet with html and how to retrieve the data from data base
help me please
help me please  how can i do dictionary with the use of array code in java, where i will type the word then the corresponding meaning for that word will appear...thanks
please help me
please help me  how to use two browse buttons, one browse button for displaying the first image and second for the second image along with its file path and also to display text file along with its path using layouts or panels
please help me
please help me  how to use two browse buttons, one browse button for displaying the first image and second for the second image along with its file path and also to display text file along with its path using layouts or panels
Advertisements
 

 

 

DMCA.com