Write a program that converts kilograms to pounds using a method. In the loop, call the method kgTolb(kg) within the loop body. The method header is defined below:
Method headers
public static double kgToLb(double kg)
/* returns the converted pounds value where = 2.2 * kg */
Create a second method that does the printing of the detail line and implement it in the algorithm above. Here is the method header:
public static void print (kg, lb)
/* prints a formatted detail line*/
Write a program that converts kilograms to pounds using a method. Implement as we did inn lab9. In the loop, call the method kgTolb(kg) within the loop body. The method header is defined below.
Method headers
public static double kgToLb(double kg)
returns the converted pounds value where lb = 2.2 * kg
Sample Execution
Enter the beginning value: 1
Conversion Table From KG(s) to LB(s)
Kilograms Pounds
---------------------
1 2.2
2 4.4
3 6.6
4 8.8
5 11.0
6 13.2
7 15.4
8 17.6
9 19.8
10 22.0
---------------------
End of Program
Algorithm
Open keyboard for input
Prompt user for beginning value and assign to bVal
Print conversion table headings
eVal = bVal 9
kg = bVal
while (kg <= eVal)
lb = kgToLb(kg)
print kg, lb to terminal //detail line
kg = kg 1
Print conversion table footer
Close input
Section 3:
Create a second method that does the printing of the detail line and implement in the algorithm above. Here is the method header:
public static void print(kg,lb)
prints a formatted detail line as in the sample output
Algorithm V2
Open keyboard for input
Prompt user for beginning value and assign to bVal
Print conversion table headings
eVal = bVal 9
kg = bVal
while (kg <= eVal)
lb = kgToLb(kg)
print(kg,lb) //detail line
kg = kg 1
Print conversion table footer
Close input
Section 4:
Create a third method that ensures that the beginning value is greater than zero. Here is the method header:
public static boolean isValidBVal(bVal)
returns true if bVal is greater than 0, otherwise false
Algorithm V3
Open keyboard for input
Prompt user for beginning value and assign to bVal
while (!isValidBVal(bVal)) //makes the method call
Print “invalid bVal, must be greater than 0.”
Prompt user for beginning value and assign to bVal
Print conversion table headings
eVal = bVal 9
kg = bVal
while (kg <= eVal)
lb = kgToLb(kg)
print(kg,lb) //detail line
kg = kg 1
Print conversion table footer
Close input
My full program
*/import java.util.Scanner;
public class KgTolb {
public static void main(String[] args) {
//Open keyboard for input
Scanner input = new Scanner(System.in);
//Prompt user for beginning value and assign to bVal
System.out.println("Enter beginning value ===> ");
double bVal = input.nextDouble();
//Print conversion table headings
System.out.println("\n Kilograms\t\t Pounds");
double eVal = bVal 9;
double kg = bVal;
System.out.println("----------------------");
}
public static double kgToLb(double kg) {
double lb = kgToLb(kg);
double kgToLbs = kg * 2.21;
for(double i = bVal; i <= bVal 10; i ) {
System.out.printf("%d\t%.2f\n", i , (i * LB_PER_KG));
kg = kg 1;
System.out.println("----------------------");
}
}
//Close input
CodePudding user response:
Is there anything missing in the method you created? No, on the contrary, there’s too much in it. You were asked to create two methods, kgToLb()
that converts one kg value and print()
that prints formatted output. Instead you kgToLb
method not only converts one kg value, it attempts to convert 11 successive kg values and also does formatted output, which was the job of print()
.
It seems to me that you are being very loyal to the overall purpose of your program and the expected sample execution. What you are missing is to follow your teacher’s design. Design meaning what goes into which method, including what is supposed to be in the main
method (“the program”).
From your exercise:
public static double kgToLb(double kg)
/* returns the converted pounds value where = 2.2 * kg */
So the kgToLb
method should convert kgs to pounds and return the converted value. Nothing else. It’s a very simple task for a method. What appears to me to be the difficult part for you is to write a method that is correspondingly simple and does not include anything else. It’s really training of abstraction and design more than it’s programming training.