Please create a class to implement a method getWeight which is in a interface. P
ID: 3632888 • Letter: P
Question
Please create a class to implement a method getWeight which is in a interface. Please input your weight(pounds), and output your weight(kilograms).(kg = lb / 2.20462)
Output:
Please input your weight(pounds):
160
You input: 160.0 pounds, and output: 72.57486550970236 kilograms
I will test your codes as below:
-------------------------------------------------
Please input your weight(pounds):
160
You input: 160.0 pounds, and output: 72.57486550970236 kilograms
Below, is what I have done so far. My teacher gave some hints in comments but I'm stuck. You don't have to do the exact code lay out, but similar so I can understand it since I'm a beginner at Java.
interface TransWeight2 {
//a method ABC;
}
import java.util.Scanner;
public class InsertWeight2 implements TransWeight2 {
public static void main(String[] args) {
Scanner scanner= new Scanner(System.in);
System.out.println("Please input your weight(pounds): ");
//create a object…and call a method
System.out.println("You input: " + lb + " pounds, and output: " + kg +" kilograms");
}
// override method ABC
I rate A+ for the best answer! Thanks!
Explanation / Answer
import java.util.Scanner; interface TransWeight2 { double getWeight(double lbs);//Only put name of method in the interface } public class InsertWeight2 implements TransWeight2 { public static void main(String[] args) { Scanner scanner= new Scanner(System.in); System.out.println("Please input your weight(pounds): "); //create a object…and call a method double lb=scanner.nextDouble(); double kg=new InsertWeight2().getWeight(lb);//converts to kg System.out.println("You input: " + lb + " pounds, and output: " + kg +" kilograms"); } //this is where you put the code inside the method public double getWeight(double lbs) { return lbs/ 2.20462; } } This is how I would program given your format. However, it is usually better to have your main method in a separate class instead of in the same class as your object, InsertWeight2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.