I have problem with generating random in array (Look at getMark() in Missworld class). It does generate different random mark for the first competition (interview). When I add in 2 more competition (swimsuit & evening gown), the mark are the same as interview. How to make it different? Cannot change or add class/instance variable but can amend or add method. Able to help on this? Have tried a lot of ways but didn't work.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
class Country
{
//Declare Variable
private String name;
private String missName;
private int age;
public Country (String name, String missName, int age)
{
setInfo(name, missName, age);
}
public String getName()
{
return name;
}
public String getMissName()
{
return missName;
}
public int getAge()
{
return age;
}
public void setInfo (String name, String missName, int age)
{
this.name = name;
this.missName = missName;
this.age = age;
}
}
class MissWorld
{
//Declare variable
public static final int SIZE = 10;
private Country name;
private double [] score;
private double cf;
private double fs;
public MissWorld(Country name, double cf)
{
this.name = name;
score = new double [SIZE];
getMark();
this.cf = cf;
getFinalScore();
}
public Country getCountry()
{
return name;
}
public double getCarriedForward()
{
return cf;
}
public void setCarriedForward(double cf)
{
this.cf = cf;
}
public double getFinalScore()
{
return finalScore();
}
public double [] getMark()
{
for (int i = 0; i < SIZE; i++)
{
score[i] = Math.random() * 10.00;
}
return score;
}
public void setMark (double [] score)
{
this.score = new double [SIZE];
for(int i =0; i < score.length; i++)
this.score [i] = score [i];
}
private double highest()
{
double max = score[0];
for (int i = 1; i < SIZE; i++)
{
if (score[i] > max)
{
max = score[i];
}
}
return max;
}
private double secondHeighest()
{
double max = score[0];
for (int i = 1; i < SIZE; i++)
{
if (score[i] > max)
{
max = score[i];
}
}
double secHighest= score[0];
for (int i = 1; i < SIZE; i++)
{
if ((score[i] > secHighest) && (score[i] < max))
{
secHighest = score[i];
}
}
return secHighest;
}
private double lowest()
{
double min = score[0];
for (int i = 1; i < SIZE; i++)
{
if (score[i] < min)
{
min = score[i];
}
}
return min;
}
private double secondLowest()
{
double min = score[0];
for (int i = 1; i < SIZE; i++)
{
if (score[i] < min)
{
min = score[i];
}
}
double secLowest= score[0];
for (int i = 1; i < SIZE; i++)
{
if ((score[i] < secLowest) && (score[i] > min))
{
secLowest = score[i];
}
}
return secLowest;
}
private double finalScore()
{
//create "total" variable before elimination of top 2 & bottom 2 marks
double total = 0.00;
for (double mark : score)
{
total += mark;
}
//create "finalScore" variable to eliminate top 2 & bottom 2 marks
double finalScore = total - highest() - secondHeighest() - lowest() - secondLowest();
return finalScore;
}
private double getTotalScore()
{
double totalscore = getFinalScore() + cf;
return totalscore;
}
public void printinfo()
{
System.out.printf("%-14s", name.getName());
for (double mark : score)
{
System.out.printf("%-6.2f", mark);
}
System.out.printf("%-9.2f", getCarriedForward());
System.out.printf("%-11.2f", finalScore());
System.out.printf("%-5.2f%n", getTotalScore());
//getSortedList().clear();
}
}
class Missuniverse
{
//Declare Variable
private static final String [] Countries = {"Brazil", "Britain", "China", "India", "Japan",
"Russia", "Singapore", "South Korea", "Thailand", "USA"};
private static final String [] Titles = {"Interview", "Swimsuit competition", "evening gown competition"};
private static void displaySortedList(String [] Countries, double [] cfArray, String event)
{
System.out.println("\n\nThe result after the event:" + " " + event + "\n");
//Bubble sort
for (int i = 0; i < MissWorld.SIZE; i++)
{
for (int j = 0; j < ((MissWorld.SIZE - 1) - i);j++)
if (cfArray[j] > cfArray[j+1])
{
cfArray[j]=cfArray[j];
cfArray[j + 1] = cfArray[j + 1];
}
else
{
double temp = cfArray[j];
cfArray[j] = cfArray[j + 1];
cfArray[j + 1] = temp;
String temp2 = Countries[j];
Countries[j] = Countries[j + 1];
Countries[j + 1] = temp2;
}
}
for (int i = 0; i < MissWorld.SIZE; i++)
{
System.out.printf("%-5d", i + 1);
System.out.printf("%-15s", Countries[i]);
System.out.printf("%-6.2f%n", cfArray[i]);
}
}
private static int getAge1()
{
Random rand = new Random();
int n = rand.nextInt(27);
if (n <= 16)
return 17;
else
return n;
}
private static void displayGameinfo(ArrayList<MissWorld> alist, String event)
{
System.out.println("\nStarting position for event: " + event +"\n");
System.out.printf("%-18s", "Country");
System.out.printf("%-17s", "Name");
System.out.printf("%-12s", "Age");
System.out.printf("%-15s%n", "c/f");
System.out.println();
for (MissWorld s : alist)
{
System.out.printf("%-18s", s.getCountry().getName());
System.out.printf("%-17s", s.getCountry().getMissName());
System.out.printf("%-12d", s.getCountry().getAge());
System.out.printf("%-15.2f%n", s.getCarriedForward());
}
}
private static void displayResultingInfo(ArrayList<MissWorld> alist)
{
System.out.printf("%-14s", "Countries");
for (int i = 1; i <= MissWorld.SIZE; i++)
System.out.printf("%-6s", "J " + i);
System.out.printf("%-9s", "c/f");
System.out.printf("%-11s", "Current");
System.out.printf("%-5s%n", "Total");
for (MissWorld s : alist)
{
s.printinfo();
}
}
private static void updateCFArray(ArrayList<MissWorld>alist, double[] cfArray)
{
double addcf;
for (int i = 0; i < MissWorld.SIZE; i++)
{
addcf = alist.get(i).getCarriedForward() + alist.get(i).getFinalScore();
alist.get(i).setCarriedForward(addcf);
cfArray[i] = addcf;
}
}
public static void main (String [] args)
{
//declare variable
double cf;
Country b0 = new Country (Countries[0], "Ice", getAge1());
Country b1 = new Country (Countries[1], "Diana", getAge1());
Country b2 = new Country (Countries[2], "Angelababy", getAge1());
Country b3= new Country (Countries[3], "Aruna", getAge1());
Country b4 = new Country (Countries[4], "Momo", getAge1());
Country b5 = new Country (Countries[5], "Sharapova", getAge1());
Country b6 = new Country (Countries[6], "Jade Seah", getAge1());
Country b7= new Country (Countries[7], "Jisoo", getAge1());
Country b8 = new Country (Countries[8], "Lisa",getAge1());
Country b9= new Country (Countries[9], "Dakota", getAge1());
cf = 0.00;
MissWorld m0 = new MissWorld(b0, cf);
MissWorld m1 = new MissWorld(b1, cf);
MissWorld m2 = new MissWorld(b2, cf);
MissWorld m3 = new MissWorld(b3, cf);
MissWorld m4 = new MissWorld(b4, cf);
MissWorld m5 = new MissWorld(b5, cf);
MissWorld m6 = new MissWorld(b6, cf);
MissWorld m7 = new MissWorld(b7, cf);
MissWorld m8= new MissWorld(b8, cf);
MissWorld m9= new MissWorld(b9, cf);
ArrayList <MissWorld> alist = new ArrayList <MissWorld>();
alist.add(m0);
alist.add(m1);
alist.add(m2);
alist.add(m3);
alist.add(m4);
alist.add(m5);
alist.add(m6);
alist.add(m7);
alist.add(m8);
alist.add(m9);
for(int i = 0; i < 3; i++)
{
displayGameinfo (alist, Titles[i]);
System.out.println();
displayResultingInfo(alist);
double [] cfArray = new double [MissWorld.SIZE];
updateCFArray(alist, cfArray);
displaySortedList(Countries, cfArray, Titles[i]);
Arrays.sort(Countries);
}
}
}