Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: How do I do this code? please help with full code

  1. #1
    Junior Member
    Join Date
    Jan 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How do I do this code? please help with full code

    Starting with Player 1, each player gets a turn answering 5 trivia questions (there are 10-questions, 5 for each player). When a question is displayed, four possible answers are also displayed. Only one of the answers is correct, and if the player selects the correct answer, he or she earns a point.

    After answers have been selected for all of the questions, the program displays the number of points earned by each player and declares the player with the highest number of points the winner.

    You are to design a class called Question to hold the data for a trivia question.
    The Question class should have String fields for the following data:

    A trivia question
    Possible answer-1
    Possible answer-2
    Possible answer-3
    Possible answer-4
    The number of the correct answers (1, 2, 3, or 4)

  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: How do I do this code? please help with full code

    It would be better if you attempted writing this class yourself.

    Post what you come up with, including any compiler messages that you can't understand.

  3. #3
    Junior Member
    Join Date
    Jan 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I do this code? please help with full code

    this is what i have for the question class

    public class Question
    {
    // Constant for the number of questions
    public final int NUM_ANSWERS = 10;

    // The trivia question
    private String questionText;

    // An array to hold possible answers.
    private final String possibleAnswers[] =
    new String[NUM_ANSWERS];

    // The number (1, 2, 3, or 4) of the
    // correct answer.
    private int correctAnswer;

    /**
    No-arg constructor
    */
    public Question()
    {

    // Initialize all fields to "" or 0;




    }

    /**
    setQuestion method
    @param question The question text.
    */
    public void setQuestion(String question)
    {
    // Add one statement
    }

    /**
    setPossibleAnswer method
    @param text The answer text.
    @param num The answer num.
    */
    public void setPossibleAnswer(String text, int num)
    {
    // Add one statement
    }

    /**
    setCorrectAnswerNum method
    @param num The correct answer number.
    */
    public void setCorrectAnswerNumber(int num)
    {
    // Add one statement
    }

    /**
    The getQuestionText method returns this object's
    question text.
    @return This object's question text.
    */
    public String getQuestionText()
    {
    return null;
    // Add one statement
    }

    /**
    The getPossibleAnswer method returns one of the
    possible answers to this object's question.
    @param num The answer number.
    @return The possible answer specified by num.
    */
    public String getPossibleAnswer(int num)
    {
    return null;
    // Add one statement
    }

    /**
    getCorrectAnswerNumber method
    @return The number of the correct answer.
    */
    public int getCorrectAnswerNumber()
    {
    return 0;
    // Add one statement
    }

    /**
    getCorrectAnswser method
    @returns the correct answer.
    */
    public String getCorrectAnswer()
    {
    return null;
    // Add one statement
    }
    }

    --- Update ---

    this is what i have for the player class

    import java.util.Scanner;

    public class Player
    {
    private int playerNumber1;
    private int playerNumber2;// The player number
    private int points; // Player's points
    private int currentAnswer; // Current chosen answer

    /**
    Constructor
    @param playerNum The player number.
    */
    public Player(int playerNum)
    {
    //Initialize playerNumber and points
    points= 0;
    playerNumber1 = points;
    playerNumber2 = points;
    }

    /**
    The chooseAnswer method gets the player's chosen
    answer to the current question.
    */
    public void chooseAnswer()
    {
    // Create a Scanner object for keyboard input.
    Scanner keyboard = new Scanner(System.in);

    // Get the user's chosen answer.
    System.out.print("Enter the number of the correct answer: ");
    int answer = keyboard.nextInt();// Add one statement to read player's answer
    System.out.println(answer);
    }

    /**
    getCurrentAnswer method
    @return The current chosen answer.
    */
    public int getCurrentAnswer()
    {
    // Add one statement
    }

    /**
    The incrementPoints method increments the player's points.
    */
    public void incrementPoints()
    {
    }

    /**
    getPoints method
    @return The player's points.
    */
    public int getPoints()
    {
    return 0;
    // Add one statement
    }

    }

    --- Update ---

    this is what i have for the trivia game class

    public class TriviaGame
    {
    public static void main(String args[]) throws IOException
    {
    // Constants
    final int NUM_QUESTIONS = 10;
    final int NUM_PLAYERS = 2;

    // Variables
    int playerTurn = 1; // The current player
    int questionNum; // The current question number
    int playerAnswer; // The player's chosen answer
    int player1points = 0; // Player 1's points
    int player2points = 0; // Player 2's points

    // Create an array of Player objects for
    // player #1 and player #2 and create the players of the // array using a for loop




    // Create an array to hold Question objects.


    // Initialize the array with data with a method.


    // Play the game.
    for (int i = 0; i < NUM_QUESTIONS; i++)
    {
    // Display the question.
    // Add one statement

    // Get the player's answer.
    // Add one statement

    // See if the correct answer was chosen.
    if ( <boolean expression> )
    {
    // Code if the player's chosen answer is correct.
    // Add two statements
    }
    else
    {
    // Code if the player chose the wrong answer.
    // Add one statement
    }

    // Switch players for the next iteration.
    if (<boolean expression>)
    // Add one statement
    else
    // Add one statement
    }

    // Show the game results by calling a method.

    }

    /**
    The initQuestions method uses the contents of the
    trivia.txt file to populate the qArray parameter
    with Question objects.
    @param qArray A Question array
    */
    public static void initQuestions(Question qArray[]) throws IOException
    {
    // Open the trivia.txt file.
    File file = new File("trivia.txt");
    Scanner inputFile = new Scanner(file);

    // Populate the qArray with data from
    // the file.
    for (int i = 0; i < qArray.length; i++)
    {
    // Create a Question object in the array.
    // Add one statement

    // Get the question text from the file.
    // Add one statement

    // Get the possible answers.
    for (int j = 1; j <= 4; j++)
    {
    // Add one statement}

    // Get the correct answer.
    // Add one statement
    }
    }

    /**
    The displayQuestion method displays a question.
    @param q The question to display.
    @param playerNum The current player number.
    */

    public static void displayQuestion(Question q, int playerNum) {
    // Display the player number.
    System.out.println("Question for player # 1" + playerNum);
    System.out.println("------------------------");

    // Display the question.
    // One println for the Question
    // and a for loop (with a println) for the possible answers




    }

    /**
    The showGameResults method shows the game results.
    @param players The array of Player objects.
    */
    public static void showGameResults(Player[] players)
    {
    // Display the stats.
    System.out.println("Game Over!");
    System.out.println("---------------------");
    // Add two println statements


    // Declare the winner. if -- else if ---- else

    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,223
    Thanks
    53
    Thanked 2,385 Times in 2,339 Posts

    Default Re: How do I do this code? please help with full code

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: How do I do this code? please help with full code

    One step at a time!

    public class Question
    {
    // Constant for the number of questions
    public final int NUM_ANSWERS = 10;
     
    // The trivia question
    private String questionText;
     
    // An array to hold possible answers.
    private final String possibleAnswers[] = new String[NUM_ANSWERS];
     
    // The number (1, 2, 3, or 4) of the correct answer.
    private int correctAnswer;
     
    /**
    No-arg constructor
    */
    public Question()
    {
     
        // Initialize all fields to "" or 0;
     
     
     
     
    }
     
    /**
    setQuestion method
    @param question The question text.
    */
    public void setQuestion(String question)
    {
        // Add one statement
    }
     
    /**
    setPossibleAnswer method
    @param text The answer text.
    @param num The answer num.
    */
    public void setPossibleAnswer(String text, int num)
    {
        // Add one statement
    }
     
    /**
    setCorrectAnswerNum method
    @param num The correct answer number.
    */
    public void setCorrectAnswerNumber(int num)
    {
        // Add one statement
    }
     
    /**
    The getQuestionText method returns this object's question text.
    @return This object's question text.
    */
    public String getQuestionText()
    {
        return null;
        // Add one statement
    }

    (Note you can edit your original post to add the [code] ... [/code] tags as Norm suggests).

    I am guessing you are supposed to work through the comments. So the first step would be "Initialize all fields to "" or 0;". Do you know what the fields are? Do you know how to assign "" or 0 to them? If so, try that and post any compiler messages you get. edit: as well as the updated code for the Question class. edit2: as you can see from my botched attempt you may have to put some of the formatting back in when you add the code tags.

  6. #6
    Junior Member
    Join Date
    Jan 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I do this code? please help with full code

    Hi, new member. I need help for with full java code for the following Problem:
    Problem: Given string data representing a mathematical expression, determine the possible location(s) of the missing parenthesis. Although we show spaces between characters for readability the input strings have no spaces. All operands will be single digits.
     
    Given (2+3*6+1. A right parenthesis is missing. It could be correctly places in several locations. 
     
    (2+3)*6+1 Location 5
    (2+3*6)+1 Location 7
    (2+3*6+1) Location 9
     
    Input: There will be five lines of input. Each line will contain a string of characters with no spaces representing a mathematical expression. Each expression will have either a single left or right parenthesis. The operators used will be: +, -, * and /.
     
    Output: For each line of input, list all the locations in that expression where the missing left or right parenthesis can be correctly placed. Note: Single digits are never enclosed.
     
    Sample Input         Sample Output
    1. (2+3*6+1           1. 5,7.9
    2. 2-5*(6+1           2. 9
    3. 5+5-2)*5           3. 1,3
    4. 3*5 + (8/4*2       4. 9,11
    5. 2+8/4*5)           5. 1,3,5

  7. #7
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: How do I do this code? please help with full code

    Please don't hijack other threads. Discussion for this is better at Java code math problem to find missing paranthesis

    Similar advise applies, however: one step at a time, post some code and say what happens.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,223
    Thanks
    53
    Thanked 2,385 Times in 2,339 Posts

    Default Re: How do I do this code? please help with full code

    Also posted here: Helping with Code
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: August 2nd, 2017, 01:28 PM
  2. Replies: 4
    Last Post: January 24th, 2013, 10:20 AM
  3. Replies: 7
    Last Post: January 24th, 2013, 09:41 AM
  4. Replies: 5
    Last Post: November 14th, 2012, 09:47 AM
  5. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM