SlideShare a Scribd company logo
Example of JAVA Program
import java.util.Scanner;
import javax.swing.JOptionPane;
public class AsburyTrentonAnArrayCalc {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner(System.in);
int choice, count = 0, counter = 0, done = 0, sizeArray = 0;
do {
choice = getMenuOption();
// SWITCH WILL SELECT WHICH TASK TO PERFORM
switch(choice){
// ADDING
case 1:
double[] num1 = new double[sizeArray];
double[] num2 = new double[sizeArray];
double[] total = new double[sizeArray];
double aNum1, aNum2, aTotal;
System.out.println("How many values are in the
arrays: ");
sizeArray = keyboard.nextInt();
// ADDS JUST 1 SET OF NUMBERS
if (sizeArray == 1){
aNum1 = getOperand("what is the 1st number to
add: ");
aNum2 = getOperand("what is the 2nd number
to add: ");
aTotal = aNum1 + aNum2;
System.out.println(aNum1 + " + " + aNum2 + " =
" + aTotal);
System.out.println("");
}
// ADDS MULTIPLE SETS OF NUMBERS
else{
num1 = getOperand("Enter the values in the first
array, seperated by spaces: ", sizeArray);
num2 = getOperand("Enter the values in the second
array, seperated by spaces: ", sizeArray);
total = add(num1, num2);
if (counter != 0){
counter = 0;
}
System.out.print("The result is [");
for (int i = 0; i < num1.length - 1; i++){
System.out.print(total[i] + ", ");
count = i;
}
System.out.print(total[count + 1] + "]");
System.out.println();
System.out.println();
}
break;
// SUBTRACTING
case 2:
double[] num3 = new double[sizeArray];
double[] num4 = new double[sizeArray];
double[] total2 = new double[sizeArray];
double sNum1 = 0, sNum2 = 0, sTotal = 0;
System.out.println("How many values are in the
arrays: ");
sizeArray = keyboard.nextInt();
// SUBTRACT JUST 1 SET OF NUMBERS
if (sizeArray == 1){
sNum1 = getOperand("what is the 1st number to
subtract: ");
sNum2 = getOperand("what is the 2nd number
to subtract: ");
sTotal = sNum1 - sNum2;
System.out.println(sNum1 + " - " + sNum2 + " = "
+ sTotal);
System.out.println("");
}
// SUBTRACT MULTIPLE SETS OF NUMBERS
else{
num3 = getOperand("Enter the values in the first
array, seperated by spaces: ", sizeArray);
num4 = getOperand("Enter the values in the second
array, seperated by spaces: ", sizeArray);
total2 = subtract(num3, num4);
if (counter != 0){
counter = 0;
}
System.out.print("The result is [");
for (int i = 0; i < num3.length - 1; i++){
System.out.print(total2[i] + ", ");
count = i;
}
System.out.print(total2[count + 1] + "]");
System.out.println();
System.out.println();
}
break;
// MULTIPLYING
case 3:
double[] num5 = new double[sizeArray];
double[] num6 = new double[sizeArray];
double[] total3 = new double[sizeArray];
double mNum1 = 0, mNum2 = 0, mTotal = 0;
System.out.println("How many values are in the
arrays: ");
sizeArray = keyboard.nextInt();
// MULTIPLY JUST 1 SET OF NUMBERS
if (sizeArray == 1){
mNum1 = getOperand("what is the 1st number
to multiply: ");
mNum2 = getOperand("what is the 2nd number
to multiply: ");
mTotal = mNum1 * mNum2;
System.out.println(mNum1 + " * " + mNum2 + "
= " + mTotal);
System.out.println("");
}
// MULTIPLY MULTIPLE SETS OF NUMBERS
else{
num5 = getOperand("Enter the values in the first
array, seperated by spaces: ", sizeArray);
num6 = getOperand("Enter the values in the second
array, seperated by spaces: ", sizeArray);
total3 = multiply(num5, num6);
if (counter != 0){
counter = 0;
}
System.out.print("The result is [");
for (int i = 0; i < num5.length - 1; i++){
System.out.print(total3[i] + ", ");
count = i;
}
System.out.print(total3[count + 1] + "]");
System.out.println();
System.out.println();
}
break;
// DIVIDING
case 4:
boolean dom = true;
double[] num7 = new double[sizeArray];
double[] num8 = new double[sizeArray];
double[] total4 = new double[sizeArray];
double dNum1, dNum2, dTotal;
System.out.println("How many values are in the
arrays: ");
sizeArray = keyboard.nextInt();
// DIVIDE JUST 1 SET OF NUMBERS
if (sizeArray == 1){
dNum1 = getOperand("what is the 1st number to
divide: ");
dNum2 = getOperand("what is the 2nd number
to divide: ");
// DENOMINATOR = 0, THIS WILL TRIGGER A
ERROR MESSAGE
if (dNum2 > 0){
dTotal = dNum1 / dNum2;
System.out.println(dNum1 + " / " + dNum2 + " =
" + dTotal);
System.out.println("");
}
else{
System.out.println("Sorry you can't divide
by 0!!");
System.out.println();
}
}
// DIVIDE MULTIPLE SETS OF NUMBERS
else{
num7 = getOperand("Enter the values in the first
array, seperated by spaces: ", sizeArray);
num8 = getOperand("Enter the values in the second
array, seperated by spaces: ", sizeArray);
// DENOMINATOR = 0, THIS WILL TRIGGER A
ERROR MESSAGE
for (int i = 0; i < num8.length; i++){
if (num8[i] < 1){
dom = false;
}
}
if (dom == true){
total4 = divide(num7, num8);
System.out.print("The result is [");
for (int i = 0; i < num7.length - 1; i++){
System.out.print(total4[i] + ", ");
count = i;
}
System.out.print(total4[count + 1] + "]");
System.out.println();
System.out.println();
}
else{
System.out.println("Sorry you can't divide by
0!!");
System.out.println("");
}
}
if (counter != 0){
counter = 0;
}
if (dom == false){
dom = true;
}
break;
// DOT PRODUCT
case 5:
System.out.println("How many values are in the
arrays: ");
sizeArray = keyboard.nextInt();
double[] num9 = new double[sizeArray];
double[] num10 = new double[sizeArray];
double total5;
double pNum1 = 0, pNum2 = 0, pTotal = 0;
// MULTIPLY JUST 1 SET OF NUMBERS
if (sizeArray == 1){
pNum1 = getOperand("what is the 1st number
for dot product: ");
pNum2 = getOperand("what is the 2nd number
for dot product: ");
pTotal = pNum1 * pNum2;
System.out.println("The result is: " + pTotal);
System.out.println();
}
// MULTIPLY MULTIPLE SETS OF NUMBERS
else{
num9 = getOperand("Enter the values in the first
array, seperated by spaces: ", sizeArray);
num10 = getOperand("Enter the values in the second
array, seperated by spaces: ", sizeArray);
total5 = dotProduct(num9, num10);
System.out.print("The result is: " + total5);
System.out.println();
}
if (counter != 0){
counter = 0;
}
break;
case 6:
double rNum1 = 0, rNum2 = 0, randNum;
int lnCounter = 0;
System.out.println("How many values are in the
arrays: ");
sizeArray = keyboard.nextInt();
double[] randNums = new double[sizeArray];
// GENERATE RANDOM NUMBER JUST 1 NUMBER
if (sizeArray == 1){
rNum1 = getOperand("what is the lower limit: ");
rNum2 = getOperand("what is the upper limit: ");
randNum = random(rNum1, rNum2);
System.out.println(randNum);
System.out.println("");
}
// GENERATE A SETS OF RANDOM NUMBERS
else{
rNum1 = getOperand("what is the lower limit: ");
rNum2 = getOperand("what is the upper limit: ");
randNums = random(rNum1, rNum2, sizeArray);
// SORT RANDOM NUMBERS
for(int i = 0; i < randNums.length -1; i++){
int smallNum = i;
for(int j = i; j < randNums.length; j++){
if(randNums[j] < randNums[smallNum]){
smallNum = j;
}
}
//SWAP THE SMALLEST NUM WITH THE
LOCATION I
double temp = randNums[i];
randNums[i] = randNums[smallNum];
randNums[smallNum] = temp;
}
System.out.print("The result is");
System.out.println();
System.out.print("[");
for (int i = 0; i < sizeArray / 3; i++){
for(int j = 0; j < 3; j++){
System.out.print(randNums[lnCounter] + ", ");
count = i;
lnCounter++;
}
System.out.println();
}
}
System.out.print("]");
System.out.println();
System.out.println();
}
if (counter != 0){
counter = 0;
break;
}
// DISPLAY STATEMENT IF USER PUTS WRONG
NUMBER IN
if (choice > 7 || choice < 1) {
counter++;
System.out.println("I'm sorry, " + choice + "
wasn't one of the options");
System.out.println("");
}
if (counter == 3 || choice == 7){
JOptionPane.showMessageDialog(null, "PLEASE
TRY AGAIN LATER");
break;
}
} while(choice != 7);
}
// METHOD TO SHOW THE MENU
public static int getMenuOption(){
Scanner keyboard = new Scanner(System.in);
System.out.println("=========MENU =========");
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("5. Dot Product");
System.out.println("6. Generate Random Array");
System.out.println("7. Exit");
System.out.println("What would you like to do: ");
int choice = keyboard.nextInt();
return choice;
}
public static double[] getOperand(String prompt, int size){
double[] num = new double[size];
Scanner keyboard = new Scanner(System.in);
System.out.println(prompt);
for (int i = 0; i < size; i++){
num[i] = keyboard.nextDouble();
}
return num;
}
public static double getOperand(Stringprompt){
Scanner keyboard = new Scanner(System.in);
System.out.println(prompt);
double num = keyboard.nextDouble();
return num;
}
public static double[] add(double[] operand1, double[] operand2){
double[] total = new double[operand1.length];
for(int i = 0; i < operand1.length; i++){
total[i] = operand1[i] + operand2[i];
}
return total;
}
public static double[] subtract(double[] operand1, double[] operand2){
double[] total = new double[operand1.length];
for(int i = 0; i < operand1.length; i++){
total[i] = operand1[i] - operand2[i];
}
return total;
}
public static double[] multiply(double[] operand1, double[] operand2){
double[] total = new double[operand1.length];
for(int i = 0; i < operand1.length; i++){
total[i] = operand1[i] * operand2[i];
}
return total;
}
public static double[] divide(double[] operand1, double[] operand2){
double[] total = new double[operand1.length];
for(int i = 0; i < operand1.length; i++){
total[i] = operand1[i] / operand2[i];
}
return total;
}
public static double[] random(double lowerLimit, double upperLimit, int size){
double[] randNum = new double[size];
int decMult = 100;
for(int i = 0; i < size; i++){
randNum[i] = (double) (Math.random() * decMult + 1);
// KEEPS LOOPING TILL THE RANDOM NUMBER IS BETWEEN THE
// SELECTED NUMBERS BY THE USER
do {
randNum[i] = (double) (Math.random() * decMult + 1);
// THIS WILL CHANGE THE DECIMAL SO THE USER CAN
CHANGE
// ABOVE 100
if (lowerLimit > 100){
decMult = 1000;
}
// THIS WILL CHANGE THE DECIMAL SO THE USER CAN
CHANGE
// ABOVE 1,000
if (lowerLimit > 1000){
decMult = 10000;
}
// THIS WILL CHANGE THE DECIMAL SO THE USER CAN
CHANGE
// ABOVE 10,000
if (lowerLimit > 10000){
decMult = 100000;
}
} while (randNum[i] < lowerLimit || randNum[i] > upperLimit);
}
return randNum;
}
public static double random(double lowerLimit, double upperLimit){
int decMult = 100;
double randomNumber = (double) (Math.random() * decMult + 1);
// KEEPS LOOPING TILL THE RANDOM NUMBER IS BETWEEN THE
// SELECTED NUMBERS BY THE USER
do {
randomNumber = (double) (Math.random() * decMult + 1);
// THIS WILL CHANGE THE DECIMAL SO THE USER CAN
CHANGE
// ABOVE 100
if (lowerLimit > 100){
decMult = 1000;
}
// THIS WILL CHANGE THE DECIMAL SO THE USER CAN
CHANGE
// ABOVE 1,000
if (lowerLimit > 1000){
decMult = 10000;
}
// THIS WILL CHANGE THE DECIMAL SO THE USER CAN
CHANGE
// ABOVE 10,000
if (lowerLimit > 10000){
decMult = 100000;
}
} while (randomNumber < lowerLimit || randomNumber > upperLimit);
return randomNumber;
}
public static double dotProduct(double[] operand1, double[] operand2){
double total = 0, num = 0;
for(int i = 0; i < operand1.length; i++){
num = operand1[i] * operand2[i];
total += num;
}
return total;
}
}

More Related Content

PPT
003 Uml Semalari [94 Slides]
PPTX
Selenium WebDriver avec Java
PPTX
Introduce Katalon tool
PDF
Unit testing with JUnit
ODP
Why Katalon Studio?
PPTX
TestNG Session presented in PB
PPT
PDF
Java 8 date & time api
003 Uml Semalari [94 Slides]
Selenium WebDriver avec Java
Introduce Katalon tool
Unit testing with JUnit
Why Katalon Studio?
TestNG Session presented in PB
Java 8 date & time api

What's hot (20)

PPT
05 junit
PPTX
Automated Test Framework with Cucumber
PDF
How to go about testing in React?
PPTX
Unit Testing with xUnit.net - Part 2
PDF
Test Case, Use Case and Test Scenario
PDF
Criando uma grid para execução de testes paralelo com Appium
PPTX
Unit Testing Using N Unit
PPTX
Memory Management in the Java Virtual Machine(Garbage collection)
PDF
애자일 테스트 프랙티스와 사례들 (부제: 협업의 힘)
PDF
Selenium IDE LOCATORS
PPT
PDF
Introduction to Robot Framework (external)
PPTX
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
PPTX
Unit tests & TDD
PPTX
Coroutines in Kotlin
PPT
Unit Test
PPTX
Katalon Studio Presentation.pptx
PDF
Robot Framework :: Demo login application
PPTX
TestNG Framework
PDF
Condition Determination Testing and Coverage. ISTQB White-box Techniques with...
05 junit
Automated Test Framework with Cucumber
How to go about testing in React?
Unit Testing with xUnit.net - Part 2
Test Case, Use Case and Test Scenario
Criando uma grid para execução de testes paralelo com Appium
Unit Testing Using N Unit
Memory Management in the Java Virtual Machine(Garbage collection)
애자일 테스트 프랙티스와 사례들 (부제: 협업의 힘)
Selenium IDE LOCATORS
Introduction to Robot Framework (external)
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
Unit tests & TDD
Coroutines in Kotlin
Unit Test
Katalon Studio Presentation.pptx
Robot Framework :: Demo login application
TestNG Framework
Condition Determination Testing and Coverage. ISTQB White-box Techniques with...
Ad

Viewers also liked (12)

PPSX
Seminar on java
PDF
Advanced Java Practical File
PPT
Introduction to-programming
DOCX
Java codes
DOCX
Java PRACTICAL file
PDF
Java programming-examples
PPTX
Java programming course for beginners
PDF
Introduction to Java Programming Language
PPT
Java basic
PDF
Introduction to Java Programming
PPTX
Introduction to java
PPT
Java tutorial PPT
Seminar on java
Advanced Java Practical File
Introduction to-programming
Java codes
Java PRACTICAL file
Java programming-examples
Java programming course for beginners
Introduction to Java Programming Language
Java basic
Introduction to Java Programming
Introduction to java
Java tutorial PPT
Ad

Similar to Example of JAVA Program (20)

PDF
Simple 27 Java Program on basic java syntax
PDF
Simple Java Program for beginner with easy method.pdf
PPTX
Presentation1 computer shaan
DOCX
Import java
PDF
I am constantly getting errors and cannot figure this out. Please he.pdf
DOCX
Java Practical1 based on Basic assignment
DOCX
Programs of java
PDF
Write a program that works with fractions. You are first to implemen.pdf
PDF
Sam wd programs
DOC
Find the output of the following code (Java for ICSE)
PDF
Java Simple Programs
PDF
Java_Programming_by_Example_6th_Edition.pdf
DOCX
100 Small programs
PDF
import java.util.Random;ASSIGNMENT #2 MATRIX ARITHMETIC Cla.pdf
PDF
The Art of Clean Code
PDF
Microsoft word java
PDF
public interface Game Note interface in place of class { .pdf
DOC
5 Rmi Print
PPTX
OBJECT ORIENTED PROGRAMMIING LANGUAGE PROGRAMS
PPTX
lab programs on java and dbms for students access
Simple 27 Java Program on basic java syntax
Simple Java Program for beginner with easy method.pdf
Presentation1 computer shaan
Import java
I am constantly getting errors and cannot figure this out. Please he.pdf
Java Practical1 based on Basic assignment
Programs of java
Write a program that works with fractions. You are first to implemen.pdf
Sam wd programs
Find the output of the following code (Java for ICSE)
Java Simple Programs
Java_Programming_by_Example_6th_Edition.pdf
100 Small programs
import java.util.Random;ASSIGNMENT #2 MATRIX ARITHMETIC Cla.pdf
The Art of Clean Code
Microsoft word java
public interface Game Note interface in place of class { .pdf
5 Rmi Print
OBJECT ORIENTED PROGRAMMIING LANGUAGE PROGRAMS
lab programs on java and dbms for students access

Example of JAVA Program

  • 1. Example of JAVA Program import java.util.Scanner; import javax.swing.JOptionPane; public class AsburyTrentonAnArrayCalc { public static void main(String[] args) { // TODO Auto-generated method stub Scanner keyboard = new Scanner(System.in); int choice, count = 0, counter = 0, done = 0, sizeArray = 0; do { choice = getMenuOption(); // SWITCH WILL SELECT WHICH TASK TO PERFORM switch(choice){ // ADDING case 1: double[] num1 = new double[sizeArray]; double[] num2 = new double[sizeArray]; double[] total = new double[sizeArray]; double aNum1, aNum2, aTotal; System.out.println("How many values are in the arrays: "); sizeArray = keyboard.nextInt(); // ADDS JUST 1 SET OF NUMBERS if (sizeArray == 1){ aNum1 = getOperand("what is the 1st number to add: "); aNum2 = getOperand("what is the 2nd number to add: "); aTotal = aNum1 + aNum2; System.out.println(aNum1 + " + " + aNum2 + " = " + aTotal); System.out.println("");
  • 2. } // ADDS MULTIPLE SETS OF NUMBERS else{ num1 = getOperand("Enter the values in the first array, seperated by spaces: ", sizeArray); num2 = getOperand("Enter the values in the second array, seperated by spaces: ", sizeArray); total = add(num1, num2); if (counter != 0){ counter = 0; } System.out.print("The result is ["); for (int i = 0; i < num1.length - 1; i++){ System.out.print(total[i] + ", "); count = i; } System.out.print(total[count + 1] + "]"); System.out.println(); System.out.println(); } break; // SUBTRACTING case 2: double[] num3 = new double[sizeArray]; double[] num4 = new double[sizeArray]; double[] total2 = new double[sizeArray]; double sNum1 = 0, sNum2 = 0, sTotal = 0; System.out.println("How many values are in the arrays: "); sizeArray = keyboard.nextInt(); // SUBTRACT JUST 1 SET OF NUMBERS if (sizeArray == 1){
  • 3. sNum1 = getOperand("what is the 1st number to subtract: "); sNum2 = getOperand("what is the 2nd number to subtract: "); sTotal = sNum1 - sNum2; System.out.println(sNum1 + " - " + sNum2 + " = " + sTotal); System.out.println(""); } // SUBTRACT MULTIPLE SETS OF NUMBERS else{ num3 = getOperand("Enter the values in the first array, seperated by spaces: ", sizeArray); num4 = getOperand("Enter the values in the second array, seperated by spaces: ", sizeArray); total2 = subtract(num3, num4); if (counter != 0){ counter = 0; } System.out.print("The result is ["); for (int i = 0; i < num3.length - 1; i++){ System.out.print(total2[i] + ", "); count = i; } System.out.print(total2[count + 1] + "]"); System.out.println(); System.out.println(); } break; // MULTIPLYING case 3: double[] num5 = new double[sizeArray]; double[] num6 = new double[sizeArray]; double[] total3 = new double[sizeArray];
  • 4. double mNum1 = 0, mNum2 = 0, mTotal = 0; System.out.println("How many values are in the arrays: "); sizeArray = keyboard.nextInt(); // MULTIPLY JUST 1 SET OF NUMBERS if (sizeArray == 1){ mNum1 = getOperand("what is the 1st number to multiply: "); mNum2 = getOperand("what is the 2nd number to multiply: "); mTotal = mNum1 * mNum2; System.out.println(mNum1 + " * " + mNum2 + " = " + mTotal); System.out.println(""); } // MULTIPLY MULTIPLE SETS OF NUMBERS else{ num5 = getOperand("Enter the values in the first array, seperated by spaces: ", sizeArray); num6 = getOperand("Enter the values in the second array, seperated by spaces: ", sizeArray); total3 = multiply(num5, num6); if (counter != 0){ counter = 0; } System.out.print("The result is ["); for (int i = 0; i < num5.length - 1; i++){ System.out.print(total3[i] + ", "); count = i; } System.out.print(total3[count + 1] + "]"); System.out.println(); System.out.println();
  • 5. } break; // DIVIDING case 4: boolean dom = true; double[] num7 = new double[sizeArray]; double[] num8 = new double[sizeArray]; double[] total4 = new double[sizeArray]; double dNum1, dNum2, dTotal; System.out.println("How many values are in the arrays: "); sizeArray = keyboard.nextInt(); // DIVIDE JUST 1 SET OF NUMBERS if (sizeArray == 1){ dNum1 = getOperand("what is the 1st number to divide: "); dNum2 = getOperand("what is the 2nd number to divide: "); // DENOMINATOR = 0, THIS WILL TRIGGER A ERROR MESSAGE if (dNum2 > 0){ dTotal = dNum1 / dNum2; System.out.println(dNum1 + " / " + dNum2 + " = " + dTotal); System.out.println(""); } else{ System.out.println("Sorry you can't divide by 0!!"); System.out.println(); } } // DIVIDE MULTIPLE SETS OF NUMBERS else{
  • 6. num7 = getOperand("Enter the values in the first array, seperated by spaces: ", sizeArray); num8 = getOperand("Enter the values in the second array, seperated by spaces: ", sizeArray); // DENOMINATOR = 0, THIS WILL TRIGGER A ERROR MESSAGE for (int i = 0; i < num8.length; i++){ if (num8[i] < 1){ dom = false; } } if (dom == true){ total4 = divide(num7, num8); System.out.print("The result is ["); for (int i = 0; i < num7.length - 1; i++){ System.out.print(total4[i] + ", "); count = i; } System.out.print(total4[count + 1] + "]"); System.out.println(); System.out.println(); } else{ System.out.println("Sorry you can't divide by 0!!"); System.out.println(""); } } if (counter != 0){ counter = 0; } if (dom == false){ dom = true; } break; // DOT PRODUCT case 5:
  • 7. System.out.println("How many values are in the arrays: "); sizeArray = keyboard.nextInt(); double[] num9 = new double[sizeArray]; double[] num10 = new double[sizeArray]; double total5; double pNum1 = 0, pNum2 = 0, pTotal = 0; // MULTIPLY JUST 1 SET OF NUMBERS if (sizeArray == 1){ pNum1 = getOperand("what is the 1st number for dot product: "); pNum2 = getOperand("what is the 2nd number for dot product: "); pTotal = pNum1 * pNum2; System.out.println("The result is: " + pTotal); System.out.println(); } // MULTIPLY MULTIPLE SETS OF NUMBERS else{ num9 = getOperand("Enter the values in the first array, seperated by spaces: ", sizeArray); num10 = getOperand("Enter the values in the second array, seperated by spaces: ", sizeArray); total5 = dotProduct(num9, num10); System.out.print("The result is: " + total5); System.out.println(); } if (counter != 0){ counter = 0; } break; case 6:
  • 8. double rNum1 = 0, rNum2 = 0, randNum; int lnCounter = 0; System.out.println("How many values are in the arrays: "); sizeArray = keyboard.nextInt(); double[] randNums = new double[sizeArray]; // GENERATE RANDOM NUMBER JUST 1 NUMBER if (sizeArray == 1){ rNum1 = getOperand("what is the lower limit: "); rNum2 = getOperand("what is the upper limit: "); randNum = random(rNum1, rNum2); System.out.println(randNum); System.out.println(""); } // GENERATE A SETS OF RANDOM NUMBERS else{ rNum1 = getOperand("what is the lower limit: "); rNum2 = getOperand("what is the upper limit: "); randNums = random(rNum1, rNum2, sizeArray); // SORT RANDOM NUMBERS for(int i = 0; i < randNums.length -1; i++){ int smallNum = i; for(int j = i; j < randNums.length; j++){ if(randNums[j] < randNums[smallNum]){ smallNum = j; } } //SWAP THE SMALLEST NUM WITH THE LOCATION I double temp = randNums[i]; randNums[i] = randNums[smallNum]; randNums[smallNum] = temp; } System.out.print("The result is");
  • 9. System.out.println(); System.out.print("["); for (int i = 0; i < sizeArray / 3; i++){ for(int j = 0; j < 3; j++){ System.out.print(randNums[lnCounter] + ", "); count = i; lnCounter++; } System.out.println(); } } System.out.print("]"); System.out.println(); System.out.println(); } if (counter != 0){ counter = 0; break; } // DISPLAY STATEMENT IF USER PUTS WRONG NUMBER IN if (choice > 7 || choice < 1) { counter++; System.out.println("I'm sorry, " + choice + " wasn't one of the options"); System.out.println(""); } if (counter == 3 || choice == 7){ JOptionPane.showMessageDialog(null, "PLEASE TRY AGAIN LATER"); break; }
  • 10. } while(choice != 7); } // METHOD TO SHOW THE MENU public static int getMenuOption(){ Scanner keyboard = new Scanner(System.in); System.out.println("=========MENU ========="); System.out.println("1. Add"); System.out.println("2. Subtract"); System.out.println("3. Multiply"); System.out.println("4. Divide"); System.out.println("5. Dot Product"); System.out.println("6. Generate Random Array"); System.out.println("7. Exit"); System.out.println("What would you like to do: "); int choice = keyboard.nextInt(); return choice; } public static double[] getOperand(String prompt, int size){ double[] num = new double[size]; Scanner keyboard = new Scanner(System.in); System.out.println(prompt); for (int i = 0; i < size; i++){ num[i] = keyboard.nextDouble(); } return num; } public static double getOperand(Stringprompt){ Scanner keyboard = new Scanner(System.in);
  • 11. System.out.println(prompt); double num = keyboard.nextDouble(); return num; } public static double[] add(double[] operand1, double[] operand2){ double[] total = new double[operand1.length]; for(int i = 0; i < operand1.length; i++){ total[i] = operand1[i] + operand2[i]; } return total; } public static double[] subtract(double[] operand1, double[] operand2){ double[] total = new double[operand1.length]; for(int i = 0; i < operand1.length; i++){ total[i] = operand1[i] - operand2[i]; } return total; } public static double[] multiply(double[] operand1, double[] operand2){ double[] total = new double[operand1.length]; for(int i = 0; i < operand1.length; i++){ total[i] = operand1[i] * operand2[i]; } return total;
  • 12. } public static double[] divide(double[] operand1, double[] operand2){ double[] total = new double[operand1.length]; for(int i = 0; i < operand1.length; i++){ total[i] = operand1[i] / operand2[i]; } return total; } public static double[] random(double lowerLimit, double upperLimit, int size){ double[] randNum = new double[size]; int decMult = 100; for(int i = 0; i < size; i++){ randNum[i] = (double) (Math.random() * decMult + 1); // KEEPS LOOPING TILL THE RANDOM NUMBER IS BETWEEN THE // SELECTED NUMBERS BY THE USER do { randNum[i] = (double) (Math.random() * decMult + 1); // THIS WILL CHANGE THE DECIMAL SO THE USER CAN CHANGE // ABOVE 100 if (lowerLimit > 100){ decMult = 1000; } // THIS WILL CHANGE THE DECIMAL SO THE USER CAN CHANGE // ABOVE 1,000 if (lowerLimit > 1000){ decMult = 10000; }
  • 13. // THIS WILL CHANGE THE DECIMAL SO THE USER CAN CHANGE // ABOVE 10,000 if (lowerLimit > 10000){ decMult = 100000; } } while (randNum[i] < lowerLimit || randNum[i] > upperLimit); } return randNum; } public static double random(double lowerLimit, double upperLimit){ int decMult = 100; double randomNumber = (double) (Math.random() * decMult + 1); // KEEPS LOOPING TILL THE RANDOM NUMBER IS BETWEEN THE // SELECTED NUMBERS BY THE USER do { randomNumber = (double) (Math.random() * decMult + 1); // THIS WILL CHANGE THE DECIMAL SO THE USER CAN CHANGE // ABOVE 100 if (lowerLimit > 100){ decMult = 1000; } // THIS WILL CHANGE THE DECIMAL SO THE USER CAN CHANGE // ABOVE 1,000 if (lowerLimit > 1000){ decMult = 10000; } // THIS WILL CHANGE THE DECIMAL SO THE USER CAN CHANGE // ABOVE 10,000 if (lowerLimit > 10000){ decMult = 100000; }
  • 14. } while (randomNumber < lowerLimit || randomNumber > upperLimit); return randomNumber; } public static double dotProduct(double[] operand1, double[] operand2){ double total = 0, num = 0; for(int i = 0; i < operand1.length; i++){ num = operand1[i] * operand2[i]; total += num; } return total; } }