-1

I can't for the life of me get this program to compile.

This is the exact assignment:

A couple of points regarding the above uml diagram:

addScore(double pScr) // is a convenience method that adds a single score to the scores ArrayList as a time.

calculateTotalScore() //is another convenience method that calculates a Diver's total score according to the rules below regarding how the total score is arrived at.

toString() //build's a string consisting of the Diver's first and last name and all the Diver's individual scores contained in the scores ArrayList and the Diver's resulting total score.

I've provided you with DiverTester class that will effectively test your Diver class. This class will read the provided data file, create instances of Diver objects and populate them with the data as it's read from the data file. The DiverTester class will store the created Diver objects in an ArrayList of it's own. Once all the data has been read and the ArrayList of Diver objects has been populated perform the same processing as required for Assignment 10. For each Diver output the diver's name and total score using the provided scoring rules. Each contestant's score is calculated by dropping the lowest and highest scores and then adding the remaining scores. Format each diver's total score to two decimal places. So for example, the output for Chen Ruolin below would be: Chen Ruolin – 56.90 points.

Now, I know what I have is rough, but I'm at a loss for where to go from here, please help!

Here is the uml file I was given

Here is the code the professor provided for the tester file:

import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;

public class DiverTester {

  static ArrayList<Diver> divers = new ArrayList<Diver>();

  public static void main(String[] args) throws IOException {
    loadDivers(); //call method to read and load the Diver data
    outputDivers();
  }

  public static void loadDivers() throws IOException {
    Scanner infile  = new Scanner(new FileReader("diving_data.txt"));
    double scr = 0.0;
    String str = null;

    while (infile.hasNext()) {
      Diver dvr = new Diver();
      str = infile.next(); //read first bit of data in row
      dvr.setFirstName(str);
      dvr.setLastName(infile.next());  //read next bit of data in row

      //for loop reads one diver's scores in
      for(int c = 0; c < 8; c++) {
        dvr.addScore(infile.nextDouble());
      }
      divers.add(dvr);
    }
  }

  public static void outputDivers() throws IOException {
    for(Diver dvr : divers) {
      System.out.println(dvr.toString() + "\n");
    }
  }

}

My code so far:

import java.util.ArrayList;

public class Diver
{
  public String firstName;
  public String lastName;
  public ArrayList scores = new ArrayList<Double>();

  public Diver()
  {
  }
  public Diver(String pFirst, String pLast, ArrayList scores)
  {
    firstName = "";
    lastName = "";

  }
  public void setFirstName(String pFirst)
  {
    firstName = pFirst;
  }
  public String getFirstName()
  {
    return firstName;
  }
  public String getLastName()
  {
    return lastName;
  }
  public void setLastName(String pLast)
  {
    lastName = pLast;
  }
  public Arraylist getScores()
  {
    return scores;
  }
  public void setScores(ArrayList scores)
  {
    scores = pScr;
  }
  public void addScore(double pScr)
  {
    scores.add(pScr);
  }
  public double calculateTotalScore()
  {
    if (scores.size() < 3) {
            return 0.0;
        }
        else {
            double sum = 0.0;
            int counter = 0;
            for(double score : scores) {
                if (counter != 0 && counter != scores.size() - 1) {
                    sum += score;
                }
                counter++;
            }
            return sum;
  }
}
  public String toString(){
    StringBuffer buf = StringBuffer();
    buf.append("\nDiver\n");
    buf.append("Name: " + getFirstName() + " "+ getLastName() + "\n");
    buf.append("Scores: " + getScores().toString());
    buf.append("Total Score: " + Double.calculateTotalScore(scores).toString());
    return buf.toString();
  }
}


Errors: 
--------------
File: /Users/gcaruso/Documents/Diver.java  [line: 34]
Error: /Users/gcaruso/Documents/Diver.java:34: cannot find symbol
symbol  : class Arraylist
location: class Diver
File: /Users/gcaruso/Documents/Diver.java  [line: 40]
Error: /Users/gcaruso/Documents/Diver.java:40: cannot find symbol
symbol  : variable pScr
location: class Diver
File: /Users/gcaruso/Documents/Diver.java  [line: 54]
Error: /Users/gcaruso/Documents/Diver.java:54: incompatible types
found   : java.lang.Object
required: double
File: /Users/gcaruso/Documents/Diver.java  [line: 64]
Error: /Users/gcaruso/Documents/Diver.java:64: cannot find symbol
symbol  : method StringBuffer()
location: class Diver
File: /Users/gcaruso/Documents/Diver.java  [line: 68]
Error: /Users/gcaruso/Documents/Diver.java:68: cannot find symbol
symbol  : method calculateTotalScore(java.util.ArrayList)
location: class java.lang.Double
-------------
Gianna Caruso
  • 113
  • 2
  • 3
  • 14

2 Answers2

0

In your diver Class' toString() method it should be StringBuffer buf = new StringBuffer() instead of StringBuffer buf = StringBufferr(); try this: ArrayList<double> scores = new ArrayList<double>();

Coding Enthusiast
  • 3,865
  • 1
  • 27
  • 50
  • I don't think StringBuilder is in my code, also I can't change the ArrayList piece because that's what my professor provided for me. – Gianna Caruso Dec 08 '14 at 15:01
  • I changed the ArrayList to that and now I just have an error along the lines of: unexpected reference, double – Gianna Caruso Dec 08 '14 at 15:10
  • I see change it to ArrayList scores = new ArrayList(); this: [link](http://stackoverflow.com/questions/20039098/issue-creating-a-double-array-list) explains why. – Coding Enthusiast Dec 08 '14 at 15:33
0

I would look into these four sections of your code.

1)

  public void setScores(ArrayList scores)
  {
    scores = pScr;
  }

The body of the method could be this.score = score i don't know where you get pScr from.

2)

StringBuffer buf = StringBuffer();

should be StringBuffer buf = new StringBuffe();

3)

Double.calculateTotalScore(scores)

should be calculateTotalScore(); 4)

public ArrayList scores = new ArrayList<Double>();

should be public ArrayList scores = new ArrayList();

Frode
  • 389
  • 3
  • 4