Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java programming using eclipse. *************************************** Complete the displayRoll switch statement, andcomplete the roll() method. Study the comments for what to do. /* * Dice.java *

Java programming using eclipse.

*************************************** Complete the displayRoll switch statement, andcomplete the roll() method. Study the comments for what to do.

/* * Dice.java * * Uses the Math.random and a switch statement to display a roll of two dice * * Requirements: * * write two methods: roll which will set to random numbers between 1 and 6 simulating a die roll, * and calls displayRoll with each * displayRoll(): which uses a switch statement to display a single die roll with dots * * provide a loop so the user may roll the two dice as many times as they want * * * * Analysis * main: * Input: choice * process: none * output results * * roll: * parameters none * input: none * process d1,d2 * output none * return total * * displayRoll * parameters roll * input none * process none * output dots * return none * * * * * * design * * main * repeat * results = call roll() * output results * input choice * while user inputs y * * * roll (none) * d1 becomes random number between 1 and 6 * call displayRoll with d1 * d2 becomes random number between 1 and 6 * call displayRoll with d2 * return d1 + d2 * * displayRoll (roll) * switch on roll * for each case 1 to 6 display dots */

import java.util.*;

public class DiceLab {

public static void main(String[] args) { char choice; int results; Scanner input = new Scanner(System.in);; do{ results = roll(); System.out.println(" The roll is: " + results + " "); System.out.print("Roll again? y or n: "); choice = input.next().toLowerCase().charAt(0); while(choice != 'y' && choice != 'n'){ System.out.print("Error! " + choice + " not valid. Roll again? y or n: "); choice = input.next().toLowerCase().charAt(0); } }while(choice == 'y'); input.close(); }

public static int roll(){ int d1=0,d2=0; // finish this method return d1 + d2; }

public static void displayRoll(int r){ switch(r){ case 1: System.out.println(); System.out.println(" *"); System.out.println(); break; case 2: System.out.println("*"); System.out.println(); System.out.println(" *"); break; // add cases 3 through 6 } System.out.println(" "); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Autonomous Database In Enterprise Architecture

Authors: Bal Mukund Sharma, Krishnakumar KM, Rashmi Panda

1st Edition

1801072248, 978-1801072243

More Books

Students also viewed these Databases questions

Question

What is a verb?

Answered: 1 week ago

Question

3. Would you say that effective teamwork saved their lives?

Answered: 1 week ago