views:

221

answers:

8

Sooo I started taking my second computer science class ever! For my first class we used python and for this class we're using Java. Our first assignment (pretty much just practice) is to convert this craps program from Python to Java and I'm just having a hell of a time.

Could someone please help with what I've done and umm give me some advice? Maybe a good site for a beginner.... Someone that kinda knows Python (only from a first CS course perspective).

1) In python

def winCraps():
roll = rollDice()
if roll == 7 or roll == 11:
    return True
elif roll == 2 or roll == 3 or roll == 12:
    return False
else:
    return rollForPoint(roll)

This is my attempt at the conversion of it over to java

public int winCraps{
    roll = rollDice();
    if (roll = 7 && 11){
    return (true);
    }
    else (roll =2 && 3 && 12){
    return(false);
    }
    else{
    return rollforPoint(roll);
}
}

2) Python

def rollDice():
raw_input("Press <Enter> to roll...")
die1 = randrange(1,7)
die2 = randrange(1,7)
sum = die1 + die2
print "You rolled", die1, "+", die2, "=", sum
return sum

This one confused the hell out of me. What would randrange be in Java??

Java

static int rollDice(){
System.out.print("Press <Enter> to roll...");
double die1 = Math.random();
double die2 = Math.random();
die1 = (int) die1*6+1;
 die2 = (int) die2*6+1;
 int sum = (int)die1 + (int)die2;
 System.out.println("You rolled  "+die1+ " + "+die2+ " = "+sum+".");
 return sum;
 }

*please bear in mind that I'm just learning this stuff lol

+2  A: 

You need to fix your if statements the "==" operator checks for equality, and you must put the variable you are checking against in each section of the statement.

public int winCraps{
    roll = rollDice();
    if (roll == 7 || roll == 11) {
        return true;
    }
    else if(roll == 2 || roll == 3 || roll == 12) {
        return false;
    }
    else{
        return rollforPoint(roll);
    }
}

In you rollDice() method, the way you assign values to each die is incorrect. I recommend reading up on random numbers (since this is homework, I'll leave that to you).

Also, remember in java you must always end each statement with a semicolon

Robert Greiner
Fred Larson
@Fred, noted, thank you.
Robert Greiner
I think you inherited that error from the OP's Java code. The Python correctly has 'or', though.
Fred Larson
Shouldn't it be `return false;` instead of `return(false);` (and the same with true)?
R. Bemrose
@Fred, I sure did. I noticed it after you pointed it out. Nice catch.
Robert Greiner
@R. Bemrose as a standard yes, but it is syntactically correct either way.
Robert Greiner
Also, the first else should be an else if.
Chris
A: 

I'd recommend that you look up the docs on randrange(). Once you know exactly what it does, google for the those keywords, plus the word Java.

One thing you'll quickly discover in working with languages is that the APIs can be very different. There might not be an equivalent of randrange in Java, but you might be able to find two or three functions that you can combine to do the same thing.

Kaleb Pederson
+1  A: 

Randrange can be replaced by methods in java.util.Random. Like Python, Java has an extensive standard library which you should reference.

Yann Ramin
A: 

One major error in your first program that you have in the Java conversion is the conditionals.

Something like (roll =2 && 3 && 12) assigns 2 to roll and then applies AND operators. You also forgot the if. You have elseif in Python.

You want something like:

else if(roll==2 || roll==3 || roll==12)

As for random numbers, there is a function for that in Java.

Uri
I'm wondering as to why this was downvoted?
Uri
+2  A: 

What would randrange be in Java?

You can get a random integer in a specific range from Java's Random class by calling the nextInt(int n) method. For example,

Random rand = new Random();
int a = rand.nextInt(7);

will give you a random integer >= 0 and < 7. This isn't exactly the same as randrange in Python, but you could use it as the index to an array of objects, or as the value of a roll of a single die.

Bill the Lizard
A: 

System.out.print isn't going to cause the system to wait for someone to hit the enter key. For that, you need to do something with System.in, most likely System.in.read(), as it blocks while waiting for input.

Also, in Java, a program starts executing with the main method. To be exact, an executable class starts something like this:

// You'll need the Random class, as per other answers
import java.util.Random;
// assuming WinCraps is the class name
public class WinCraps {
    // args in this example is a string array of command-line arguments
    public static void main(String[] args) {
        // This is where your main method (that calls winCraps?) would be
    }

    // Other methods
}

Also, any method in this class called directly from main must also be static.

R. Bemrose
what about using a scanner to read input? java.util.Scanner
Casey
A: 

1) In Java "OR" operator is "||" not "&&" and comparison operator is "==" as in Python

So

if roll == 7 or roll == 11:

Should be

if( roll == 7 || roll == 11 ) {

and not

 if( roll = 7 && 11 ){

2) randrange is : random generator from there you can search: Random in Java

Which will lead you to something like: Random.nextInt()

Use this algorithm ( a) search Internet for Python function, b) understand what it does c) search it in java ) for the next assignment you have and you're done.

You can always ask here again, that's what this site is all about after all

OscarRyz
what's with the down vote?
OscarRyz
Maybe it's because you have "roll == 7 or roll == 1" followed by "should be" followed by the exact same text as you did the first time?
Kaleb Pederson
@Kaleb: Probably doh!, well that's why downvoters should make a comment, so we can fix it. Thanks
OscarRyz
A: 

Write out in English what the Python program does. Go through it line by line and explain to yourself what computations are evoked, in other words...what is happening?

Afterwards, write the Java program from that description.

Never ever try to convert the text of a program from one language to another. You'll run into a LOT of problems that way because every language is different, no matter how similar they look.

omouse