Write a method header on line two with the following specs:
Returns: a double
Name: negate
Parameters: a double called "num"
Then, starting on line 4, write the code that will return the opposite value of num (if it's positive, make it negative, otherwise make it positive)
Examples:
-
4 ==> -4
-
-10 ==> 10
-
1 ==> -1
Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
|
class Main {
public static double negate(double n)
{
return n-(n*2);
}
//test case below (dont change):
public static void main(String[] args){
System.out.println(negate(8)); //should be -8
System.out.println(negate(-2)); //should be 2
}
}
|
반응형
'Java_beginner(Repl.it) > Auto-Graded-Course(AP CS A)' 카테고리의 다른 글
042 - Method Header Practice 7 (0) | 2019.12.19 |
---|---|
041 - Method Header Practice 6 (0) | 2019.12.19 |
039 - Method Header Practice 4 (0) | 2019.12.19 |
038 - Method Header Practice 3 (0) | 2019.12.10 |
036 - Method Header Practice 1,2 (0) | 2019.12.10 |