0% found this document useful (0 votes)
20 views

Activity1 PATIÑO M M

act
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Activity1 PATIÑO M M

act
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Activity #1 – Basic Operators

NAME: Michael Angelo M. Patiño SECTION: CEIT-37-301A

1. Create a java program to compute the area and perimeter of a rectangle given the
length and width (in centimeters). Output the area in sq. cm.; sq. m.; sq. in.; and sq. ft.
For perimeter in cm.; m.; in.; and ft.
2. Use:
a. BufferedReader to get input from the user and System.out to output the result.
b. JOptionPane to get input from the user and to output the result in message box.
c. Scanner Scan to get input from the user and System.out to output the result.
3. Use different inputs on each item on the above methods of getting input from the user.
4. Screenshot/Print Screen the outputs of your programs in #2a, #2b, and #3.
5. Submit the source code (Java Program codes) and 3 sample outputs in PDF format.

Screen Layout:
Enter Length of Rectangle : __________ cm.
Enter Width of Rectangle : __________ cm.

Area = __________ sq. cm.


__________ sq. m.
__________ sq. in.
__________ sq. ft.

Perimeter = __________ cm.


__________ m.
__________ in.
__________ ft.
Filename: Activity1_Lastname_Firstname(Initial)_Middle Initial.pdf (e.g.

Activity1_Marcos_B_B.pdf)

A. BufferedReader to get input from the user and System.out to output the result.
Code:
import java.io.*;
public class
A_Activity1_Orge_A_A
{ public static void main(String[] args) throws
Exception
{
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader number = new BufferedReader(input);

System.out.print("Enter Length of Rectangle: ");


double length = Float.parseFloat(number.readLine());
System.out.print("Enter Width of Rectangle: "); double
width = Float.parseFloat(number.readLine());
double area = length * width;
double a_meter = area / 1000;
double a_inches = area / 6.452;
double a_feet = area / 929;

System.out.println("Area = " + area + " sq. cm");


System.out.println(" " + a_meter + " sq. m");
System.out.println(" " + a_inches + " sq. in");
System.out.println(" " + a_feet + " sq. ft");
double perimeter = 2 * (length +
width); double p_meter = area / 100;
double p_inches = area / 2.54; double
p_feet = area / 30.48;

System.out.println("Perimeter = " + perimeter + " cm");


System.out.println(" " + p_meter + " m");
System.out.println(" " + p_inches + " in");
System.out.println(" " + p_feet + " ft");
}
}

Output:

B. JOpitonPane to get input from the user and to output the result in message box.

Code:
import javax.swing.JOptionPane;
public class
B_Activity1_Orge_A_A
{ public static void main(String[]
args)
{
String l_rectangle = JOptionPane.showInputDialog("Enter Length of Rectangle:
");
float length = Float.parseFloat(l_rectangle);
String w_rectangle = JOptionPane.showInputDialog("Enter Width of Rectangle:
"); float width =
Float.parseFloat(w_rectangle);
double area = length * width;
double a_meter = area / 1000;
double a_inches = area / 6.452;
double a_feet = area / 929;

String msg1 = "Area = " + area + " sq. cm" + "\n" +


" " + a_meter + " sq. m" + "\n" +
" " + a_inches + " sq. in" + "\n" +
" " + a_feet + " sq. ft";
double perimeter = 2 * (length +
width); double p_meter = area / 100;
double p_inches = area / 2.54; double
p_feet = area / 30.48;
String msg2 = "Perimeter = " + perimeter + " cm" + "\n" +
" " + p_meter + " m" + "\n" +
" " + p_inches + " in" + "\n" +
" " + p_feet + " ft";

JOptionPane.showMessageDialog(null, msg1 + "\n" + msg2);


}
}

Output:

C. Scanner Scan to get input from the user and System.out to output the result.
Code:
import java.util.Scanner;
public class
C_Activity1_Orge_A_A
{ public static void main(String[]
args)
{
Scanner size = new Scanner(System.in);
System.out.print("Enter Length of Rectangle: ");
Float length = size.nextFloat();

System.out.print("Enter Width of Rectangle: ");


Float width = size.nextFloat();
double area = length * width;
double a_meter = area / 1000;
double a_inches = area / 6.452;
double a_feet = area / 929;

System.out.println("Area = " + area + " sq. cm");


System.out.println(" " + a_meter + " sq. m");
System.out.println(" " + a_inches + " sq. in");
System.out.println(" " + a_feet + " sq. ft");

double perimeter = 2 * (length + width);


double p_meter = area / 100;
double p_inches = area / 2.54;
double p_feet = area / 30.48;

System.out.println("Perimeter = " + perimeter + " cm");


System.out.println(" " + p_meter + " m");
System.out.println(" " + p_inches + " in");
System.out.println(" " + p_feet + " ft");
}
}

Output:

You might also like