Author Topic: How to find the value of an element in an array? [Java]  (Read 1181 times)

   I have a slow paced, badly taught computer science class in which we use "JCreator" as our JRE. The class is pretty easy despite being poorly taught, as all of the content was taken from another teacher from another school, and the teacher shows signs of not knowing what she's talking about, but that's not important.

   I came to an area in the content that was written very peculiarly and i can't seem to understand it. What i need to do for an assignment is to find the mean, median, and mode of some arrays in order to display that I learned from the passage, which isn't making much sense. I don't really know where to start. I'll post the code by itself to give you some context on what i'm trying to find out. I'd appreciate it if any of you could help me with this. (I'm not asking you to do it, i'm really just trying to figure it out). What exactly does toString and fill do, and
is list[index] actually just an identifier/variable or is the "index" between brackets a reserved word?
Code: [Select]
import java.util.Arrays;
import java.util.Random;


public class TextLab06st
{

public static void main(String args[])
{
System.out.println("\nTextLab06\n");
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
{

private int list[]; // the actual array of integers
private int size; // user-entered number of integers in the array
private double mean; // used for the  80, 100 and 110 point versions
private double median; // used for the 100 and 110 point versions
private int mode; // used for the 110 point version only

public Statistics(int s)
{
size = s;
list = new int[size];
mean = median = mode = 0;
}

public void randomize()
{
// This provided method creates the same exact list of "random" numbers for every execution.
// You will learn more about this in Chapter 14.  For now just use the provided method.
Random rand = new Random(12345);
for (int k = 0; k < size; k++)
list[k] = rand.nextInt(31) + 1;  // range of 1..31
}

public void computeMean()
{


}

public void computeMedian()
{


}

public void computeMode()
{
// precondition: The list array has exactly 1 mode.


}

public void displayStats()
{
System.out.println(Arrays.toString(list));
System.out.println();
System.out.println("Mean:    " + mean);
System.out.println("Median:  " + median);
System.out.println("Mode:    " + mode);
}

}






(This is probably the wrong place for this thread, but I wasn't sure where else to put it. It was either this or mod discussion, but I don't remember if mod discussion was exclusively blockland or not. It's been a little while).


« Last Edit: April 24, 2014, 01:25:45 PM by KadeBL_ID12958 »


I feel like this would logically belong in coding help, but I guess its kinda hard to tell.

You can probably make an assumption base on the names of the functions "toString" and "fill" do.
toString will return a string that textually represents an object; what exactly a textual representation of an object is, depends on the object.
Code: [Select]
static String toString(int[] a)
Returns a string representation of the contents of the specified array.
fill is used to set an array of something, to a specific value.
Code: [Select]
static void fill(int[] a, int val)
Assigns the specified int value to each element of the specified array of ints.
 
static void fill(int[] a, int fromIndex, int toIndex, int val)
Assigns the specified int value to each element of the specified range of the specified array of ints.

I assume in the context of the code/explanation that list[index] is just a variable "list" which represents an array of size "index". Index is not a magic word last time I checked. If that helps any.



That clears up a lot, thanks. I was assuming something along those lines, but it seemed a little blurry because of the way the text reads.



This too.

For some stupid reason a lot of explanatory books/etc designed to teach higher level concepts/applications (Computer Science/Physics/Chemistry/Calculus) Like to be cripplingly terrible at explaining things in simple English. Because clearly there was a reason the people who wrote them are not English professors.

Yeah, my class's content was written by a Mr. Leon Schram and his kid-- to understand any of it I have to do exercises which ask questions in plain english, however with this unit, i couldn't finish them all because it was worded so strangely.

I found out what was frustrating me.

My problem here is that I don't know how to get the sum of all the elements of the array. I've searched around a lot and I can't find a direct answer. This assignment calls for 3 functions that require I use the sum of all the elements, yet I can't find out how. The content doesn't say anything about this. I mean i suppose i can call it individually like
Code: [Select]
list[1] + list[2] + list[3] but seeing as there are 20 elements, something tells me I need a more efficient way to add them all together.

Everything says to use a for loop but that only seems to work for the length? I'm missing something.
« Last Edit: April 24, 2014, 05:06:10 PM by KadeBL_ID12958 »