Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho