Write a Java code that will create a supportive class named, Conversion, which contains
three methods as specified below.
1) Method Name: Gallon2Liter(), Type: value-returning, Parameter: 1 parameter named g of double type, Operation: Return the result of g×3.78541178
2) Method Name: Inc2Cm(), Type: value-returning, Parameter: 1 parameter named i of doubletype, Operation: Return the result of i×2.54
3) Method Name: Pound2Kg(), Type: value-returning, Parameter: 1 parameter named p of double type, Operation: Return the result of p×0.45359237
In the “main” class, create an instance named “c1” of the “Conversion” class.
Use the “c1” instance to access all the three methods, “Gallon2Liter()”, “Inc2Cm()”, and “Pound2Kg()”, to convert the following values.
• 16.75 gallons
• 37.98 inches
• 140.27 pounds
public class Coversion{
public double Gallon2Liter(double g) {
//converting gallon to litres
return g*3.78541178;
}
public double Inc2Cm(double i) {
//converting inch to cm
return i*2.54;
}
public double Pound2Kg(double p) {
//converting pound to kg
return p*0.45359237;
}
}
public class Sample {
public static void main(String[] args) {
//creating Conversion object
Coversion c1=new Coversion();
//calling all method and printing result
System.out.println("16.75 gallons\t"+c1.Gallon2Liter(16.75)+" litres");
System.out.println("37.98 inches\t"+c1.Inc2Cm(37.98)+" cm");
System.out.println("140.27 pounds\t"+c1.Pound2Kg(140.27)+" kg");
}
}
Comments
Leave a comment