54.8k views
1 vote
Write convert() method to cast double to int

Complete the convert() method that casts the parameter from a double to an integer and returns the result.
Note that the main() method prints out the returned value of the convert() method.
Ex: If the double value is 19.9, then the output is:
19
Ex: If the double value is 3.1, then the output is:
3
public class LabProgram {
public static int convert(double d){
/* Type your code here *
}
public static void main(String[] args) {
System.out.println(convert(19.9));
System.out.println(convert(3.1));
}
}

User Tim Lytle
by
3.7k points

1 Answer

3 votes

Answer:

Step-by-step explanation:

The code provided is written in Java. The statement is provided right under the /*Type your code here*/. In Java, in order to case one primitive to another you simply need to type the primitive type that you are trying to cast too before the variable and within parenthesis. The output of the new code can be seen in the attached image below.

class LabProgram {

public static int convert(double d){

/* Type your code here */

return (int) d;

}

public static void main(String[] args) {

System.out.println(convert(19.9));

System.out.println(convert(3.1));

}

}

Write convert() method to cast double to int Complete the convert() method that casts-example-1
User Arvind Bhardwaj
by
3.4k points