Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can you help me on Switch- Case statement in Java Die program. This is part of a

ID: 3728746 • Letter: C

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 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;

}

}

}

Explanation / Answer

Code:

import javafx.scene.image.Image;

//********************************************************************

// Die.java Author: Lewis/Loftus

//

// Represents one die (singular of dice) with faces showing values

// between 1 and 6.

//********************************************************************

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()

{
Image pic;
switch (faceValue)

{

case 1:
System.out.println("die1");

pic = image("die1.jpg");
break;
case 2:
System.out.println("die2");

pic = image("die2.jpg");
break;
case 3:
System.out.println("die3");

pic = image("die3.jpg");
break;
case 4:


System.out.println("die4");
pic = image("die4.jpg");
break;
case 5:
System.out.println("die5");
pic = image("die5.jpg");
break;
case 6:
System.out.println("die6");
pic = image("die6.jpg");

break;

return pic;

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote