473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Need help for printing statistics from a vector of objects

Hi all,

I really need your help! I am new to java and have some problems with my code. I have a program which inputs questionnaires and creates an object for each questionnaire. These objects are then saved in a vector which in turn is then encoded in an XML file. For all this I have made a class called MainInput. The problem is with MainOutpu class. I want to make a class that prints the questionnaires in alphabetical order (which is already done) and then shows the number of people who responded to the same question with the same response. Moreover I also need to make some statistics like taking the average of all ages, another saying the maximum age, the minimum age...

Here is the code of my MainOutput. If anyone can help me it would be greatly apprciated as this is for an assignment that needs to be given tomorrow! Thanks a lot!

Expand|Select|Wrap|Line Numbers
  1. import java.beans.XMLDecoder;
  2. import java.beans.XMLEncoder;
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedOutputStream;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.util.Scanner;
  9. import java.util.Vector;
  10. import java.util.Comparator;
  11. import java.util.*;
  12. import java.math.BigDecimal;
  13.  
  14.  
  15. class MainOutput{
  16. private static int lessthan25 = 0, twentysixto45 = 0, biggerthan46 = 0, male = 0, female = 0, age;
  17. private static char sex;
  18. private static int answers[];
  19. private static int format [];
  20. private static int responses[][];
  21. private static BigDecimal average;
  22.  
  23.  
  24.     public static void printQuestionnaire(QuestionnaireFields questionnaire){
  25.  
  26.             System.out.println("SerialNumber: " + questionnaire.getSerialNumber());
  27.             System.out.println("Sex: "+ questionnaire.getSex());
  28.             System.out.println("Age: " + questionnaire.getAge());
  29.             System.out.println("Postcode: " +questionnaire.getPostcode());
  30.             System.out.println("Answers");
  31.             int[] answers = questionnaire.getAnswers();
  32.                  for(int count=0; count<answers.length; count++){
  33.                        System.out.print(" Answer "+(count+1)+ ": " + answers[count]);
  34.                  }
  35.             System.out.println();
  36.             System.out.println();
  37.     }
  38.  
  39.     public static void getStatistics (QuestionnaireFields questionnaire)throws IOException{
  40.             Vector<QuestionnaireFields> loaded = null;
  41.                     XMLDecoder decoder = null;
  42.                     try {
  43.                         decoder = new XMLDecoder(new BufferedInputStream(
  44.                                 new FileInputStream("QuestionnaireData.xml")));
  45.                         loaded = (Vector<QuestionnaireFields>) decoder.readObject();
  46.                     } catch (Exception e) {
  47.                         loaded = new Vector<QuestionnaireFields>();
  48.                     } finally {
  49.                         if (decoder != null) {
  50.                             decoder.close();
  51.                         }
  52.                     }
  53.  
  54.                     age = questionnaire.getAge();
  55.  
  56.                     if (age <= 25){
  57.                         lessthan25 ++;
  58.                     }else if (age >25 && age <45){
  59.                         twentysixto45++;
  60.                     }else{
  61.                         biggerthan46++;
  62.                     }
  63.  
  64.                     Vector averageAge = new Vector();
  65.                     averageAge.add (age);
  66.  
  67.                     Iterator iter = averageAge.iterator();
  68.                     BigDecimal bdSum = new BigDecimal(0);
  69.                     while(iter.hasNext()){
  70.                             bdSum = bdSum.add((BigDecimal)iter.next());
  71.                     }
  72.                     BigDecimal average = bdSum.divide(BigDecimal.valueOf(averageAge.size()));
  73.  
  74.  
  75.                     sex = questionnaire.getSex();
  76.                     if(sex == 'm' | sex == 'M'){
  77.                             male++;
  78.                     }else{
  79.                             female++;
  80.                     }
  81.  
  82.                     answers = questionnaire.getAnswers();
  83.  
  84.  
  85.                     for(int count=0; count<responses.length; count++){
  86.                         for(int count1=0; count1<responses[count].length; count1++){
  87.                             if (answers[count] == (responses[count].length)){
  88.                                 responses[count][count1]++;
  89.                             }
  90.                         }
  91.                     }
  92.  
  93.  
  94.  
  95.                 }
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.     public static void main (String [] args) throws IOException{
  103.  
  104.         Vector<QuestionnaireFields> loaded = null;
  105.                 XMLDecoder decoder = null;
  106.                 try {
  107.                     decoder = new XMLDecoder(new BufferedInputStream(
  108.                             new FileInputStream("QuestionnaireData.xml")));
  109.                     loaded = (Vector<QuestionnaireFields>) decoder.readObject();
  110.                 } catch (Exception e) {
  111.                     loaded = new Vector<QuestionnaireFields>();
  112.                 } finally {
  113.                     if (decoder != null) {
  114.                         decoder.close();
  115.                     }
  116.             }
  117.  
  118.         Collections.sort (loaded, new Comparator<QuestionnaireFields>(){
  119.             public int compare (QuestionnaireFields one, QuestionnaireFields two){
  120.                 return one.getSerialNumber()- two.getSerialNumber();
  121.             }
  122.         });
  123.  
  124.         for (int count = 0; count < loaded.size(); count ++){
  125.             printQuestionnaire (loaded.get (count));
  126.         }
  127.  
  128.  
  129.         format = CheckingStructureSetting.loading();
  130.         responses = new int [format.length][];
  131.         for (int count = 0; count<format.length; count++){
  132.             responses[count] = new int [format[count]];
  133.         }
  134.  
  135.  
  136.  
  137.         for (int count = 0; count < loaded.size(); count ++){
  138.             getStatistics (loaded.get (count));
  139.         }
  140.  
  141.         System.out.println ("Age: <= 25: "+lessthan25+"   26-45: "+twentysixto45+"   >=46: "+biggerthan46);
  142.  
  143.         System.out.println("Male: "+male+" Female: "+female+"         Total: "+(male+female));
  144.  
  145.         System.out.println("Average age" + average);
  146.  
  147.  
  148.         for (int count=0; count<responses.length; count++){
  149.                 System.out.println ("Question: "+(count+1));
  150.                 System.out.print ("Responses: ");
  151.                     for(int count1=0; count1<responses[count].length; count1++){
  152.                         System.out.print (" "+(count1+1) + ":");
  153.                         System.out.print (responses[count][count1]);
  154.                              if ((count1+1) == responses[count].length){
  155.                                 System.out.println(" ");
  156.                             }
  157.                     }
  158.         }
  159.  
  160.  
  161.     }
  162. }
Mar 14 '08 #1
0 1431

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: lawrence | last post by:
Dear Peter, Do we know anyone else who writes PHP code? There is too much work to do, especially if Costin and I are going to join our software together. The easiest way for us to join our...
0
by: Venkat Ravi | last post by:
HI, I have two controls of third party with followin names xyz.dll which references Type libraray of Application 1 and xyz.dll which references Type libraray of Application 2 Incidentally...
2
by: Michael Jasn | last post by:
Hi all, I am trying to print the contents of a std::list<Point> in gdb (actually ddd). I have this function void BezierCurveEvaluator::evaluateCurve(const std::vector<Point>& ptvCtrlPts) ...
10
by: mahurshi | last post by:
I've got a gate structure that looks like this /* Defining sGATE structure */ struct sGATE { string name; vector<int> input; int output; };
3
by: Jeff User | last post by:
Hi I want to create a rather simple object with a few string attributes to hold some database user information such as: public class UserInfo { //Basic user information for each user...
0
by: Fabiano Sidler | last post by:
Hi folks! For getting a plan how a stack-based VM like Python works, I added a function that prints out the current object stack. Unfortunately, it crashes the VM. Could someone here take a look...
4
by: =?Utf-8?B?UHVjY2E=?= | last post by:
The function that I'm trying to call through DLLImport has a parameter that has a C code's vector's Itrator to a structure. I Have marshalled the structure in C# but how do I do the C type...
1
by: dmstarke | last post by:
I need to write a program that uses vectors to create a 2 dimensional array in which the user is prompted to input the dimensions of the matrix and then enter the elements one by one. Im having...
4
by: alexjcollins | last post by:
The following program demonstrates the problem: #include <vector> #include <iostream> #include <tvmet/Vector.h> typedef tvmet::Vector<double, 3Vector3d; class Mesh {
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.