Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class AnArrayCalculator {
  5.  
  6. public static void main(String[] args) {
  7. int choice = getMenuOption();
  8. Scanner scan = new Scanner(System.in);
  9. switch (choice) {
  10. case 1:
  11. System.out.println("How many values are in the array?");
  12. int size1 = scan.nextInt();
  13. double[] operand1 = getOperand("Enter the values in the first array, separated by spaces:", size1);
  14. double[] operand2 = getOperand("Enter the values in the second array, separated by spaces:", size1);
  15. System.out.println("The result is: " + Arrays.toString(add(operand1, operand2)));
  16. case 2:
  17. System.out.println("How many values are in the array?");
  18. int size2 = scan.nextInt();
  19. double[] operand3 = getOperand("Enter the values in the first array, separated by spaces:", size2);
  20. double[] operand4 = getOperand("Enter the values in the second array, separated by spaces:", size2);
  21. System.out.println("The result is: " + Arrays.toString(subtract(operand3, operand4)));
  22. case 3:
  23. System.out.println("How many values are in the array?");
  24. int size3 = scan.nextInt();
  25. double[] operand5 = getOperand("Enter the values in the first array, separated by spaces:", size3);
  26. double[] operand6 = getOperand("Enter the values in the second array, separated by spaces:", size3);
  27. System.out.println("The result is: " + Arrays.toString(multiply(operand5, operand6)));
  28. default:
  29. System.exit(0);
  30. }
  31.  
  32. }
  33.  
  34. public static int getMenuOption() {
  35. System.out.println(
  36. "Menu \n1. Add \n2. Subtract \n3. Miltiply \n4. Divide \n5. Dot Product \n6. Generate Random Array \n7. Quit");
  37. System.out.println("What would you like to do?");
  38. Scanner scan = new Scanner(System.in);
  39. int number = scan.nextInt();
  40. return number;
  41. }
  42.  
  43. public static double[] getOperand(String prompt, int size) {
  44. System.out.println(prompt);
  45. Scanner scan = new Scanner(System.in);
  46. String values = scan.nextLine();
  47. Scanner input = new Scanner(values);
  48. double[] operand = new double[size];
  49.  
  50. while (input.hasNextDouble()) {
  51. for (int i = 0; i < size; i++) {
  52. operand[i] = input.nextDouble();
  53. }
  54. }
  55. return operand;
  56. }
  57. public static double[] add(double[] operand1, double[] operand2) {
  58. double[] sum = new double[operand1.length];
  59. for (int i = 0; i < operand1.length; i++) {
  60. sum[i] = operand1[i] + operand2[i];
  61. }
  62. return sum;
  63. }
  64. public static double[] subtract(double[] operand1, double[] operand2) {
  65. double[] difference = new double[operand1.length];
  66. for (int i = 0; i < operand1.length; i++) {
  67. difference[i] = operand1[i] - operand2[i];
  68. }
  69. return difference;
  70. }
  71. public static double[] multiply(double[] operand1, double[] operand2) {
  72. double[] product = new double[operand1.length];
  73. for (int i = 0; i < operand1.length; i++) {
  74. product[i] = operand1[i] * operand2[i];
  75. }
  76. return product;
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement