SlideShare a Scribd company logo
1 of 14
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

What's hot

Oops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in JavaOops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in JavaMadishetty Prathibha
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword pptVinod Kumar
 
Java AWT Calculadora
Java AWT CalculadoraJava AWT Calculadora
Java AWT Calculadorajubacalo
 
Java multi threading
Java multi threadingJava multi threading
Java multi threadingRaja Sekhar
 
WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAsivasundari6
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaArafat Hossan
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streamsShahjahan Samoon
 
Summer training presentation on "CORE JAVA".
Summer training presentation on "CORE JAVA".Summer training presentation on "CORE JAVA".
Summer training presentation on "CORE JAVA".SudhanshuVijay3
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Javaparag
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in javaVishnu Suresh
 
Introduction to method overloading &amp; method overriding in java hdm
Introduction to method overloading &amp; method overriding  in java  hdmIntroduction to method overloading &amp; method overriding  in java  hdm
Introduction to method overloading &amp; method overriding in java hdmHarshal Misalkar
 
Swift Programming Language
Swift Programming LanguageSwift Programming Language
Swift Programming LanguageCihad Horuzoğlu
 

What's hot (20)

Welcome to python workshop
Welcome to python workshopWelcome to python workshop
Welcome to python workshop
 
Java basics
Java basicsJava basics
Java basics
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Oops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in JavaOops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in Java
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
Java AWT Calculadora
Java AWT CalculadoraJava AWT Calculadora
Java AWT Calculadora
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
 
WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVA
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
 
Summer training presentation on "CORE JAVA".
Summer training presentation on "CORE JAVA".Summer training presentation on "CORE JAVA".
Summer training presentation on "CORE JAVA".
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
Introduction to method overloading &amp; method overriding in java hdm
Introduction to method overloading &amp; method overriding  in java  hdmIntroduction to method overloading &amp; method overriding  in java  hdm
Introduction to method overloading &amp; method overriding in java hdm
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Swift Programming Language
Swift Programming LanguageSwift Programming Language
Swift Programming Language
 
Java naming conventions
Java naming conventionsJava naming conventions
Java naming conventions
 
sets and maps
 sets and maps sets and maps
sets and maps
 
Java Threads
Java ThreadsJava Threads
Java Threads
 

Viewers also liked

Viewers also liked (12)

Seminar on java
Seminar on javaSeminar on java
Seminar on java
 
Advanced Java Practical File
Advanced Java Practical FileAdvanced Java Practical File
Advanced Java Practical File
 
Introduction to-programming
Introduction to-programmingIntroduction to-programming
Introduction to-programming
 
Java codes
Java codesJava codes
Java codes
 
Java PRACTICAL file
Java PRACTICAL fileJava PRACTICAL file
Java PRACTICAL file
 
Java programming-examples
Java programming-examplesJava programming-examples
Java programming-examples
 
Java programming course for beginners
Java programming course for beginnersJava programming course for beginners
Java programming course for beginners
 
Introduction to Java Programming Language
Introduction to Java Programming LanguageIntroduction to Java Programming Language
Introduction to Java Programming Language
 
Java basic
Java basicJava basic
Java basic
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 

Similar to Example of JAVA Program

ch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.pptch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.pptMahyuddin8
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory archana singh
 
Import java
Import javaImport java
Import javaheni2121
 
FileName EX06_1java Programmer import ja.pdf
FileName EX06_1java Programmer  import ja.pdfFileName EX06_1java Programmer  import ja.pdf
FileName EX06_1java Programmer import ja.pdfactocomputer
 
CipherDriver.javapackage Unit_6;import java.util.;public cl.pdf
CipherDriver.javapackage Unit_6;import java.util.;public cl.pdfCipherDriver.javapackage Unit_6;import java.util.;public cl.pdf
CipherDriver.javapackage Unit_6;import java.util.;public cl.pdfravikapoorindia
 
Write a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfWrite a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfarri2009av
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptxKimVeeL
 
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian KomputerKOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian KomputerAiman Hud
 
Write a java program to randomly generate the following sets of data.pdf
Write a java program to randomly generate the following sets of data.pdfWrite a java program to randomly generate the following sets of data.pdf
Write a java program to randomly generate the following sets of data.pdfarshin9
 
Java Programpublic class Fraction {   instance variablesin.pdf
Java Programpublic class Fraction {   instance variablesin.pdfJava Programpublic class Fraction {   instance variablesin.pdf
Java Programpublic class Fraction {   instance variablesin.pdfaroramobiles1
 
import java.uti-WPS Office.docx
import java.uti-WPS Office.docximport java.uti-WPS Office.docx
import java.uti-WPS Office.docxKatecate1
 
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdfSumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdfankkitextailes
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4Abed Bukhari
 
import java.util.; public class ClientMerge_2 {     static Scan.pdf
import java.util.; public class ClientMerge_2 {     static Scan.pdfimport java.util.; public class ClientMerge_2 {     static Scan.pdf
import java.util.; public class ClientMerge_2 {     static Scan.pdfpremkhatri99
 

Similar to Example of JAVA Program (20)

ch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.pptch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.ppt
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory
 
Import java
Import javaImport java
Import java
 
Java file
Java fileJava file
Java file
 
Java file
Java fileJava file
Java file
 
FileName EX06_1java Programmer import ja.pdf
FileName EX06_1java Programmer  import ja.pdfFileName EX06_1java Programmer  import ja.pdf
FileName EX06_1java Programmer import ja.pdf
 
CipherDriver.javapackage Unit_6;import java.util.;public cl.pdf
CipherDriver.javapackage Unit_6;import java.util.;public cl.pdfCipherDriver.javapackage Unit_6;import java.util.;public cl.pdf
CipherDriver.javapackage Unit_6;import java.util.;public cl.pdf
 
Write a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfWrite a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdf
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptx
 
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian KomputerKOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
 
Write a java program to randomly generate the following sets of data.pdf
Write a java program to randomly generate the following sets of data.pdfWrite a java program to randomly generate the following sets of data.pdf
Write a java program to randomly generate the following sets of data.pdf
 
Java Programpublic class Fraction {   instance variablesin.pdf
Java Programpublic class Fraction {   instance variablesin.pdfJava Programpublic class Fraction {   instance variablesin.pdf
Java Programpublic class Fraction {   instance variablesin.pdf
 
import java.uti-WPS Office.docx
import java.uti-WPS Office.docximport java.uti-WPS Office.docx
import java.uti-WPS Office.docx
 
Ann
AnnAnn
Ann
 
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdfSumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
 
The Art of Clean Code
The Art of Clean CodeThe Art of Clean Code
The Art of Clean Code
 
Practice
PracticePractice
Practice
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
 
Los dskn
Los dsknLos dskn
Los dskn
 
import java.util.; public class ClientMerge_2 {     static Scan.pdf
import java.util.; public class ClientMerge_2 {     static Scan.pdfimport java.util.; public class ClientMerge_2 {     static Scan.pdf
import java.util.; public class ClientMerge_2 {     static Scan.pdf
 

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; } }