Question
Can you help me on Switch- Case statement in Java Die program. This is part of a larger program, but, I just need help with
Can you help me on Switch- Case statement in Java Die program.
This is part of a larger program, but, I just need help with the bottom section of the code I provided.
Just need help with the bottom small section of code. What I am trying to accomplish, I need to have six images come from a separate file to this Die program. And then a PairOfDice program will get two images one at a time, die1 die2, then off to a GUIDriver program. What I am trying to do is use a switch -case block to retrieve the images as called. I am new at coding and think I have made mistakes in my code, could you please check and correct any errors. If there is a better way to retrieve these images, please let me know. Also, I think I might need the break; statement between case statements? Not sure
Thank-you
import javafx.scene.image.Image;
//********************************************************************
// Die.java Author: Lewis/Loftus
//
// Represents one die (singular of dice) with faces showing values
// between 1 and 6.
//********************************************************************
public class Die
{
private final int MAX = 6; // maximum face value
private int faceValue; // current value showing on the die
//-----------------------------------------------------------------
// Constructor: Sets the initial face value.
//-----------------------------------------------------------------
public Die()
{
faceValue = 1;
}
//-----------------------------------------------------------------
// Rolls the die and returns the result.
//-----------------------------------------------------------------
public int roll()
{
faceValue = (int)(Math.random() * MAX) + 1;
return faceValue;
}
//-----------------------------------------------------------------
// Face value mutator.
//-----------------------------------------------------------------
public void setFaceValue (int value)
{
faceValue = value;
}
//-----------------------------------------------------------------
// Face value accessor.
//-----------------------------------------------------------------
public int getFaceValue()
{
return faceValue;
}
//-----------------------------------------------------------------
// Returns a string representation of this die.
//-----------------------------------------------------------------
public String toString()
{
String result = Integer.toString(faceValue);
return result;
}
//----------------------------------------------------------------
//getter of 6 die images from file
//----------------------------------------------------------------
public Image getDieImage()
{
switch (faceValue)
{
Image pic;
case 1:
pic = image("die1.jpg");
case 2:
pic = image("die2.jpg");
case 3:
pic = image("die3.jpg");
case 4:
pic = image("die4.jpg");
case 5:
pic = image("die5.jpg");
case 6:
pic = image("die6.jpg");
return pic;
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started