I have to write a program that will define a number of public static methods:
add
takes two doubles, returns their sum
max
takes two doubles, returns the larger value (3.0 is larger than -10.0)
rev
takes a String, returns the reversed String ("Hi, Bob" reversed is "boB ,iH")
pi
takes nothing, returns exactly 3.14159
store
takes a double, remembers it, returns nothing
recall
takes nothing, returns the last double from store
even
takes an int, returns true or false if it’s even or not
iota
takes an int (let’s call it n), returns an array of n ints containing 1001, 2002, 3003, ..., n*1001.
Assume that n > 0.
sum
takes an array of doubles, returns their sum
mean
takes an array of doubles, returns their mean (total divided by how many). It must call the sum method to calculate the total. Assume that the array is non-empty.
I start with this. It seems really easy. But I'm not sure where to go. Can someone help me or do the first few methods so I know how to start? Thanks
package Methods; import java.util.Scanner; public class Methods { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int[] a; boolean b; int n; double d; String s; for (;;) { System.out.println("Enter command and arguments"); String op = scan.next(); if (op.equals("quit")) break; else if (op.equals("add")) { d = add(scan.nextDouble(), scan.nextDouble()); System.out.println(d); } else if (op.equals("max")) { d = max(scan.nextDouble(), scan.nextDouble()); System.out.println(d); } else if (op.equals("rev")) { s = rev(scan.nextLine()); System.out.println(s); } else if (op.equals("pi")) { d = pi(); System.out.println(d); } else if (op.equals("store")) { store(scan.nextDouble()); } else if (op.equals("recall")) { d = recall(); System.out.println(d); } else if (op.equals("even")) { b = even(scan.nextInt()); System.out.println(b); } else if (op.equals("iota")) { a = iota(scan.nextInt()); for (int i=0; i<a.length; i++) System.out.print(a[i] + " "); System.out.println(); } else if (op.equals("sum")) { scan.skip(" "); s = scan.nextLine(); int count = s.split(" ").length; Scanner linescan = new Scanner(s); double[] data = new double[count]; for (int i=0; i<data.length; i++) data[i] = linescan.nextDouble(); d = sum(data); System.out.println(d); } else if (op.equals("mean")) { scan.skip(" "); s = scan.nextLine(); int count = s.split(" ").length; Scanner linescan = new Scanner(s); double[] data = new double[count]; for (int i=0; i<data.length; i++) data[i] = linescan.nextDouble(); d = mean(data); System.out.println(d); } else { System.out.println("Invalid command: " + op); } } } }