3

OK so my code works for the task at hand. the assignment is to flip a Coin object that is instantiated from a separate Coin class (not shown here). I have written the code correctly so as to calculate the maximum streak of consecutive flips resulting in Heads as the output. I was wondering how I could possibly highlight this streak so when I look the output in the console the streak is visible as it is hard to notice the streak in the list of 100 flips.

here is my code:

public class Runs
{
public static void main (String[] args)
{
final int FLIPS = 100; // number of coin flips
int currentRun =0; // length of the current run of HEADS
int maxRun =0; // length of the maximum run so far
// Create a coin objecti
Coin coin = new Coin();

// Flip the coin FLIPS times
for (int i = 0; i < FLIPS; i++)
{
// Flip the coin & print the result
    coin.flip();
    int flipCount = i + 1;
    System.out.println("Flip " + flipCount +":"+ " " + coin.toString());

// Update the run information
    if (coin.isHeads()==true)
    {

        if (maxRun<currentRun)
        {
        maxRun=currentRun;
        }
        currentRun+=1;
    }

    else
        {
        currentRun = 0;
        }

}
// Print the results
System.out.println("Maximum run of heads in a row! : " + maxRun);
    }
}
kinghenry14
  • 1,187
  • 1
  • 11
  • 34

2 Answers2

1

I'm not 100% sure what you mean by "highlight". You could always print out a few *s before the number if you just want to make it more noticeable. If you're using Eclipse, the easiest way to actually change the text color is to print out the code you would like highlighted with System.err.println(outputToHighlight). It will print it out red. This is the way error messages are usually printed to the console. This would only work in Eclipse though.

Perhaps a better way to solve your issue would be to print out less of the coin flips however!

Memento Mori
  • 3,327
  • 2
  • 22
  • 29
  • Yes this is a good idea, I will use this in Eclipse going forward for debugging. Since this was a HW assignment for Java class I have to leave number of FLIPS at 100 however. Thanks for the outputToHighlight function though, that does look rather useful. – kinghenry14 Mar 12 '13 at 01:47
  • @user2158884 cheers! If you're going to use it though, just remember it's the ".err" instead of ".out" in your print statement that's doing it. I just made up that outputToHighlight variable as the variable that would be printed out red. – Memento Mori Mar 12 '13 at 01:53
  • ok good to know! i thought that was some sort of function, thanks for clearing that up. So essentially the system.err.println is highlighting whatever you are putting in as the argument to this function as if it was an error then? – kinghenry14 Mar 12 '13 at 01:57
  • @kinghenry14 Yup exactly! It's printing out your arguments as if it were an error. And in Eclipse, errors are printed out red by default. – Memento Mori Mar 12 '13 at 01:58
1

Rather than "highlight" the output, which may be device/OS specific, output a mini report of where it occurred and how long it was.

Here's how the code could look (I've simplified it for you too - see notes in code):

int maxRun = 0;
int currentRun = 0;
int runStart = 0;

for (int i = 0; i < FLIPS; i++) {
    coin.flip();
    System.out.println("Flip " + (i+1) +": " + coin); // toString() is redundant

    if (coin.isHeads()) { // never compare a boolean with a boolean constant, just use it
        currentRun++; // use ++ in preference to +=1, and this should be before maxRun test
        if (maxRun < currentRun) {
            maxRun = currentRun;
            runStart = currentRun - i; // this will produce a 1-based position
        }
    } else {
        currentRun = 0;
    }
}

System.out.println("Largest run was " + maxRun + " long, starting at " + runStart);
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Thank you. Looking at someone else's edits always help. i know that when you do println() whatever is in there is automatically converted to a string, but i suppose out of habit i used the toString method there. THanks for also pointing out the boolean comparison to the constant, i thought it would work without putting ==true but didn't double check just went with what I had. I like the runStart implementation also, that makes it a bit easier to follow. – kinghenry14 Mar 12 '13 at 01:51