I don\'t know what is wrong with this roulette program I can\'t seem to get it to run.
Game Class:
public class Game {
public static void main(String[] args) {
Table table = new Table();
BinBuilder bb = new BinBuilder();
Outcome black = new Outcome(\"Black\", 35);
Bet bet = new Bet(10, black);
table.placeBet(bet);
Bin bin = bb.wheel.get(8);
System.out.println(bin.toString());
System.out.println(table.bets.toString());
System.out.println(black.toString());
ListIterator i = table.bets.listIterator();
Iterator b = bin.outcomes.iterator();
while(i.hasNext()) {
System.out.println(i.next().outcome.name.toString());
while(b.hasNext()){
System.out.println(b.next().name.toString());
if(i.next().outcome.equals(b.next())){
System.out.println(\"Win!\");
}
else{
System.out.println(\"Win :/\");
}
}
}
}
}
Player Class
public class Player {
public Table table;
public Outcome black;
public Bet bet;
public Player(Table table) {
table = new Table();
black = new Outcome(\"Black\", 1);
}
void placeBets() {
Bet bet = new Bet(100, black);
table.placeBet(bet);
}
void win(Bet bet) {
System.out.println(\"You\'ve won: \" + bet.winAmount());
}
void lose(Bet bet) {
System.out.println(\"You lost!\" + bet.loseAmount() + \":/\");
}
}
Outcome class
public class Outcome implements Comparable {
public String name;
public int odds;
public Outcome(String name, int odds){
this.name = name;
this.odds = odds;
}
public int winAmount(int amount){
return amount*this.odds;
}
public boolean equals(Outcome other){
return (this.name.equals(other.name));
}
public String toString() {
Object[] values= { name, new Integer(odds) };
String msgTempl= \"{0} ({1}:1)\";
return MessageFormat.format( msgTempl, values );
}
@Override
public int compareTo(E arg0) {
if(this.equals(arg0)){
return 0;
}
return 1;
}
}
Table Class
public class Table {
public int limit = 1000;
public LinkedList bets;
public Table() {
bets = new LinkedList();
}
public boolean isValid(Bet bet) {
int sum = 0;
for(Bet bett: bets) {
sum += bett.amountBet;
}
return (sum>limit);
}
public void placeBet(Bet bet) {
bets.add(bet);
}
ListIterator iterator() {
return bets.listIterator();
}
}
Wheel Class
public class Wheel extends TreeSet {
Vector bins;
NonRandom rng;
Set all_outcomes;
Wheel(NonRandom rng){
this.rng = rng;
rng = new NonRandom();
all_outcomes = new TreeSet();
bins = new Vector(38);
for (int i=0; i<38; i++){
bins.add(i, new Bin());
}
}
Bin next(){
int rand = rng.next(38);
return bins.elementAt(rand);
}
Bin get(int bin){
return bins.elementAt(bin);
}
public Outcome getOutcome( String name ){
TreeSet result= new TreeSet();
for( Iterator i = all_outcomes.iterator(); i.hasNext(); ) {
Outcome oc= i.next();
if( oc.name.contains(name) ) {result.add( oc );}
}
return result.first();
}
public void addOutcome(int bin, Outcome outcome) {
all_outcomes.add(outcome);
this.bins.elementAt(bin).add(outcome);
}
}
Bet Class
public class Bet {
public int amountBet;
public Outcome outcome;
public Bet(int amount, Outcome outcome) {
this.outcome = o.
internship ppt on smartinternz platform as salesforce developer
I dont know what is wrong with this roulette program I cant seem.pdf
1. I don't know what is wrong with this roulette program I can't seem to get it to run.
Game Class:
public class Game {
public static void main(String[] args) {
Table table = new Table();
BinBuilder bb = new BinBuilder();
Outcome black = new Outcome("Black", 35);
Bet bet = new Bet(10, black);
table.placeBet(bet);
Bin bin = bb.wheel.get(8);
System.out.println(bin.toString());
System.out.println(table.bets.toString());
System.out.println(black.toString());
ListIterator i = table.bets.listIterator();
Iterator b = bin.outcomes.iterator();
while(i.hasNext()) {
System.out.println(i.next().outcome.name.toString());
while(b.hasNext()){
System.out.println(b.next().name.toString());
if(i.next().outcome.equals(b.next())){
System.out.println("Win!");
}
else{
System.out.println("Win :/");
}
}
}
}
}
2. Player Class
public class Player {
public Table table;
public Outcome black;
public Bet bet;
public Player(Table table) {
table = new Table();
black = new Outcome("Black", 1);
}
void placeBets() {
Bet bet = new Bet(100, black);
table.placeBet(bet);
}
void win(Bet bet) {
System.out.println("You've won: " + bet.winAmount());
}
void lose(Bet bet) {
System.out.println("You lost!" + bet.loseAmount() + ":/");
}
}
Outcome class
public class Outcome implements Comparable {
public String name;
public int odds;
public Outcome(String name, int odds){
this.name = name;
this.odds = odds;
}
public int winAmount(int amount){
3. return amount*this.odds;
}
public boolean equals(Outcome other){
return (this.name.equals(other.name));
}
public String toString() {
Object[] values= { name, new Integer(odds) };
String msgTempl= "{0} ({1}:1)";
return MessageFormat.format( msgTempl, values );
}
@Override
public int compareTo(E arg0) {
if(this.equals(arg0)){
return 0;
}
return 1;
}
}
Table Class
public class Table {
public int limit = 1000;
public LinkedList bets;
public Table() {
bets = new LinkedList();
}
public boolean isValid(Bet bet) {
int sum = 0;
for(Bet bett: bets) {
sum += bett.amountBet;
}
return (sum>limit);
4. }
public void placeBet(Bet bet) {
bets.add(bet);
}
ListIterator iterator() {
return bets.listIterator();
}
}
Wheel Class
public class Wheel extends TreeSet {
Vector bins;
NonRandom rng;
Set all_outcomes;
Wheel(NonRandom rng){
this.rng = rng;
rng = new NonRandom();
all_outcomes = new TreeSet();
bins = new Vector(38);
for (int i=0; i<38; i++){
bins.add(i, new Bin());
}
}
Bin next(){
int rand = rng.next(38);
return bins.elementAt(rand);
}
Bin get(int bin){
return bins.elementAt(bin);
5. }
public Outcome getOutcome( String name ){
TreeSet result= new TreeSet();
for( Iterator i = all_outcomes.iterator(); i.hasNext(); ) {
Outcome oc= i.next();
if( oc.name.contains(name) ) {result.add( oc );}
}
return result.first();
}
public void addOutcome(int bin, Outcome outcome) {
all_outcomes.add(outcome);
this.bins.elementAt(bin).add(outcome);
}
}
Bet Class
public class Bet {
public int amountBet;
public Outcome outcome;
public Bet(int amount, Outcome outcome) {
this.outcome = outcome;
this.amountBet = amount;
}
public int winAmount() {
int winAmount = this.outcome.odds * this.amountBet;
return winAmount + this.amountBet;
}
public int loseAmount() {
int loseAmount = this.amountBet;
return loseAmount;
6. }
public String toString() {
return this.amountBet+" on "+"["+this.outcome.toString()+"]";
}
}
Bin Class
public class Bin {
public TreeSet outcomes;
int i =0;
Bin(){
outcomes = new TreeSet();
}
Bin(Outcome[] outcomes){
this();
for(Outcome anOutcome: outcomes){
add(anOutcome);
}
}
Bin(Collection outcomes){
this();
for(Outcome anOutcome: outcomes){
add(anOutcome);
}
}
void add(Outcome outcome){
this.outcomes.add(outcome);
}
public String toString(){
String string ="";
Iterator it1 =this.outcomes.iterator();
while(it1.hasNext()){
string += "["+it1.next().toString()+"]";
7. }
return string;
}
}
BinBuilder Class
public class BinBuilder {
public Wheel wheel;
public NonRandom nrng;
public BinBuilder() {
this.nrng = new NonRandom();
this.wheel = new Wheel(nrng);
buildBins(wheel);
}
public Wheel getWheel() {
return wheel;
}
private void buildBins(Wheel wheel){
straightBets(wheel);
splitBets(wheel);
streetBets(wheel);
cornerBets(wheel);
lineBets(wheel);
dozenBets(wheel);
columnBets(wheel);
evenMoneyBets(wheel);
}
void straightBets(Wheel wheel){
for(int i = 1; i<37; i++){
Outcome outcome = new Outcome(""+i, 35);
String u = ""+i;
wheel.addOutcome(i , outcome);
}
}
void splitBets(Wheel wheel){
for(int r = 0; r<12; r++){
8. int n = (3*r) +1;
Outcome outcome = new Outcome("'"+n+", "+n+1+"'", 17);
wheel.addOutcome(n , outcome);
wheel.addOutcome(n+1 , outcome);
n = (3*r) +2;
Outcome outcome2 = new Outcome("'"+n+", "+n+1+"'", 17);
wheel.addOutcome(n , outcome2);
wheel.addOutcome(n+1, outcome2);
}
for(int n = 1; n<34; n++){
Outcome outcomen = new Outcome(n+", "+n+3, 17);
wheel.addOutcome(n , outcomen);
wheel.addOutcome(n+3 , outcomen);
}
}
void streetBets(Wheel wheel){
for(int r = 0; r<12; r++){
int n = (3*r) +1;
Outcome outcome = new Outcome(""+n, 11);
wheel.addOutcome(n , outcome);
wheel.addOutcome(n+1 , outcome);
wheel.addOutcome(n+2 , outcome);
}
}
void cornerBets(Wheel wheel){
for(int r = 0; r<11; r++){
int n = (3*r) +1;
Outcome outcome = new Outcome(n +", " +n +1+", " +n +3+", " +n +4, 8);
wheel.addOutcome(n , outcome);
wheel.addOutcome(n+1 , outcome);
wheel.addOutcome(n+3 , outcome);
wheel.addOutcome(n+4 , outcome);
int s = (3*r) +2;
Outcome outcome2 = new Outcome(s +", "+ s +1+", "+ s+3+", "+ s +4, 17);
wheel.addOutcome(n , outcome2);
wheel.addOutcome(n+1, outcome2);
10. Outcome outcome_l = new Outcome("Low",1);
wheel.addOutcome(n , outcome_l);
}
else {
Outcome outcome_h = new Outcome("High",1);
wheel.addOutcome(n , outcome_h);
}
if(n%2==0){
Outcome outcome_e = new Outcome("Even",1);
wheel.addOutcome(n , outcome_e);
}
else {
Outcome outcome_o = new Outcome("Odd",1);
wheel.addOutcome(n , outcome_o);
}
int[] Red = {1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36};
if (Arrays.binarySearch(Red, n) >= 0){
Outcome outcome_r = new Outcome("Red",1);
wheel.addOutcome(n , outcome_r);
}
else {
Outcome outcome_b = new Outcome("Black",1);
wheel.addOutcome(n , outcome_b);
}
}
}
}
NonRandom Class
public class NonRandom {
private long value;
public void NonRandom(){
value = 0;
}
public int next(int bits) {
return (int) value;
11. }
public void setSeed(long value) {
this.value = value;
}
}
Solution
In the main method you are calling the i.next() and b.next() method twice in a loop which cause
an error. I have made the changes. Kindly go through the changes that i have made.
------------------------------------------------code--------------------------------------------------------------
---------------------------------
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Set;
import java.util.TreeSet;
import java.util.Vector;
import java.util.LinkedList;
public class Game {
public static void main(String[] args) {
Table table = new Table();
BinBuilder bb = new BinBuilder();
Outcome black = new Outcome("Black", 35);
Bet bet = new Bet(10, black);
table.placeBet(bet);
Bin bin = bb.wheel.get(8);
System.out.println(bin.toString());
System.out.println(table.bets.toString());
System.out.println(black.toString());
12. ListIterator i = table.bets.listIterator();
Iterator b = bin.outcomes.iterator();
while(i.hasNext()) {
Outcome outcome=i.next().outcome;
System.out.println(outcome);
while(b.hasNext()){
Outcome binOutcome=b.next();
System.out.println(binOutcome);
if(outcome.equals(binOutcome)){
System.out.println("Win!");
}
else{
System.out.println("Win :/");
}
}
}
}
}
class Player {
public Table table;
public Outcome black;
public Bet bet;
public Player(Table table) {
table = new Table();
black = new Outcome("Black", 1);
}
void placeBets() {
Bet bet = new Bet(100, black);
table.placeBet(bet);
}
13. void win(Bet bet) {
System.out.println("You've won: " + bet.winAmount());
}
void lose(Bet bet) {
System.out.println("You lost!" + bet.loseAmount() + ":/");
}
}
class Outcome implements Comparable {
public String name;
public int odds;
public Outcome(String name, int odds){
this.name = name;
this.odds = odds;
}
public int winAmount(int amount){
return amount*this.odds;
}
public boolean equals(Outcome other){
return (this.name.equals(other.name));
}
public String toString() {
Object[] values= { name, new Integer(odds) };
String msgTempl= "{0} ({1}:1)";
return MessageFormat.format( msgTempl, values );
}
@Override
public int compareTo(E arg0) {
if(this.equals(arg0)){
14. return 0;
}
return 1;
}
}
class Table {
public int limit = 1000;
public LinkedListbets;
public Table() {
bets = new LinkedList();
}
public boolean isValid(Bet bet) {
int sum = 0;
for(Bet bett: bets) {
sum += bett.amountBet;
}
return (sum>limit);
}
public void placeBet(Bet bet) {
bets.add(bet);
}
ListIterator iterator() {
return bets.listIterator();
}
}
class Wheel extends TreeSet {
Vector bins;
NonRandom rng;
Set all_outcomes;
15. Wheel(NonRandom rng){
this.rng = rng;
rng = new NonRandom();
all_outcomes = new TreeSet();
bins = new Vector(38);
for (int i=0; i<38; i++){
bins.add(i, new Bin());
}
}
Bin next(){
int rand = rng.next(38);
return bins.elementAt(rand);
}
Bin get(int bin){
return bins.elementAt(bin);
}
public Outcome getOutcome( String name ){
TreeSet result= new TreeSet();
for( Iterator i = all_outcomes.iterator(); i.hasNext(); ) {
Outcome oc= i.next();
if( oc.name.contains(name) ) {result.add( oc );}
}
return result.first();
}
public void addOutcome(int bin, Outcome outcome) {
all_outcomes.add(outcome);
this.bins.elementAt(bin).add(outcome);
16. }
}
class Bet {
public int amountBet;
public Outcome outcome;
public Bet(int amount, Outcome outcome) {
this.outcome = outcome;
this.amountBet = amount;
}
public int winAmount() {
int winAmount = this.outcome.odds * this.amountBet;
return winAmount + this.amountBet;
}
public int loseAmount() {
int loseAmount = this.amountBet;
return loseAmount;
}
public String toString() {
return this.amountBet+" on "+"["+this.outcome.toString()+"]";
}
}
class Bin {
public TreeSet outcomes;
int i =0;
Bin(){
outcomes = new TreeSet();
}