Return01java The call of a void method is performed by using the method name by

Return01java the call of a void method is performed

This preview shows page 4 - 8 out of 40 pages.

Return01.java The call of a void method is performed by using the method name, by itself, as a program statement. Observe in line 11 that add1(n1,n2); is the entire program statement. In line 12 a call is made to method add2 . A return method must be called in a program statement that uses the returned value. In this case line 12 prints the value returned by method add2 . 5 public class Return01 6 { 7 public static void main(String[ ] args) 8 { 9 int n1 = 100; 10 int n2 = 200; 11 add1(n1,n2); 12 System.out.println(add2(n1,n2)); 13 } 14 15 public static void add1(int a, int b) 16 { 17 int result = a + b; 18 System.out.println(result); 19 } 20 21 public static int add2(int a, int b) 22 { 23 int result = a + b; 24 return result; 25 } 26 } ----jGRASP exec: java Return01 300 300
Image of page 4
UNIT 2b Using Objects Continued Page 95 The previous program example showed that a return method was supposed to be used in a program statement that used its value. Is that required? Will there be a syntax error if that is not done? Much is learned about computer programming by making intentional mistakes. It is beneficial to make various errors and observe the consequences. Program Return02 is such an example. In this program the return method add is called improperly. Return02.java Observe in line 11 that add(n1,n2); is the entire program statement. That is fine for a void method , but not for a return method . Look at the first output box below. It shows that Java is not excited. The program does compile. The second output shows an output of Inside add . This proves that the method is called. However nothing is done with the int parameters. 5 public class Return02 6 { 7 public static void main(String[ ] args) 8 { 9 int n1 = 100; 10 int n2 = 200; 11 add(n1,n2); 12 } 13 14 public static int add(int a, int b) 15 { 16 System.out.println("Inside add"); 17 int result = a + b; 18 return result; 19 } 20 } ----jGRASP exec: javac Return02.java ----jGRASP exec: java Return02 Inside add
Image of page 5
Page 96 Java Programming for AP ® Computer Science A 2020-2021 Edition Return03.java This program seems correct at first glance. There is a program statement in the main method that displays the return value and everything necessary seems to be in the add method. The problem is that method add declares in line 14 that a boolean value will be returned. Inside the body of the method you see result returned and that is an int value. There will be no output execution and Java objects with a syntax error message. 5 public class Return03 6 { 7 public static void main(String[ ] args) 8 { 9 int n1 = 100; 10 int n2 = 200; 11 System.out.println(add(n1,n2)); 12 } 13 14 public static boolean add(int a, int b) 15 { 16 int result = a + b; 17 return result; 18 } 19 } ----jGRASP exec: javac Return03.java Return03.java:17: error: incompatible types: int cannot be converted to boolean return result; ^ 1 error ----jGRASP wedge2: exit code for process is 1. The data type, in the heading of the return method, must match the data type of the actual return value.
Image of page 6