English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Write a program that reads the height and base of a rectangle, and computes its perimeter.
(perimeter=2 * height + 2 * base)

2007-06-26 15:00:25 · 1 answers · asked by ZG786 1 in Computers & Internet Programming & Design

1 answers

This one doesn't have a lot of error handling, and the code to read a double could probably stand being refactored into a separate method, but it works as required.

==============================
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Perimeter {

public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(
System.in ) );

System.out.print("Length: ");
double length = Double.parseDouble( br.readLine().trim() );

System.out.print("Width: ");
double width = Double.parseDouble( br.readLine().trim() );

System.out.println("Perimeter = " + (2.0*length + 2.0*width));
} catch (IOException ioe) {
ioe.printStackTrace();
System.exit(1);
}
}
}

========================
Execution and output:

Length: 10.2
Width: 10.5
Perimeter = 41.4

2007-06-26 15:11:07 · answer #1 · answered by McFate 7 · 0 0

fedest.com, questions and answers