1: How to Answer These Questions

Write methods to solve all of the questions. Here are two example questions and their solutions. Notice that the first method prints a value, the second method returns a value.

1.1 Hello <Name>

Write a method that accepts a name as a parameter and prints out “Hello ” <name>

1.1.1 Example

1:  hello("Kim")
2:   *** Output ***
3:  Hello Kim

1.2 Average of two numbers

Write a method that accepts two numbers and returns the average of the two numbers.

1.2.1 Example

1:  System.out.println(average(3,4));
2:   *** Output ***
3:  3.5

1.3 Solutions

 1:  public class NinetyNine {
 2:
 3:      NinetyNine()
 4:      {
 5:          hello("Kim");
 6:          System.out.println(average(3,4));
 7:      }
 8:
 9:      void hello(String s)
10:      {
11:          System.out.println("Hello " + s);
12:      }
13:
14:      double average(double x, double y)
15:      {
16:          return (x+y)/2;
17:      }
18:
19:      public static void main(String[] args)
20:      {
21:          new NinetyNine();
22:      }
23:  }
24:

Leave a Reply

Your email address will not be published. Required fields are marked *