Question 4 cbt questions are by nature randomized • import java.util.Random; • public class CBT { • public static void main(String args[]) { • String coursecode[] = {"ENT201", "GST222"}; • String Dept[] = {"CSC DEPT", "PHYSICS DEPT", "BCH DEPT"}; • for (int i = 0; i < 2; i++) { • System.out.println("Inside " + coursecode[i]); • for (int k =1; k <= 125; k++ ) { • System.out.printf("Candidate %d of %s department", k, Dept[0]); • for (int j =1; j <= 35; j++) { • System.out.printf("Question %d :: %s question %d", j, coursecode[i], randomquestion()); • System.out.println(); • } • System.out.println(); • } • System.out.println(); • for (int k =1; k <= 102; k++ ) { • System.out.printf("Candidate %d of %s department", k, Dept[1]); • for (int j =1; j <= 35; j++) { • System.out.printf("Question %d :: %s question %d", j, coursecode[i], randomquestion()); • System.out.println(); • } • System.out.println(); • } • System.out.println(); • for (int k =1; k <= 178; k++ ) { • System.out.printf("Candidate %d of %s department", k, Dept[2]); • for (int j =1; j <= 35; j++) { • System.out.printf("Question %d :: %s question %d", j, coursecode[i], randomquestion()); • System.out.println(); • } • System.out.println(); • } • System.out.println(); • } • • } • static int randomquestion() { • Random randomquestion = new Random(); • return randomquestion.nextInt(800) +1; • } • •} Question 3(ii) • in the olden days travellers use • import java.util.Random; • public class TravellerDiviner { • public static void main(String args[]) { • int leadTraveller[] = new int[12]; • for (int i = 0; i < 12; i++) { • boolean done = false; • System.out.println("city " + i); • int trial =0; • while (!done) { • • if (diviner() == 0) { • System.out.printf("consult trial %d ", (trial +1)); • System.out.println(" This is a wrong way, reconsult diviner"); • trial++; • } • else { • leadTraveller[i] = 1; • done= true; • trial=0; • } • } • System.out.println(); • } • System.out.println("Traveller has sucessfully been lead through 12 cities"); • System.out.println("Choosing the right path each time "); • for (int i=0; i < 12; i++) { • System.out.printf("City %d Diviner choice %d", i, leadTraveller[i]); • System.out.println(); • } • } • static int diviner() { • Random diviner = new Random(); • return diviner.nextInt(2); • • • } } Question 3(I) • WRITE A JAVA PROGRAM THAT PERMIT 4 PEOPLE TO PLAY A LUDO... • import java.util.Random; • public class FourManLudo { • public static void main(String args[]){ • //Allow four men to roll dice • //Decide winner sum throw after 25 • int player1[], player2[]; • int player3[], player4[]; • int sump1, sump2, sump3, sump4; • int first, second, third, fourth; • sump1 = sump2 = sump3 = sump4 =0; • player1 = new int[25]; • player2 = new int[25]; • player3 = new int[25]; • player4 = new int[25]; • //playing loop • for(int i=0; i <25; i++) { • player1[i] = rolldice(); • player2[i] = rolldice(); • player3[i] = rolldice(); • player4[i] = rolldice(); • } • //summing loop • for(int i=0; i < 25; i++) { • sump1 += player1[i]; • sump2 += player2[i]; • sump3 += player3[i]; • sump4 += player4[i]; • } • //winner printing • // Compare variables to find the first value • fourth = Math.min(Math.min(sump1, sump2), Math.min(sump3, sump4)); • // Compare variables to find the fourth value • first = Math.max(Math.max(sump1, sump2), Math.max(sump3, sump4)); • // Compare variables to find the second and third values • third = Math.min(Math.max(Math.min(sump1, sump2), Math.min(sump3, sump4)), Math.max(Math.min(sump1, sump3), Math.min(sump2, sump4))); • second = Math.max(Math.min(Math.max(sump1, sump2), Math.max(sump3, sump4)), Math.min(Math.max(sump1, sump3), Math.max(sump2, sump4))); • System.out.println("player1 overall = " + sump1); • System.out.println("player2 overall = " + sump2); • System.out.println("player3 overall = " + sump3); • System.out.println("player4 overall = " + sump4); • System.out.println(); • • System.out.printf("The first in 25 rounds is %d", first); • System.out.println(); • System.out.printf("The second in 25 rounds is %d", second); • System.out.println(); • System.out.printf("The third in 25 rounds is %d", third); • System.out.println(); • System.out.printf("The fourth in 25 rounds is %d", fourth); • • } • static int rolldice() { • Random myrand = new Random(); • return myrand.nextInt(6)+1; • • } } Question 2 • Taking The score in 3 of your 200L second semester....... • import java.util.Scanner; • public class ScorePredict { • public static void main(String args[]) { • System.out.println("System predict your current score based on.."); • System.out.println("Based on your previous scores....."); • int csc214, csc206, csc212; • int csc301, csc311, csc305; • int difficult = -20; • int medium = -10; • int easy = 5; • Scanner myscan = new Scanner(System.in); • System.out.println("Enter your score for CSC 214 to predict 301 .... same lecturer"); • csc214 = myscan.nextInt(); • System.out.println(); • System.out.println("Enter your score for CSC 206 to predict 311 .... same lecturer"); • csc206 = myscan.nextInt(); • System.out.println(); • System.out.println("Enter your score for CSC 212 to predict 305 .... same lecturer"); • csc212 = myscan.nextInt(); • System.out.println(); • csc301 = (2*csc214) +medium- csc214; • csc311 = (2*csc206) + difficult - csc206; • csc305 = (2 * csc212) + easy - csc212; • System.out.printf("Your likely score in CSC 301 is %d", csc301); • System.out.println(); • System.out.printf("Your likely score in CSC 311 is %d", csc311); • System.out.println(); • System.out.printf("Your likely score in CSC 305 is %d", csc305); • System.out.println(); • • } } Question 1 • A = x^3 + 5x^3 + (3x^2)/7 ..... •. •. •. • import java.lang.Math; • public class MathFormula { • public static void main(String args[]) { • double A, B, C; • double w, x, y, z; • w=4.0; • x=25.0; • y = 16.0; • z = 49.0; • A = (Math.pow(x, 3)) + 5*(Math.pow(x, 3)) + 3*(Math.pow(x, 2))/7; • B = (Math.sqrt(x)) + 4*w + (Math.pow(w, x)); • C = (Math.sin(y+z) - Math.cos(y-z))/Math.tan(x); • System.out.println("A = " + A ); • System.out.println("B = " + B); • System.out.println("C = " + C); • • } } Question 6 • Simulate World cup draw in java • import java.util.ArrayList; • import java.util.Collections; • import java.util.List; • class Team { • private String name; • public Team(String name) { • this.name = name; • } • public String getName() { • return name; • } • } • class Pot { • private List<Team> teams; • public Pot() { • • teams = new ArrayList<>(); } • public void addTeam(Team team) { • teams.add(team); • } • public List<Team> getTeams() { • return teams; • } • } • class WorldCupDraw { • private List<Pot> pots; • private List<Team> drawnTeams; • public WorldCupDraw(List<Pot> pots) { • this.pots = pots; • this.drawnTeams = new ArrayList<>(); • } • public void runDraw() { • for (Pot pot : pots) { • List<Team> teamsInPot = new ArrayList<>(pot.getTeams()); • Collections.shuffle(teamsInPot); • for (Team team : teamsInPot) { • System.out.println("Drawn Team: " + team.getName()); • drawnTeams.add(team); • } • System.out.println(); • } • } • public List<Team> getDrawnTeams() { • return drawnTeams; • } • } • public class WorldCupSimulation { • public static void main(String[] args) { • // Define teams • List<Team> teams = new ArrayList<>(); • teams.add(new Team("Brazil")); • teams.add(new Team("Germany")); • teams.add(new Team("Italy")); • teams.add(new Team("Argentina")); • teams.add(new Team("Netherlands")); • teams.add(new Team("France")); • teams.add(new Team("Spain")); • teams.add(new Team("England")); • teams.add(new Team("Portugal")); • teams.add(new Team("Belgium")); • teams.add(new Team("Uruguay")); • teams.add(new Team("Croatia")); • teams.add(new Team("Switzerland")); • teams.add(new Team("Denmark")); • teams.add(new Team("Colombia")); • teams.add(new Team("Mexico")); • teams.add(new Team("Sweden")); • teams.add(new Team("Poland")); • teams.add(new Team("Senegal")); • teams.add(new Team("Peru")); • teams.add(new Team("USA")); • teams.add(new Team("Austria")); • teams.add(new Team("Chile")); • teams.add(new Team("Australia")); • teams.add(new Team("Japan")); • teams.add(new Team("Greece")); • teams.add(new Team("Nigeria")); • teams.add(new Team("Egypt")); • teams.add(new Team("South Korea")); • teams.add(new Team("Morocco")); • teams.add(new Team("Norway")); • teams.add(new Team("Ivory Coast")); • teams.add(new Team("Slovakia")); • // Define pots and add teams to them • List<Pot> pots = new ArrayList<>(); • for (int i = 0; i < 8; i++) { • Pot pot = new Pot(); • for (int j = i * 4; j < (i * 4) + 4; j++) { • pot.addTeam(teams.get(j)); • } • pots.add(pot); • } • // Create the draw simulation • WorldCupDraw drawSimulation = new WorldCupDraw(pots); • // Run the draw simulation • drawSimulation.runDraw(); • • } } • NAME:: Adeleke Adedamola Ayobami • MATRIC::: 190404078 • CSC 305 ASSIGNMENT • DEPT :: COMPUTER SCIENCE