java question 1 Write a static method to return in a return

java question:

1. Write a static method to return (in a return statement) a double (parameter) rounded to the nearest 100th. Use the Math class\' round method. DON\'T PRINT IN THIS METHOD. Put this method in a class called MyMath . Hints: multiply the double parameter by 100, round that, divide the rounded result by 100.

Solution

MyMath.java

import java.util.Scanner;


public class MyMath {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println(\"Enter Number: \");
       double n = scan.nextDouble();
       n = round(n);
       System.out.println(\"Rounded: \"+n);
   }
   public static double round(double n){
       n = Math.round(n/100);
       n = n * 100 ;
       return n;
   }

}

Output:

Enter Number:
151
Rounded: 200.0

Enter Number:
149
Rounded: 100.0

java question: 1. Write a static method to return (in a return statement) a double (parameter) rounded to the nearest 100th. Use the Math class\' round method.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site