Project Help!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Project Help!

  1. #1
    Join Date
    Feb 2005
    Posts
    4

    Post Project Help!

    This program will allow a user to enter Voter information, which will be formatted and tabulated into a final report.

    Sample Input and Output:

    Enter 4 digit voter number: 1234

    Enter Voter Type (Republican = 1, Democrat = 2, Independent = 3): 1

    Enter Gender Type (Female = 1, Male = 2): 1

    Enter Voter Age: 30

    //Then echo data back to user

    Voter#1: //the number should go up each time

    Voter Number: 1234 Voter Type: 1 Gender Type: 1 Voter Age: 30

    Enter another Voter (Y/N): Y //if yes is entered, go back and get another voter

    Once N is entered the following report will be printed:

    Vote 2000 Statistics

    Voters entered in to the system X
    Average Voter Age XX


    Two methods and one class should be used. Have a method called GetAVoter that collects and echos the data from the user and one called PrintVoterReport that prints the report. You will need to determine how and where to do the counts and totals. Remember that the variable can only hold one value at a time.


    Here is the psuedo code that i got.....

    1. START LOOP

    2. WRITE: Enter voter number (assuming the voter number is something the user enters, not auto-generated by the program)

    3. READ: voterNumber

    4. WRITE: Enter voter age

    5. READ: voterAge

    6. WRITE: Enter voter type

    7. READ: voterType

    8. WRITE: Enter another voter?

    9. IF YES: loop to line 1

    10. ELSE: exit loop

    11. PRINT: results


    I am needing help on how to start writting the program.

    Any help would be greatly apperciated.
    Tera Williamson

  2. #2
    Join Date
    Feb 2005
    Posts
    4

    Post Is this anywhere on the right line??

    // The "Voter" class.
    public class Voter
    {
    String VoterNumber // Voter Number

    String VoterAge // Voter Age

    String VoterType // Voter Type

    public void GetAVoter() throws IOException
    {
    BufferedReader inData; //Input stream for strings

    inData = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter voter number: ");
    VoterNumber = inData.readline();
    System.out.println("Enter voter age: ");
    VoterAge = inData.readline();
    System.out.println("Enter voter type: ");

    }

    public static void main (String[] args)
    {
    GetAVoter
    read = new Voter();
    read.VoterNumber();
    read.VoterAge();
    read.VoterType();
    {
    // Place your code here
    } // main method
    } // Voter class



    I am so lost!
    Tera Williamson

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    This one should do. I haven't make any check for the
    4-digit voter number (range 1000-9999) though.

    Code:
    import java.io.*;
    import java.util.*;
    import java.text.*;
    // The "Voter" class.
    
    class Voter {
      private int voterAge; // Voter Age
      private int voterType; // Voter Type
      private int voterGender; // voter gender
      private int voterNumber; // Voter Number
      //Input stream for strings
      static BufferedReader inData= new BufferedReader(new InputStreamReader(System.in));
    
      /**
       * Constructor
       */
      public Voter (int voterNumber, int voterType, int voterGender, int voterAge) {
        this.voterAge=voterAge;
        this.voterType=voterType;
        this.voterNumber=voterNumber;
        this.voterGender=voterGender;
      }
      /**
       * Gets
       * @return
       */
      public int getVoterAge() {
        return voterAge;
      }
    
      /**
       * Get String input
       * @param prompt
       * @param values a string array of permitted values, if null then anything goes
       * @return
       */
      public static String getStringInput(String prompt, String [] values) {
        while (true) {
          System.out.println(prompt);
          String s = null;
          try {
            s = inData.readLine().trim();
          }
          catch (IOException ex) {
            System.out.println("Input error: "+ex.getMessage());
            System.out.println("Please try again");
            continue;
          }
          /**
           * Check permitted values if specified
           */
          if (values!=null && values.length > 0) {
            for (int i=0;i<values.length;i++) {
              if (s.equals(values[i])) return s; // value passed the check
            }
            // user did not use a permitted value
            System.out.print("You must enter one of these values:");
            for (int i=0;i<values.length;i++) {
              System.out.print("'"+values[i]+"' ");
            }
            System.out.println();
          } else {
            return s; // no check, just return
          }
        }
      }
      /**
       * Get integer input
       * @param prompt
       * @param values, an integer array of permitted values, if null then anything goes
       * @return
       */
      public static int getIntegerInput(String prompt, int [] values) {
        while (true) {
          System.out.println(prompt);
          try {
            int value = Integer.parseInt(inData.readLine().trim());
            if (value < 1) {
              System.out.println("You must enter a whole positive number");
              System.out.println("Please try again");
              continue;
            }
            /**
             * Check permitted values if specified
             */
            if (values!=null && values.length > 0) {
              for (int i = 0; i < values.length; i++) {
                if (value==values[i]) return value; // value passed the check
              }
              // user did not use a permitted value
              System.out.print("You must enter one of these values:");
              for (int i = 0; i < values.length; i++) {
                System.out.print(values[i]+" ");
              }
              System.out.println();
              continue;
            } else {
              return value; // no check, just return
            }
          }
          catch (IOException ex) {
            System.out.println("Input error: "+ex.getMessage());
            System.out.println("Please try again");
          }
          catch (NumberFormatException ex) {
            System.out.println("You must enter a whole positive number");
            System.out.println("Please try again");
          }
        }
      }
      public String toString() {
        return "Voter number: " + this.voterNumber +
            " Voter Type: " + this.voterType +
            " Gender Type: " + this.voterGender +
            " Voter Age: " + this.voterAge;
      }
      /**
       * Prompts for input and returns a Voter object
       * @return
       */
      public static Voter getAVoter() {
        int num  = getIntegerInput("Enter 4 digit voter number: ", null);
        int age = getIntegerInput("Enter voter age: ", null);
        int type = getIntegerInput("Enter Voter Type (Republican = 1, Democrat = 2, Independent = 3): ",
                                    new int []{1,2,3});
        int gender = getIntegerInput("Enter Gender Type (Female = 1, Male = 2): ",
                                      new int []{1,2});
        return new Voter(num,type,gender,age);
      }
    }
    /**
     * Driver class
     */
    public class VoterDriver {
    
      public static void main (String[] args) {
        ArrayList voterList = new ArrayList();
        int voterCount = 0;
        int voterLimit=2; // set low for testing.....
        DecimalFormat twoDecimals=new DecimalFormat("0.00");
    
        String reply=Voter.getStringInput("Start entering voters ? (Y/N)",
                                          new String []{"Y","N"});
        while (reply.equals("Y") && voterCount < voterLimit) {
          Voter aVoter = Voter.getAVoter();
          voterList.add(aVoter);
          System.out.println("Voter #"+(voterCount+1));
          System.out.println(aVoter.toString());
          voterCount++;
          if (voterCount < voterLimit) {
            reply = Voter.getStringInput("Enter new voter ? (Y/N)",
                                         new String[] {"Y", "N"});
          }
        }
        System.out.println();
        if (voterList.size()==0) {
          System.out.println("No voters have been entered");
        } else {
          System.out.println("Voters entered into the system: "+voterList.size());
          // calculate average age
          long ageTotal = 0;
          for (int i = 0; i < voterList.size(); i++) {
            Voter aVoter = (Voter) voterList.get(i);
            ageTotal += aVoter.getVoterAge();
          }
          double ageAverage=(double)ageTotal/(double)voterList.size();
          System.out.println("Average Voter Age: "+twoDecimals.format(ageAverage));
        }
    
      }
    }
    eschew obfuscation

  4. #4
    Join Date
    Feb 2005
    Posts
    4

    Post Thanks!

    import java.io.*;
    import java.util.*;
    import java.text.*;
    // The "Voter" class.

    class Voter {
    private int voterAge; // Voter Age
    private int voterType; // Voter Type
    private int voterGender; // voter gender
    private int voterNumber; // Voter Number
    //Input stream for strings
    static BufferedReader inData= new BufferedReader(new InputStreamReader(System.in));

    /**
    * Constructor
    */
    public Voter (int voterNumber, int voterType, int voterGender, int voterAge) {
    this.voterAge=voterAge;
    this.voterType=voterType;
    this.voterNumber=voterNumber;
    this.voterGender=voterGender;
    }
    /**
    * Gets
    * @return
    */
    public int getVoterAge() {
    return voterAge;
    }

    /**
    * Get String input
    * @param prompt
    * @param values a string array of permitted values, if null then anything goes
    * @return
    */
    public static String getStringInput(String prompt, String [] values) {
    while (true) {
    System.out.println(prompt);
    String s = null;
    try {
    s = inData.readLine().trim();
    }
    catch (IOException ex) {
    System.out.println("Input error: "+ex.getMessage());
    System.out.println("Please try again");
    continue;
    }
    /**
    * Check permitted values if specified
    */
    if (values!=null && values.length > 0) {
    for (int i=0;i<values.length;i++) {
    if (s.equals(values[i])) return s; // value passed the check
    }
    // user did not use a permitted value
    System.out.print("You must enter one of these values:");
    for (int i=0;i<values.length;i++) {
    System.out.print("'"+values[i]+"' ");
    }
    System.out.println();
    } else {
    return s; // no check, just return
    }
    }
    }
    /**
    * Get integer input
    * @param prompt
    * @param values, an integer array of permitted values, if null then anything goes
    * @return
    */
    public static int getIntegerInput(String prompt, int [] values) {
    while (true) {
    System.out.println(prompt);
    try {
    int value = Integer.parseInt(inData.readLine().trim());
    if (value < 1) {
    System.out.println("You must enter a whole positive number");
    System.out.println("Please try again");
    continue;
    }
    /**
    * Check permitted values if specified
    */
    if (values!=null && values.length > 0) {
    for (int i = 0; i < values.length; i++) {
    if (value==values[i]) return value; // value passed the check
    }
    // user did not use a permitted value
    System.out.print("You must enter one of these values:");
    for (int i = 0; i < values.length; i++) {
    System.out.print(values[i]+" ");
    }
    System.out.println();
    continue;
    } else {
    return value; // no check, just return
    }
    }
    catch (IOException ex) {
    System.out.println("Input error: "+ex.getMessage());
    System.out.println("Please try again");
    }
    catch (NumberFormatException ex) {
    System.out.println("You must enter a whole positive number");
    System.out.println("Please try again");
    }
    }
    }
    public String toString() {
    return "Voter number: " + this.voterNumber +
    " Voter Type: " + this.voterType +
    " Gender Type: " + this.voterGender +
    " Voter Age: " + this.voterAge;
    }
    /**
    * Prompts for input and returns a Voter object
    * @return
    */
    public static Voter getAVoter() {
    int num = getIntegerInput("Enter 4 digit voter number: ", null);
    int age = getIntegerInput("Enter voter age: ", null);
    int type = getIntegerInput("Enter Voter Type (Republican = 1, Democrat = 2, Independent = 3): ",
    new int []{1,2,3});
    int gender = getIntegerInput("Enter Gender Type (Female = 1, Male = 2): ",
    new int []{1,2});
    return new Voter(num,type,gender,age);
    }
    }
    /**
    * Driver class
    */
    public class VoterDriver {

    public static void main (String[] args) {
    ArrayList voterList = new ArrayList();
    int voterCount = 0;
    int voterLimit=2; // set low for testing.....
    DecimalFormat twoDecimals=new DecimalFormat("0.00");

    String reply=Voter.getStringInput("Start entering voters ? (Y/N)",
    new String []{"Y","N"});
    while (reply.equals("Y") && voterCount < voterLimit) {
    Voter aVoter = Voter.getAVoter();
    voterList.add(aVoter);
    System.out.println("Voter #"+(voterCount+1));
    System.out.println(aVoter.toString());
    voterCount++;
    if (voterCount < voterLimit) {
    reply = Voter.getStringInput("Enter new voter ? (Y/N)",
    new String[] {"Y", "N"});
    }
    }
    System.out.println();
    if (voterList.size()==0) {
    System.out.println("No voters have been entered");
    } else {
    System.out.println("Voters entered into the system: "+voterList.size());
    // calculate average age
    long ageTotal = 0;
    for (int i = 0; i < voterList.size(); i++) {
    Voter aVoter = (Voter) voterList.get(i);
    ageTotal += aVoter.getVoterAge();
    }
    double ageAverage=(double)ageTotal/(double)voterList.size();
    System.out.println("Average Voter Age: "+twoDecimals.format(ageAverage));
    }

    }
    }


    When I run this i ger 2 errors. They are under Driver class. They are VoteDriver and ArrayList.

    Also, Is a more simple code for this project. I am only in a beginners programming class. We haven't even dicussed some of this stuff yet.

    Thanks a lot for your help. I greatly apperciate it.
    Tera Williamson

  5. #5
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    If you get errors then you are not running the same code,
    or do you mean compile errors ?.
    Anyway, the ArrayList and the DecimalFormat requires
    that all the imports on top of the file are included.

    There are simpler ways to do this, and they are guaranteed
    to be messy and longwinded...

    The Classes; ArrayList and DecimalFormat is no big deal,
    check the documentation
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center