PDA

View Full Version : Help for uni project. Please I'm going crazy!!!


andrewsco
10-07-2004, 11:19 PM
I have a project that i need a bit of help with. I have done the first bit, but am unsure on the second bit.

Here is the problem:

Exercises 3 and 4. Write a Java program to

1. Read three integers, say n, bottom and top, from the input stream;
2. Read n ‘double’ numbers and count how many of them fall in each of the intervals (-,bottom), (bottom,bottom+1), ..., (top-1,top), (top,); store these counts (frequencies) in an array with top-bottom+2 elements;
3. Display the observed frequencies in a histogram, using an appropriate number of ‘*’ for each (horizontal) bar. For example, if n is 20, bottom is 1, top is 4, and the observed frequencies are 5, 0, 8, 3, 4, the histogram may appear as follows (you can decide how to label the bars, and what title or caption to include):

(-inf,1) | *****
(1,2) |
(2,3) | ********
(3,4) | ***
(4,inf) | ****


Exercise 3 consists of steps 1 and 2 above (print the resulting array); exercise 4 is step 3.






import java.io.*;
import javagently.*;
public class Frequency
{
public static void main() throws IOException
{

Stream input = new Stream (System.in);
int n = 0; //Stores how many numbers to be inputted
int bottom = 0; //stores min number
int top = 0; // stores max number
int i = 0; // used for loop variables
double newestNum = 0; // variable for temp. storing doubles as they are read in


System.out.print("How many numbers would you like to input: ");
n = input.readInt();


System.out.print("Please enter the upper bound: ");
top = input.readInt();


System.out.print("Please enter the lower bound: ");
bottom = input.readInt();


if(bottom>top)
{
System.out.print("Value for bottom must be less than that of top");
System.exit(0);
}

int [] freq = new int [top-bottom+2]; //an array to store all possibilites for graph

for (i = 0; i < n; i++) // will read in n numbers
{
System.out.print("Please enter a number: ");
newestNum = input.readDouble();


if (newestNum < bottom)
{
freq [0]++;
}

else if (newestNum >= top)
{
freq [top-bottom+1]++;
}
else
{
freq [(int)newestNum-bottom+1]++; //If you dont know how this works write out an example with say bottom = 1, top = 4 }
}

for (i=0; i < freq.length; i++)
System.out.print(freq[i] + " ");

}
}


I'm not too sure on how to get the intervals to be printed? I was thinking on similar lines to above, ie having a for loop, with the array initialized to [0], if it is print (-inf, bottom) if its [top-bottom+1] then it is (top, inf) but I cant get my head around the middle bit. Would I need two vaklues, say x and y, and need to compare them, or is there an easier way? Some code would be VERY appreciated, as I am tearing my hair out!

Andy

Jason
10-08-2004, 07:31 PM
we don't do homework assignments here we are here to help....here is an idea I don't think it works though, I haven't done java in a long time...

//print historgram rough idea
int freq_place = 0;
int bottom_to_top = bottom;
System.out.println("(-inf,"+bottom_to_top + ") |"+showStars(frequency[freq_place]));
bottom_to_top++;
freq_place ++;
do{
System.out.println("("+bottom_to_top +", " + (bottom_to_top+1) +") |" + showStars(frequency[freq_place]));
bottom_to_top++;
freq_place ++;
}while(bottom_to_top != top)
System.out.println("("+bottom_to_top + ", inf) |"+showStars(frequency[freq_place]));



//method for printing the number of stars courtesy of Parmeisan
private String showStars(int numToShow)
{
String output = null;
for (int i = 0; i < numToShow; i++)
{
output += "*";
}
}




Jason

andrewsco
10-09-2004, 05:51 PM
Hi.

Thanks for the reply.

I didnt ask anyone to 'do' my homework for me, in fact thats not what I wanted as I want to learn how to do it. I do however want to be able to do it, and sometimes a bit of code helps in that respect, but thanks for the post and il have a browse through it on the compiler and see what I can do.

Sco