Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Need help to code these with java!!!

  • 16-09-2010 11:45am
    #1
    Closed Accounts Posts: 11


    Am learning how to use this language and 've been given these problems to do. thing is, i just started this last week, and have barely got past loops. Help, anyone???
    1)A Pythagorean triplet is a set of three natural numbers, a < b < c, for which a2 + b2 = c2
    For example, 32 + 42 = 9 + 16 = 25 = 52.
    There exists exactly one Pythagorean triplet for which a + b + c = 1000. Your output should
    look like this (of course with the right numbers, 10, 20 and 30 are obviously not a valid
    solution)
    The Solution is a=10, b=20, c=30.
    2)The sum of the squares of the first ten natural numbers is,
    1^2 + 2^2 + ... + 10^2 = 385
    The square of the sum of the first ten natural numbers is,
    (1 + 2 + ... + 10)^2 = 55^2 = 3025
    Hence the difference between the sum of the squares of the first ten natural numbers and
    the square of the sum is 3025 - 385 = 2640.
    Find the difference between the sum of the squares of the first one hundred natural
    numbers and the square of the sum. Your output should look like this. (Replace 10, 20 and
    30 with your right answer!):
    Sum of squares = 10
    Square of sum = 20
    The Square of sum - Sum of squares is 30.

    And finally.....
    3)The Game of Pig is a dice game with simple rules:
    • There are two players, they play with one die
    • The goal of the game is to reach 100 points
    • Each turn a player rolls the die until he rolls a 1 (called a pig), until he
    holds or until he reaches the 100 point mark.
    • Whenever a player rolled the die and did not roll a 1 he continues. He is
    now faced with two choices:
    • roll again: If he rolls anything between 2 and 6 the the number is added
    to the playerʼs turn total. If he rolls a 1 then the playerʼs turn total is
    erased and it is the opponentʼs turn
    • hold: The turn total is added to the playerʼs score and it is the
    opponentʼs turn

    a)Implement the Game of Pig for two human players playing on the same machine. You
    need to ask the player if he wants to hold or roll after each dice roll. Also you should
    provide information to whose turn it is and the current score.
    (Hint: Use nextLine() if you want to have a String, use nextInt() if you want to have an int).

    b)Implement the Game of Pig for one human player. He should have an opponent that is
    played by the computer. Use code comments to relate the strategy behind your computer
    player.
    (Hint: Your computer player should make the decisions roll or hold based upon the turn
    total, the score of the human player and/or the score of the computer player.

    Have less than 8 hrs to do this..............


Comments

  • Registered Users, Registered Users 2 Posts: 2,481 ✭✭✭Fremen


    We're not really here to do your work for you. If there's something you don't understand, try to articulate what it is that's bugging you, and people will try to help out.

    To be honest, I doubt you'll get much sympathy- it looks like you've left everything until the very last minute. Not really the best way to approach an assignment.


  • Closed Accounts Posts: 6,556 ✭✭✭the_monkey


    unfortunately have to agree , why not post some of the code you have allready attempted and then ask for some pointers...


  • Closed Accounts Posts: 11 jayvenom


    okay


  • Closed Accounts Posts: 11 jayvenom


    for the 4th one:

    public class PythagoreanTriplet
    {

    public static void main(String[] args)
    {

    final int MAXIMUMNUMBER=1000;
    int number1,number2,number3;

    System.out.println("Program to determine the Pythagorean Triplet for which a + b + c =1000");
    System.out.println("
    ");
    System.out.println(" ");

    for(int i=1;i<=500;i++)
    {

    for(int j=1;j<=500;j++)
    {

    for(int x=1;x<=500;x++)
    {
    number1= i * i;
    number2 = j * j;
    number3= x * x;


    if(((number1 + number2)==number3) && ((i + j + x)==MAXIMUMNUMBER))
    {
    System.out.println("The solution is " + "a=" + i + " ," + "b=" + j + "," + "c="+ x);
    System.exit(0);
    }

    }

    }

    }




    }

    }


  • Closed Accounts Posts: 11 jayvenom


    what do y'all think?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 10,902 ✭✭✭✭28064212


    jayvenom wrote: »
    what do y'all think?
    That works, doesn't it? Are you looking for coding style criticisms or what?

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Registered Users, Registered Users 2 Posts: 2,481 ✭✭✭Fremen


    He's running three nested loops 500 times each, which means 125,000,000 operations. Maybe it's too slow?
    Again, OP, we need more input from you than just a code snippet. If you want help, you need to be specific.


    You have two conditions

    [latex]x+y+z = 1000[/latex]

    and

    [latex]x^2 + y^2 = z^2[/latex]

    which is the same as

    [latex] z = \sqrt{x^2 + y^2} [/latex]

    If you substitute that into the first equation, you have a relation in X and Y for which you want integer solutions. You should be able to loop through all the possibilities much faster.


  • Closed Accounts Posts: 11 jayvenom


    ok that works.


  • Closed Accounts Posts: 11 jayvenom


    i still cant figure out how to begin coding the first one, tho.


  • Closed Accounts Posts: 11 jayvenom


    i mean the third one


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 2,481 ✭✭✭Fremen


    The pig question is really more programming than maths. It's the kind of thing that's covered in a standard java textbook. Just break it down into different cases. Who's turn is it? Prompt them to enter a command to hold or roll again. How do you generate a random dice roll? What was their roll? if the roll was 1, what happens? etc...
    You might want to use a "while" loop and use a boolean "gameisover" as a loop guard. How do you decide if a game is over?

    The game is probably simple enough that there's an optimal way for a computer to play. You need to think about that a bit and post your ideas here though.


  • Closed Accounts Posts: 11 jayvenom


    ok, so this is what i did for the game of pig.
    comments???

    import java.util.*;
    /**
    * @author Joshua-Derek Bossman-Adotevi
    */
    public class GameofPig {
    public static void main(String[] args) {
    Scanner diceThrow = new Scanner(System.in);
    //initializing players and scores
    int player1 = 1;
    int player2 = 2;
    int player = 1;

    int player1turn; // score for one roll of the dice for fisrt player
    int player1turnTotal = 0; // score for one round of play for first player
    int player1totalScore = 0; //overall score for first player
    int player2turn; // score for one roll of the dice for second player
    int player2turnTotal = 0; // score for one round of play for second player
    int player2totalScore = 0; //overall score for second player

    //For first player's turn
    System.out.println("This is a game of pig. Two players roll a dice until one");
    System.out.println("players score reaches 100. That player then wins the game.");
    System.out.println("The game begins with player One.");
    System.out.println("
    ");
    System.out.println("");

    while(player1totalScore < 100 && player2totalScore < 100){
    if(player == 1){
    System.out.println("Player 1, press 'p' to play or 'h' to hold: ");
    String choice = diceThrow.nextLine();

    if(choice.equals("p")){
    player1turn = (int)(Math.random()*6+1);
    System.out.println("Your dice throw is: " + player1turn);

    if(player1turn != 1){
    player1turnTotal = player1turnTotal + player1turn;
    System.out.println("Your turn total is:" + player1turnTotal);
    System.out.println("
    ");
    }
    else {
    System.out.println("PIG!!!");
    System.out.println("Your score for this round is set to 0. Player 2 plays next.");
    System.out.println("
    ");
    // switching players if the first player plays a 1
    player1turnTotal = 0;
    player = 2;
    player1 = 2;
    player2 = 1;
    }

    }
    else if(choice.equals("h")){
    player1totalScore = player1totalScore + player1turnTotal;
    // switching players if the first player holds
    player = 2;
    player1 = 2;
    player2 = 1;
    }

    }
    /*
    This is for the second players turn
    */
    if(player == 2){
    System.out.println("Player 2, press 'p' to play or 'h' to hold: ");
    String choice = diceThrow.nextLine();

    if(choice.equals("p")){
    player2turn = (int)(Math.random()*6+1);
    System.out.println("Your dice throw is: " + player2turn);

    if(player2turn != 1){
    player2turnTotal = player2turnTotal + player2turn;
    System.out.println("Your turn total is:" + player2turnTotal);
    System.out.println("
    ");
    }
    else {
    System.out.println("PIG!!!");
    System.out.println("Your score for this round is set to 0. Player 1 plays next.");
    System.out.println("
    ");
    // switching players if the second player plays a 1
    player2turnTotal = 0;
    player = 1;
    player1 = 1;
    player2 = 2;
    }
    }
    else if(choice.equals("h")){
    player2totalScore = player2totalScore + player2turnTotal;
    // switching players if the second player holds
    player = 1;
    player1 = 1;
    player2 = 2;
    }

    }
    }
    System.out.println("Player 1 total score is: " + player1totalScore);
    System.out.println("Player 2 total score is: " + player2totalScore);
    if(player1totalScore > player2totalScore){
    System.out.println("PLAYER 1 WINS!!!!!!");
    }
    else{
    System.out.println("PLAYER 2 WINS!!!!!!");
    }
    }

    }


  • Closed Accounts Posts: 11 jayvenom


    i knew this was not complete. tested it out and it worked up to a point. but the counters were not reset when a player gave up his his turn. this made the game end faster and nullified the purpose, i think. Thanks for the help, guys.


Advertisement