Random Numbers and Statistics in Java Homework Sample

Request 2 integer values from the user between 0-100, and then generate 3 random values in the range. Display the 3 values in order (as well as their odd/even status). Then display the sum of the values, and the product of the values, and finally the quotient. For more Java programming assignments contact us for a quote.

Solution:

package lab03;

import java.util.Scanner;

public class Lab03 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int count = 0;
int begin, end;
Scanner in = new Scanner(System.in);
// Read begin
System.out.print(“Please enter an integer between 0-99:”);
while (true) {
if (count >= 2) {
System.out.println(“Outside range. Program ending.”);
return;
} else if (count > 0) {
System.out.print(“Outside range. Please enter an integer between 0-99:”);
}
begin = in.nextInt();
count++;
if (begin >= 0 && begin <= 99) {
break;
}
}
// Read end
count = 0;
System.out.print(“Next, please enter another integer between 0-99:”);
while (true) {
if (count >= 2) {
System.out.println(“Outside range. Program ending.”);
return;
} else if (count > 0) {
System.out.print(“Outside range. Please enter another integer between 0-99:”);
}
count++;
end = in.nextInt();
if (end >= 0 && end <= 99) {
break;
}
}

//Find min max
int min, max;
if (begin > end) {
max = begin;
min = end;
} else {
max = end;
min = begin;
}

System.out.println(“The range begins at ” + min + ” and ends at ” + max);
// Generate 3 number
int num1 = min + (int) (Math.random() * ((max – min) + 1));
int num2 = min + (int) (Math.random() * ((max – min) + 1));
int num3 = min + (int) (Math.random() * ((max – min) + 1));

if (num1 > num2) {
//swap
int temp = num1;
num1 = num2;
num2 = temp;
}
if (num2 > num3) {
//swap
int temp = num2;
num2 = num3;
num3 = temp;
}
if (num1 > num2) {
//swap
int temp = num1;
num1 = num2;
num2 = temp;
}
System.out.println(“Three sorted random integers between ” + min + ” and ” + max + ” are:”);
System.out.print(num1 + “\t”);
if (num1 % 2 == 1) {
System.out.println(“Odd”);
} else {
System.out.println(“Even”);
}
System.out.print(num2 + “\t”);
if (num2 % 2 == 1) {
System.out.println(“Odd”);
} else {
System.out.println(“Even”);
}
System.out.print(num3 + “\t”);
if (num3 % 2 == 1) {
System.out.println(“Odd”);
} else {
System.out.println(“Even”);
}

System.out.println(“Sum = ” + (num1 + num2 + num3));
System.out.println(“Product = ” + (num1 * num2 * num3));
System.out.println(“Quotient (Int1 / Int2 / Int3) = ” + ((double)num1 / (double)num2 / (double)num3));
}

}