Question: Need help with a couple programs in Java Program 1 ) The Dice Game Write a TwoDice class with instance variables face, a two element
"Need help with a couple programs in Java
Program The Dice Game
Write a TwoDice class with instance variables face, a two element array to hold values you can roll with a pair of dice.
Default constructor places random values on each face
Constructor with parameters n m for the initial face values. Validate the parameters to allow only
Accessors getFace getFace and getTotal which returns an integer.
Mutators setFace setFace to set each to only
Mutator roll which randomize the numbers on face and face to
toString method to return a string like or
equals method to compare a pair of dice with another pair of dice. Return true if they have the same face values.
printMe method for DEBUG. This should simply print the value in toString
Write a client DiceClientYOURNAME.
Instantiate and initialize a pair of dice
Instantiate score, a element integer array of integers and initialize it to all
Write a play method for a new game, it should reset the game and then continuously roll the dice until you win or lose or the array is full.
Each roll is entered in the array until you win, lose, or the array is full.
Write a printArray method to print the score array on ONE line. Make it easy to read by adding a after every scores eg
The rules of the game: A player rolls the two dice and adds the number shown on them.
Roll the two dice up to times and save the total in the array.
If the total is or you win. Put this in the array, report the results and exit the game.
If the total is or you lose. Put this in the array, report the results and exit the game.
If the total is anything else, save the total as the goal put it in the array, and you get to roll again.
The new objective is to roll the same total as the goal
You keep rolling until you either get goal or a Show the array.
If you roll goal you win. Put this in the array, report the results and exit the game.
If you roll a you lose. Put this in the array, report the results and exit the game.
Sample Terminal
You win:
You win:
You win:
You lose:
You lose:
You win:
Program Arrays of Objects Copy the files Song.java, CompactDiscYOURNAME.java and ClassicSongs.java as directed by your instructor. Song.java will be completed in class. ClassicSongs.java is the data file that will be used by CompactDiscYOURNAME.java, the file you will be editing.
CompactDiscYOURNAME.java:
This program creates a list of songs for a CD by reading from a file.
import java.io;
import java.util.Scanner;
public class CompactDiscYOURNAME
public void test throws IOException File inputFile new FileClassicSongsjava"";
Scanner in new ScannerinputFile;
String title;
String artist; int count;
ADD LINES FOR TASK # HERE
Declare an element array of Song objects, called cd
while inhasNextLine
String oneLine innextLine;
System.out.printlnDebugcount: oneLine ;
Fill the array by creating a new song with the title and artist and storing it in the appropriate position in the array. count;
Print each element of the cd array
System.out.println
Finished ;
inclose;
ClassicSongs.java:
Ode to Joy, Bach
The Sleeping Beauty, Tchaikovsky
Lullaby, Brahms
Canon, Bach
S QO ymphony No Beethoven
The Blue Danube Waltz, Strauss
Song.java:
public class Song
private String title; The song's title
private String artist; The song's artist
Constructor @param title A reference to a String object containing the song's title.
@param artist A reference to a String object containing the song's artist.
public SongString title, String artist
this.title title; this.artist artist; The toString method @return A String object containing the name of the song and the artist. public String toString
return title by artist
;
In CompactDisc.java, there are comments indicating where the missing code is to be placed. Declare an array of Songs, called cd with a size of
Fill the array by creating a new song with the title and artist and storing it in the appropriate position in the array.
Print the contents of the array to the console.
Compile, debug, and run. Your terminal output should be:
Contents of Classics:
Ode to Joy by Bach
The Sleeping Beauty by Tchaikovsky
Lullaby by Brahms
Canon by Bach
Symphony No by Beethoven
The Blue Danube Waltz by Strauss
Program Code Puzzle
Instance Variables:
n
array
Use these to write a one dimension array array students who last name AG
Constructor: NxNYourInitials int n creates an array with n elements AND filled with n For example NxNYourInitials instantiate n and the arrays with
Methods recommended order: toString printMe addRow int rr shake addCol int cc addDiag addDiagYou can use printMe to check the other methods.
ClientNxM will instantiate an puzzle and then solve it Have a method to solve a x and another method to solve a x I will run them when grading your work.
I've personally completed about half the program and here it is:
import java.util.Random;
import java.util.ArrayList;
public class NxNAA
private int n;
private int arr;
size n n;
sqr new intsize;
public String toString
return arr.toStringsqr;
public void printMe
System.out.printlnarraytoStringsqr;
public void shake
forint i ; i size; i;
sqri i ;
Random rdm new Random;
int temp ;
int randomVal rdmnextIntsize;
int randomVal rdmnextIntsize;
whilerandomVal randomVal
temp sqrrandomVal;
sqrrandomVal sqrrandomVal;
sqrrandomVal temp;
public int addRow int n
add a specific row to get the sum
int sum ;
int root int Math.sqrtsize;
for int i root n; i Math.sqrtsize n root; i
sum sqri;
return sum;
public int addCol int n
int sum ;
for int i n; i size; i n if n doesn't work then use Math.sqrtsize
sum sqri;
return sum;
public int addDiag int n
public int addDiag int n
Output:
Terminal example for a x:
YOUR NAME GOT IT: tries, sum
Terminal example for a xthis run may take minutes to run:
YOUR NAME GOT IT: tries, sum<
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
