In: Computer Science
Java language.
1. write a conversion method that have 2 parameter 1. feet and 2. inches(as small as 1/8 inch)(make sure precondition, can't be smaller than 1/8 inch)
and give output as feet in decimal place.
2 Write a method to calculate the area of a rectangle that take in width and height (feet) (as small as 1/8 inch)(make sure precondition, can't be smaller than 1/8 inch) and round up to the closet feet.
Write test cases for both method in J unit (including precheck the precondition (as small as 1/8 inch))
JAVA CODE:
package ideone;
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
//method to convert feets and inches to feets
static double feet(double feets,double inch)
{
//if inches is less than 1/8 then
precondition is not satisfied
if(inch < 0.125){
System.out.println("Precondition not satisfied.");
return 0;
}
//returning answer by converting
inches into feet and adding it into the given feets
return feets +
inch*0.0833333;
}
//method to calculate area of rectangle
static double area(double height,double width)
{
//if either height or width of
rectangle is less than 1/8 inches(which is equal to 0.01041667
feets).
if(width < 0.01041667 || height
< 0.01041667){
System.out.println("Precondition not satisfied.");
return 0;
}
//returning area of rectangle i.e
height * width
return width*height;
}
public static void main (String[] args) throws
java.lang.Exception
{
//TEST CASE 1
double
test_feet1=5.00,test_inches1=0.345;
double
test_height1=.65,test_width1=0.345;
if((int)feet(test_feet1,test_inches1)!=0.00){
System.out.println("Test Case 1(FEET) :");
System.out.println("feet = 5.00 , inches = 0.345 ");
System.out.println("Feets in decimal places : "+
feet(test_feet1,test_inches1));
}
if(area(test_height1,test_width1)!=0.00){
System.out.println("Test Case 1(AREA) :");
System.out.println("height = .65 , width = 0.345 ");
System.out.println("Area in decimal places : "+
area(test_height1,test_width1));
}
System.out.println("");
//TEST CASE 2
double
test_feet2=8.535,test_inches2=0.098;
double
test_height2=.65,test_width2=0.01005;
System.out.println("Test Case
2(FEET) :");
System.out.println("feet = 8.535 ,
inches = 0.098 ");
if(feet(test_feet2,test_inches2)!=0.00){
System.out.println("Feets (in decimal places) : "+
feet(test_feet2,test_inches2));
}
System.out.println("Test Case
2(AREA) :");
System.out.println("height = .65 ,
width = 0.01005 ");
if(area(test_height2,test_width2)!=0.00){
System.out.println("Area (in decimal places) : "+
area(test_height2,test_width2));
}
}
}
CODE SCREENSHOT:
OUTPUT: