Question

Goal: Translate this Python script into Java: magic8ball.py Deliverable: Magic8Ball.java Grading Breakdown: Functionality: 35; Source code...

  • Goal: Translate this Python script into Java: magic8ball.py
  • Deliverable: Magic8Ball.java
  • Grading Breakdown: Functionality: 35; Source code comments: 5; Indentation: 5; Properly Submitted: 5.
  • Example: Here is a Java class that simulates the roll of a single die:
    import java.util.Random;
    public class DiceRoll {
        public static void main(String[ ] args) 
        {
            Random r = new Random( );
    
            int roll1 = r.nextInt(6) + 1;
            int roll2 = r.nextInt(6) + 1;
            System.out.println("Sum of rolls:" + (roll1 + roll2));
        }
    }
    
    Here is the original Python script:
    import random
    roll1 = random.randrange(6) + 1
    roll2 = random.randrange(6) + 1
    print(f"Sum of rolls: {roll1 + roll2}")
0 0
Add a comment Improve this question Transcribed image text
Answer #1
  • import random roll1 = random.randrange(6) + 1 roll2 = random.randrange(6) + 1 print(f"Sum of rolls: {roll1 + roll2}")

The corresponding Java code is given as:

CODE

import java.util.Random; public class Main { public static void main(String[] args) { Random rand = new Random(); int roll1 = rand.nextInt(6) + 1; int roll2 = rand.nextInt(6) + 1; System.out.println("Sum of rolls: " + (roll1 + roll2)); } }
Add a comment
Know the answer?
Add Answer to:
Goal: Translate this Python script into Java: magic8ball.py Deliverable: Magic8Ball.java Grading Breakdown: Functionality: 35; Source code...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Java Coding) Game1: You win if one get one six in four rolls of one dice. (To be Simulated 1,000,000 times) Game2: You win if one get double sixes in twenty four rolls of two dice. (To be simulated 1...

    (Java Coding) Game1: You win if one get one six in four rolls of one dice. (To be Simulated 1,000,000 times) Game2: You win if one get double sixes in twenty four rolls of two dice. (To be simulated 1,000,000 times) Given below that the class die, oddstester, and gameSimulator(partially complete and the bold missing parts need to be done), need to find the probability of winning game 1 and 2 (after the 1 million plays) hence the getWins and...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • JAVA I need a switch so that users can input one selection then another. These selections...

    JAVA I need a switch so that users can input one selection then another. These selections are for a simple calculator that uses random numbers 1. + 2. - 3. * 4./ 5. RNG. This has to be simple this is a beginners class. Here is what I have import java.util.Scanner; import java.util.Random; import java.util.*; public class You_Michael02Basic_Calculator {    public static void main(String[] args) {        // Generate random numbers        int num1,num2;        num1 =(int)(Math.random()*100+1);...

  • I am currently doing homework for my java class and i am getting an error in...

    I am currently doing homework for my java class and i am getting an error in my code. import java.util.Scanner; import java.util.Random; public class contact {       public static void main(String[] args) {        Random Rand = new Random();        Scanner sc = new Scanner(System.in);        System.out.println("welcome to the contact application");        System.out.println();               int die1;        int die2;        int Total;               String choice = "y";...

  • Please Help! I need to update the code below to meet these criteria! 1. The constructor...

    Please Help! I need to update the code below to meet these criteria! 1. The constructor of die class initializes face of Die to 3 2. roll method updates face of die to value from 1-6 3. getFace() method provides current face to the main program 4. main create a Die object 5. main create a Die object 6. main rolls die 5 times 7. main computes sum of rolls 8. main computes actual average of the rolls <--------- Code...

  • JAVA Modify the code to create a class called box, wherein you find the area. I...

    JAVA Modify the code to create a class called box, wherein you find the area. I have the code in rectangle and needs to change it to box. Here is my code. Rectangle.java public class Rectangle { private int length; private int width;       public void setRectangle(int length, int width) { this.length = length; this.width = width; } public int area() { return this.length * this.width; } } // CONSOLE / MAIN import java.awt.Rectangle; import java.util.Scanner; public class Console...

  • This is a basic Java Question referencing Chapter 5 methods. The goal is to make a...

    This is a basic Java Question referencing Chapter 5 methods. The goal is to make a quick program that has the computer guess a number, after which the user will input if they would like to play the easy, medium or hard level. After this point the user will be allowed to guess a certain amount of times, before the computer tells them they are correct, incorrect, and gives them the guessed number. The Question is as follows: Define a...

  • JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The...

    JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The game chooses a random phrase from the list. This is the phrase the player tries to guess. The phrase is hidden-- all letters are replaced with asterisks. Spaces and punctuation are left unhidden. So if the phrase is "Joe Programmer", the initial partially hidden phrase is: *** ********** The user guesses a letter. All occurrences of the letter in the phrase are replaced in...

  • USE JAVA Using recursion, find the largest element in an array. Skeleton code is provided. Please...

    USE JAVA Using recursion, find the largest element in an array. Skeleton code is provided. Please do not change the DataSetDemo code. Hint: Find the largest element in the subset containing all but the last element. Then compare that maximum to the value of the last element. Skeleton Code: DataSet: /** Computes the maximum of a set of data values. */ public class DataSet { private int[] values; private int first; private int last; /** Constructs a DataSet object. @param...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT