Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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)

imageimage

The result after the Event: Interview 1 2 3 4 5 6 7 8 9 10 Brazil Russia Venezuella Australia Russia Brazil USA Japan Australia Venezuella South Korea British Columbia Thailand China Thailand British Columbia South Korea Japan USA 22.99 28.28 23.61 33.69 17.47 42.15 22.58 Starting Position For Event: SwimSuit Competition Country China 28.44 25.70 26.83 Name Name 1 Name 2 Name 3 Name 4 Name 5 Name 6 Name 7 Name 8 Name 9 Name 10 Age 21 29 21 21 28 23 21 27 20 20 c/f 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00

Step by Step Solution

There are 3 Steps involved in it

Step: 1

Step 1 Plan for the compareTo method Calculate the difference between the total ... blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Java An Introduction To Problem Solving And Programming

Authors: Walter Savitch

8th Edition

0134462033, 978-0134462035

Students also viewed these Programming questions

Question

How do you think this problem should be treated?

Answered: 1 week ago

Question

5 Explain how sentence style affects emphasis within your message

Answered: 1 week ago

Question

6. How do you choose the medium for your message?

Answered: 1 week ago