Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- public class AnArrayCalculator {
- public static void main(String[] args) {
- int choice = getMenuOption();
- Scanner scan = new Scanner(System.in);
- switch (choice) {
- case 1:
- System.out.println("How many values are in the array?");
- int size1 = scan.nextInt();
- double[] operand1 = getOperand("Enter the values in the first array, separated by spaces:", size1);
- double[] operand2 = getOperand("Enter the values in the second array, separated by spaces:", size1);
- System.out.println("The result is: " + Arrays.toString(add(operand1, operand2)));
- case 2:
- System.out.println("How many values are in the array?");
- int size2 = scan.nextInt();
- double[] operand3 = getOperand("Enter the values in the first array, separated by spaces:", size2);
- double[] operand4 = getOperand("Enter the values in the second array, separated by spaces:", size2);
- System.out.println("The result is: " + Arrays.toString(subtract(operand3, operand4)));
- case 3:
- System.out.println("How many values are in the array?");
- int size3 = scan.nextInt();
- double[] operand5 = getOperand("Enter the values in the first array, separated by spaces:", size3);
- double[] operand6 = getOperand("Enter the values in the second array, separated by spaces:", size3);
- System.out.println("The result is: " + Arrays.toString(multiply(operand5, operand6)));
- default:
- System.exit(0);
- }
- }
- public static int getMenuOption() {
- System.out.println(
- "Menu \n1. Add \n2. Subtract \n3. Miltiply \n4. Divide \n5. Dot Product \n6. Generate Random Array \n7. Quit");
- System.out.println("What would you like to do?");
- Scanner scan = new Scanner(System.in);
- int number = scan.nextInt();
- return number;
- }
- public static double[] getOperand(String prompt, int size) {
- System.out.println(prompt);
- Scanner scan = new Scanner(System.in);
- String values = scan.nextLine();
- Scanner input = new Scanner(values);
- double[] operand = new double[size];
- while (input.hasNextDouble()) {
- for (int i = 0; i < size; i++) {
- operand[i] = input.nextDouble();
- }
- }
- return operand;
- }
- public static double[] add(double[] operand1, double[] operand2) {
- double[] sum = new double[operand1.length];
- for (int i = 0; i < operand1.length; i++) {
- sum[i] = operand1[i] + operand2[i];
- }
- return sum;
- }
- public static double[] subtract(double[] operand1, double[] operand2) {
- double[] difference = new double[operand1.length];
- for (int i = 0; i < operand1.length; i++) {
- difference[i] = operand1[i] - operand2[i];
- }
- return difference;
- }
- public static double[] multiply(double[] operand1, double[] operand2) {
- double[] product = new double[operand1.length];
- for (int i = 0; i < operand1.length; i++) {
- product[i] = operand1[i] * operand2[i];
- }
- return product;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement