Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Have Programming Question? Ask it here!
 
 
 


David Ferguson
Java Sorting
2 Answer(s)      2 years and 8 months ago
Posted in : Java Beginners


Could somebody help me with this question.

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.


Advertisement
View Answers

November 28, 2011 at 4:59 PM


import java.io.*;
import java.util.*;

class Candidate{
public String name;
public  int vote ;
public Candidate(){}
public Candidate(String name,int vote) {
super();
this.name = name;
this.vote=vote;
}

public String getName(){
return name;
}
public int getVote() {
return vote;
}
}

class VoteComparator implements Comparator{
    public int compare(Object can1, Object can2){
        int vote1 = ((Candidate)can1).getVote();        
        int vote2 = ((Candidate)can2).getVote();

        if(vote1 > vote2)
            return 1;
        else if(vote1 < vote2)
            return -1;
        else
            return 0;    
    }
}
public class ProcessVotes{
public static void main(String[] args) throws Exception{
List<Candidate> list = new ArrayList<Candidate>();
list.add(new Candidate("A",10000));
list.add(new Candidate("B",20000));
list.add(new Candidate("C",15000));
list.add(new Candidate("D",12000));
list.add(new Candidate("E",17000));
System.out.println(" ");
int v=0;
String n="";
               Collections.sort(list,new VoteComparator());
                for(Candidate data: list){
                v=data.getVote();
                n=data.getName();
                System.out.println(data.getName()+" \t "+data.getVote());
                } 
                System.out.println(n+" receives highest vote i.e. "+v);
   }
}


November 28, 2011 at 4:59 PM


import java.io.*;
import java.util.*;

class Candidate{
public String name;
public  int vote ;
public Candidate(){}
public Candidate(String name,int vote) {
super();
this.name = name;
this.vote=vote;
}

public String getName(){
return name;
}
public int getVote() {
return vote;
}
}

class VoteComparator implements Comparator{
    public int compare(Object can1, Object can2){
        int vote1 = ((Candidate)can1).getVote();        
        int vote2 = ((Candidate)can2).getVote();

        if(vote1 > vote2)
            return 1;
        else if(vote1 < vote2)
            return -1;
        else
            return 0;    
    }
}
public class ProcessVotes{
public static void main(String[] args) throws Exception{
List<Candidate> list = new ArrayList<Candidate>();
list.add(new Candidate("A",10000));
list.add(new Candidate("B",20000));
list.add(new Candidate("C",15000));
list.add(new Candidate("D",12000));
list.add(new Candidate("E",17000));
System.out.println(" ");
int v=0;
String n="";
               Collections.sort(list,new VoteComparator());
                for(Candidate data: list){
                v=data.getVote();
                n=data.getName();
                System.out.println(data.getName()+" \t "+data.getVote());
                } 
                System.out.println(n+" receives highest vote i.e. "+v);
   }
}


Related Tutorials/Questions & Answers:
Sorting in Java
Sorting in Java  Sorting in Java
Java sorting
Java sorting  can somebody help me know how java sorts? means which... the following links: Java bubble sort Java Heap Sort Java Insertion Sort Java Merge Sort Java Selection Sort Java Quick Sort   hello
Advertisements
Bubble Sorting in Java
Bubble Sorting in Java  Hi, What is Bubble Sorting? Guide me where to learn Bubble Sorting in Java? Thanks   Hi, Check the tutorial at Bubble Sorting in Java. Thanks
Java Sorting
Java Sorting  Could somebody help me with this question. 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
Sorting
Sorting  can any help me know which sorting algorithm java uses for sorting collection and arrays
sorting array in java
sorting array in java  How to sort array in Java or JavaScript?   JavaScript Sorting array tutorial   Java Sort array of strings import java.util.*; class ArrayExample{ public static void main(String
Java Sorting and Searching
Java Sorting and Searching   If anyone could help me with this I would be really thankful! Write a program that stores the names of these artists in a String array. The program should prompt the user to enter the name
Sorting in java - Java Beginners
Sorting in java  Hello.. I want you to help me with this question.. 1. A statistics company wants to keep information of families. The information of a family is the family name, the number of members and first name of each
java sorting codes - Java Beginners
java sorting codes  I want javasorting codes. please be kind enogh... the following link: http://www.roseindia.net/java/beginners/arrayexamples/ Here you will get various sorting algorithms. Hope that it will be helpful for you
java array sorting manual way
java array sorting manual way  Hi, Anyone has any sample code on how to sort a array manually without using the inbuild sorting algorithm? Thanks
merge sorting in arrays - Java Beginners
merge sorting in arrays  Write a program to insert string or characters to an array and apply merge sorting on this array  Hi Friend, Please visit the following link: http://www.roseindia.net/java/beginners
sorting
sorting   write a program to arrange sorting in rows,column and diagonal
Sorting algorithms - Java Beginners
Sorting algorithms  I'v being given an assignment and I still couldn't find a proper answer for the following questions.Can anyone pls pls help me wi it? 1)Compare and contrast efficiencies of Shell,Quick,Heap and Radix sort
SORTING - Java Interview Questions
("Items after sorting are ="+list); } } Thanks
Java Hashmap Sorting - Java Beginners
Java Hashmap Sorting   I have a sorting issue with a Hashmap. My constraint is that I MUST use the Hashmap and work with existing code. I do..., it loses the original alphabetical sorting done by the database. So, my problem
sorting - Java Beginners
; Easy Sorting: For all the values in the array A, find the largest and store
Array sorting - Java Beginners
Array sorting   Hello All. I need to sort one array based on the arrangement of another array. I fetch two arrays from somewhere and they are related. For example, String type[] = {"X","T","3","R","9"}; String
sorting
sorting  write to a program to arrange the sorting in rows, column and digonal
Comparison between the types of sorting in java
Comparison between the types of sorting in java  welcome all i wanna program in java compare between selection,insertion,bubble,merge,quick sort... of sorting. please help me thanks all
sorting
sorting  how to do sorting without using bubble sort,selection sort
NAME SORTING. . .anyone? - Java Beginners
NAME SORTING. . .anyone?  how can I sort names without using the 'name.sort' method? please help. . .anyone? the program should sort the first three(3) letters of the names tnx java masters out there!! (^_^) cVm  Hi
sorting student record - Java Beginners
sorting student record  Program in java for inserting, recording, deleting, editing and searching student details   can u explain about recording ? u want to store value in database or in file or opertinng run time
ARRAYS SORTING - Java Interview Questions
ARRAYS SORTING  How To Sort An Array With Out Using Sort Method ?I Want Code?  Hi, Here is the code in java. You can find both Ascending and Descending order code. Ascending order is commented. public class
sorting an array of string with duplicate values - Java Beginners
sorting an array of string  Example to sort array string
sorting an array of string with duplicate values - Java Beginners
sorting an array of string with duplicate values  I have a sort method which sorts an array of strings. But if there are duplicates in the array it would not sort properly
Sorting String arrays in java - Java Beginners
Sorting String arrays in java  I have to make an interface that creates an array of unknown size, resizes the array when needed, and sorts the array... not think that my sorting thusfar is correct. Can anyone help? Please help
group by sorting
group by sorting  group by sorting of data in SQL
Sorting and Searching
Sorting and Searching  Hi i would really be thankful if someone could help me with this A program is required to ask users to rate the Java...) { Scanner input = new Scanner(System.in); System.out.print("Rate Java(0-10
Sorting Array List in Java
Sorting Array List in Java In this example you will learn about sorting array list in java using an example. By default array list are sorted as the element are inserted into the list. In Java ArrayList extends AbstractList implements
Sorting an ArrayList
Sorting an ArrayList  print("code sample");Hello All, I am working on a program for school and I am having trouble with sorting my list... really new at Java...This is only the second program I am writing for school
linear sorting
linear sorting  what is linear sorting? can any send me an example   Hi Friend, Try this: import java.util.*; class ArraySort{ public...("After Sorting: "); for(int i=0;i<arr.length;i++){ System.out.println
sorting numbers
sorting numbers  How to sort the numbers in ascending order   import java.util.*; class SortNumbers{ public static void main(String[] args) { Scanner input=new Scanner(System.in
Sorting Program
Sorting Program  To sort 10items in alphabetical order   Arrays.sort(name of ur array)   import java.util.*; class ArrayExample{ public static void main(String[] args) { String array[]=new
sorting and storing data
sorting and storing data   sorting and storing data in UITableView
Sorting the array
Sorting the array  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
Sorting Router Interface in a list.
, GigabitEthernet 1/11 How can i achieve this type of sorting in java. It's really urgent...Sorting Router Interface in a list.  Hi All, I have list which contains router interface as GigabitEthernet 1/0, GigabitEthernet 1/1,GigabitEthernet
Alphabetically sorting order
Alphabetically sorting order  Write a java program that takes a list of words from the command line and prints out the arguments in an alphabetically sorted order   Hi Friend, Try the following code: import
sorting Line in c
sorting Line in c  Hi I need help with sorting line in programming c asking user to enter 1-10 characters and then sort them in lower case, alphabetical and ascending order. Any help will be appreciated! please help! void
Imp-HTML Table Sorting
Imp-HTML Table Sorting   Hi, I am working on a project and as per requirement from client i have to sort a table based on the ID Column. I want... of HTML table sorting where sorting happenes whenever user clicks on column header
Sorting along life span
Sorting along life span  how to find total age in date of birth and date of death.and also sort along life span.using object class and collection API
What are the different functions in sorting an array?
What are the different functions in sorting an array?  What are the different functions in sorting an array
Array Sorting - Development process
Array Sorting  Hello sir, Now i m doing various Array sorting(Bubble,insertion,Quick,merge etc ) I want to display Array Sort in steps by step like... is the code to display array sorting with moving graphics(color)in steps
sorting folders automatically
sorting folders automatically  i want to sort folders automatically,let say the folders to be displayed in a webpage(hyperlinked to its resource).the page should display only the folder that belongs to last 15 days.it should
Sorting the letters in a String
Sorting the letters in a String  I have recently learned how to sort different strings using the insertion sort method but I still cannot sort the letters in one particular string. For instance, if I enter in the string "Hello my
Sorting Xml file - XML
Sorting Xml file  I have an xml file like this: Smith USA Bob USA John UK Smith SG Amy AUC And I would... Smith USA SG I will do my implementation in Java. So, I would
 

 

 

DMCA.com