import java.util.ArrayList; import java.util.Collections; import java.util.Random; class Country { // Declare private variables private String name; private
Question:
import java.util.ArrayList; import java.util.Collections; import java.util.Random;
class Country { // Declare private variables private String name; private String missName; private int age; // Default constructor public Country(){ }
// Other constructor public Country(String name, String missName, int age) { this.name = name; this.missName = missName; this.age = age; }
// Assessor methods public String getName() { return name; }
public String getMissName() { return missName; } public int getAge() { return age; }
// Mutator methods public void setInfo(String name, String missName, int age) { this.name = name; this.missName = missName; this.age = age; } }
class MissWorld implements Comparable { // Declare variables public static int SIZE = 10; private Country name; private double[] score; private double cf; private double fs;
// Other constructor public MissWorld(Country name) { this.name = name; this.score = new double [SIZE]; this.cf = cf; this.fs = fs; }
// Assessor methods public Country getCountry() { return name; }
public double[] getScore() { return score; } public double getCarriedForward() { return cf; }
public double getFinalScore() { return fs; } private ArrayList getSortedList() { ArrayList arrayList = new ArrayList(); for (int i = 0; i < score.length; i++) { arrayList.add(score[i]); } Collections.sort(arrayList); return arrayList; } private double highest() { ArrayList arrayList = getSortedList(); double highest = arrayList.get(9); return highest; } private double secondHighest() { ArrayList arrayList = getSortedList(); double secondHighest = arrayList.get(8); return secondHighest; } private double lowest() { ArrayList arrayList = getSortedList(); double lowest = arrayList.get(0); return lowest; } private double secondLowest() { ArrayList arrayList = getSortedList(); double secondLowest = arrayList.get(1); return secondLowest; } public double finalScore() { double finalScore, sum = 0.0; for (int i = 0; i < score.length; i++) { sum += score[i]; } finalScore = (sum - highest() - secondHighest() - lowest() - secondLowest()); return finalScore; } public double getTotalScore() { double totalScore = getCarriedForward() + finalScore(); return totalScore; } public void setCarriedForward(double cf) { this.cf = cf; } public void setFinalScore(double fs) { this.fs = fs; }
public void printInfo() { System.out.printf("%-20s",getCountry().getName()); for (int i = 0; i < 10; i++){ score[i] = (Math.random() * 10); System.out.printf("%10.2f", score[i]);
} System.out.printf("%10.2f",getCarriedForward()); System.out.printf("%10.2f",finalScore()); System.out.printf("%10.2f ",getTotalScore()); System.out.println(); }
@Override public int compareTo(MissWorld o) { return (int) ((this.fs+this.cf)-(o.fs+o.cf)); } }
public class ChenZhaoLin_25_A3 { private static final String[] Countries = {"China","Thailand","British Columbia", "South Korea","Japan","USA","Australia", "Venezuella","Russia","Brazil"}; private static final String[] Titles = {"Interview","SwimSuit Competition","Evening Gown Competition"}; private static double[] cfArray = new double[MissWorld.SIZE]; private static void displayGameInfo(ArrayList aList,String event){ System.out.println("Starting Position For Event: "+event); System.out.printf(" %-20s%10s%10s%10s ","Country","Name","Age","c/f"); for(MissWorld mW : aList) { System.out.printf("%-20s%10s%10d%10.2f ",mW.getCountry().getName(),mW.getCountry().getMissName(),mW.getCountry().getAge(),cfArray[aList.indexOf(mW)]); } System.out.printf(" %-20s%10s%10s%10s%10s%10s%10s%10s%10s%10s%10s%10s%10s%10s ","Countries","J 1","J 2","J 3","J 4","J 5","J 6","J 7","J 8","J 9","J 10","c/f","Current","Total"); for(MissWorld mW: aList) { mW.printInfo(); } System.out.println(); } private static void displaySortedList(ArrayList aList,String event) { System.out.println(" The result after the Event: "+event); ArrayList tempList=new ArrayList<>(); for(int i = 0; i < 10; i++) { tempList.add(aList.get(i)); } Collections.sort(tempList); Collections.reverse(tempList); displayResultInfo(tempList); } private static void displayResultInfo(ArrayList aList) { for(int i = 0; i < 10; i++) { System.out.printf(" %-10d%-20s%-10.2f ",(i+1),aList.get(i).getCountry().getName(), (aList.get(i).getCarriedForward() + aList.get(i).finalScore())); } } private static void updateCFArray(ArrayList aList) { for (int i = 0; i < aList.size(); i++) { aList.get(i).setCarriedForward(aList.get(i).getTotalScore()); } } private static int getAge() { Random rand = new Random(); return rand.nextInt(10)+20; } public static void main(String[] args){ ArrayList aList = new ArrayList<>(); for(int i=0;i < 10; i++){ aList.add(new MissWorld(new Country(Countries[i], "Name " + (i + 1), getAge()))); } for(String title : Titles){ displayGameInfo(aList,title); displaySortedList(aList,title); updateCFArray(aList); System.out.println(); } } }
Hi, my code has 2 problem,
1. It doesn't display the c/f ( carried forward)
Java An Introduction To Problem Solving And Programming
ISBN: 9780134462035
8th Edition
Authors: Walter Savitch