-2

so im super stuck.. im doing my toString() method and it needs to show a bar graph with stars that correlate with the number of grades. ex.

how it should look if 5 As, 3 Bs, 3 Cs, 2 Ds, 1 F,

*****A

***B

***C

**D

*F

my teacher gave me a start, but i have no idea what to do besides concatenating the single variable thats been given. keep in mind I'm learning still and haven't learned the other ways such as string building or arrays

public class GradeDistribution {
private int mNumberAs;
private int mNumberBs;
private int mNumberCs;
private int mNumberDs;
private int mNumberFs;

public GradeDistribution(int numberOfAs, int numberOfBs,
                            int numberOfCs, int numberOfDs,
                            int numberOfFs)
{
    mNumberAs = numberOfAs;
    mNumberBs = numberOfBs;
    mNumberCs = numberOfCs;
    mNumberDs = numberOfDs;
    mNumberFs = numberOfFs;

}

public GradeDistribution()
    {
        mNumberAs = 0;
        mNumberBs = 0;
        mNumberCs = 0;
        mNumberDs = 0;
        mNumberFs = 0;
    }
public void setAllGrades(int A,int B, int C, int D, int F)
{
    mNumberAs = A;
    mNumberBs = B;
    mNumberCs = C;
    mNumberDs = D;
    mNumberFs = F;
}
public void setNumberAs( int A)
{
    mNumberAs = A;
}
public void setNumberBs(int B)
{
    mNumberBs = B;
}
public void setNumberCs(int C)
{
    mNumberCs = C;
}
public void setNumberDs(int D)
{
    mNumberDs = D;
}
public void setNumberFs(int F)
{
        mNumberFs = F;
}
public int getNumberOfGrades()
{
    return mNumberAs + mNumberBs + mNumberCs + mNumberDs + mNumberFs;
}
public int getPercentAs()
{   double totalGrade = mNumberAs + mNumberBs + mNumberCs + mNumberDs + mNumberFs;
    double averageAs = (mNumberAs / totalGrade * 100);
    return (int)averageAs;
}
public int getPercentBs()
{
    double totalGrade =  mNumberAs + mNumberBs + mNumberCs + mNumberDs +      mNumberFs;
    double averageBs = (mNumberBs / totalGrade * 100);
    return (int)averageBs;
}
public int getPercentCs()
{
    double totalGrade =  mNumberAs + mNumberBs + mNumberCs + mNumberDs + mNumberFs;
    double averageCs = (mNumberCs / totalGrade * 100);
    return (int) averageCs;
}
public int getPercentDs()
{
    double totalGrade =  mNumberAs + mNumberBs + mNumberCs + mNumberDs + mNumberFs;
    double averageDs = (mNumberDs / totalGrade * 100);
    return (int) averageDs;
}
public int getPercentFs()
{
    double totalGrade =  mNumberAs + mNumberBs + mNumberCs + mNumberDs +     mNumberFs;
    double averageFs = (mNumberFs / totalGrade * 100);
    return (int)averageFs;
}
public String toString()
{
    String output = "";
    for(int a = 1; a <= mNumberAs; a++)
    {

}
}
}
Nam Duong
  • 23
  • 3

1 Answers1

0

In your for loop you are iterating over the number of As that were given. So you can append * to your string as you iterate. output = output + "*"; When the loop is done, append an A and a new line \n, and then do the same for Bs, Cs, etc:

String output = "";
for(int a = 1; a <= mNumberAs; a++) {
    output = output + "*";
}

output = output + "A\n";

// do the same for the number of Bs, Cs, etc
Matt
  • 3,677
  • 1
  • 14
  • 24
  • can you explain it a bit for me? because I'm less curious about the answer and more curious about how it works. your solution does work though! – Nam Duong Mar 23 '17 at 07:16
  • it just appends the character `*` to the string in a for loop, like `output = '*' + '*' + '*' + '*' + 'A';` there are also different ways to do the same, but this is quite easy to write and understand I guess :) – xander Mar 23 '17 at 07:19
  • You can use this for generating the specific number of `*'s ` http://stackoverflow.com/questions/2255500/can-i-multiply-strings-in-java-to-repeat-sequences – avlec Mar 23 '17 at 07:23
  • When you define a `for` loop you can specify an iterator (in this case `a`), and you can give it a starting value (in this case `1`), and a constraint (while `a <= mNumberAs`) and something to do on each iteration (increment a with `a++`). So each time the loop runs, we set `output` to it's current value concatenated with a new character `*`. The loop stops when the constraint no longer holds (we have incremented `a` to a value greater than `mNumberAs`), we will have gone through 5 times and `output` will now contain 5 `*` characters. – Matt Mar 23 '17 at 08:07