Advertisement
Shailrshah

Areas of a Square and a Rectangle via Method Overloading

Aug 24th, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. import java.io.*;
  2. class CalArea{
  3.     public static float area(float side){
  4.         return side*side;
  5.     }
  6.     public static float area (float length, float breadth){
  7.         return length * breadth;
  8.     }
  9. }
  10. class Area{
  11.     private static String getInput(String prompt){
  12.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  13.         System.out.print(prompt);
  14.         System.out.flush();
  15.         try{
  16.             return stdin.readLine();
  17.         }catch(Exception e){
  18.             return "Error: " + e.getMessage();
  19.         }
  20.     }
  21.     static public void main (String[] args){
  22.         CalArea o = new CalArea();
  23.         String input = getInput("Enter side's value: ");
  24.         float side = Float.parseFloat(input);
  25.         System.out.println("The Sqare's area is "+o.area(side)+" units square.");
  26.         input = getInput("Enter length's value: ");
  27.         float length = Float.parseFloat(input);
  28.         input = getInput("Enter breadth's value: ");
  29.         float breadth = Float.parseFloat(input);
  30.         System.out.println("The Rectangle's area is "+o.area(length, breadth)+" units square.");
  31.     }
  32. }
  33.  
  34. // Output:-
  35. // C:\Java>java Area
  36. // Enter side's value: 56.43
  37. // The Sqare's area is 3184.345 units square.
  38. // Enter length's value: 12.78
  39. // Enter breadth's value: 80.11
  40. // The Rectangle's area is 1023.8058 units square.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement