Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

In desperate need of Java programming help


  • Please log in to reply

#1
SportzTawk

SportzTawk

    New Member

  • Member
  • Pip
  • 2 posts
In my Computer Science class yesterday, we were given a programming assignment that we could complete by Friday at Midnight for extra credit. I am pretty decent in the language, as far as basics, and using for and while loops, but I am struggling with Array Lists for some reason. I was wondering if there was anyone on here that knows enough about Java to assist me. I won't post all of them, but here are the first few terms of the program:

Score -- A single score for either technical or artistic merit on a single performance by a single judge.

* Fields: rating (double), judgeName (String)
* Methods
o a two-parameter constructor
o an accessor method for rating
o a mutator method for rating
o an accessor method for judgeName
o a mutator method for judgeName

PerformanceScore -- All ratings of all judges for a single performance.

* Fields: technicalMerit (ArrayList of Score), artisticMerit (ArrayList of Score)
* Methods
o public a no-parameter Constructor that initializes the two fields to empty ArrayLists
o public addScore with three parameters: a char containing an 'a' or 't' indicating that this is either an artistic or technical score, a double containing: the actual score, and a String containing the name of the judge
o public getScores with no parameters that returns a string containing all the scores in the following format:

Technical Scores
judgeName1: score1
judgeName2: score2
etc.
Artistic Scores
judgeName1: score1
judgeName2: score2
etc.

o public technicalMeritRating with no parameters and returns a double which is the average of all the technicalMerit scores omitting the max and min score
o public artisticMeritRating with no parameters and returns a double which is the average of all the artisticMerit scores omitting the max and min score
o private maxRating with one parameter (either the technicalMerit or artisticMerit ArrayList) and returns the maximum rating in the list
o private minRating with one parameter (either the technicalMerit or artisticMerit ArrayList) and returns the minimum rating in the list




Now i have the code for Score:

public class Score
{  
	private double rating;
	private String judgeName;

	public Score(double rating, String judgeName)
	{
		this.rating = rating;
		this.judgeName = judgeName;
	}

	public void getRating()
	{
		System.out.println("" + rating);	
	}
	
	public void newRating(double newRating)
	{
		rating = newRating;	
	}
	
	public void getJudgeName()
	{
		System.out.println("" + judgeName);	
	}
	
	public void newJudge(String newJudge)
	{
		judgeName = newJudge;	
	}
}

And then I have the beginning code of PerformanceScore:

import java.util.ArrayList;
import java.util.Iterator;


public class PerformanceScore
{

	private ArrayList<Score> technicalMerit;
	private ArrayList<Score> artisticMerit;

 
	public PerformanceScore()
	{
 
		technicalMerit = new ArrayList<Score>();
		artisticMerit = new ArrayList<Score>();
	}

	public void addScore(char letter, double actualScore, String judgeName)
	{
		if(letter == 'a') 
			 artisticMerit.add(new Score(actualScore, judgeName));
		else
			 technicalMerit.add(new Score(actualScore, judgeName));
}
			  
	public void getScores()
	{
		System.out.println("Technical Scores");
		for(int i = 0; i < technicalMerit.size(); i++)
		System.out.println( "" + artisticMerit.get(i) );
  }

		System.out.println("Artistic Scores");
		for(int i = 0; i < artisticMerit.size(); i++)
		System.out.println( "" + artisticMerit.get( i ) );
	}
		

}

I was wondering if anyone could tell me if I'm using the right Variables in things like the addScore method and how I can print out the scores in the style shown at the beginning of this post.
  • 0

Advertisements


#2
Dan

Dan

    Trusted Tech

  • Retired Staff
  • 1,771 posts
Howdy SportzTawk -- welcome to G2G :)

Disclaimer: It's been a fair while since I've used Java, so if some of the syntax/code is a little off, there's a good chance it's C++'ish. However, it should be close enough for you to work out :)

Here's a few pointers that will hopefully get you going in the right direction.
public void getRating()
	{
		System.out.println("" + rating);	
	}
This isn't technically an "accessor" method, and will in actual fact make it very difficult for you to get the formatting you desire. What an accessor method should be doing is returning a value. So, instead of having a void getRating() it should be (in the above examples case) double getRating() and instead of printing a line, you should simply return rating. This applies to all of your accessor methods. Changing all of your accessor methods to do this will make the printing side of things far easier for you, as well.
System.out.println( "" + artisticMerit.get(i) );
This is a logic error; what you are actually doing here is getting the Score object from the array. A step that may help you would be to do something like this:
System.out.println("Artistic Scores");
for(int i = 0; i < artisticMerit.size(); i++) {
	Score score = artisticMerit.get(i);
	System.out.print(score.getJudgeName() + ": " + score.getRating() + "\n");
}
While not necessarily the most efficient method, it should hopefully let you see where you were going wrong.

Finally, in regards to your addScore function, I would suggest that you change it into an "if (a) .. else if (t) .. else" statement, and use the else to catch any errors, so that if they enter the wrong letter it won't destroy your program.

Your mutator functions look perfect.

- Dan

Edited by Dan, 03 April 2009 - 04:01 AM.

  • 0

#3
SportzTawk

SportzTawk

    New Member

  • Topic Starter
  • Member
  • Pip
  • 2 posts
Firstly, thanks so much for the help. By the time you had answered this I've already figured it out. However, I've been stuff on a different set of codes that I really really am having a hard time getting to work.

I've finished most of it, but now I'm not sure how to write this code, or at least I can't interpret in my head what exactly it's asking... They are these three:

#a one-parameter constructor (the competitor): The constructor should initialize the competitor field to the parameter and initialized the scores field to an empty PerformanceScore.

# public getCompetitor: an accessor method for the competitor field (returns a reference to a Competitor object

# public listScores: no parameters; displays the result of scores.getScores() on the terminal window

Should the first be something like:

public Performance(Competitor competitor)
	{
		this.competitor = competitor;
		scores = new PerformanceScore();
	}


Should the second be something like:

public String getCompetitor() 
{
	 return "" + competitor;
}


Would I need to add anything to that?

And for the third, would the code be something like:

public void listScores() 
{
   System.out.println("" + scores.getScores());
}

EDITED TO ADD:

I guess i thought I understood the next ones... but not so much:

# public addScore with three parameters: a char containing an 'a' or 't' indicating that this is either an artistic or technical score, a double containing: the actual score, and a String containing the name of the judge (this just calls scores.addScore allowing us to add scores from the Performance class

# public performanceRating: no parameters; returns the average of the technicalMeritRating and the artisticMeritRating

In the first one, am I just supposed to put:

public void addScore(char letter, double actualScore, String judgeName) 
{
}

And is that supposed to be empty?

And for the second one, How would I use the average codes i got in an earlier method, and find those average for this method? Those method names are "artisticMeritRating" and "technicalMeritRating"
  • 0

#4
Dan

Dan

    Trusted Tech

  • Retired Staff
  • 1,771 posts
public String getCompetitor() 
{
	 return "" + competitor;
}
The requirements stated that they wanted a reference to the object; I don't believe this will return a reference, although it's been so long since I've used Java I could be wrong. Maybe something like:
public String getCompetitor() 
{
	Competitor reference = competitor;
	return reference;
}
The difference here is that you are making a copy of competitor, so if you modify the copy, it won't modify the original value.

The third item looks OK to me, so long as it's using the same formatting as you needed earlier.

# public addScore with three parameters: a char containing an 'a' or 't' indicating that this is either an artistic or technical score, a double containing: the actual score, and a String containing the name of the judge (this just calls scores.addScore allowing us to add scores from the Performance class

You're missing one line in your function, change it to something like this.
public void addScore(char letter, double actualScore, String judgeName) 
{
	scores.addScore(letter, actualScore, judgeName);
}

And for the second one, How would I use the average codes i got in an earlier method, and find those average for this method? Those method names are "artisticMeritRating" and "technicalMeritRating"

An easy way to do this would be to add an extra function to your PerformanceScore class.. although the requirement is a little ambiguous, so it's not clear whether or not he wants the average of the two, or each independently. You'll need to decide that and make the necessary changes:
public int getAverage(char letter)
{
	int average = 0;
	int total = 0;
	if(letter == 'a')
	{
		for(int i = 0; i < artisticMerit.size(); i++) {
			Score score = artisticMerit.get(i);
			total = total + score.getRating();
		}
		average = total / artisticMerit.size();
	}
	else if (letter == 't')
	{
		for(int j = 0; j < technicalMerit.size(); j++) {
			Score score = technicalMerit.get(j);
			total = total + score.getRating();
		}
		average = total / technicalMerit.size();
	}
	else 
		// Catch your errors here
	return average;
}
You would then need to call this from your performanceRating() function, once for each and print out the average for each.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP