Example of JAVA Program

TrentonAsbury 495 views 14 slides Sep 27, 2015
Slide 1
Slide 1 of 14
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14

About This Presentation

No description available for this slideshow.


Slide Content

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 NUMBE RS
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(String prompt){

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;

}



}
Tags