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 1) 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 1..6 on each face
Constructor with parameters n, m for the initial face values. Validate the parameters to allow only 1..6.
Accessors getFace1(), getFace2() and getTotal() which returns an integer.
Mutators setFace1(), setFace2() to set each to only 1..6
Mutator roll() which randomize the numbers on face1 and face2 to 1..6
toString() method to return a string like ""3,4"" or ""6,5""
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 DiceClient1YOURNAME.
Instantiate and initialize a pair of dice
Instantiate score, a 20 element integer array of integers and initialize it to all 0.
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 5 scores e.g.
48985-70000-00000-00000
The rules of the game: A player rolls the two dice and adds the number shown on them.
Roll the two dice up to 20 times and save the total in the array.
If the total is 7 or 11, you win. Put this in the array, report the results and exit the game.
If the total is 2,3, or 12, 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 7. 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 7, you lose. Put this in the array, report the results and exit the game.]
Sample Terminal
You win: 1010000-00000-00000-00000
You win: 70000-00000-00000-00000
You win: 811800-00000-00000-00000
You lose: 63370-00000-00000-00000
You lose: 455129-66700-00000-00000
You win: 88000-00000-00000-00000
Program 2) 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 test1() throws IOException { File inputFile = new File(""ClassicSongs.java"");
Scanner in = new Scanner(inputFile);
String title;
String artist; int count=0;
// ADD LINES FOR TASK #3 HERE
//Declare an 6 element array of Song objects, called cd
while( in.hasNextLine()){
String oneLine = in.nextLine();
System.out.println(""Debug""+count+"": ""+ 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 ..."");
in.close(); }}
ClassicSongs.java:
Ode to Joy, Bach
The Sleeping Beauty, Tchaikovsky
Lullaby, Brahms
Canon, Bach
S QO08 ymphony No.5, 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 Song(String 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 6.
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.5 by Beethoven
The Blue Danube Waltz by Strauss
Program 3) Code Puzzle
Instance Variables:
n
array
Use these to write a one dimension array array (students who last name A-G)
Constructor: NxNYourInitials( int n ) creates an array with n 2 elements AND filled with 1,2,3,..., n 2. For example NxNYourInitials(5) instantiate n =5 and the arrays with 1,2,3,...25.
Methods (recommended order): toString(), printMe(), addRow( int rr ), shake(), addCol( int cc ), addDiag1((), addDiag2().You can use printMe() to check the other methods.
ClientNxM will instantiate an puzzle and then solve it. Have a method to solve a 3x3 and another method to solve a 4x4. 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 int[size];
public String toString(){
return arr.toString(sqr);
}
public void printMe(){
System.out.println(array.toString(sqr));
}
public void shake(){
for(int i =0; i < size; i++;){
sqr[i]= i +1;
}
Random rdm = new Random();
int temp =0;
int randomVal = rdm.nextInt(size);
int randomVal2= rdm.nextInt(size);
while(randomVal = randomVal2){
temp = sqr[randomVal];
sqr[randomVal]= sqr[randomVal2];
sqr[randomVal2]= temp;
}
}
public int addRow( int n ){
//add a specific row to get the sum
int sum =0;
int root =(int) Math.sqrt(size);
for (int i = root * n; i < Math.sqrt(size)* n + root; i++){
sum += sqr[i];
}
return sum;
}
public int addCol( int n ){
int sum =0;
for (int i = n; i < size; i += n +1){//if n doesn't work then use Math.sqrt(size)
sum += sqr[i];
}
return sum;
}
public int addDiag( int n ){
}
public int addDiag2( int n ){
}
}
Output:
Terminal example for a 3x3:
294
753
618
YOUR NAME GOT IT: 31230 tries, sum=15
Terminal example for a 4x4(this run may take minutes to run):
97126
162133
511810
414115
YOUR NAME GOT IT: 1127660899 tries, sum<

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!