NIT2112 Object Oriented Programming Indivitual assignment
NIT2112 Object Oriented Programming College of Engineering and Science ‘Unique Six’ Assignment Name: Lankesh Ekanayake ID: s4652720 Name: Wicckramaarachchige Perera ID: s4652725 Content Part 1 Class Die Class Player Class testSixNumbers Description UML Diagrams Output Part 2 Class DisplaySixNumbersPanel Class SixNumbersPanel Description UML Diagrams Output Part 3 Class Player Class DisplaySixNumbersPanel Class SixNumbersPanel Description UML Diagrams Output Readme document Part 1 Class Die import java.util.Random; //importing the java Random class to the project class Die { //implementing the Class Random r = new Random(); // Generate new random object public int rollTheDie() { // method rollTheDie is used to //create a random number and modulate it by 6, then return the //answer as the value int x = r.nextInt(); if (x % 6 == 0) { return 1; } else if (x % 6 == 1) { return 2; } else if (x % 6 == 2) { return 3; } else if (x % 6 == 3) { return 4; } else if (x % 6 == 4) { return 5; } else { return 6; } } } Class Player public class Player { private Die d=new Die(); // creates a new die object public int play(){ // method play used to get an integer from the die object ad return it return d.rollTheDie(); } } Class TestSixNumbers public class TestSixNumbers { public static void main(String[] args) { //main method Player p1 = new Player(); //create new player object int arr1[] = {0, 0, 0, 0, 0, 0}; // array for store rolled values int count=0; // to store loop count int br=0; // to indicate that when the loop should break while (br==0) { //main loop count=count+1; int x = p1.play(); //get the value from die through player for (int i = 1; i < 7;="" i++)="" {="" if="" (x="=" i)="" {="" check="" the="" number="" system.out.println("number="" rolled:="" "+i);="" arr1[i-1]="i;" store="" the="" value="" string="" s="So far, you have: " ;="" for(int="" p=""><7;p++){ if(arr1[p-1]!="0){" s="s+p+"" ";="" }="" }="" system.out.println(s="" );="" if(s.equals("so="" far,="" you="" have:="" 1="" 2="" 3="" 4="" 5="" 6="" ")){//check="" whether="" all="" numbers="" available="" system.out.println("congratulations,="" you="" have="" taken="" "+count+"="" rolls="" to="" get="" one="" of="" each="" number="" between="" 1="" and="" 6.="" ");="" br="1;" if="" all="" numbers="" available="" break="" the="" loop="" }="" }="" }="" }="" }="" }="" description="" the="" java.util.random="" class="" can="" generate="" random="" numbers.="" in="" class="" die,="" public="" int="" rollthedie()="" method="" creates="" a="" random="" number="" x,="" using="" r.nextint();="" method.="" then="" we="" used="" the="" modulation="" operator="" to="" filter="" 1="" to="" 6="" numbers.="" and="" returned="" the="" value.="" in="" class="" player,="" it="" creates="" an="" object="" of="" die="" class.="" when="" the="" play="" method="" called,="" the="" object="" returns="" the="" value="" to="" the="" player.="" and="" player="" returns="" it="" to="" the="" class="" testsixnumbers.="" we="" decided="" to="" execute="" main="" operations="" in="" class="" testsixnumbers,="" because="" it="" makes="" the="" player="" duplication="" process="" lot="" easyer.="" in="" class="" testsixnumbers="" ,="" i="" purposely="" created="" some="" variables,="" player="" p1="new" player();="" ---p1="" for="" player="" object="" int="" arr1[]="{0," 0,="" 0,="" 0,="" 0,="" 0};="" ---="" array="" for="" store="" 1-6="" numbers="" int="" count="0;" ----="" counter="" for="" store="" the="" loop="" count="" int="" br="0;" ----="" indicator="" which="" used="" for="" figure="" out="" the="" time="" to="" break="" the="" loop="" uml="" diagrams="" class="" die="" -="" r="" :="" random="" +="" rollthedice()="" :="" int="" class="" player="" -="" d:="" die="" +="" play()="" :="" int="" class="" testsixnumbers="" +="" main(string[]="" args)="" output="" part="" 2="" displaysixnumberspanel="" import="" java.awt.color;="" import="" java.awt.event.actionevent;="" import="" java.awt.event.actionlistener;="" import="" java.util.arrays;="" import="" javax.swing.*;="" import="" essential="" packages="" public="" class="" displaysixnumberspanel="" {="" static="" int="" count="0;" static="" int="" arr1[]="{0," 0,="" 0,="" 0,="" 0,="" 0};="" static="" int="" arr2[]="{1," 2,="" 3,="" 4,="" 5,="" 6};="" static="" int="" br="0;" public="" static="" void="" main(string[]="" args)="" {="" jframe="" w1="new" jframe("six="" numbers="" game");="" sixnumberspanel="" mypanel="new" sixnumberspanel();="" w1.add(mypanel);="" mypanel.setbackground(color.cyan);="" w1.setsize(250,="" 250);="" w1.setvisible(true);="" w1.setdefaultcloseoperation(jframe.exit_on_close);="" w1.setresizable(false);="" jbutton="" roll="new" jbutton();="" roll.setsize(50,="" 30);="" roll.setlocation(50,="" 100);="" roll.settext("roll="" the="" die");="" mypanel.add(roll);="" roll.setvisible(true);="" jlabel="" jl2="new" jlabel("");="" jl2.setsize(100,="" 100);="" mypanel.add(jl2);="" player="" p1="new" player();="" actionlistener="" al="new" actionlistener()="" {="" when="" the="" button="" clicked="" this="" listener="" indicates="" @override="" public="" void="" actionperformed(actionevent="" e)="" {="" then="" execute="" these="" commands="" if="" (br="=" 0)="" {="" int="" x="p1.play();" mypanel.tf7.settext(string.valueof(x));="" count="count" +="" 1;="" jl2.settext("="" no="" of="" rolls="" :="" "+count);="" for="" (int="" i="1;" i="">7;p++){>< 7; i++) { if (x == i) { arr1[i - 1] = i; 7;="" i++)="" {="" if="" (x="=" i)="" {="" arr1[i="" -="" 1]=""> 7; i++) { if (x == i) { arr1[i - 1] = i;>