hi i am doing a coin toss simulator for java that must be done a certain way. it must have a string for sideup to hold the string of "heads" or "tails" made by a no arg constructor, the toss method must be void and it must have a getsideup method, then we must run the coin toss 20 times and diplay the number of heads and tails... i can do it easy with none void methods and just returning the result, but getting around this void and getsideup is driving me nuts.
this is what i have so far.
import java.util.Random;
public class coin {
public static String sideUp;
public static void toss() {
Random rand = new Random();
int sideup = rand.nextInt(2);
if (sideup == 0) {
sideUp = "heads";
} else {
sideUp = "tails";
}
}
public static String getsideup() {
System.out.println(sideUp);
return sideUp;
}
public static void main(String[] args) {
// coin coin = new coin();
int hcount = 0;
int tcount = 0;
for (int i = 1; i <= 20; i++) {
if (getsideup().equals("heads")) {
hcount++;
} else {
tcount++;
}
}
System.out.println("total heads = " + hcount + " total tails = " + tcount);
}
}
im hoping someone can tell me what im doing wrong and put me in the right direction.
You're not calling toss() at the beginning of your loop. That's required to set a value to sideUp, and required to give sideUp to change every toss.
The simple fix is put toss in your for loop before the if statement but I see a lot that can be done here. First I would add a constructor for the Coin class and add hcount and tcount to the class variables and make heads and tails constants:
private String sideUp;
private int hcount;
private int tcount;
private static final String HEADS = "Heads";
private static final String Tails = "Tails";
Coin()
{
this.sideUp = HEADS;
this.hcount = 0;
this.tcount = 0;
}
Then I would make a method to check the toss:
public void checkToss()
{
if (getsideup().equals(HEADS))
hcount++;
else
tcount++;
}
Now add the toss() and checkCoin() method to the for loop before the if statement. It should look like this:
for (int i = 1; i <= 20; i++)
{
coin.toss();
coin.checkToss();
}
I would also make a getter for the heads count and tails count:
public int getHeadsCount()
{
return this.hcount;
}
public int getTailsCount()
{
return this.tcount;
}
Everything put together looks like this:
import java.util.Random;
public class Coin
{
private String sideUp;
private int hcount;
private int tcount;
private static final String HEADS = "Heads";
private static final String TAILS = "Tails";
Coin()
{
this.sideUp = HEADS;
this.hcount = 0;
this.tcount = 0;
}
public void toss()
{
Random rand = new Random();
int sideup = rand.nextInt(2);
if (sideup == 0)
sideUp = HEADS;
else
sideUp = TAILS;
}
public String getsideup()
{
System.out.println(sideUp);
return sideUp;
}
public void checkToss()
{
if (getsideup().equals(HEADS))
this.hcount++;
else
this.tcount++;
}
public int getHeadsCount()
{
return this.hcount;
}
public int getTailsCount()
{
return this.tcount;
}
public static void main(String[] args)
{
Coin coin = new Coin();
for (int i = 1; i <= 20; i++)
{
coin.toss();
coin.checkToss();
}
System.out.println("Total Heads = " + coin.getHeadsCount() + " Total Tails = " + coin.getTailsCount());
}
}
import java.util.Random;
public class coin
{
public static String sideUp;
public int hcount=0;
public int tcount=0;
public static void toss()
{
Random rand = new Random();
int sideup = rand.nextInt(2);
if (sideup == 0)
{
sideUp = "heads";
hcount++;
}
else
{
sideUp = "tails";
tcount++;
}
}
public static void main(String[] args)
{
for(int i=0;i<20;i++)
{toss();}
System.out.println("total heads = " + hcount + " total tails = " + tcount);
}
}
for(int i = 1; i <= 20; i++)
{
toss();
if (getsideup().equals("heads"))
{
hcount++;
} else
{
tcount++;
}
}
Related
Here is my program so far:
import java.util.Arrays;
public class HangmanWord {
private String[] possibleWords = {"laptop", "college", "programing"};
private String word;
private char[] progress;
private int wrongCount = 0;
public HangmanWord() {
int randomPossibleWord = (int) (Math.random() * possibleWords.length-1);
String word = possibleWords[randomPossibleWord];
char[] progress = new char[word.length()];
Arrays.fill(progress,'-');
}
public void display() {
System.out.print(progress);
System.out.println();
}
public boolean guess(char c) {
boolean matchFound = false;
for (int i = 0; i < word.length(); i++ ) {
if (word.charAt(i) == c ) {
progress[i] = c;
matchFound = true;
}
}
return false;
}
public boolean isSolved() {
for (int i = 0; i < progress.length; i++ ) {
if(progress[i] == '-'){
return false;
}
}
return true;
}
public int getWrongCount() {
return wrongCount;
}
public String getWord() {
return word;
}
}
public class Hangman {
public static void main(String[] args) {
int MAX_INCORRECT = 5;
System.out.println("Welcome to Hangman.");
HangmanWord wordObj = new HangmanWord();
System.out.print("Here is your word: ");
wordObj.display();
}
}
My output should look something like this:
Welcome to Hangman.
Here is your word: ------
However, I am getting the following errors:
Welcome to Hangman.
Here is your word: Exception in thread "main" java.lang.NullPointerException
at java.io.Writer.write(Writer.java:127)
at java.io.PrintStream.write(PrintStream.java:503)
at java.io.PrintStream.print(PrintStream.java:653)
at HangmanWord.display(HangmanWord.java:16)
at Hangman.main(Hangman.java:9)
You are redefining the variable word and progress in your constructor. You should simply use them without declaring them as a new variables because they're already defined. Currently you are defining them locally, the constructor works but uses those local defined variables and not your object's word and progress variables, therefore when you leave scope and call display() it will use your object's progress array which was never actually initialized.
Change it to the following so you aren't redefining the variables word and progress like so
public HangmanWord() {
int randomPossibleWord = (int) (Math.random() * possibleWords.length-1);
word = possibleWords[randomPossibleWord];
progress = new char[word.length()];
Arrays.fill(progress,'-');
}
I wrote this code that random picks two cards from a deck, but I'm having trouble trying to get it to compare, I've run into some issues and want to work that out step by step but unsure how to compare both statements with only one defined variable?
package question1;
public class HouseOfCards {
public static void main(String[] args) {
String [] SuitNames= {
"Spades","Diamonds","Clubs","Hearts"
};
String [] CardNames= {
"Ace","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"
};
String SuitName="hi",SuitName2="die";
String CardName="hi", CardName2="die";
for (int i=0; i<=1; i++){
int numCard=52;
int randNum=(int)(Math.random() * numCard);
int randNum2=(int)(Math.random() * numCard);
int suitNum = randNum / 13;
int cardNum = randNum % 13;
int suitNum2 = randNum2 / 13;
int cardNum2 = randNum2 % 13;
SuitName = SuitNames[suitNum];
CardName = CardNames[cardNum];
SuitName2 = SuitNames [suitNum2];
CardName2 = CardNames[cardNum2];
System.out.println(CardName + " of " + SuitName);
}
if (CardName.equals(CardName2)){
System.out.println("Same Rank");
}
else if (SuitName.equals(SuitName2)){
System.out.println("Same Suit");
}
}
}
i edited my code so that it displays if its the same rank between both random picked cards or same suit between random picked cards, but sometime it doesn't print the statement, why?
Try something like this:
public class HouseOfCards {
public class HouseOfCards {
public static void main(String[] args) {
int Spades=1;
String [] suitNames= {
"Spades","Diamonds","Clubs","Hearts"
};
String [] cardNames= {
"Ace","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"
};
String card1="";
String card2="";
String suitName="";
String cardName1="";
String suitName2="";
String cardName2="";
int cardPosition1 = 0;
int cardPosition2 = 0;
for (int i=0; i<=1; i++){
int numCard=52;
int randNum=(int)(Math.random() * numCard);
int suitNum = randNum / 13;
int cardNum = randNum % 13;
if(i==0) {
suitName = suitNames[suitNum];
cardName1 = cardNames[cardNum];
card1 = cardName1+" of "+suitName;
}
else {
suitName2 = suitNames[suitNum];
cardName2 = cardNames[cardNum];
card2 = cardName2+" of "+suitName2;
}
}
System.out.println(card1);
System.out.println(card2);
for (int i = 0; i < cardNames.length; i++) {
if(cardName1.equals(cardNames[i])) {
cardPosition1 = i;
}
if(cardName2.equals(cardNames[i])) {
cardPosition2 = i;
}
}
System.out.println(cardPosition1);
System.out.println(cardPosition2);
if(cardPosition1>cardPosition2) {
System.out.println("First card has bigger number");
}
else if (cardPosition1<cardPosition2) {
System.out.println("Second card has bigger number");
}
else if (cardPosition1==cardPosition2) {
System.out.println("Equal cards (probably different suites you can check it further)");
}
}
}
I have managed to print 200 random numbers, from those numbers I want the program to return the largest number out of it.
import java.util.Random;
class random {
public static int genRandom() {
return new Random().nextInt(1000000);
}
public static void main(String[] args) {
Random ran = new Random();
for (int i = 0; i < 200; i++) {
System.out.println(ran.nextInt(1000000));
}
}
}
Not the best answer but according to your code you can use :
static int maxNumber = 0;
public static int genRandom() {
return new Random().nextInt(1000000);
}
public static void main(String[] args) {
for (int i = 0; i < 200; i++) {
int randomNumber = genRandom();
if (randomNumber > maxNumber) {
maxNumber = randomNumber;
}
System.out.println(randomNumber);
}
System.out.println("The largest number is: " + maxNumber);
}
Java Code
public class TestProgram {
public static void main(String[] args) throws FileNotFoundException {
Random ran = new Random();
ArrayList<Integer> randNum = new ArrayList<Integer>();
for (int i = 0; i < 200; i++) {
//System.out.println(ran.nextInt(1000000));
randNum.add(ran.nextInt(1000000));
}
//System.out.print(randNum);
Collections.sort(randNum);
System.out.println(randNum);
System.out.println("Largest Number is " + randNum.get(randNum.size()-1));
}
}
I need help. I am supposed to run the coin toss 40 times and count each time it is heads and tails, but for some reason it keeps returning either all heads or all tails and I can't figure out why. I have been given a UML diagram that I must follow. It calls for a private String called sideUp, a public no arg constructor called Coin, a public void method called toss, and a String method called getSideUp.
Coin class:
import java.util.*;
public class Coin {
private String sideUp;
public Coin(){
toss();
}
public void toss(){
Random myRand = new Random();
int face = myRand.nextInt(2);
if(face == 0){
sideUp = "heads";
}
else{
sideUp = "tails";
}
getSideUp();
}
public String getSideUp(){
return sideUp;
}
}
CoinDriver:
public class CoinDriver {
public static void main(String[] args){
Coin coin = new Coin();
int headsCount = 0;
int tailsCount = 0;
for(int i = 1; i <= 40; i++){
System.out.println(coin.getSideUp());
if(coin.getSideUp().equals("heads")){
headsCount++;
}
else if(coin.getSideUp().equals("tails")){
tailsCount++;
}
}
System.out.println("Total number of heads: " + headsCount + "\nTotal number of tails: " + tailsCount);
}
}
You only call toss() in the constructor, so nothing changes it once it has been created.
Call toss() on the Coin in the for loop to get a new result.
You need to make sure that the coin is re-tossed each time you iterate the body of the for-loop in CoinDriver. Try these instead:
import java.util.*;
public class Coin {
public String toss() {
Random myRand = new Random();
int face = myRand.nextInt(2);
if (face == 0) {
return "heads";
} else{
return "tails";
}
}
}
public class CoinDriver {
public static void main(String[] args){
Coin coin = new Coin();
int headsCount = 0;
int tailsCount = 0;
for(int i = 1; i <= 40; i++) {
if (coin.toss().equals("heads")) {
headsCount++;
} else {
tailsCount++;
}
}
System.out.println("Total number of heads: " + headsCount + "\nTotal number of tails: " + tailsCount);
}
}
I'm supposed to write a code which checks if a given number belongs to the Fibonacci sequence. After a few hours of hard work this is what i came up with:
public class TP2 {
/**
* #param args
*/
public static boolean ehFibonacci(int n) {
int fib1 = 0;
int fib2 = 1;
do {
int saveFib1 = fib1;
fib1 = fib2;
fib2 = saveFib1 + fib2;
}
while (fib2 <= n);
if (fib2 == n)
return true;
else
return false;
}
public static void main(String[] args) {
int n = 8;
System.out.println(ehFibonacci(n));
}
}
I must be doing something wrong, because it always returns "false". Any tips on how to fix this?
You continue the loop while fib2 <= n, so when you are out of the loop, fib2 is always > n, and so it returns false.
/**
* #param args
*/
public static boolean ehFibonacci(int n) {
int fib1 = 0;
int fib2 = 1;
do {
int saveFib1 = fib1;
fib1 = fib2;
fib2 = saveFib1 + fib2;
}
while (fib2 < n);
if (fib2 == n)
return true;
else
return false;
}
public static void main(String[] args) {
int n = 5;
System.out.println(ehFibonacci(n));
}
This works. I am not sure about efficiency..but this is a foolproof program,
public class isANumberFibonacci {
public static int fibonacci(int seriesLength) {
if (seriesLength == 1 || seriesLength == 2) {
return 1;
} else {
return fibonacci(seriesLength - 1) + fibonacci(seriesLength - 2);
}
}
public static void main(String args[]) {
int number = 4101;
int i = 1;
while (i > 0) {
int fibnumber = fibonacci(i);
if (fibnumber != number) {
if (fibnumber > number) {
System.out.println("Not fib");
break;
} else {
i++;
}
} else {
System.out.println("The number is fibonacci");
break;
}
}
}
}
you can also use perfect square to check whether your number is Fibonacci or not. you can find the code and some explanation at geeksforgeeks.
you can also see stackexchange for the math behind it.
I'm a beginner but this code runs perfectly fine without any issues. Checked with test cases hopefully it'll solve your query.
public static boolean checkMember(int n) {
int x = 0;
int y = 1;
int sum = 0;
boolean isTrue = true;
for (int i = 1; i <= n; i++) {
x = y;
y = sum;
sum = x + y;
if (sum == n) {
isTrue=true;
break;
} else {
isTrue=false;
}
}
return isTrue;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.print(checkMember(n));
}