![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Nov 2005
Posts: 14
Rep Power: 0
![]() |
Hi, I am fairly new to Java Programming, as well as these forums..but I am having a problem in which I need help.
Our class assignment is to make a program which random numbers are generated into an array. The array length is determined by the user, as is the range of the random numbers. We have to do this using different methods, and the rules are, we can't use static, or class variables. After calculating the random numbers, a menu appears in which the user can choose to: - Show all of the numbers in the array - Find the largest number in the array - Find the smallest number in the array - Find the sum of all the numbers in the array - Find the average of all the numbers in the array - Find the median of all the numbers in the array - Find the mode of all the numbers in the array The ones in red are the things I am having problems with. Now, for the showing the numbers in the array bit. It works when I print the array in the same method it is calculated in, but when I pass it to the menu method, it prints all of the same number, ex: 4 4 4 4 4 4 4 4 Here is the code for the main method: import java.io.*;
import java.text.DecimalFormat;
public class Random_Amount {
public static void main(String args[ ])throws IOException {
Random_Amount rAm= new Random_Amount();
int intNumber, intNumber1= 999999,intRangeHigh, intRangeLow, intRangeSum, intTotal= 0, intRan, intHighNum= -99999, intLowNum= 99999;
intNumber= rAm.Amount();
intRangeLow= rAm.Low();
intRangeHigh= rAm.High();
intRangeSum= intRangeHigh - intRangeLow;
int intList[]= new int[intNumber1];
for (int i= 0; i < intNumber; i++) {
intList[intNumber]=intRan= (int)((Math.random()*(intRangeSum)+1)+intRangeLow);
intTotal+=intRan;
if (intRan > intHighNum)
intHighNum= intRan;
if (intRan < intLowNum)
intLowNum= intRan;
}
System.out.println();
rAm.Menu(intLowNum, intHighNum, intTotal, intNumber, intList);
}And here is the code for the menu method: private void Menu(int intLowNum, int intHighNum, int intTotalNum, int intAmount, int intArray[])throws IOException {
Random_Amount rAm= new Random_Amount();
String strChoice;
int intChoice;
double dblAverage;
DataInputStream stdin= new DataInputStream(System.in);
DecimalFormat oneDecimal= new DecimalFormat("0.0");
System.out.println("Main Menu");
System.out.println();
System.out.println("Enter 1 to view the random numbers");
System.out.println();
System.out.println("Enter 2 to find the largest number of the random numbers");
System.out.println();
System.out.println("Enter 3 to find the smallest number of the random numbers");
System.out.println();
System.out.println("Enter 4 to find the sum of the random numbers");
System.out.println();
System.out.println("Enter 5 to find the average of the random numbers");
System.out.println();
System.out.println("Enter 6 to find the median of the random numbers");
System.out.println();
System.out.println("Enter 7 to find the mode of the random numbers");
System.out.println();
System.out.println("Enter 8 to end the program");
System.out.println();
strChoice= stdin.readLine();
intChoice= Integer.parseInt(strChoice);
System.out.println();
while (intChoice!=8) {
switch (intChoice) {
case 1: for (int i= 0; i < intAmount; i++)
System.out.println(intArray[intAmount]);
break;
case 2: System.out.println("The largest number is "+intHighNum); break;
case 3: System.out.println("The smallest number is "+intLowNum); break;
case 4: System.out.println("The sum of all the numbers is "+intTotalNum); break;
case 5:
dblAverage= (( double ) intTotalNum) / (( double ) intAmount);
System.out.println("The average of all the numbers is "+oneDecimal.format(dblAverage));
break;
case 6:System.out.println();
break;
case 7: System.out.println(); break;
default: System.out.println("Invalid Input"); break;
}
strChoice= stdin.readLine();
intChoice= Integer.parseInt(strChoice);
}
System.out.println("Sequence ended by User's request");
}Please don't criticise my code, I am new to it, as I mentioned earlier ![]() So yeah...can someone help me with that bit? As for the median part, I sorted the array, and tried to find the median, but it just prints out a 0, can someone help me with this? void findMedian(int intArray[], int intArraySize)
{
int i, j, k;
int intMinimum, intTemporary, intDivide, intMedian;
for (i = 0; i < intArraySize-1; i++) {
intMinimum = i;
for (j = i+1; j < intArraySize; j++) {
if (intArray[j] < intArray[intMinimum])
intMinimum = j;
}
intTemporary = intArray[i];
intArray[i] = intArray[intMinimum];
intArray[intMinimum] = intTemporary;
}
if (intArray.length%2 == 0) { //If the length of the array is even
intDivide = intArray.length/2;
intMedian = (intArray[intDivide] + intArray[intDivide-1])/2; //The median is defined as the average of the middle numbers
}
else { //If the length of the array is odd
intDivide = intArray.length/2;
intMedian = intArray[intDivide]; //Defines the median as the middle number
}
System.out.println ();
System.out.println("The median of all random numbers is " + intMedian);
}And finally, for the mode, I have no idea how to find that :\ If someone could help me with these problems, that would be great ![]() Here is the full code, {Just for Reference} import java.io.*;
import java.text.DecimalFormat;
public class Random_Amount {
public static void main(String args[ ])throws IOException {
Random_Amount rAm= new Random_Amount();
int intNumber, intNumber1= 999999,intRangeHigh, intRangeLow, intRangeSum, intTotal= 0, intRan, intHighNum= -99999, intLowNum= 99999;
intNumber= rAm.Amount();
intRangeLow= rAm.Low();
intRangeHigh= rAm.High();
intRangeSum= intRangeHigh - intRangeLow;
int intList[]= new int[intNumber1];
for (int i= 0; i < intNumber; i++) {
intList[intNumber]=intRan= (int)((Math.random()*(intRangeSum)+1)+intRangeLow);
intTotal+=intRan;
if (intRan > intHighNum)
intHighNum= intRan;
if (intRan < intLowNum)
intLowNum= intRan;
}
System.out.println();
rAm.Menu(intLowNum, intHighNum, intTotal, intNumber, intList);
}
private int Amount()throws IOException {
DataInputStream stdin= new DataInputStream(System.in);
String strNum;
int intNum;
System.out.println("Please enter the amount of numbers to calculate.");
strNum= stdin.readLine();
intNum= Integer.parseInt(strNum);
System.out.println();
return intNum;
}
private int Low()throws IOException {
DataInputStream stdin= new DataInputStream(System.in);
String strRangeLow;
int intRangeLow;
System.out.println("Please enter the lowest number you would like to calculate.");
strRangeLow= stdin.readLine();
intRangeLow= Integer.parseInt(strRangeLow);
System.out.println();
return intRangeLow;
}
private int High()throws IOException {
DataInputStream stdin= new DataInputStream(System.in);
String strRangeHigh;
int intRangeHigh;
System.out.println("Please enter the highest number you would like to calculate.");
strRangeHigh= stdin.readLine();
intRangeHigh= Integer.parseInt(strRangeHigh);
System.out.println();
System.out.println();
System.out.println();
return intRangeHigh;
}
private void Menu(int intLowNum, int intHighNum, int intTotalNum, int intAmount, int intArray[])throws IOException {
Random_Amount rAm= new Random_Amount();
String strChoice;
int intChoice;
double dblAverage;
DataInputStream stdin= new DataInputStream(System.in);
DecimalFormat oneDecimal= new DecimalFormat("0.0");
System.out.println("Main Menu");
System.out.println();
System.out.println("Enter 1 to view the random numbers");
System.out.println();
System.out.println("Enter 2 to find the largest number of the random numbers");
System.out.println();
System.out.println("Enter 3 to find the smallest number of the random numbers");
System.out.println();
System.out.println("Enter 4 to find the sum of the random numbers");
System.out.println();
System.out.println("Enter 5 to find the average of the random numbers");
System.out.println();
System.out.println("Enter 6 to find the median of the random numbers");
System.out.println();
System.out.println("Enter 7 to find the mode of the random numbers");
System.out.println();
System.out.println("Enter 8 to end the program");
System.out.println();
strChoice= stdin.readLine();
intChoice= Integer.parseInt(strChoice);
System.out.println();
while (intChoice!=8) {
switch (intChoice) {
case 1: for (int i= 0; i < intAmount; i++)
System.out.println(intArray[intAmount]);
break;
case 2: System.out.println("The largest number is "+intHighNum); break;
case 3: System.out.println("The smallest number is "+intLowNum); break;
case 4: System.out.println("The sum of all the numbers is "+intTotalNum); break;
case 5:
dblAverage= (( double ) intTotalNum) / (( double ) intAmount);
System.out.println("The average of all the numbers is "+oneDecimal.format(dblAverage));
break;
case 6:System.out.println(); break;
case 7: System.out.println(); break;
default: System.out.println("Invalid Input"); break;
}
strChoice= stdin.readLine();
intChoice= Integer.parseInt(strChoice);
}
System.out.println("Sequence ended by User's request");
}
void findMedian(int intArray[], int intArraySize)
{
int i, j, k;
int intMinimum, intTemporary, intDivide, intMedian;
for (i = 0; i < intArraySize-1; i++) {
intMinimum = i;
for (j = i+1; j < intArraySize; j++) {
if (intArray[j] < intArray[intMinimum])
intMinimum = j;
}
intTemporary = intArray[i];
intArray[i] = intArray[intMinimum];
intArray[intMinimum] = intTemporary;
}
if (intArray.length%2 == 0) { //If the length of the array is even
intDivide = intArray.length/2;
intMedian = (intArray[intDivide] + intArray[intDivide-1])/2; //The median is defined as the average of the middle numbers
}
else { //If the length of the array is odd
intDivide = intArray.length/2;
intMedian = intArray[intDivide]; //Defines the median as the middle number
}
System.out.println ();
System.out.println("The median of all random numbers is " + intMedian);
}
}Oh, and if it matters..we are using Java Workshop 2.0 (Have to, stupid school :mad: ) Last edited by Java|Tera; Nov 27th, 2005 at 2:28 PM. Reason: Changed Title |
|
|
|
|
|
#2 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Nov 2005
Posts: 14
Rep Power: 0
![]() |
No no, I mean, don't tell me I'm stupid for doing something wrong..
![]() If something is wrong, please go ahead and tell me. |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
can i make a suggestion?
replace: System.out.println("Enter 7 to find the mode of the random numbers");
System.out.println();with: System.out.println("Enter 7 to find the mode of the random numbers\n");
__________________
Hoes telling me to calm down but I'm like fuck that shit!
|
|
|
|
|
|
#5 | |
|
Newbie
Join Date: Nov 2005
Posts: 14
Rep Power: 0
![]() |
Quote:
|
|
|
|
|
|
|
#6 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
The same thing, pretty much.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
"\n" is a newline character. I'm not up on Java, so the suggestion may or may not be valid. If the output buffer isn't guaranteed to be flushed until a new println appears, then you'd want to leave it as you have it, since the last println would guarantee that the previous message is output. My guess is that your advisor is unaware of the possible implications, also.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Nov 2005
Posts: 14
Rep Power: 0
![]() |
Ok, but I still don't know how to do mode :\
|
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I'll look at your code in detail a little later. You know what mode is, right?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Nov 2005
Posts: 14
Rep Power: 0
![]() |
Yes.
Unfortunately it is due in approximately 16 hours. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|