Question: Refactor code into object oriented programming design Rules must be printed at the beginning of the game and before the name prompt. Entered values must
Refactor code into object oriented programming design
- Rules must be printed at the beginning of the game and before the name prompt.
- Entered values must be entered on the same line as the prompt (use System.out.print(); rather than System.out.println(); ).
- For example: Enter your name: Kevin
- System must accept entered letters, such as L, R, Y, N, in either uppercase or lowercase.
- Use the String method, toUppercase(), e.g., yesNo = yesNo.toUpperCase();
- Dice roll must be sorted for presentation and when choosing L or R. Use Arrays.sort();
- Headers must be aligned over columns when presenting roll and point values.
- System must consist of a Driver class and at least 2 object classes, such as Roll and Turn, or Roll, Turn, and Game. (NEW)
- System should be submitted in one .java file, with the Driver class first followed by object classes.
- Name of your source code main class as follows: YourName_Project2.java
- Object classes must be organized as follows: (NEW)
- Variables declared at the beginning of the class.
- Exception is local vars, such as int i in a for loop, or temp vars in a method.
- Constructors follow variables.
- Methods follow constructors.
- Variables declared at the beginning of the class.
- All data variables in the object classes have a visibility of private. (NEW)
- This will require you to use setter and getter methods for these variables.
- All methods in the object classes have a visibility of public and are not static. (NEW)
- Use arrays for storing of dice values, e.g., int[] roll1 = new int[3];
- while or for loop to execute the indicated number of turns.
- Algorithm for generating the random die values using Random class.
- System.out.printf() method for formatting of printing turn, roll, and game results.
- String array to store the values associated with each turn.
- do-while loop to enable continuous play with Y/N response.
- for loops for various activities updating dice values, printing of turn history, etc. especially for those associated with arrays.
import java.util.Scanner; import java.util.Random;
public class Project1{ static int session[][][]; public static int Diceroll(){ Random rand = new Random(); return rand.nextInt(6)+1; } public String readl(){ Scanner sc = new Scanner(System.in); return sc.nextLine(); } public static int getMin(int d1,int d2,int d3){ return Math.min(d1, Math.min(d2, d3)); } public static int getMax(int d1,int d2,int d3){ return Math.max(d1, Math.max(d2, d3)); } public static String checkBonus(int d1,int d2,int d3){ String bonus = "none"; if(d1==d2){ if(d2==d3){ return "triple"; } return "pair"; } else if(d1==d3 || d2==d3){ return "pair"; } else{ if((getMax(d1,d2,d3)-getMin(d1,d2,d3))==2) return "straight"; } return "none"; } public static int print_rolls(int d1,int d2,int d3,int roll,int turn){ Scanner sc = new Scanner(System.in); int points = d1+d2+d3; String res = checkBonus(d1,d2,d3); boolean triple = false; boolean pair = false; boolean straight = false; System.out.println("\t1\t2\t3\tSum\tPair\tTrip\tStrait\tPoints"); System.out.println("\t--\t--\t--\t---\t----\t----\t---\t---"); System.out.print("Roll-"+(roll+1)+"\t"+d1+"\t"+d2+"\t"+d3+"\t"+points+"\t"); String pair_point = "0"; String trip_point = "0"; String straight_point = "0"; if(res == "pair"){ pair_point = "2"; points += 2; pair = true;
} System.out.print(""+pair_point+"\t"); if(res=="triple"){ trip_point = "30"; points +=30; triple = true;
} System.out.print(""+trip_point+"\t"); if(res == "straight"){ straight_point = "10"; points +=10; straight = true;
} System.out.println(""+straight_point+"\t"+points); session[turn][roll][0] = d1; session[turn][roll][1] = d2; session[turn][roll][2] = d3; session[turn][roll][3] = points; session[turn][roll][4] = Integer.parseInt(pair_point); session[turn][roll][5] = Integer.parseInt(trip_point); session[turn][roll][6] = Integer.parseInt(straight_point); return points; } public static void main(String arg[]){
Scanner sc = new Scanner(System.in); System.out.println("Welcome to the Java Dice Game, Lock N Roll! " + "The object of the game is to roll 3 dice and earn the most points. " + "Player earns points by maximizing the sum of the dice and earning bonus points. " + "Bonus points are earned when rolling a pair, triples, or a straight: " + " o Pair Bonus: 2 points " + " o Triple Bonus: 30 points " + " o Straight Bonus: 10 points " + "Player gets 2 rolls. After the first roll, player chooses to either " + "\"Lock\" or \"Roll\" a die again, for each of the 3 dice. " + "Scoring for the turn is complete after the second roll. " + ""); System.out.println("Enter your name to begin this fast-action game, and let's LOCK N ROLL!"); System.out.print("Name : "); String user = sc.nextLine(); boolean continue_ = true; while(continue_){ System.out.print("How many turns would you like in this game? "); int turns = sc.nextInt(); session = new int[turns][2][8]; String c = sc.nextLine(); for(int i=0; i System.out.println("Now it's time to LOCK N ROLL!"); System.out.println("Dice1 value: "+d1+" Enter L or R: "); lnr = sc.nextLine(); if(lnr.equals("r") || lnr.equals("R")) d1 = Diceroll(); System.out.println("Dice2 value: "+d2+" Enter L or R: "); lnr = sc.nextLine(); if(lnr.equals("r") || lnr.equals("R")) d2 = Diceroll(); System.out.println("Dice3 value: "+d3+" Enter L or R: "); lnr = sc.nextLine(); if(lnr.equals("r") || lnr.equals("R")) d3 = Diceroll(); result = print_rolls(d1,d2,d3,1,i); System.out.println("\troll-1\troll-2\tSum\tPair\tTrip\tStrait\tPoints\tImprv"); System.out.println("\t--\t--\t--\t---\t----\t----\t---\t---"); System.out.print("Turn-"+(i+1)+"\t"+session[i][0][0]+" "+session[i][0][1]+" "+session[i][0][2]+" "+"\t"); System.out.print(""+session[i][1][0]+" "+session[i][1][1]+" "+session[i][1][2]+" "+"\t"); System.out.print(""+session[i][1][3]+"\t"+session[i][1][4]+"\t"+session[i][1][5]+"\t"+session[i][1][6]+"\t"); System.out.println(session[i][1][3]+"\t"+(session[i][1][3]-session[i][0][3])); } System.out.println("**************************************************************"); System.out.println("Turn History for the session"); for(int i=0; i public static void main(String[] args) { int x = 0; System.out.println(xmethod(5)); } public static int xmethod(int n, long t) { System.out.println("int"); return n; } public static long xmethod(long n) { System.out.println("long"); return n; } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
