6 Replies - 2461 Views - Last Post: 12 December 2010 - 12:51 PM Rate Topic: -----

#1 moneycode   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-December 10

Help: Display data in histogram + declare number ranges as variable

Posted 11 December 2010 - 05:12 PM

I am completely stuck on the following example I saw in a text book (the beginning java objects) i borrowed from the library:

A research institution wants to make a graphical display to show how many high blood pressure patients were at a different stage of BP (in systolic mm Hg) at a certain time.
The program should let you enter in the different heart beat rates until the entered value exceeds 230 (the maximum value in the blood pressure chart).
The program should display a histogram and like the following:
140-159 ********
160-179 ***
180-209 ***
210-230 *
I used 15 patients (the question had like ~50) in this case and the stars stand for one each.


I like to do some random programming in my spare time but this one just confuses me. How would I declare the variables as '140-159' etc. because all I have seen were either letters or words. So basically I'm lost from the first moment.

I would ask my IT teacher but he is not really a Java Expert, so I googled a bit and got to this forum.

I have tried to rewrite the '140-159' to 'a' and so on make life easier but it just doesn't seem to work. And I have no clue on how to convert the number of each to be displayed in stars like above.


package javaapplication1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


/**
 *
 * @author moneycode
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)throws IOException {
       
        // TODO code application logic here
              
     int a;
     int b;
     int c;
     int d;

     a=0;
     b=0;
     c=0;
     d=0;

     int marks;
     BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
        System.out.println("Enter marks of student: ");
        marks = Integer.parseInt(br.readLine());

        if (159>=marks && marks>=140)
            a = a+1;
        if (179>=marks && marks>=160)
            b = b+1;
        if (180>=marks && marks>=209)
            c = c+1;
        if (210>=marks && marks>230)
            d = d+1;
        
        
        
        System.out.println(a);
        System.out.println(B)/>;
        System.out.println(c);
        System.out.println(d);




I know the code looks very incomplete with many gaps but these are the points where I'm stuck. The book didn't have example on these type of questions :/

Key questions:

-How do I declare a number as variable?
-How do I create a loop in this example?
-How do I display the counted number of the variables as 'stars'.



This question has been in my mind for the past week or so and I need to return the book just before Christmas so I would like to get this example and understand it before i have to give it back.

Anyway, I would really appreciate any help. If you don't want to post a code or a hint to this thread you can PM me the code or the instructions.

This post has been edited by macosxnerd101: 11 December 2010 - 05:46 PM
Reason for edit:: Please keep all correspondance via the forums. Do not use the PM system to collaborate.


Is This A Good Question/Topic? 0
  • +

Replies To: Help: Display data in histogram + declare number ranges as variable

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Help: Display data in histogram + declare number ranges as variable

Posted 11 December 2010 - 05:58 PM

Rather than declaring your variable, then initializing it in a separate statement as follows:
int a;
a = 0;



It might be easier just to say: int a = 0; for your declaration and initialization.

As for the loops, I'd use two key loops here: a while loop and a for loop. A while loop is used when you want to iterate until a certain condition, and a for loop is used generally to make a set number of iterations.

The basic syntax of the while and for loops are as follows:
while(someBooleanCondition){
   //code]
}

for(var-declaration and initialization; condition; var-modification){
   //code
}



An example of each:
boolean condition = true;

//as long as the condition variable is true
while(condition){

      //get input
     String x = JOptionPane.showInputDialog("Play again?");

      //if the user doesn't want to play agin
     if(x.equals("No"))
           //then condition becomes false, causing the loop to terminate
          condition = false;

      //otherwise, the loop repeats
}

//from 0 through 19, print out i on each iteration
for(int i = 0; i < 20; i++){
    System.out.println(i);
}



Now as they are applicable to your program, I'd use a while loop when processing your File as you don't know how many elements are in it. So the logic I'd use:
String line <-- null;
while((line <-- readLine()) != null){
    convert line to an int
    increment the appropriate counter variable
}



Now as for a for loop, I'd iterate from 0 to each counter variable, and System.out.print() a * on each iteration. Then after each loop, use System.out.println() to go to the next line.

Quote

How do I declare a number as variable?

Can you clarify this some? I'm not sure what you're asking here.

Also, you may want to check out the Java Tutorials Section for some good beginner tutorials. I'd personally recommend NeoTifa's Basic Java for N00blets tutorial as a starting point.

Hope this helps some! :)
Was This Post Helpful? 0
  • +
  • -

#3 moneycode   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-December 10

Re: Help: Display data in histogram + declare number ranges as variable

Posted 11 December 2010 - 06:09 PM

Thanks that looks awesome, I will have a go at it.

Oh by the declaring the number as variable bit, I meant how can I declare the range '140-159'as a variable so I can count how many patients have Blood pressure level between 140-159.

Do you know on how to display your output as stars?

say you have:

int a;
a = 4;

System.out.Println(a)



This is a small example. So what will I have to do to display 4 stars (*) instead of the value 4?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Help: Display data in histogram + declare number ranges as variable

Posted 11 December 2010 - 06:18 PM

You would have to look more into Object-Oriented programming to accomplish the range feature. It's probably beyond the immediate scope of your assignment, but something you should be covering after you get through Strings and arrays.

As for your question, you should use a for loop. The variable declaration part should be 0, the condition should be the same as in my demo, only using your variable rather than a number. As for the rest, read the excerpt from my last post.

Quote

Now as for a for loop, I'd iterate from 0 to each counter variable, and System.out.print() a * on each iteration. Then after each loop, use System.out.println() to go to the next line.


Also here, Println is lower-cased, and you need a semi-colon at the end of the statement.
System.out.Println(a)



Hope this helps some. :)
Was This Post Helpful? 0
  • +
  • -

#5 moneycode   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-December 10

Re: Help: Display data in histogram + declare number ranges as variable

Posted 12 December 2010 - 12:38 PM

Hi, there I have done this so far:

package javaapplication1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;





/**
 *
 * @author moneycode
 */
public class Main {
    private static String count;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
       
        // TODO code application logic here
   final int SENTINAL = 230;
   int marks;
   int a=0;
   int b=0;
   int c=0;
   int d=0;
        
        
        

        Scanner in = new Scanner( System.in );

        
        System.out.print("Enter the blood pressure of patient, stop when bp exceeds 230 : ");
        marks = in.nextInt();


        while ( marks < SENTINAL )
        {
            if (marks>=140 && marks<=159)
                a=a+1;
            if (marks>=160 && marks<=179)
                b=b+1;
            if (marks>=180 && marks<=209)
                c=c+1;
            if (marks>=210 && marks<=230)
                d=d+1;
                        
            System.out.print("Enter marks : ");
            marks = in.nextInt();
        }

        System.out.println("140-159 " + a);
        System.out.println("160-179 " + B)/>;
        System.out.println("180-209 " + c);
        System.out.println("210-230 " + d);

    }

}




This brings out something like like:

140 - 159 4
160 - 179 6
180 - 209 3
210 - 230 2


How would I make it look like the following:

140 - 159 ****
160 - 179 ******
180 - 209 ***
210 - 230 **

Thanks in advance.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Help: Display data in histogram + declare number ranges as variable

Posted 12 December 2010 - 12:41 PM

Are you reading my posts? You haven't used a for loop as I suggested twice. I provided a demo of a for loop in one of my posts, and linked to a tutorial that covered for loops.
Was This Post Helpful? 0
  • +
  • -

#7 m-e-g-a-z   User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,457
  • Joined: 19-October 09

Re: Help: Display data in histogram + declare number ranges as variable

Posted 12 December 2010 - 12:51 PM

I would make a method, something like this...

	public String stars(int a) {

		StringBuilder st = new StringBuilder();	

		for(int i=0;i<a;i++){

			st.append("*");
		}
		return st.toString();
	}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1