The following lab was originally assigned but skipped over because we were off for MLK day. I would still like to do it but am just not following what I should be doing. Help?

In this lab you are going to extend the code you wrote in you first lab. You are going to add to the class Coin a variable value that represents the value of the coin in cents (e.g. can be 1, 5, 10, or 25). This value, together with the bias, should be specified as the constructor to the class Coin.

You are also going to create a new class Change that stores an array of Coin objects. The array of coins, which can be of arbitrary size, should be given as input parameter to the constructor of the class Change. Add to the class Change a toString() method that prints the coins in the change. An example return value of the method is “dime nickel tails quarter tails”, which means that the 3rd and 5th coin are tails and their values cannot be seen. Add a method flip(int i) to the class Change that flips the ith coin. Test the class Change by creating a third class called Game. In the main method of this class, create a random change of size 10 coins.

Show the change to the user and print the sum of the coins that are heads. Then allow the user to flip all the coin if they want to do so. If the user decides to flip the coins, show the new values for the coins and the new sum for the head coins. The goal of the game is to the get the highest possible sum for the value of the coins that are heads.

Here is my original lab 01. Where do I go from here?

import javax.swing.JOptionPane; //Import class for use of JOptionPane windows


public class Coin
{
private static double face;
private static double bias = .5;
final static int HEAD = 1;
final int TAILS =2;
private static int value;

public Coin(double b, int v)
{
    bias = b;
    value = v;
}

public static void main(String[] args)  //Main method
{
    int counter = 0;    //Local variable to control for loop
    boolean data;       //Local variable to evaluate outcome of flip method
    //
    int input;          //Local variable to store JOP input     
    //                  Solicits user to run the program
    input = JOptionPane.showConfirmDialog(null, "Would you like to change the bias and run " +
            "the program?", "Coin", JOptionPane.YES_NO_OPTION);
    //
    if (input == 1) //Evaluates user choice as integer value
    {               //JOP closes program based on choice of no option
    JOptionPane.showMessageDialog(null, "The Coin Program Exits. Good Bye!",
            "Program Terminates", JOptionPane.CANCEL_OPTION);
    System.exit(0); //Program terminates
    }
    //
    while (input == 0)  //While loop begins based on yes
    {
        setBias();  //Calls method, passing no arguments
        //
        for (int i = 0; i <10; i++)  //For loop to toss the coin a set # of times
        {
        flip(); //Calls method, passing no arguments
        data = isHead();    //Calls method, assigning to variable result of return
        //
            if (data == true)   //If statement to evaluate is toss is a HEAD
            {
            counter ++;     //If toss is HEAD, increment counter variable
            }
        }   //JOP tells user the result of the program at current bias
        JOptionPane.showMessageDialog(null, "            You tossed HEADS " + counter + " times",
                "Results", JOptionPane.PLAIN_MESSAGE);
        //
        counter = 0;    //Resets counter variable
            //JOP prompts user to run the program again
        input = JOptionPane.showConfirmDialog(null, "Would you like to change the bias and run " +
                "the program?", "Coin", JOptionPane.YES_NO_OPTION);
    }   //Given a choice of no by user, program terminates
    JOptionPane.showMessageDialog(null, "The Coin Program Exits. Good Bye!",
            "Program Terminates", JOptionPane.CANCEL_OPTION);
    System.exit(0); //Program terminates
}//End of Main
//

    public static void flip()   //Method to control tossing of coin
    {
        double calc = 0;    //Local variable to create toss value
        //
        calc = 2 * (Math.random()) * (1 - bias);    //Assigns to calc variable the result of math
        //                                          //formula to generate a random value of toss
        if (calc <= .5)  //If statement evaluates generated number
        {
            face = 1;   //Assigns to face data field an integer value for comparison
        }
        else face = 2;  //Assigns to face data field an integer value for comparison
    }//End of Method
    //
    public static boolean isHead()  //Method for comparing generated number to constant for HEAD
    {
        if (face == HEAD)   //If statement compares generated number to constant for HEAD
            {
            return true;    //Returns true to main method if values are equal
            }
        else return false;  //Returns false to main method if values are false
    }//End of Method
    //
    public static void setBias()    //Method to allow user to change the bias value to HEAD
    {       
        String biasInput;   //Local variable to accept user input from JOP
            //Prompts user to change bias value
        biasInput = JOptionPane.showInputDialog(null, "Input your desired bias value", "Bias Change",
                JOptionPane.INFORMATION_MESSAGE);
        bias = Double.parseDouble(biasInput);   //Parses bias value from string to double type
    }//End of Method
}//End of Class

BTW, I have class Thur. so I would love to finish this before.

Recommended Answers

All 5 Replies

can you lease specify how is your output look like...

i ran it and it shows and ask input your desired bias value...

and return only "Your tossed heads 10" always for any input...

i think you need logic..

Sorry, I posted the wrong version. That was after playing with it and cutting and pasting which I shouldn't have done. Anyhow, here is the original working version.

import javax.swing.JOptionPane; //Import class for use of JOptionPane windows

public class Coin
{
private static double face;
private static double bias = .5;
final static int HEAD = 1;
final int TAILS =2;

public static void main(String[] args)  //Main method
{
    int counter = 0;    //Local variable to control for loop
    boolean data;       //Local variable to evaluate outcome of flip method
    //
    int input;          //Local variable to store JOP input     
    //                  Solicits user to run the program
    input = JOptionPane.showConfirmDialog(null, "Would you like to change the bias and run " +
            "the program?", "Coin", JOptionPane.YES_NO_OPTION);
    //
    if (input == 1) //Evaluates user choice as integer value
    {               //JOP closes program based on choice of no option
    JOptionPane.showMessageDialog(null, "The Coin Program Exits. Good Bye!",
            "Program Terminates", JOptionPane.CANCEL_OPTION);
    System.exit(0); //Program terminates
    }
    //
    while (input == 0)  //While loop begins based on yes
    {
        setBias();  //Calls method, passing no arguments
        //
        for (int i = 0; i <10; i++)  //For loop to toss the coin a set # of times
        {
        flip(); //Calls method, passing no arguments
        data = isHead();    //Calls method, assigning to variable result of return
        //
            if (data == true)   //If statement to evaluate is toss is a HEAD
            {
            counter ++;     //If toss is HEAD, increment counter variable
            }
        }   //JOP tells user the result of the program at current bias
        JOptionPane.showMessageDialog(null, "            You tossed HEADS " + counter + " times",
                "Results", JOptionPane.PLAIN_MESSAGE);
        //
        counter = 0;    //Resets counter variable
            //JOP prompts user to run the program again
        input = JOptionPane.showConfirmDialog(null, "Would you like to change the bias and run " +
                "the program?", "Coin", JOptionPane.YES_NO_OPTION);
    }   //Given a choice of no by user, program terminates
    JOptionPane.showMessageDialog(null, "The Coin Program Exits. Good Bye!",
            "Program Terminates", JOptionPane.CANCEL_OPTION);
    System.exit(0); //Program terminates
}//End of Main
//

    public static void flip()   //Method to control tossing of coin
    {
        double calc = 0;    //Local variable to create toss value
        //
        calc = 2 * (Math.random()) * (1 - bias);    //Assigns to calc variable the result of math
        //                                          //formula to generate a random value of toss
        if (calc <= .5)  //If statement evaluates generated number
        {
            face = 1;   //Assigns to face data field an integer value for comparison
        }
        else face = 2;  //Assigns to face data field an integer value for comparison
    }//End of Method
    //
    public static boolean isHead()  //Method for comparing generated number to constant for HEAD
    {
        if (face == HEAD)   //If statement compares generated number to constant for HEAD
            {
            return true;    //Returns true to main method if values are equal
            }
        else return false;  //Returns false to main method if values are false
    }//End of Method
    //
    public static void setBias()    //Method to allow user to change the bias value to HEAD
    {       
        String biasInput;   //Local variable to accept user input from JOP
            //Prompts user to change bias value
        biasInput = JOptionPane.showInputDialog(null, "Input your desired bias value", "Bias Change",
                JOptionPane.INFORMATION_MESSAGE);
        bias = Double.parseDouble(biasInput);   //Parses bias value from string to double type
    }//End of Method
}//End of Class

again the same....

again the same....

I just ran it several times in Eclipse and got the appropriate outputs.

i just ran it several times in eclipse and got the appropriate outputs.

post your output by printing it at console....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.