Results 1 to 4 of 4
- 04-29-2012, 08:13 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 2
- Rep Power
- 0
Help desperately needed - synchronized swaps
Hi everyone,
I've got a problem that I'm hoping someone can find an answer to in fairly short order (college work issue)
I've been given a task to read in votes for 5 candidates, and then display the votes and candidates names in descending order
The question in full reads:
Implement a program to process votes for 5 candidates in a talent contest.
The program should use a String array to hold the names of the 5 candidates and an integer array to record the number of votes for each contestant.
It should prompt the user to enter the number of the candidate they wish to vote for (in the range 0 – 4), until -1 is entered, which signifies the end of voting. An error message should be output if the candidate selected is not in the required range.
At the end of voting, the program should sort the votes into descending order and output them, before outputting messages showing who was in 3rd, 2nd and 1st place:
• you will need to declare 2 arrays: one for integer votes to be input by the user and one for the names which you should initialise in the program - done
• you will need to modify the input method from the lectures so that it inputs votes until -1 is entered - done
• you will need to modify the sort method from the lectures so that it sorts into descending order, rather than ascending - done
• you will need to modify the output method from the lectures so that it outputs the candidate and the number of votes they received - Not there yet
• you will need 2 swap methods: one to swap names and one to swap votes so that the names and their associated votes remain synchronised - Help needed!
My issue is that this means I need a int array for votes and a string array for the names, and I need to keep them synchronized while I sort them for output. - this aspect has not been taught, and there are no references for how to do this anywhere on in my course notes. I'll post my current workings below, but please be aware I've been fudging around with a stringSwap method with no success.
We're using very basic language so far, so what you see in my code below is pretty much what I've been taught.
Java Code://output candidates from third place to first by votes import java.util.*; public class Test { public static void main (String [] args) { int [] arr = new int[5]; input(arr); sort(arr); output(arr); //for testing } public static void input(int[] arr) { Scanner kybd = new Scanner(System.in); System.out.println("Enter Candidate Numbers. -1 ends: "); int input = kybd.nextInt(); while(input > -1) { if(input > 0 && input < 6) { arr[input - 1]++; } input = kybd.nextInt(); } } public static void sort(int[] arr) { for (int pass = 1; pass < arr.length; pass++) { int largestPos = findLargest(arr, arr.length - pass); if (largestPos != arr.length - pass) { toString(largestPos, arr.length - pass); swap(arr, largestPos, arr.length - pass); } output(arr); //this is for testing purposes only } } public static int findLargest(int [] arr, int num) { int largestPos = 0; for (int i = 1; i <= num; i++) { if (arr[i] <= arr[largestPos]) { largestPos = i; } } return largestPos; } public static void swap(int [] arr, int first, int second) { int temp = arr[first]; arr[first] = arr[second]; arr[second] = temp; } public static void output(int [] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); } public static String toString(int num, int numTwo) { String [] text = {"Cliff", "Dave", "Eric", "Bob", "Andy"}; stringSwap(text, num, numTwo); return text[num]; } public static void stringSwap(int [] text, int num, int numTwo) { int temp = text[num]; text[num] = text[num2]; text[num2] = temp; } }
Any help would be much appreciated. Thanks!Last edited by Chromatism; 04-29-2012 at 08:34 PM.
- 04-29-2012, 10:01 PM #2
Re: Help desperately needed - synchronized swaps
Why do they call it rush hour when nothing moves? - Robin Williams
- 04-29-2012, 10:44 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 2
- Rep Power
- 0
-
Re: Help desperately needed - synchronized swaps
I think I'll let them help you guys in the other forum.
Similar Threads
-
Desperately Need Help With Java Programming Assignment
By Hadsvich in forum New To JavaReplies: 15Last Post: 04-21-2012, 01:38 AM -
Difference b/w "synchronized","synchronize",and "synchronized()"
By Bala_Rugan in forum New To JavaReplies: 1Last Post: 09-08-2010, 04:08 PM -
Declaring a Variable for RGB swaps? Dr Java
By brandnew956 in forum New To JavaReplies: 5Last Post: 03-03-2010, 01:44 AM -
Desperately need help
By nel636 in forum New To JavaReplies: 0Last Post: 12-03-2008, 12:43 AM -
I desperately need help
By dc2acgsr99 in forum Java AppletsReplies: 4Last Post: 04-30-2008, 08:18 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks