I am trying to learn OOP, starting with Java as I have read and been told it's the best place to start. With that said, I am trying to create a game for fun to help my learning, but I also know game programming and design can be more challenging.
So my goal here is to generate a new value from the RollDice D20 without having to reset or restart the program. You'll notice when I print out the values I print the same instance twice to demonstrate what I am avoiding, and a new instance to show that the new instance does indeed generate a new value. Perhaps, I am not approaching this in the right way, but this is a hurdle I am hoping to overcome with some help!
What I ultimately want is to figure out how to generate a new instance or at least a new roll value as many times as I want. Any and all help is greatly appreciated! I have added the code below as an example. Also any other feedback is appreciated.
import java.util.Random;
class RollDice
{// Begin RollDice Class
// Initiate method rollDice
public static int rollDice(int number, int nSides)
{
// System.out.println( "--- Welcome to the Dice Game v2! ---" ); //
// welcomes player
Random r = new Random();
// Declare class variables
int num = 0;
int roll = 0;
if (nSides >= 3)
{
for (int i = 0; i < number; i++)
{
roll = r.nextInt(nSides) + 1;
// System.out.println("Roll is: " + roll);
num = num + roll;
}
}
else
{
System.out.println("Error num needs to be from 3");
}
return num;
} // end method rollDice
int d4 = rollDice(1, 4);
int d6 = rollDice(1, 6);
int d8 = rollDice(1, 8);
int d10 = rollDice(1, 10);
int d12 = rollDice(1, 12);
int d20 = rollDice(1, 20);
public RollDice()
{
this.d4 = d4;
}
public void setD4(int D4)
{
this.d4 = D4;
}
public int getD4()
{
return d4;
}
// ////////////////
{
this.d6 = d6;
}
public void setD6(int D6)
{
this.d6 = D6;
}
public int getD6()
{
return d6;
}
// ////////////////
{
this.d8 = d8;
}
public void setD8(int D8)
{
this.d8 = D8;
}
public int getD8()
{
return d8;
}
// ////////////////
{
this.d10 = d10;
}
public void setD10(int D10)
{
this.d10 = D10;
}
public int getD10()
{
return d10;
}
// ////////////////
{
this.d12 = d12;
}
public void setD12(int D12)
{
this.d12 = D12;
}
public int getD12()
{
return d12;
}
// ////////////////
{
this.d20 = d20;
}
public void setD20(int D20)
{
this.d20 = D20;
}
public int getD20()
{
return d20;
}
// ////////////////
}// End RollDice Class
class Champion
{// Begin Champion Class
RollDice champDice = new RollDice();
int champroll = champDice.getD20();
public Champion()
{
this.champroll = champroll;
}
public void setChampRoll(int ChampRoll)
{
this.champroll = ChampRoll;
}
public int getChampRoll()
{
return champroll;
}
}// End Champion Class
public class DiceRollTest
{
public static void main(String ars[])
{
Champion tChampion = new Champion();
Champion pChampion = new Champion();
System.out.println("Your Champion defends with a " + tChampion.getChampRoll() + "\n");
System.out.println("Your Champion defends with a " + tChampion.getChampRoll() + "\n");
System.out.println("Your Champion defends with a " + pChampion.getChampRoll() + "\n");
}
}
Your RollDice class is not accomplishing what you want. All it's doing is storing a single dice roll result for each type of dice you have. Therefore, when you go an call getChampRoll() on your Champion object, all it's doing is returning the roll that already took place when you constructed your RollDice class.
Instead, you should make rollDice() a member function of your RollDice class. RollDice should then take in its constructor an argument indicating which dice should be rolled when rollDice() is called. That way, when you call getD20(), it will roll the D20 and give you the result.
I'll leave you with this as a starting point:
import java.util.Random;
public class Die {
private int mSides;
private Random mRandom;
public Die(int sides) {
this.mSides = sides;
mRandom = new Random(System.currentTimeMillis());
}
public int roll() {
return mRandom.nextInt(mSides + 1);
}
public int roll(int times) {
int sum = 0;
for (int i = 0; i < times; i++) {
sum += roll();
}
return sum;
}
}
This class can be inherited/subclassed for each dice you want to create. Then you can toss a few of these in your champion's pockets :)
First, you should create new Constructor. In this constructor you should inicialize Random object, and probably how many sides will dice have
class RollDice{
Random rand;
int sides;
public RollDice(int x){
sides = x;
rand = new Random()
}
}
then, in that class you can add method which will generate new value
public int roll(){
return rand.nextInt(x);
}
you may as well don,t generate RollDice object with fixed value of dice's sides and make the method like:
public int roll(int x){
return rand.nextInt(x);
}
if you want java to generate new value after you enter something on console just use loop with System.in.read() and if or switch statement (for example if someone enters 1 - generate new value, if 0, end program)
Related
I am working on a lab that requires me to return the number it flips it requires to get heads. I used a for loop, and a while loop but it appears that returning the method values along with the loops is not making the code runnable. I would appreciate the assistance. There are two pieces of codes, each different classes calling upon another class.
import java.util.*;
public class GVCoin {
// true for heads, false for tails
private boolean isHeads;
private int flips, heads;
private Random r;
public GVCoin() {
r = new Random();
heads = 0;
flips = 0;
isHeads = true;
}
public void flip() {
isHeads = r.nextBoolean(); // Flip the coin by randomly choosing true / false
flips++; // Increment flip count
if(isHeads){
heads++; // Increment heads count if current flip results in heads
}
}
public boolean isHeads() {
return isHeads; // Return true if coin is currently heads
}
public String toString() {
String str;
str = "Flips: " + flips + " Heads: " + heads + " isHeads: " + isHeads;
return str; // Return String representation of important values
}
public int numFlips() {
return flips; // Return number of total flips
}
public int numHeads() {
return heads; // Return number of total heads
}
public int numTails() {
return flips - heads; // Return number of total tails
}
public void setToHeads(boolean h) {
isHeads = h;
}
public GVCoin(int seed) { // Create the coin with a random seed
this();
r = new Random(seed);
}
}
public class TossingCoins {
public int flipForHeads(GVCoin coin, int goal) {
int i = 0;
do {
coin.flip();
i++;
} while (i < goal);
return i;
}
public static void main(String[] args) {
TossingCoins game = new TossingCoins();
GVCoin coin = new GVCoin(15); // Create a GVCoin object with seed value 15
int numHeads = 100; // Desire 100 heads
int totalFlips;
totalFlips = game.flipForHeads(coin, numHeads);
System.out.println("Total number of flips for 100 heads: " + totalFlips);
}
}
I suggest you not to use seed when you create Random. If you do, the outcome will be always the same.
Here some working code. I removed some code you wrote which I thought not actually needed. Cheers!
class GVCoin {
private Random r;
public GVCoin() {
r = new Random();
}
public GVCoin(int seed) {
this();
r = new Random(seed);
}
public boolean flip() {
return r.nextBoolean();
}
}
public class TossingCoins {
public int flipForHeads(GVCoin coin, int goal) {
int count = 0;
int numberOfHeads = 0;
while (numberOfHeads < goal) {
count++;
boolean isHead = coin.flip();
if (isHead) {
numberOfHeads++;
}
}
return count;
}
public static void main(String[] args) {
TossingCoins game = new TossingCoins();
GVCoin coin = new GVCoin();
int numberOfHeads = 5;
int totalFlips;
totalFlips = game.flipForHeads(coin, numberOfHeads);
System.out.println("Total number of flips for "+ numberOfHeads + " heads: " + totalFlips);
}
}
I managed to figure out the code. On the method call I simply formatted the code as follows:
int flips = 0;
while (coin.numHeads() < goal) {
coin.flip();
flips++ ;
}
return flips;
I wrote this code in Eclipse Java and for some reason, it doesn't run. It doesn't say it has any errors in it and no red marks appear anywhere in the code. I'm not sure what is wrong with it, please help.
Here is the description of what I needed to write: Design and implement a class called PairOfDice, composed of two six-sided Die objects. Create a driver class called BoxCars with a main method that rolls a PairOfDice object 1000 times, counting the number of boxcars (two sixes) that occur.
public class dieGames {
public class PairOfDice {
private int die1;
private int die2;
public PairOfDice() {
roll();
}
public void roll() {
die1 = (int)(Math.random()*6) + 1;
die2 = (int)(Math.random()*6) + 1;
}
public int getValueDie1() {
return die1;
}
public int getValueDie2() {
return die2;
}
public String toString() {
return "Die 1: " + die1 + ", Die 2: " + die2;
}
}
public class BoxCars
{
public void main(String[] args)
{
final int numRolls = 1000;
int numBoxCars = 0;
PairOfDice twoDice = new PairOfDice();
for (int i = 0; i < numRolls; i++)
{
twoDice.roll();
if (twoDice.die1 == 6 && twoDice.die2 == 6)
{
numBoxCars++;
}
}
System.out.println("Number of Box Cars in " + numRolls +
" rolls is " + numBoxCars);
}
}
}
A couple of things that I can see here:
you never create an instance of your diceGames class
your main method is not static, which is required
your main method is inside another class definition, which is also never instantiated
a couple of adjustments to your code make it run fine (commented in the code below):
// this class remains unchanged, except for making the PairOfDice class static
// so that it's accessible to the main method
public class dieGames {
public static class PairOfDice {
private int die1;
private int die2;
public PairOfDice() {
roll();
}
public void roll() {
die1 = (int)(Math.random()*6) + 1;
die2 = (int)(Math.random()*6) + 1;
}
public int getValueDie1() {
return die1;
}
public int getValueDie2() {
return die2;
}
public String toString() {
return "Die 1: " + die1 + ", Die 2: " + die2;
}
}
// removed containing class, which was unnecessary
// made main method static
public static void main(String[] args)
{
// create an instance of the class
dieGames game = new dieGames();
final int numRolls = 1000;
int numBoxCars = 0;
PairOfDice twoDice = new PairOfDice();
for (int i = 0; i < numRolls; i++)
{
twoDice.roll();
if (twoDice.die1 == 6 && twoDice.die2 == 6)
{
numBoxCars++;
}
}
System.out.println("Number of Box Cars in " + numRolls +
" rolls is " + numBoxCars);
}
}
From this, I get output of Number of Box Cars in 1000 rolls is x, where x is some value depending on the run.
If you want to meet the assignment as you post it, the "driver class" BoxCars can be a separate .java file containing the main method:
public class BoxCars {
public static void main(String[] args)
{
final int numRolls = 1000;
int numBoxCars = 0;
PairOfDice twoDice = new PairOfDice();
for (int i = 0; i < numRolls; i++)
{
twoDice.roll();
if (twoDice.die1 == 6 && twoDice.die2 == 6)
{
numBoxCars++;
}
}
System.out.println("Number of Box Cars in " + numRolls +
" rolls is " + numBoxCars);
}
}
In this case, you'll need to either make a getter method for die1 and die2, or make them public in the first file, which should now just be the PairOfDice class without the enclosing dieGames class. Notice that this main method now instantiates PairOfDice and not dieGames, since that class isn't required.
This question already has answers here:
Java: How To Call Non Static Method From Main Method?
(9 answers)
Closed 3 years ago.
my console doesn't seem to be showing any output on my projects, on some, it does at first. I am new to this so I don't understand it, this is an example of my code
public class TestAmazing {
public static void main(String args[]) {
// Put your data type declarations below.
int count = 0;
double cost = 3.45;
char choice = 'x';
boolean goodChoice = true;
short lowest = '5';
// Put the code for your calculations in this method.
// temp in a room
}
public void roomtemp() {
int person = 1;
int temp = 20 + (person);
System.out.println("the temperature in the room is" + temp);
}
// nunber of jackpot ball
public void bonusball() {
int bonusball;
bonusball = (int) (Math.random() * 59);
System.out.println("the jackpot ball is " + bonusball);
// population of china
}
public void currentpopulationofchina() {
// check whether a game is finished or not
long populationOfChina2017 = 1394200000;
long populationexpectedincreaseto2019 = 5840000;
long populationOfChina = populationOfChina2017 + populationexpectedincreaseto2019;
System.out.println("the population of china is" + populationOfChina);
}
// check where a game is finished or not
public void gameloadingstatus() {
int gameLoading;
gameLoading = (int) (Math.random() * 100);
if (gameLoading == 100) {
System.out.println("game is ready");
} else {
System.out.println("game is not ready");
}
}
}
Replace your main method with the following code and it should work as you are expecting:
public static void main(String args[]) {
// Put your data type declarations below.
int count = 0;
double cost = 3.45;
char choice = 'x';
boolean goodChoice = true;
short lowest = '5';
// Put the code for your calculations in this method.
// temp in a room
TestAmazing t=new TestAmazing();
t.roomtemp();
t.bonusball();
t.currentpopulationofchina();
t.gameloadingstatus();
}
The reason why it didn't work for you is because you have missed to call the methods you have created in the class. I have added the following lines in the main method to complete that missing part:
TestAmazing t=new TestAmazing();
t.roomtemp();
t.bonusball();
t.currentpopulationofchina();
t.gameloadingstatus();
None of your methods are static, so you'll need to create an instance of your TestAmazing class, and call methods via your instance.
That might look something like:
public static void main(String args[]) {
TestAmazing test = new TestAmazing();
test.roomtemp();
test.bonusball();
// etc...
}
Comment from QA:
this worked thank you! do I have to always do this and put it in the
same place? – Joe Emery
Not necessarily. It depends on the requirements of your program. If all of your methods are STATIC then you don't need an instance of your class. In that case, then you'd simply call all of your methods directly from main, like this:
public class TestAmazing {
public static void main(String args[]) {
// Put your data type declarations below.
int count = 0;
double cost = 3.45;
char choice = 'x';
boolean goodChoice = true;
short lowest = '5';
// Put the code for your calculations in this method.
// temp in a room
roomtemp();
bonusball();
currentpopulationofchina();
gameloadingstatus()
}
public static void roomtemp() {
int person = 1;
int temp = 20 + (person);
System.out.println("the temperature in the room is" + temp);
}
// nunber of jackpot ball
public static void bonusball() {
int bonusball;
bonusball = (int) (Math.random() * 59);
System.out.println("the jackpot ball is " + bonusball);
// population of china
}
public static void currentpopulationofchina() {
// check whether a game is finished or not
long populationOfChina2017 = 1394200000;
long populationexpectedincreaseto2019 = 5840000;
long populationOfChina = populationOfChina2017 + populationexpectedincreaseto2019;
System.out.println("the population of china is" + populationOfChina);
}
// check where a game is finished or not
public static void gameloadingstatus() {
int gameLoading;
gameLoading = (int) (Math.random() * 100);
if (gameLoading == 100) {
System.out.println("game is ready");
} else {
System.out.println("game is not ready");
}
}
}
Notice how every method has static in its declaration.
I have a project I am working on in my java class and cannot figure out what a few lines of code.
The project consists of flipping a coin and using various methods to detect how many heads or tails as well as how many flips have occurred so far.
import java.util.*;
public class GVcoin{
// true for heads, false for tails
private boolean isHeads;
private int flips, heads;
private Random r;
//Create the coin
public GVcoin(){
r = new Random();
heads = 0;
flips = 0;
isHeads = true;
}
//Flip the coin by random choosing true / false
public void flip(){
isHeads = r.nextBoolean();
flips++;
if(isHeads){
heads++;
}
}
//return true if coin is currently heads
public boolean isHeads(){
return isHeads;
}
//return String representation of important values
public String toString(){
String str;
str = "Flips: " + flips + " Heads: " + heads + " isHeads: " + isHeads;
return str;
}
//return number of total flips
public int numFlips(){
return flips;
}
//return number of total heads
public int numHeads(){
return heads;
}
//return number of total tails
public int numTails(){
return flips - heads;
}
//Set the coin to heads (or tails) to start
public void setToHeads(boolean h){
isHeads = h;
}
//Create the coin with a random seed
public GVcoin(int seed){
this();
r = new Random(seed);
}
}
The problem I can not seem to figure out is in a different class called TossingCoins and is the method public int consecutiveHeads(GVcoin c, int goal)
public class TossingCoins {
public int countHeads(GVcoin c, int goal){
while(c.numHeads() != goal) {
c.flip();
}
return c.numFlips();
}
public int flipForTails(GVcoin c, int goal){
while(c.numTails() != goal) {
c.flip();
}
return c.numFlips();
}
public int consecutiveHeads(GVcoin c, int goal){
while(c.numHeads() != goal){
c.flip();
}
}
// This method creates a TossingCoins object and calls the method for testing
public static void main(String[] args) {
TossingCoins game = new TossingCoins();
GVcoin c = new GVcoin();
int numHeads = game.countHeads(c, 100);
}
}
What I have tried so far is see if the numHeads is not equal to the goal of heads. Then if it is not equal to the number of heads(goal), I would set the flip to add one. Which wold count 1, 2, 3, 4, 5 and so on till it hits the amount flips needed to hit the number of heads(goal).
Suppose given a class Die and it contains a random value for a six sided die.
Another class PairOfDice needs to access getvalue in Die and store two die values.
An error: cannot find symbol occurs when PairOfDice is executed.
How can this problem be fixed? And are there any other suggestions for the java code?
public class Die {
public static Random rand = new Random();
private int sides; // Number of sides
private int value; // Die's value
public Die() {
sides = 6;
roll();
}
public void roll() {
value = rand.nextInt(sides) + 1;
}
public int getSides() {
return sides;
}
public int getValue() {
return value;
}
The second class given is:
public class PairOfDice {
private int dieOne;
private int dieTwo;
public void main(String[] args){
Die die;
die = new Die();
}
private void dieOne(int value){
dieOne = die.getValue();
}
private void dieTwo(int value){
dieTwo = die.getValue();
}
public int getDieOneValue(){
return dieOne;
}
public int getDieTwoValue(){
return dieTwo;
}
}
This quest should be generalized:
I wrote the Die class with two public constructors. If the constructor is without the parameter, default size of die is six, else you can have any number of sides.
Then, I wrote the Dices class with two constructors. First one have the number of dices (with 6 sides), and second one have the List of dices with preferred sides.
If you want to learn how to generalize the problem (any problem) you can check my code. (Of course, it can be done more efficiently and with more elegance, but here is the simple code):
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
class Die {
private Random RAND = new Random();
private int noOfSides;
private int value;
// Default constructor without the parameter of sides gives the six sized die
public Die() {
this.noOfSides = 6;
}
// The constructor WITH number of sides
public Die(int noOfSides) {
this.noOfSides = noOfSides;
}
// rolling the die
public void roll() {
this.value = RAND.nextInt(noOfSides) + 1;
}
public int getValue() {
if (value == 0) roll(); // if the die is never rolled -> roll it!
// else return the rolled value
return value;
}
// just for curiosities
public int getNoOfSides() {
return noOfSides;
}
public String toString() {
return "Die has the " + noOfSides + " sides, and the last roll value was " + getValue();
}
}
class Dices {
private int noOfDices;
private List<Die> myDices = new ArrayList<Die>();
// NO constructor without the number of dices
private Dices() {
}
public Dices(int noOfDices) {
this.noOfDices = noOfDices;
// example is for 6 sided dices
for (int i = 0; i < noOfDices; i++) {
getMyDices().add(new Die());
}
}
// example with the list of dices with predefined sizes
public Dices(List<Die> myDices){
this.myDices = myDices;
}
public List<Die> getMyDices() {
return myDices;
}
public String toString() {
String s = "";
for (Die die : getMyDices()) {
s = s + die + "\n";
}
return s;
}
}
public class Answer {
public static void main(String[] args) {
//test with two dices (6 size):
Dices twoDices = new Dices(2);
System.out.println(twoDices);
//test with 4 dices size 3, 7, 9, 22
Dices fourDices = new Dices
(List.of(new Die(3),
new Die(7),
new Die(9),
new Die(22)));
System.out.println(fourDices);
}
}
You can see, if the die is never rolled, getValue first roll the die, then return the value. Otherwise you can roll the die and the value will be stored into the private field value...
We will presume that the Die class works as expected.
So, one could think of these changes to PairOfDice that likely resolve the immediate issue.
public class PairOfDice {
private Die dieOne = new Die();
private Die dieTwo = new Die();
public static void main(String[] args) {
PairOfDice pair = new PairOfDice();
System.out.printf("Pair: %d - %d%n", pair.getDieOneValue(), pair.getDieTwoValue());
}
public int getDieOneValue() {
dieOne.roll();
return dieOne.getValue();
}
public int getDieTwoValue() {
dieTwo.roll();
return dieTwo.getValue();
}
}
The PairOfDice class should hold references to two dice (in the private Die instance variables).
The getDieXValue() methods work with the instance variables, and return a value, after generating a roll().
Now, the question is whether the requirement is to store the values of two dice, or just access to the ability to get the values. If truly the need is to store the values, then one could do:
public class PairOfDice {
private int dieOneValue;
private int dieTwoValue;
public PairOfDice {
Die die = new Die();
// get a value for the first die
die.roll();
dieOneValue = die.getValue();
// get a value for the 2nd die
die.roll();
dieTwoValue = die.getValue();
}
public int getDieOneValue() {
return dieOneValue;
}
...
Personally, if one is going to create objects, then store and use the objects.