Help writing a program called Methods.java that will define a number of public s
ID: 3624984 • Letter: H
Question
Help writing a program called Methods.java that will define a number of public static methods that follow: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.
Explanation / Answer
public class Methods { public static double storedValue; public static double add(double x, double y) { return x+y; } public static double max(double x, double y) { if (x > y) { return x; } else { return y; } } public static String rev(String inString) { String returnString = ""; for (int i = inString.length()-1; i >= 0; i--) { returnString += inString.charAt(i); } return returnString; } public static double pi() { return 3.14159; } public static void store(double doubleToStore) { storedValue = doubleToStore; } public static double recall() { return storedValue; } public static boolean even(int anInt) { if (anInt % 2 == 0) { return true; } else { return false; } } public static int[] iota(int n) { int[] returnArr = new int[n]; for (int i = 1; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.