java - Bar chart not functioning the way I would want it -


i hoping if can me coding. i'm attempting create bar chart doesn't seem going my. m trying make output below when run [red, yellow, blue](0) repeating. feel i'm close solving this. if can push me in right direction in can appreciated greatly.

import java.util.hashset; import java.util.arrays; import java.util.set; public class test {      /**      * @param args      */     public static void main(string[] args) {         // todo auto-generated method stub         //these arrays not modified , should use these 2 arrays.         short[] points ={1,2,1,1,1,1,1,1,3,4,1,5};         string[] teams ={"red","yellow","blue","blue","blue","red","yellow","red","yellow","red","blue","blue"};         set<string> uniqueteams = new hashset<string>(arrays.aslist(teams));         barchart(points, teams, uniqueteams);     }      public static void barchart(short[] points, string[] teams, set<string> uniqueteams){         byte count=0;         for(int index=0; index < points.length; index++){             if(teams.equals(uniqueteams)){                 count++;             }         }         for(int index=0; index < points.length; index++){             system.out.println(uniqueteams + "("+ count + ")");         }      } }  //output should this: // //red(7): ******* // //yellow(6): ****** // //blue(9): ********* 

i have idea of how way don't know how. if can answer question below. how able or create new array without having doubles of in teams array? array string[] uniqueteams = {"red, "yellow", "blue"}; not initialize or declare create way make program create if makes sense.

you're printing out uniqueteams directly. uniqueteams of type set. should doing cycling through each of items inside set, , printing out stars next them.

oop approach

if me, not have in separate data structures, makes messy code. why not create bar object, contain value , name. need cycle through collection of type bar , call tostring() method`, override.

while i'm here

i figure might give step step oop approach. object oriented programming, want contain similar data in objects. example, name , value of bar great object make; data relates class.

class bar { private int count; private string name;     // values here store count , name of bar.  public bar(string name, int count) {             // assign values in constructor.     this.name = name;     this.count = count; } // override object tostring() method, , replace our code: public string tostring() {     string stars = "";     for(int x = 0; x < count; x++)     {         stars += "*";     }             // create stars string, , append name , count.     return name + ":" + count + " | " + stars; } } 

now access code much, simpler solution. first, create arraylist, barchart store values, , parameterize type bar:

  arraylist<bar> barchart = new arraylist<bar>(); 

then can add test cases in:

  barchart.add(new bar("red", 10));   barchart.add(new bar("blue", 20));   barchart.add(new bar("green", 12)); 

now, because have overridden tostring method, can pass objects system.out.println() function. like:

  for(bar b : barchart) {       system.out.println(b);     } 

output

red:10 | *********
blue:20 | ********************
green:12 | ************


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -