Any Java Experts? More Help! - Fantasy Baseball Cafe 2014

## Any Java Experts? More Help!

Moderator: Baseball Moderators

### Re: Any Java Experts?

Art Vandelay wrote:I saw the thread title and got all excited thinking I'd finally be able to put my degree in South Pacific Studies to use, but alas, you're talking about the computer program. I don't know anything about computers, but if anyone has any questions about the most heavily populated island in the world, let me know.

what is Indonesia's GDP
Dan Lambskin
Hall of Fame Hero

Posts: 10225
Joined: 20 Mar 2005
Home Cafe: Baseball

### Re: Any Java Experts?

Last year it was just over \$500 billion, what's interesting, though, is that the per capita GDP is less than \$4,000 (in US dollars). Indonesia's economy was making some huge strides under Yudhoyono before the global economic downturn slowed things down quite a bit. A major problem has been that prices of some of Indonesia's main exports--principally rubber, coffee, peanuts, and palm oil--have really been hard hit by the economic crisis, while prices of its major imports have been steadily increasing (fuel and food).
Art Vandelay
Hall of Fame Hero

Posts: 5265
Joined: 12 Mar 2005
Home Cafe: Baseball

### Re: Any Java Experts? Part 2!

Java is still extremely confusing for me. I've posted my next project and some of it has been written. However, I need some more help on where to go next. Any help would be awesome
sox 06
General Manager

Posts: 4425
Joined: 22 Apr 2006
Home Cafe: Baseball

### Re: Any Java Experts? Part 2!

sox 06 wrote:
public static int determineWinner(Card [] h1, Card [] h2) - This method should determine the winner of the game and return the win/loss amount based on the following rules:

If the user (hand h1) has blackjack (see method below), the return value is 15.
If the first hand (h1) has over 21, the return value is -10.
If the second hand has over 21, the return amount is 10.
If the first hand is higher than the second (but neither has busted), the return value is 10.
If the second hand is higher than the first (but neither has busted). the return value is -10.
If the first and second hand have the same value, no money is exchanged. The return value is 0

One quick suggestion before the rest, run your card ranks 1-13 and not 2-14. You will need a lot fewer checks later down the road if everything >= 10 is worth ten (not having to worry about the ace as much). Might not be a big deal in a small program like this, but would be much bigger headache if it grew beyond the simple stuff.

For the rest of it I'm not sure where you are having problems here. This is really basic logic. You will need to call a function to check the user hand for blackjack (doesnt appear they want you to check h2), if not then call a function to add each array up to give you the hand total variable (h1_total and h2_total or whatever you want to call it).After that it will be a if-elseif to go through the possible remaining results.

if(isBlackJack(h1)){ varResult = 15;
} //end blackjack checks
else {
// check non-blackjack results .. first setup hand totals
h1_total = getHandTotal(h1);
h2_total = getHandTotal(h2);
if(h1_total > 21)
{
varResult = -10;
}
else if( h2_total > 21)
{
varResult = 10;
}
else if(blah)
{
//blah
}
} // end non blackjack checks
return varResult;

public static int getHandTotal(Card [] h) - This method should return the total value of the hand passed into it. Various cards have the following values:

2 - 10: Two through ten points each respectively
Jack, Queen, or King: Ten points each
Ace: Eleven points (this differs from a regular Blackjack game where the value could be 1 or 11)

Loop through the array values, if its an ace add 11 to the total, if 10-K add 10, anything else add face value. Return the total.

public static boolean isBlackjack(Card [] h) - This method tests whether the hand passed in is a Blackjack hand. A Blackjack hand is any hand that sums to 21 with two cards. This requires that one be an ace and the other have a value of 10. This method will be used to determine the appropriate payout in the determineWinner() method.

Here is one that is much easier if you took my rerank suggestion earlier.

if( (h[0] == ace || h[1] == ace) && (h[0] == facecard || h[1] == facecard){
return TRUE; // blackjack
}
return FALSE; // everything else is not blackjack

Bury me a Royal.
Tavish
Mod in Retirement

Posts: 11070
(Past Year: 26)
Joined: 3 May 2004
Home Cafe: Baseball

### Re: Any Java Experts? Part 2!

Thank you so much Tavish for your help. I'm having trouble with two last things (I'm changing my major after struggling so much with Java). I need to get the game to loop while the user still has money. Also, I need it to print out the total amount of money that the user has. There is a printMenu towards the bottom, but I don't know how to utilize it. Thanks again

public static void main(String [] args) {
//establishing cards for hands
int userCardNumber=0;
int cpuCardNumber=0;
int top = 0;
int userNumCards;
int userMoney = 100;
int wantToPlay;
Scanner input = new Scanner(System.in);
Card [] deck = makeDeck();
Card[] userHand = new Card[7];//user hand array is 7 cards
Card[] cpuHand = new Card[7];//cpu hand array is 7 cards

shuffleDeck(deck);
userHand[userCardNumber++] = deck[top++];//deal a card to the user
userHand[userCardNumber++] = deck[top++];//deal a card to the user, top card also
cpuHand[cpuCardNumber++] = deck[top++];//deal a card to the cpu
cpuHand[cpuCardNumber++] = deck[top++];//deal a card to the cpu, top card also

System.out.println("User Money= \$" +userMoney);
System.out.print("User Hand:");
printHand(userHand);
System.out.println("Top Card:" + cpuHand[0]);//displaying top card
System.out.println("How Many Cards Do You Want?");//asking user to take card(s)
userNumCards = input.nextInt();//input is the # the user enters

if(determineWinner(userHand, cpuHand) >= 0){//comparing hands
System.out.println("User Has Won");
}
else{
System.out.println("CPU Has Won");
}
}

int h1_total;
int h2_total;
if(isBlackJack(h1)){//if hand one is BlackJack...
return 15;///...return 15
} //end blackjack checks
// check non-blackjack results .. first setup hand totals
h1_total = getHandTotal(h1);
h2_total = getHandTotal(h2);
if(h1_total > 21){
return -10;//if the first hand is greater than 21, money =-10
}
else if(h2_total > 21){
return 10;//if second hand is greater than 21, money =10
}
else if(h1_total > h2_total){
return 10;//if the first hand is greater than the second, money =10
}
else if(h1_total < h2_total){
return -10;//if the first hand total is less than the second hand total,
//money =-10
}
return 0;//if none of the above are true, then money =0
}

System.out.println();
System.out.println("***********OPTIONS***********");
System.out.println("* 1 - Print hand *");
System.out.println("* 2 - Take another card *");
System.out.println("* 3 - Stand on current hand *");
System.out.println("* 4 - Show money balance *");
System.out.println("*****************************");
System.out.println("*****************************");
System.out.println();

}

}
sox 06
General Manager

Posts: 4425
Joined: 22 Apr 2006
Home Cafe: Baseball

### Re: Any Java Experts? Part 2!

I saw the thread title and I thought that it was about coffee. I love coffee.

I know nothing about programming except that it makes porn machines work.
Mookie4ever

Posts: 19545
(Past Year: 280)
Joined: 17 Dec 2003
Home Cafe: Baseball
Location: Hakuna, Montana

### Re: Any Java Experts? Part 2!

Look at your code for the golf game. You will use the same type of while loop to print the menu and ask for the user input.

Code: Select all
`if(determineWinner(userHand, cpuHand) >= 0){//comparing handsSystem.out.println("User Has Won");}else{System.out.println("CPU Has Won");}}int h1_total;int h2_total;if(isBlackJack(h1)){//if hand one is BlackJack...return 15;///...return 15} //end blackjack checks`

I'm assuming all the hand comparisons should be inside a function you are calling "determineWinner". determineWinner will return the value you want to change the players money by so set up a variable and make its value what is returned.

Code: Select all
`int handResult = determineWinner(userHand, cpuHand); // returned valueif(handResult >= 0){//comparing handsSystem.out.println("User Has Won");}else{System.out.println("CPU Has Won");}userMoney += handResult;  //win or lose you add the value to the player's money pool}public static int determineWinner(Card[] h1, Card[]h2){int h1_total;int h2_total;if(isBlackJack(h1)){//if hand one is BlackJack...return 15;} //end blackjack checks`

Bury me a Royal.
Tavish
Mod in Retirement

Posts: 11070
(Past Year: 26)
Joined: 3 May 2004
Home Cafe: Baseball

### Re: Any Java Experts? More Help!

I'm back again and I've updated my original post with yet another Java problem. Thanks again for any hints
sox 06
General Manager

Posts: 4425
Joined: 22 Apr 2006
Home Cafe: Baseball

### Re: Any Java Experts? More Help!

Something like this?

Code: Select all
`public class FibonacciSum {      static final long FINAL_SUM = 4000000;              public static void main(String[] args) {          long i = 1;          long j = 2;          long k = j;               long sum = 0;                    if( k%2 == 0) {                  sum = sum + k;          }          k = i + j;                        while(k < FINAL_SUM) {              if( k%2 == 0) {                  sum = sum + k;              }              k = i + j;              i = j;              j = k;                    }                           System.out.println(" the sum is "+sum);      }              } `
The Artful Dodger
Chief Wikitect

Posts: 21943
(Past Year: 677)
Joined: 3 Feb 2006
Home Cafe: Baseball

### Re: Any Java Experts? More Help!

The Artful Dodger wrote:Something like this?

Code: Select all
`public class FibonacciSum {      static final long FINAL_SUM = 4000000;              public static void main(String[] args) {          long i = 1;          long j = 2;          long k = j;               long sum = 0;                    if( k%2 == 0) {                  sum = sum + k;          }          k = i + j;                        while(k < FINAL_SUM) {              if( k%2 == 0) {                  sum = sum + k;              }              k = i + j;              i = j;              j = k;                    }                           System.out.println(" the sum is "+sum);      }              } `

The answer I was getting with my code = 4613730
Do I just need to change the value of my variables a bit?
Thanks for the help
sox 06
General Manager

Posts: 4425
Joined: 22 Apr 2006
Home Cafe: Baseball

PreviousNext

### Who is online

Users browsing this forum: No registered users and 4 guests

Forums Articles & Tips Sleepers Rankings Leagues

Today's Games
Sunday, Sep. 21
(All times are EST, weather icons show forecast for game time)

 • Toronto at NY Yankees(1:05 pm)
 • Washington at Miami(1:10 pm)
 • NY Mets at Atlanta(1:35 pm)
 • Boston at Baltimore(1:35 pm)
 • Milwaukee at Pittsburgh(1:35 pm)
 • Chi White Sox at Tampa Bay(1:40 pm)
 • Cleveland at Minnesota(2:10 pm)
 • Detroit at Kansas City(2:10 pm)
 • Seattle at Houston(2:10 pm)
 • LA Dodgers at Chi Cubs(2:20 pm)
 • Texas at LA Angels(3:35 pm)