| |||||||||||
array in java Posted by marocain26 (marocain26), 27 January 2004 Hello everybody,Thanks for replying to my message in advance. I just started programming i JAVA and i need to help writing a program that allows the user to enter the number of candidates in an election, their last name, and the number of votes received by each candidate: Inputs: Number of candidates: 4 Name: Votes:.... Outputs: Candidate Votes % of Total Votes ... ... Using a one dimensional array, here is what o got so far: */ import javax.swing.*; public class Candidates { // data members private int[] num; private String[] name; private double[] votes; // methods public Candidates(int numC, String nameC, double votesC) { num = new int[4]; name = new String[nameC]; votes = new double[votesC]; } public void readNum() { String numStr; String nameStr; String voteStr; for(int i = 0; i < num.length; i++); String numStr = JOptionPane.ShowInputDialog("Enter the number of candidates"); int numC = Integer.parseInt(numStr); String nameStr = JOptionPane.ShowInputDialog("Enter the name"); int nameC = nameStr; String voteStr = JOptionPane.ShowInputDialog("Enter the number of votes"); double votesC = Double.parseDouble(voteStr); } } public int TotPercent() { double[] percent; for(int i = 0; i < num.length; i++) { if(name[i] && votes[i]) { percent[i] = votes[i] / votes[i]+ votes[i+1]+ votes[i+2]+ votes[i+3]; } } } } public class TestCandidates { public static void main(String[] args) { Candidates.voteDisplay = new Candidates(4); voteDisplay.readNum(); System.out.println("Candidate Votes Received % of Total Votes" + name[i] Thanks for any feedback. Posted by admin (Graham Ellis), 27 January 2004 It's always important to know what you're writing a program for, and to plan ahead and come up with design plans before you start coding .... there's one school of thought that says that you should have a very full design before you write even a single line of code and on occasions that I've used that technique it has worked very well for me. At the very least, outline your application framework before you start.Have you written an application framework? What do you want your application to do? What objects are you going to use and what API is going to talk to them? Remember to plan objects for code re-usability. It strikes me that you're unclear as to what your application will actually do; you describe it as a program that "allows the user to enter ...." but you don't say what you'll be doing with the data entered; then you provide a method which looks like it's attempting to produce some sort of average?? There are design tools out there like UML that help you model bigger application designs - but these are way over the top for something that's not going to be even 1000 lines long. What you can / should do is to sketch out some object diagrams, and a couple of flowcharts to start you off. The when you're clear of the structure of your classes, sketch out the structure of these diagrams in Java source files - class and method declarations, class and object variables, etc. ... and add in comments that describe the functionallity of each block so that you know what you have to write to complete the job. I suspect that if you follow this suggestion, the coding itself will become much clearer, and you'll be back here asking for advise about specific Java tasks and constructs rather than the application as a whole This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |