View Full Version : Java Help!!!!!!!!!!! Anyone Much Appriciated!!!!!!!!!
Xg Corkin
28-03-2007, 12:09 AM
Problem:
The Sports Studies Department of the Institute would like to produce statistics on the Belfast and London marathons in 2006. In particular, they want to compare the times of a selected group of ten runners from different countries in the United Kingdom who competed in each marathon.
You are required to write a program to read each runners? time to finish each marathon and to produce the following statistics
? List of names of the female runners and a list of the names of the male runners.
? The average race time for each runner over the two races.
? The country, name and time of the fastest and slowest runner from the group in each race.
? The average race time to complete the each race.
? The fastest female runner overall, from either race.
? The fastest male runner overall, from either race.
All times should be input as a whole number in seconds and should be displayed in the output as hours minutes and seconds
Data: COUNTRY - NAME - GENDER - BELFAST TIME - LONDON TIME.
N Ireland - Tracy McCormick - F- 11087 - 12001
England - Ray Plant - M - 9657 - 9767
Ireland Philip Wood - M -9576 - 9675
Scotland - Joan Watson - F - 16000 - 15976
Wales - Suzanne Perry - F - 12000 - 12789
Thing i need help with:
? Method calculateandDisplayStats(),and associated methods
- A method calculateandDisplayRaceStats() which takes an array of Strings representing the names of the runners and an array of integers holding the runners? race times for one race and displays the statistics for that race. This method should call other value methods, one to return the average runners? race time for a race and another to return the position in the array of the runner with the fastest race time and a similar one for the runner with the slowest race time.
So far i have this done for this problem:
public class Assignment {
public static void main(String[] args)
{
readstats();
displaylists();
System.out.println("\n");
System.out.println("End of output - press <ENTER> to quit.");
uuInOut.ReadLine();
}
static final int RUNNERS = 5;
static int[] londontime = new int[RUNNERS];
static int[] belfasttime = new int[RUNNERS];
static String[] name = new String [RUNNERS];
static char[] gender = new char [RUNNERS];
static String[] country = new String [RUNNERS];
/**
Method to read the names and marks
*/
static void readstats()
{
for (int i = 0; i < RUNNERS; i++)
{
System.out.println("Input the gender M or F:> ");
System.out.flush();
gender[i] = uuInOut.ReadChar();
System.out.println("Input the name:> ");
System.out.flush();
name[i] = uuInOut.ReadString();
System.out.println("Input the country:> ");
System.out.flush();
country[i] = uuInOut.ReadString();
FiveOhOne
28-03-2007, 11:07 AM
Javas Poo. But Mr. Duke is your man...
Duke87
28-03-2007, 11:20 AM
Assuming that by 'array' of Strings/Integers they mean the old school hardcore arrays as opposed to the newer Java object, the method you want will probably look something like:
public void calculateAndDisplayStats(String[] runners, Integer[] times) {
if (runners.length != times.length) {
somethings broken
}
for (int i = 0 ; i < runners.length ; i++) {
System.out.println("Name: " + runners[i] + " Time: " + times[i]);
}
System.out.println("Average: " + calculateAverage(times);
}
private Integer calculateAverage(Integer[] times) {
Integer sum = new Integer();
for (int i = 0 ; i < times.length ; i++) {
sum += times[i];
}
return sum / times.length;
}
Then a couple more private methods to find the lowest and highest member of the array of times. Easiest way is probably to loop through keeping track of the lowest one you've found so far. Something like:
Integer lowest = Integer.MAX_VALUE;
for (int i = 0 ; i < times.length ; i++) {
if (times[i] < lowest) {
lowest = times[i];
}
}
And the opposite for highest. Hopefully that makes sense, gimme a shout if you need any more help.
Xg Corkin
28-03-2007, 07:45 PM
- A method calculatePersonalStats() which will take the following parameters:
- String array of runner names and
- Two arrays of integers representing the race time for the two races
- and display the average race time for each runner over the two races
KH Torrance
28-03-2007, 07:57 PM
I didn't see anything in Duke's code for outputting the time in "h:m:s" rather than all seconds (as I think they were input), but you'd basically just use a couple of divide by 60's. Shouldn't be difficult. I'd do one for you but by the time I finish it and hit 'post', I'll find someone's beaten me to it!
I don't understand the second bit you've posted.
Can't you just modify the code you've got for the first part?
There's nothing that looks much different, really. :-S
Xg Corkin
28-03-2007, 07:57 PM
@ KH Torrance - Im a novice at java at the minute, i've only started it and because we're getting of for easter soon i want to get this completed before i get of otherwise i would take my time and do it myself! Use are to smart for me...lol
BASICALLY WHAT I WAS GIVEN TO WRITE THE PROGRAM!
The Sports Studies Department of the Institute would like to produce statistics on the Belfast and London marathons in 2006. In particular, they want to compare the times of a selected group of ten runners from different countries in the United Kingdom who competed in each marathon.
You are required to write a program to read each runners? time to finish each marathon and to produce the following statistics
? List of names of the female runners and a list of the names of the male runners.
? The average race time for each runner over the two races.
? The country, name and time of the fastest and slowest runner from the group in each race.
? The average race time to complete the each race.
? The fastest female runner overall, from either race.
? The fastest male runner overall, from either race.
All times should be input as a whole number in seconds and should be displayed in the output as hours minutes and seconds
Your program should include the following methods:-
A method readStats () which takes an array of Strings and two arrays of integers as parameters to hold the names, Belfast race time and London Race time respectively, for each runner. It should read these values from the keyboard into the arrays.
A method calculateandDisplayRaceStats() which takes an array of Strings representing the names of the runners and an array of integers holding the runners? race times for one race and displays the statistics for that race. This method should call other value methods, one to return the average runners? race time for a race and another to return the position in the array of the runner with the fastest race time and a similar one for the runner with the slowest race time.
A method calculatePersonalStats() which will take the following parameters:
String array of runner names and
Two arrays of integers representing the race time for the two races
and display the average race time for each runner over the two races
A method calculateFastestOverall()which will take a the following parameters:
String array of runner names and
Two arrays of integers representing the race time for the two races
Array representing the sex of the runners
and display the name and race time for the fastest female and the name and race time for the fastest male.
Method to display the time as hours minutes and seconds
You may wish to include other methods as well as those indicated above.
Your program should follow good programming practice regarding layout, variable names and comments. Output should be presented clearly with a consistent format.
Use the data below to fully test your program and include the printouts of your output with your submission.
Marathon Statistics for group of 5 runners
Country Name Age Time Belfast Time London
N Ireland Tracy McCormick F 11087 12001
England Ray Plant M 9657 9767
Ireland Philip Wood M 9576 9675
Scotland Joan Watson F 16000 15976
Wales Suzanne Perry F 12000 12789
PROGRAM SHOULD INCLUDED
? Main method, including definition data structures
? Method readStats ()
? Display lists of male and female runners
? Method calculateandDisplayStats(),and associated methods
? Method calculatePersonalStats(),and associated methods
? Method calculateFastestOverall (), and associated methods
? Method/methods to display the time in hours minutes and seconds
? Comments and clarity
? Presentation and correctness of output and testing
ANY OTHER HELP DUKE WOULD BE GREATLY APPRECIATED, LIKE WRITING THE PROGRAM AGAIN! LOL
AT THE MINUTE MY PROGRAM IS NOT WORKING, I CAN INPUT THE DATA BUT IM GETTING NO OUTPUT FROM THE PROGRAM. Grrrrrrrrr!
WERES DUKE WHEN YOU NEED HIM!lol!
Xg Corkin
28-03-2007, 10:54 PM
PROBLEMS!
http://img373.imageshack.us/img373/4770/problemna6.png
http://img338.imageshack.us/img338/2017/problem2dj5.png
KH Torrance
29-03-2007, 07:32 AM
picture 1
I can't tell where the method's coming from, but my first thought is that you need anObject.displayOneList(xx)
^ Edit: Seen the method below, so that's probably not right!
picture 2
I guess you input a string or (not sure if this would give an error) a floating point value. You will need a try and catch statement to make sure that the user enters an integer, and outputs an appropriate error message than asks for another entry if he doesn't.
picture 3
"int" is a type, not the name of an array! :-) From what I can tell, all you need to do is remove the square brackets from around the [i] in the line you've selected.
EDIT: Just seen that picture 2 is part of picture 1! How'd you get it to run with errors in the code? :-S Anyway I think the compilation problem will go away as soon as you've fixed everything else.
Edit Again: Don't you need single quotes around the M and F? Because they are a character, not a string? That could be where the problem is. You know if you click the thing you get more information about the error!
Duke87
29-03-2007, 10:36 AM
Right, I guess we'd better establish how much Java you actually know. Do you understand Objects? If so, why are all those methods static?
I guess you probably want to write a class that does all this stuff, and instantiate and control it through you main method. To be honest, there's no point in us writing it for you, since you'll just have as much trouble next time they set something Java based.
Xg Corkin
29-03-2007, 03:49 PM
TURNS OUT I HAVE TO NEXT WEDNESDAY TO DO THIS!
WOOOOOT
ANYWAY HERES WHAT I HAVE DONE SO FAR! ANY TIPS TO MAKE IT SIMPLER WOULD BE GREAT!
PROGRAM:::::
public class Assignment { // main class of the assignment
public static void main(String[] args) //starts the program
{
readstats();//read stats as i enter them
displaylists();//define the displaylist static void
System.out.println("\n");
System.out.println("End of output - press <ENTER> to quit.");//press enter to end the ouput when finished
uuInOut.ReadLine();
}
static final int RUNNERS = 5;//final integer is RUNNERS
static int[] londontime = new int[RUNNERS];//defining the london time as an integer
static int[] belfasttime = new int[RUNNERS];//defining the belfast time as an integer
static String[] name = new String [RUNNERS];//defining the name as string
static char[] gender = new char [RUNNERS];//defining the gender as character
static String[] country = new String [RUNNERS];//defining the country as string
static int[]sum = new int [RUNNERS];
/**
Method to read the names and marks
*/
static void readstats() //starts the input for this method
{
for (int i = 0; i < RUNNERS; i++)
{
System.out.println("Input the gender M or F:> ");//prints: input the gender M or F so you can input the gender
System.out.flush();
gender[i] = uuInOut.ReadChar();
System.out.println("Input the name:> ");//prints input the name so you can input the name of the runner
System.out.flush();
name[i] = uuInOut.ReadString();
System.out.println("Input the country:> ");//prints input the country so you can input the country of the runner
System.out.flush();
country[i] = uuInOut.ReadString();
System.out.println("Input the belfast time:> ");//prints input the belfast time so you can input the belfast time of the runner
System.out.flush();
belfasttime[i] = uuInOut.ReadInt();
System.out.println("Input the london time:> ");//prints input the london time so you can input the london time of the runner
System.out.flush();
londontime[i] = uuInOut.ReadInt();
System.out.println();
}
}
static void displaylists()
{
displayOnelist ('M', "Male Runner");// telling you that M equals Male Runner
displayOnelist ('F', "Female Runner");//telling you that F equals Female Runner
}
static void displayOnelist(char x, String message)
{
System.out.print("\n");
for (int i = 0; i < RUNNERS; i++);
{
if (gender[i] == x)
{
System.out.print (name[i] + " ");
System.out.print(gender[i] + " ");
System.out.flush();
System.out.println();
}
}
}
/**
* Method "calculateAndDisplayStats" used to call other methods that
* either return the position of the fastest or slowest runner of the belfast
* race. The method also calls
*/
static void calculateAndDisplayStats()
{
int averageBelfast = 0;
int averageLondon = 0;
int remainder = 0;
int hours = 0;
int minutes = 0;
int seconds = 0;
int fastestBelfastPosition = 0;
int slowestBelfastPosition = 0;
{
System.out.println("********* Belfast Race Stats *********");
averageBelfast = getAverageBelfastTime(belfasttime);
System.out.println("The average Belfast time is " + averageBelfast);
fastestBelfastPosition = getFastestBelfast(belfasttime);
System.out.println("The fastest belfast time is from " +name[fastestBelfastPosition]);
slowestBelfastPosition = getSlowestBelfast(belfasttime);
System.out.println("The slowest belfast time is from " +name[slowestBelfastPosition]);
}
}//end calculateAndDisplayStats
/**
* Method "getAverageBelfastTime" that adds together all of the belfast times
* and then divides them by five to get the average. The method then returns
* then average
*/
static int getAverageBelfastTime(int[] belfasttime)
{
int averageBelfast = 0;
{
averageBelfast = (belfasttime[0] + belfasttime[1] + belfasttime[2] + belfasttime[3] + belfasttime[4])/5;
return averageBelfast;
}
}//end getAverageBelfastTime
/**
* Method "getFastestBelfast" finds the position in the array of
* the runner with the fastest time. The method then returns
* the position of the fastest runner.
*/
static int getFastestBelfast(int[] belfasttime)
{
{
int fastest = 32000;
int fastestBelfastPosition = 0;
for (int i=0; i<RUNNERS; i++)
{
if (belfasttime[i]<fastest)
fastest = belfasttime[i];
fastestBelfastPosition = i;
}
return fastestBelfastPosition;
}
}//end getHighestBelfast
/**
* Method "getSlowestBelfast" finds the position in the array of
* the runner with the slowest time. The method then returns
* the position of the slowest runner.
*/
static int getSlowestBelfast(int[] timeBelfast)
{
{
int slowest = 0;
int slowestBelfastPosition = 0;
for (int i=0; i<RUNNERS; i++)
{
if (belfasttime[i]>slowest)
slowest = belfasttime[i];
slowestBelfastPosition = i;
}
return slowestBelfastPosition;
}
}//end getSlowestBelfast
/**
* London Race statistics
*/
{
int averagelondon = 0;
int remainder = 0;
int hours = 0;
int minutes = 0;
int seconds = 0;
int fastestlondonPosition = 0;
int slowestlondonPosition = 0;
hours = averagelondon / 3600;
remainder = averagelondon % 3600;
minutes = remainder / 60;
seconds = remainder % 60;
System.out.println("********* london Race Stats *********");
averagelondon = getAveragelondonTime(londontime);
System.out.println("The average london time is " + averagelondon);
fastestlondonPosition = getFastestlondon(londontime);
System.out.println("The fastest london time is from " +name[fastestlondonPosition]);
slowestlondonPosition = getSlowestlondon(londontime);
System.out.println("The slowest london time is from " +name[slowestlondonPosition]);
static int getAveragelondonTime(int[] londontime)
{
int averagelondon = 0;
{
averagelondon = (londontime[0] + londontime[1] + londontime[2] + londontime[3] + londontime[4])/5;
return averagelondon;
}
}
static int getFastestlondon(int[] londontime)
{
{
int fastest = 32000;
int fastestlondonPosition = 0;
for (int i=0; i<RUNNERS; i++)
{
if (londontime[i]<fastest)
fastest = londontime[i];
fastestlondonPosition = i;
}
return fastestlondonPosition;
}
}//end getHighestBelfast
/**
* Method "getSlowestBelfast" finds the position in the array of
* the runner with the slowest time. The method then returns
* the position of the slowest runner.
*/
static int getSlowestlondon(int[] londontime)
{
{
int slowest = 0;
int slowestlondonPosition = 0;
for (int i=0; i<RUNNERS; i++)
{
if (belfasttime[i]>slowest)
slowest = belfasttime[i];
slowestlondonPosition = i;
}
return slowestlondonPosition;
}
}
}//end getSlowestlondon
KH Torrance
29-03-2007, 04:03 PM
I don't understand why you set int fastest = 32000. Where'd that number come from? Couldn't you set the variable int fastest = placeTime[0] to begin with, so at least you know you'll end up with an actual time achieved by one of the runners?
Apart from that, it seems okay. But I might copy and paste it into JBuilder later to have a proper look.
Xg Corkin
29-03-2007, 04:19 PM
I dnt know, we were told to set it at 32000 for some reason but don't no why! can you explain the ( int fastest = placeTime[0] ) a bit more so i can understand it?
Cool well get back to me with any more problems that you can see once you have had a better look at it!
Thanks Corkin!
KH Torrance
29-03-2007, 05:20 PM
Well, the way you have it, if none of the times (in your london/belfastTime arrays) are faster than 32000 seconds, then your program will print that the fastest time is 32000.
How can you have 32000 as the fastest time, when none of the runners got that time?
Using "int fastest = placeTime[0];" (where 'place' is either belfast or london) instead of "int fastest = 32000;" is basically setting the variable 'fastest' to the time held in the first position of the array.
So, when you run the loop, if no times are faster, then you've got a fastest time that actually exists.
If any times are faster than that, well, it works just like normal.
It also means you can start the loop from the second position to save comparing time[0] with itself; so you'd use int i = 1 for the initialization.
Edit:
And what are you importing to use uuInOut?
Xg Corkin
29-03-2007, 05:59 PM
Ahh thats sounds better, i get what your saying Torrance, much appreciated!
Yeh im using uuInOut!
Xg Corkin
01-05-2007, 10:45 PM
HELPPPPPP!!!!!!! LOL! I HVNT DONE MUCH ON THIS TYPE OF JAVE STUFF IN WAS WONDERING WERE DUKE IS!lol! OJOJ!!!! NE1 THT CAN HELP ON THIS PROBLEM PLZZZ?????
Expressimo Delivery Service is a package delivery business. The company allows two types of packaging, letter and box, and three types of service, Next Day Priority, Next Day Standard and 2-Day. It uses a computer system with a text based interface to compute the delivery charge, so the management wish to replace it with a more user friendly visual system. You are hired as a freelance computer consultant to implement this service and develop an application that computes the delivery charge.
The following table shows the formula for computing the charge:
Package Type Next Day Priority Next Day Standard 2-Day
Letter ?12.00 up to 8oz ?10.50 up to 8oz Not Available
Box "?15.75 for the first lb. Add ?1.25 for each additional lb." "?13.75 for the first lb. Add ?1.00 for each additional lb." "?7.00 for the first lb. Add ?0.50 for each additional lb."
Design an applet and write a program to calculate the delivery charge. The applet should list delivery options and package types and allow the user to select one item from each menu (or other suitable component). The user should then input the weight of the letter or box in ounces. The final cost of delivery should then be displayed in a TextField or if the input is invalid a message displayed on the Status Bar
It is important to make use of features, such as Colour, TextField, Label, Choice and Button objects to provide a user friendly clearly presented applet which suitable error messages for invalid input.
this is the things that need to be done!
You should submit:
• A Dictionary giving the name, type and brief description of each GUI component;
• Algorithm for the application program;
• Listings of the applet and html files;
• Screen Print of your applet;
• The path in your account of your working solution, you may be asked to demonstrate your solution.
vBulletin® v3.7.1, Copyright ©2000-2009, Jelsoft Enterprises Ltd.