Question: why are some of your boolean statements set up with a variable setter, rather than a regular boolean setter? I'm not sure about your code... are there any other classes that you are leaving out? Any methods?
Also, you should get Eclipse. It points out your errors immediately, offers suggestions to improve the flow of your code, and does most of the constructor/menial work for you.
Doesn't an array need a Constant variable for its size, Haven't used Java in a While but what about declaring variable types such as int and boolean...
sorry had 1 minute before bell and even the teacher couldn't explain why it wouldn't work.
The whole code uses a previous set class called Expo for random and scanner, it was already in so why change it?
import java.util.Arrays;
public class Lab12MATH080 { public static void main(String args[]) { System.out.println("\ Lab12MATH08\ " System.out.print("Enter the quantity of random numbers ===>> " int listSize = Expo.enterInt(); System.out.println(); Statistics intList = new Statistics(listSize); intList.randomize(); intList.computeMean(); intList.computeMedian(); intList.computeMode(); intList.displayStats(); System.out.println(); } }
class Statistics extends Lab12MATH080 { private int list[]; private int size; private double mean; private double median; private double medianIndex; private double medianLowIndex; private double medianHighIndex; private int mode; private int modeLookUp; private int modeLookUp2; private boolean found = false; public Statistics(int s) { size = s; list = new int[s]; } public void randomize() { Expo.startSeed(12345); for (int k = 0; k < size; k++) { int rndNumber = Expo.seedRandom(10,30); list[k] = rndNumber; } } public void computeMean() { for(int i = 0; i < size; i++) { mean += list[i]; } mean /= size; }
it gives me the correct number of equal numbers. All i need is a way to println the actual index value.
ex:
see there's two 17's so the mode = 1 when it has mode++.
I need to get it to say 17, but when i put mode = list[modeLookUp]; i get an arrays out of bounds exception error. when i put a stop to the for loops with a while and boolean I always get mode = 0
Uhh... you got me there. But what type of compiler are you using? Cause in "my" Java, to move to the next line, you need to do the escape sequence, which instead of "\\" is "\ ". Either way, i was looking for syntax errors. I didn't know what you were trying to find.
if(list[modeLookUp] == list[modeLookUp2]); { found = true; System.out.print(list[modeLookUp]) }
I was wondering how are you stopping the for loop...it isn't being told to stop so it will run through the entire array and change the mode value...Dunno maybe its 0 because your going out of bounds of the array...the last number that is indexed, so you get an error your function stops and it returns a 0
HecticHermit: There's a flaw with that, I think, because if you do that, every time it goes through the array to check the mode it will print out whatever number correlates, while I think Graham just wants it printed out once at the end.
Graham, the problem with the first set of code is that if you have multiple repeated numbers, mode will equal whatever the final repeated number is, whether it is the mode or not. If our array had {0, 1, 1, 1, 0}, your original code would end up with mode=0.
Your second program I presume has mode=0 at the beginning, and when a number is found to match it will add 1 to mode. Problem is, if you use {0, 1, 1, 1, 0} as your array, mode=10 by the time you're through with the code.
Here's a corrected write of your code. I have added the variables "incidences," which acts as a counter for the number of times a number appears in the array, "maxIncidences," which stores the greatest number of repeated values, and "x," which acts as a disposable value for each value in the array. What it does is that it will start with the first value in the array and record the number of incidences of the value through the whole array. Then, it will check to see whether this incidence is larger than the greatest incidence thus far. If it is, maxIncidence is given the value of the new incidence, and the new mode is the value in that array. Then, it will head to the second value, rinse and repeat.
public void computeMode() { while(found = false) { for (modeLookUp = 0; modeLookUp < size; modeLookUp++) { int incidences = 0; for (modeLookUp2 = 0; modeLookUp2 < size; modeLookUp2++) { if (list[modeLookUp2] == list[modeLookUp1]) incidences++; } if (incidences > maxIncidences) { maxIncidences = incidences; mode = list[modeLookUp]; } } found=true; } }
Well he also wants the number that repeats to be printed out...I wasnt working on it long...havent programed anything in a while and Im still a beginner anyway..Parsat you program? didnt know that....maybe I should start something....