The calculation is done as an integer calculation.
If you replace 2 by 2.0 you will get 50.5. I recommend adding a comment to that line to explain this to future readers. Alternatively you can explicitly cast to a double:
((double) (number1 + number2)) / 2
Answer from CompuChip on Stack OverflowVideos
The calculation is done as an integer calculation.
If you replace 2 by 2.0 you will get 50.5. I recommend adding a comment to that line to explain this to future readers. Alternatively you can explicitly cast to a double:
((double) (number1 + number2)) / 2
Just replace this function
import java.util.Scanner;
public class test {
private static int number1 = 100;
private static int number2 = 1;
public static double avgAge() {
return (number1 + number2) / 2.0;
}
public static void main(String[] args) {
System.out.println("Average number: " + test.avgAge()); //Average number: 50.5
}
}
Double parameter can be null when double can't.
First off you need to understand the difference between the two types.
double is a primitive type whereas Double is an Object.
The code below shows an overloaded method, which I assume is similar to your lab code.
void doStuff(Double d){ System.out.println("Object call"); }
void doStuff(double d){ System.out.println("Primitive call"); }
There are several ways you can call these methods:
doStuff(100);
doStuff(200d);
doStuff(new Double(100));
These calls will result in:
"Primitive call"
"Primitive call"
"Object call"