This will give you experience with methods, arrays, and loops in Java. As always
ID: 3759995 • Letter: T
Question
This will give you experience with methods, arrays, and loops in Java. As always, include comments in your program wherever appropriate.
1. (7 points)public static double[] getUserInput()
(a) Prompts the user for how many numbers they will enter, we will cal this size.
(b) Check to make sure that the value of size is 1 or greater.
(c) With the value of size, create a single dimensional array of doubles with the number of elements being the value of size. For example, if size has the value of 4, then your array has 4 elements.
(d) With the created array of doubles, write a loop that reads values in form the keyboard and stores them into the array. If the value of size is 4, you will read in 4 values from the keyboard.(this code is in the slides)
(e) Finally, return the array using the keyword return.
2. (5 points)public static double arithmeticMean(double[] nums)
This method returns the arithmetic mean of the numbers provided by the input parameter, nums.
3. (5 points) public static double geometricMean(double[] nums)
This method returns the geometric mean of the numbers provided by the input parameter, nums.
4. (7 points)public static double[] minAndmax(double[] nums)
This method returns the smallest number and the largest number in the array provided by the input parameter. You don't need to return the location of the min and max.
5. (7 points)public static double[] scaleUp(double[] nums, double fac- tor)
This method multiplies every number provided by the first input parameter with a scaling factor (the second parameter). Make sure that the factor is a value greater than or equal to 1.0. If the factor is less than one, just return from the method.
6. (7 points) public static double[] scaleDown(double[] nums, double factor)
This method divides every number provided by the first input parameter with a scaling factor (the second parameter). Make sure that the factor is a value greater than or equal to 1.0. If the factor is less than one, just return from the method.
7. (7 points) Main method:
In this method,
1. obtain the numbers from the user using the method getUserInput() described above.
2. provide the user with 6 possible choices, each corresponding to one of the methods above and a exit option. Exclude getUserInput as an option.
Option 1 - arithmeticMean
Option 2 - geometricMean
Option 3 - minAndmax
Option 4 - scaleUp
Option 5 - scaleDown
Option 6 - Exit
HINT: A switch statement would be the easiest structure to use for this menu.
3. prompt the user to pick one. Check the user input for its validity, it has to be a number between 1 and 6.
(a) If option 4 (Scale up) or 5 (Scale down) is selected by the user then prompt the user for a value for factor.
4. perform the requested operation, and output the corresponding statis- tic(s). If Exit option was selected then terminate the program.
5. loop back to step 2. Note that this program should only use 1 array for the entire program. DO NOT CREATE a new user array for each method.
Explanation / Answer
import java.util.*;
import java.lang.*;
import java.util.InputMismatchException;
public class Project
{
public static double[] getUserInput()
{
Scanner sc = new Scanner(System.in);
List<Double> inputList = new ArrayList<Double>();
System.out.println("Please enter number/numbers. Please ente enter twice to confirm");
System.out.println(inputList);
double arr[] = new double[inputList.size()];
System.out.println(inputList.size());
return arr;
}
public static double arithmeticMean(double[] nums)
{
double mean = 0;
double sum = 0;
try
{
for (int i = 0; i < nums.length; i++)
{
sum = sum + nums[i];
}
mean = sum / nums.length;
}
catch (ArithmeticException ex)
{
System.out.println(ex);
}
return mean;
}
public static double geometricMean(double[] nums)
{
double gm = 1.0;
for (int i = 0; i < nums.length; i++)
{
gm *= nums[i];
}
gm = Math.pow(gm, 1.0 / (double) nums.length);
return gm;
}
public static double[] minAndmax(double[] nums)
{
double min = nums[0];
double max = nums[0];
for (int i = 1; i < nums.length; i++)
{
if (nums[i] < min)
min = nums[i];
else if (nums[i] > max)
max = nums[i];
else
{
System.out.println("You broke it");
}
}
double [] minAndmax = {min, max};
return minAndmax;
}
public static double[] scaleUp(double[] nums, int factor)
{
for (int i = 0; i < nums.length; i++)
nums[i] *= factor;
return nums;
}
public static double[] scaleDown(double[] nums, int factor)
{
for (int i = 0; i < nums.length; i++)
nums[i] /= factor;
return nums;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
double[] input = {1, 2.8, 5.3, 100, -5, -6.5};
System.out.println("Input stuff dood:");
boolean exit = false;
while (!exit)
{
System.out.println();
System.out.println("1 - Arithmetic mean, 2 - Geometric mean, 3 - Min and Max, 4 - Scale Up, 5 - Scale Down, 6 - Re-enter numbers, 7 - Quit");
System.out.print("Input -> "); int choice = sc.nextInt();
System.out.println();
switch (choice)
{
case 1:
{
System.out.println("Arithmetic mean");
System.out.println(arithmeticMean(input));
break;
}
case 2:
{
System.out.println("Geometric mean");
System.out.println(arithmeticMean(input));
break;
}
case 3:
{
System.out.println("Min and Max");
for (double i : minAndmax(input))
{
System.out.print(i + ", ");
}
break;
}
case 4:
{
System.out.println("Scale Up");
System.out.print("Please enter factor by which you want to scale -> ");
int factor = sc.nextInt();
for (double i : scaleUp(input, factor))
{
System.out.print(i + ", ");
}
break;
}
case 5:
{
System.out.println("Scale Down");
System.out.print("Please enter factor by which you want to scale -> ");
int factor = sc.nextInt();
for (double i : scaleDown(input, factor))
{
System.out.print(i + ", ");
}
break;
}
case 6:
{
break;
}
case 7:
{
exit = true;
break;
}
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.