Okay so this is for homework. I don't want a complete answer, just someone to help nudge me in the right direction. I am fairly new to Java, so please take it easy :P Okay so my professor is having us do a Dice simulation program with OOP. This is just one portion of the entire problem, but it's the part I'm stuck on, so if you need more I can provide more. But the instructions say:Make a Roll function that will simulate rolling each die, and return the total
This function should only allow values between the appropriate range (i.e. if each die only has 4 faces, only numbers 1 - 4 should be allowed).
What I have so far for this particular function is this:
public double Roll()
{
for(int i = 0; i < numDice; i++)
{
double total = Math.random()*numFaces + 1;
}
return total;
I don't know where to go from here, and I don't know what really to do. This is my first programming class and it's way over my head :P So like I said, if I could just get pointed in the right direction (using dummy talk, cause I'm still having a hard time grasping this whole thing) that would be awesome. And I can provide more of the actual problem if need be.
Just noticed you used double , replace double with int or short. Look at the following tutorial.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Your also not so hot with naming conventions. Read this.
http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html
You don't need to roll for each face, only for each dice. Do this.
Please note, for this you could use shorts as well.
public int roll() {
return new Random().nextInt(6) + 1; // Returns number between 1 and 6
}
If you want to roll multiple dice you could do this.
public int roll(int amount) {
int total = 0;
for(int i = 0; i < amount; i++) {
total += new Random().nextInt(6) + 1;
}
return total;
}
You're defining total inside the for loop, which makes it invisible outside it. Also, you're overwriting it (=) instead of adding to it (+=) on each die throw.
public double Roll()
{
double total = 0;
for(int i = 0; i < numDice; i++)
{
total += Math.random()*numFaces + 1;
}
return total;
}
I would also recommend to follow Java style and rename the method roll (methods start with a lowercase letter). The return type also strikes me as odd, I would probably use int in a similar situation.
You could create a class named Die to represent a die. This fits with OOP as 'die' would be one of your nouns. Now think of what configuration and behavior a Die might have. You may wish to have a Die have a configurable number of faces, but default to 6 if it isn't specified. You also want to be able to roll a die. Lets see what we have so far. Implementation left up to the reader.
public class Die {
public Die(){
this(6);
}
public Die(int faces){
//TODO: Write a constructor that takes number faces as an argument.
}
public int roll(){
//TODO: Implement a roll for this die, considering the number of faces.
}
}
Ok, so you have the beginning of a Die class. Now you might think, wouldn't it be cool if I could group die together as Dice (a set of dice if you will).
public class Dice {
public Dice(Die... dice){
//TODO: Implement constructor that takes an array of die objects.
}
public int roll(){
//TODO: Roll all the dice, sum and return the result.
}
}
Next you might think, man wouldn't it be sweet if I could treat Dice and a single Die as a single type, I mean they both can be rolled. Let us create an interface called Rollable that specified the roll behavior and have both Dice and Die implement it.
public interface Rollable {
int roll();
}
Then go back and change the class declarations to be.
public class Die implements Rollable
and
public class Dice implements Rollable
Now code that needs to roll things only needs to worry about Rollable instead of Die or Dice
First of all, you need to declare the total outside the for-loop. Then initialize it to zero.
Then, inside the for-loop. Increase the variable.
If numFaces should be able to be different for each die, you will need to get the value for it inside the for-loop, on each iteration.
Also, you should ask yourself: Which possible values do I want for the total variable? Should it be int or double?
You need to define total outside the for loop and then do total+=… inside the loop
Related
I'm making a horse-race themed program for my mom that compares the money taken in by her employees and ties it to a horse. I've created two methods which entirely rely on each other and have no idea how to call them into my main method. I, of course, also need to add a graphical element to this at some point, and figuring out how to make the program work with decimal integers would also be ideal. My main issue right now is I need to know how to call print3largest and inputs in my main method, or how to generally make this not a dumpster fire and maybe reduce it to less than 3 methods that aren't entangled like this.
I've searched through repository websites for hours now looking for a solution, but as I have no professional experience in any kind of programming I severely lack the terminology to find an answer, assuming anyone else is stupid enough to run into the same problem I have. I'm extremely limited in my programming knowledge, with java being the only thing I've ever messed with thanks to a course in high school. Sadly, that hardly helps as it was almost entirely through an interface that was essentially just scratch.
import java.util.Scanner;
class HorseComparison
{
public static void main(String[] args)
{
//no clue how to call print3largest or inputs here without ruining everything
}
static void print3largest(int arr[], int arr_size, String firsthorse, String secondhorse, String thirdhorse)
{
int i, first, second, third;
if (arr_size < 3)
{
System.out.print(" Invalid Input ");
return;
}
third = first = second = Integer.MIN_VALUE;
for (i = 0; i < arr_size ; i ++)
{
if (arr[i] > first)
{
third = second;
second = first;
first = arr[i];
}
else if (arr[i] > second)
{
third = second;
second = arr[i];
}
else if (arr[i] > third)
third = arr[i];
}
inputs(first, second, third);
System.out.println("The horse in the lead is " + firsthorse + " with " +
first + " dollars.");
System.out.println("The runner up is " + secondhorse + " with " +
second + " dollars.");
System.out.println("Third place is " + thirdhorse + " with " +
third + " dollars.");
}
static void inputs(int first, int second, int third)
{
Scanner sc = new Scanner(System.in);
int size;
System.out.println("How many horses are competing?");
size = sc.nextInt();
int[] arr = new int[size];
System.out.println("Enter the amount of money taken in by each horse (rounded to the nearest dollar and separated by spaces)");
//For reading the element
for(int i=0;i<size;i++) {
arr[i] = sc.nextInt();
int n = arr.length;
String firsthorse;
String secondhorse;
String thirdhorse;
System.out.println("Which horse has taken in "+ first +"?");
firsthorse = sc.toString();
System.out.println("Which horse has taken in "+ second +"?");
secondhorse = sc.toString();
System.out.println("Which horse has taken in "+ third +"?");
thirdhorse = sc.toString();
print3largest(arr, n, firsthorse, secondhorse, thirdhorse);
}
}
}
I want it to display the 3 highest amounts along with the input name of the horse tied to those amounts.
I don't feel like there really is enough information about what the program is intended to do for a clear, direct answer to be provided, but I will do my best.
First, what I would suggest, is you take a good look at this program and determine how you can separate out each responsibility. For example, do you really need to call inputs from print3largest, or could you possibly call this directly from your main?
Once you have established the intent of each function, consider making each function return a result. Generally speaking, you want parameters to be immutable. Learning functional programming habits now will help you down the road.
Here is what I would do:
Copy this file to a backup file.
Start a new Java project, create your class.
Write all of your display code. That is, develop the initial user experience. What inputs do you want to ask from the user? Capture those inputs.
Given those inputs, write your core algorithm, which currently appears to be primarily in print3largest. Return those results back to the caller.
Display your results back to the end user.
This might result in more functions, but that isn't a bad thing. I would also advise that you consider creating a separate class to hold some of this logic. This will give you an opportunity to learn about objects and separation of concerns.
you can call static methods directly by the method name
print3largest()
or you can use the classname before method name example
HorseComparision.print3largest() ```
Since both methods are static and so the Main method. Static methods can be called
Directly with method name, if you are calling from inside the class. Eg : print3largest(.. args), inputs(.. args)
call using ClassName.Method name. This can be used if you are calling method from outside or inside the class. Eg: HorseComparison.print3largest(.. args), HorseComparison.inputs(.. args)
so I've been studying Java for about 3 months and I am supposed to do a Hangman code using only arrays, loops, and if statements. the word to be guesses is read from another file and saved as a string. I have to be able to save the wrong guesses in an array. and after each guess print all the wrong guesses so far, as well as the gameboard with underscores for not guessed letters and the correct guesses of course in their place. here is my code so far :
for(int l = 0; l<wordlength;l++)
{
System.out.print("_");
}
System.out.println();
System.out.println("WRONG: ");
for(int c = 0; c<numofGuesses;c++)
{
System.out.println();
System.out.print("GUESS"+guessN+"/"+numofGuesses+": ");
char guess1=in.next().charAt(0);
char guess = Character.toUpperCase(guess1);
guessN = guessN+1;
for (int j = 0; j<wordlength;j++)
{
if (guess==guessword.charAt(j))
{
System.out.println("Great guess!");
System.out.print (guessword.charAt(j));
}
else
{
System.out.print("_");
WRONG[u]=guess;
u++;
}
}
if you guess A it prints correctly "A___" but then if you guess B after instead of printing "AB__" (the word to guess is ABLE) i get "B__" also the wrong array is not storing and printing all the wrong guesses each time. please help I've been trying for 5 days and that's all I did the entire day today and I couldn't get past this.
Because this sounds a lot like a homework assignment, I will give directions for solving this, but not provide a full working solution. Hopefully, seeing how one could1 go about approaching such a problem is enough of a step in the right direction to be able to solve it yourself.
Let's first think about what we need to do.
Read a word that needs to be guessed, say String toBeGuessed.
You did this. ✔
Keep track of the characters the player has guessed so far.
Keep track of the number of turns a player has gotten.
Keep track of if the word has been guessed (player won!).
Say that the number of guesses a player can make is fixed. This can be modeled using a constant:
/**
* Number of guesses a player can take.
*/
public static final int NUM_GUESSES = 10;
Now let's think about the main logic of our hangman game. It is good to first think about the structure of your program and only later actually implement it. When thinking of the program structure, we don't bother with specifics of the programming language of your choice yet. In pseudocode, it would be something like the following, maybe (let's indicate what you already have with ✔).
for turn from 1 upto NUM_GUESSES do ✔
show player what they guessed so far
show the gameboard
ask player for their new guess ✔
save player's guess and update internal state
check if the player won, let them know if they did
if player did not win
let them know
Right. So, we need to somehow store the guesses that a player made. Every guess is a character, and we know there will be at most NUM_GUESS guesses in total. A good option (and one that is suggested by your exercise) is an array!
/**
* Characters that have been guessed so far.
*/
private char[] guessed;
This can be initialized as follows, since we know the maximum number of guesses:
this.guessed = new char[NUM_GUESSES];
This gives us an array of NUM_GUESSES characters that are initialized to 0 (see here). Since users won't guess that character, we can use it to represent guesses that have not been done yet. Alternatively, we can keep track of the current turn of the player in a separate variable. Your choice!
In the following, I will not keep track of the current turn in a separate variable, just to show more of arrays and loops. It might be a fun exercise to change this to using an int turn variable!
show player what they guessed so far
Alright, this should be fairly straightforward now. We basically need to print the part of the guessed array that is not 0. That can be done using a loop, like so for example:
System.out.print("You so far guessed: ");
for (int i = 0; i < guessed.length; ++i) {
if (i > 0) {
System.out.print(", ");
}
if (guessed[i] != 0) {
System.out.print(guessed[i]);
} else {
break; // stop the loop as soon as we run into a 0
}
}
System.out.println(".");
This will print something like You so far guessed: a, b, c. when the player guessed those characters. See how we only print the comma when some other character was printed before?
show the gameboard
The next point of the program structure is trickier to get right. Let's think a bit about structure again.
for each character in toBeGuessed
if the character has been guessed
print it
else
print an underscore
Looping over every character of a word can be done as follows.
int length = toBeGuessed.length();
for (int i = 0; i < length; ++i) {
char character = toBeGuessed.charAt(i);
// do something with character here
}
How do you find if a character has been guessed yet? Well, by checking if it is stored in the guessed array. This again can be done using a loop. That loop will be very similar to the one we have written above, when showing what the player guessed so far. I think you should be able to figure that one out.
save player's guess and update internal state
We move on to the next point of the program structure. Say that we have a char guess that the player guessed. We need to store this in our array guessed. Where? Well, at the first open spot, that seems a reasonable choice. To find that one, let's use a loop again, and break the loop when we have found an open spot.
for (int i = 0; i < guessed.length; ++i) {
if (guessed[i] == 0) {
guessed[i] = guess;
break;
}
}
check if the player won, let them know if they did
What we need to know in order to see if the player won, is simply if the number of characters they guessed right is equal to the number of characters in toBeGuessed. You could modify the loop for showing the gameboard to not print characters, but count correct ones. Then at the end compare to toBeGuessed.length() and if they are equal, the player won.
if player did not win, let them know
This should be fairly easy, if you got the previous point working.
When you did all the above and stitched it together, you should have a working version of hangman. Your very own, something to be proud of!
Some tips and tricks:
you can implement most of the points described above as separate methods;
when you do so, you can write one main method that calls the other methods (this will make it easier to read your own code and make changes to it);
try to put as little code as possible in the main method.
Here is a little template that you can start from.
import java.io.PrintStream;
import java.util.Scanner;
public class HangMan
{
/** Number of guesses a player can take. */
public static final int NUM_GUESSES = 10;
/** Word to be guessed in a game of hangman. */
private String toGuess;
/** Letters that have been guessed so far. */
private char[] guessed;
/**
* Construct a new game of hangman, ready to be played.
*/
public HangMan(String toGuess)
{
this.toGuess = toGuess;
this.guessed = new char[NUM_GUESSES];
}
// your other methods go here
/**
* Read guesses from given input and print results to given output.
* Continues until guesses have run out, or word was guessed.
*/
public void play(Scanner in, PrintStream out)
{
for (int round = 0; round < NUM_GUESSES; ++round) {
showGuessedSoFar(out);
showGameBoard(out);
char guess = askGuess(in, out);
saveGuess(guess);
if (hasPlayerWon()) {
out.println("You won!");
return;
}
}
// at this point, player ran out of guesses and hence lost
out.println("You lost...");
}
/**
* The bit that runs our hangman game.
*/
public static void main(String[] args)
{
// read word to guess from arguments, with a default value
// you would probably insert your "read word from file" code here
HangMan game = new HangMan(args.length >= 1 ? args[0] : "ABLE");
// play a game, using system input and output
game.play(new Scanner(System.in), System.out);
}
}
Good luck!
TL;DR. Trying to teach one how to think about a problem and how to write code that executes the solution one thought of. Features some example code with arrays and loops.
1 This is only one possible solution, there are always many ways to solve a given problem.
First, I have searched. And all of the answers I have found have been about NOT having the same reference to the same object in two different array lists. so maybe that should tell me I'm doing this wrong in itself? But I'm not sure of how else to manage this.
I am trying to learn Java. And part of that is Swing.
My app I am working on is a simple tournament/bracket app (think march madness. or any simple bracket tree).
Simplified code with just the issue at hand:
class Bracket extends JPanel {
ArrayList<Round> rounds = new ArrayList<Round>();
public void declareWinner(Team team, Game game, Round round) {
// code that gets current round, game, team numbers to calculate the next round, game, team numbers.
int currentRoundNum = rounds.indexOf(round);
int currentGameNum = rounds.get(currentRoundNum).games.indexOf(game);
int currentTeamNum = rounds.get(currentRoundNum).games.get(currentGameNum).teams.indexOf(team);
int nextRoundNum = currentRoundNum + 1;
int nextGameNum = (int) (Math.floor(gameNumber / 2));; // to position the team in the appropriate game slot in the next round
int nextTeamNum = ((gameNumber % 2) == 0) ? 0 : 1; // even game numbers will be the top team in the next round, odd team numbers will be the bottom team in the next round
// here is where things are getting wonky. Trying to set the next round slot to the team that is being declared the winner
rounds.get(nextRoundNum).games.get(nextGameNum).teams.set(nextTeamNum, team);
}
public Bracket(int numRounds) {
// code that creates the bracket structure. # of rounds. the correct # of games depending on the round. etc. Creates empty shell of "placeholder" teams with no name/info.
for(int i = 0; i < numRounds; i++) {
int numGames // set to (number of games of the last round added) * 2
rounds.add(0, new Round(numGames)
}
}
}
class Round extends JPanel {
ArrayList<Game> games = new ArrayList<Game>();
public Round(int numGames) {
for(int i = 0; i < numGames; i++) {
games.add(new Game());
}
}
// more code...
}
class Game extends JPanel {
ArrayList<Team> teams = new ArrayList<Team>();
// more code... creates two teams per each game in constructor
}
class Team extends JPanel {
String teamName;
public Team(String name) {
teamName = name;
add(new JLabel(name));
}
// simple team info, just going for functionality. not much else is here yet.
}
So, lets say in the first round (index 0 of rounds), in the first game (index 0 of rounds.get(0).games), Team 1 (index 0 of rounds.get(0).games.get(0).teams) wins. It's calculating all the stuff correctly. The team is placed in the correct slot. BUT, it completely removes the team from the current position in the round. So now I'm left with only 1 team in the first game of the first round.
It won't let me have the same Team object referenced in both ArrayLists in Rounds[0].Games[0].Teams and Rounds[1].Games[0].Teams. They are nested array lists in each, so 2 different Array Lists. Am I failing because it's bad to extend the JComponents on the classes themselves, and I should completely refactor this?
I can't see exactly what is going on in your example code, as it is not self-contained. However from your description it looks like you are falling foul of the property that Swing components can only be added to one container at a time. If you add the same component to a second container it is automatically removed from the first one.
This doesn't affect the contents of your ArrayLists in the slightest - you can have the same object in as many different ArrayLists as you like.
It also looks like you are muddying the waters by storing this sort of data inside objects which extend Swing components. I suggest you consider separating out a data structure (the Model) from the display components (the View) to make things clearer. Just get the data structure working first, then build the view from it once you have verified it is correct.
On an unrelated note, it looks like you could simplify the start of your code, where before you had:
public void declareWinner(Team team, Game game, Round round) {
// code that gets current round, game, team numbers to calculate the next round, game, team numbers.
int currentRoundNum = rounds.indexOf(round);
int currentGameNum = rounds.get(currentRoundNum).games.indexOf(game);
int currentTeamNum = rounds.get(currentRoundNum).games.get(currentGameNum).teams.indexOf(team);
...
you could replace these lines with
public void declareWinner(Team team, Game game, Round round) {
// code that gets current round, game, team numbers to calculate the next round, game, team numbers.
int currentRoundNum = rounds.indexOf(round);
int currentGameNum = round.games.indexOf(game);
int currentTeamNum = game.teams.indexOf(team);
...
since you already have references to those objects passed in as arguments.
I am writing code for a yahtzee game in java and I need to write a method which allows the user to choose which dice to keep and which dice to re-roll. The part I'm having trouble with is that the user must be able to choose more than one value to keep.
For example on the first roll they may choose to keep 3's, 5's, and a 6. This is what I have so far, but the correct dice are not being kept when I test it.
//keep method
public void keep(int[] keepThis) {
for(int i = 0; i < keepOrRollArray.length; i++) {
for(int p = 0; p < keepThis.length; p++) {
if(faceValueArray[i] == keepThis[p])
keepOrRollArray[p] = 'K';
}
}
}
//keep method tested after a roll
newDiceArray.roll();
int[] userValue = new int[2];
userValue[0] = 3;
userValue[1] = 4;
newDiceArray.keep(userValue);
System.out.println("Practice roll: " + "\n" + newDiceArray +"\n");
First off, I'm guessing that there is quite a bit more to your code than what you have posted. (Otherwise, your code is egregiously incomplete, not to put too fine of a point on it). Without this additional code, we can't really help much.
In the meantime, however, if what you have isn't "keeping" the correct dice, you might ought to try having five separate questions querying the user whether they want to keep each of the dice: "Do you want to keep dice #1", "Do you want to keep dice #2" etc. Then update your keepThis array appropriately.
Preface- I'm an A level computer science student, and for the most part it's a pretty easy subject. We are graded based on a written test, a programming assignment, and a programming test where we are given pre-written code and asked to modify it.
As homework I was given an old piece of exam code, with the homework being to go through and add comments as to what the code is for.
This is easy to do, as the code is just for a text based noughts and crosses game.
Most of the code isn't too bad (Some of the exam board code is seriously questionable), however the method for choosing which player goes first is a bit strange-
char getWhoStarts() {
char whoStarts;
int randomNo;
Random objRandom = new Random();
randomNo = objRandom.nextInt(100);
if (randomNo % 2 == 0) {
whoStarts = 'X';
} else {
whoStarts = 'O';
}
return whoStarts;
}
Obviously, this method works, but I don't really understand why someone would ever generate a random integer, and then check if it is even, when all they want is an evenly weighted random. I could understand if this method had more than two return possibilities, or needed to be weighted, however in this case it makes no sense.
I would replace it with this -
char getWhoStarts() {
Random r = new Random();
if(r.nextBoolean()) {
return 'X';
} else {
return 'O';
}
}
Also, the boolean could probably be a global variable, rather than being initialised each time the method is called.
Could someone explain to me, if there is a reason to do this, what is that reason?
And are there any benefits of a random integer rather than a random boolean.
Thanks.
This might be a carryover from C, where the only library function that generates random values (rand) only returns integers. It's actually not a good idea just to look at the remainder modulo two on older implementations, since they famously used to alternate between even and odd numbers. I'd go with the provided function to generate a Boolean value - it's cleaner and less error-prone.