In: Computer Science
#include <iostream>
using namespace std;
void showDouble(int); //Function prototype
int main()
{
int num;
for (num = 0; num < 10; num++)
showDouble(num);
return 0;
}
// Definition of function showDouble
void showDouble(int value)
{
cout << value << â\tâ << (value * 2) << endl;
}
Please do the following Program and hand in the code and sample runs.
Write function main which calls these four functions. In your function definitions make sure that the length and width are positive numbers.
Hand in 2 runs showing valid and invalid data.
/*If you any query do comment in the comment section else like the solution*/
import java.util.Scanner;
public class Main {
public static Scanner keyboard = new Scanner(System.in);
public static void main(String[]args) {
double length;
double width;
double area;
length = getLength();
width = getWidth();
area = calculateArea(length, width);
printResults(length, width, area);
System.out.println();
}
private static void printResults(double length, double width, double area) {
System.out.printf("Dimensions: %.1f X %.1f\n", length, width);
System.out.printf("Area: %.2f\n", area);
}
private static double calculatePerimeter(double length, double width) {
return (2*(length + width));
}
private static double calculateArea(double length, double width) {
return (length*width);
}
private static double getWidth() {
double width;
do {
System.out.print("Enter the width: ");
width = keyboard.nextDouble();
if(width <= -1) {
System.out.println("Width must be positive! Try Again");
}
}while(width <= -1);
return width;
}
private static double getLength() {
double length;
do {
System.out.print("Enter the width: ");
length = keyboard.nextDouble();
if(length <= -1) {
System.out.println("Width must be positive! Try Again");
}
}while(length <= -1);
return length;
}
}