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!
[code=java]
import java.beans.XMLD ecoder;
import java.beans.XMLE ncoder;
import java.io.Buffere dInputStream;
import java.io.Buffere dOutputStream;
import java.io.FileInp utStream;
import java.io.FileOut putStream;
import java.io.IOExcep tion;
import java.util.Scann er;
import java.util.Vecto r;
import java.util.Compa rator;
import java.util.*;
import java.math.BigDe cimal;
class MainOutput{
private static int lessthan25 = 0, twentysixto45 = 0, biggerthan46 = 0, male = 0, female = 0, age;
private static char sex;
private static int answers[];
private static int format [];
private static int responses[][];
private static BigDecimal average;
public static void printQuestionna ire(Questionnai reFields questionnaire){
System.out.prin tln("SerialNumb er: " + questionnaire.g etSerialNumber( ));
System.out.prin tln("Sex: "+ questionnaire.g etSex());
System.out.prin tln("Age: " + questionnaire.g etAge());
System.out.prin tln("Postcode: " +questionnaire. getPostcode());
System.out.prin tln("Answers");
int[] answers = questionnaire.g etAnswers();
for(int count=0; count<answers.l ength; count++){
System.out.prin t(" Answer "+(count+1) + ": " + answers[count]);
}
System.out.prin tln();
System.out.prin tln();
}
public static void getStatistics (QuestionnaireF ields questionnaire)t hrows IOException{
Vector<Question naireFields> loaded = null;
XMLDecoder decoder = null;
try {
decoder = new XMLDecoder(new BufferedInputSt ream(
new FileInputStream ("Questionnaire Data.xml")));
loaded = (Vector<Questio nnaireFields>) decoder.readObj ect();
} catch (Exception e) {
loaded = new Vector<Question naireFields>();
} finally {
if (decoder != null) {
decoder.close() ;
}
}
age = questionnaire.g etAge();
if (age <= 25){
lessthan25 ++;
}else if (age >25 && age <45){
twentysixto45++ ;
}else{
biggerthan46++;
}
Vector averageAge = new Vector();
averageAge.add (age);
Iterator iter = averageAge.iter ator();
BigDecimal bdSum = new BigDecimal(0);
while(iter.hasN ext()){
bdSum = bdSum.add((BigD ecimal)iter.nex t());
}
BigDecimal average = bdSum.divide(Bi gDecimal.valueO f(averageAge.si ze()));
sex = questionnaire.g etSex();
if(sex == 'm' | sex == 'M'){
male++;
}else{
female++;
}
answers = questionnaire.g etAnswers();
for(int count=0; count<responses .length; count++){
for(int count1=0; count1<response s[count].length; count1++){
if (answers[count] == (responses[count].length)){
responses[count][count1]++;
}
}
}
}
public static void main (String [] args) throws IOException{
Vector<Question naireFields> loaded = null;
XMLDecoder decoder = null;
try {
decoder = new XMLDecoder(new BufferedInputSt ream(
new FileInputStream ("Questionnaire Data.xml")));
loaded = (Vector<Questio nnaireFields>) decoder.readObj ect();
} catch (Exception e) {
loaded = new Vector<Question naireFields>();
} finally {
if (decoder != null) {
decoder.close() ;
}
}
Collections.sor t (loaded, new Comparator<Ques tionnaireFields >(){
public int compare (QuestionnaireF ields one, QuestionnaireFi elds two){
return one.getSerialNu mber()- two.getSerialNu mber();
}
});
for (int count = 0; count < loaded.size(); count ++){
printQuestionna ire (loaded.get (count));
}
format = CheckingStructu reSetting.loadi ng();
responses = new int [format.length][];
for (int count = 0; count<format.le ngth; count++){
responses[count] = new int [format[count]];
}
for (int count = 0; count < loaded.size(); count ++){
getStatistics (loaded.get (count));
}
System.out.prin tln ("Age: <= 25: "+lessthan2 5+" 26-45: "+twentysixto45 +" >=46: "+biggerthan46) ;
System.out.prin tln("Male: "+male+" Female: "+female+" Total: "+(male+female) );
System.out.prin tln("Average age" + average);
for (int count=0; count<responses .length; count++){
System.out.prin tln ("Question: "+(count+1) );
System.out.prin t ("Responses: ");
for(int count1=0; count1<response s[count].length; count1++){
System.out.prin t (" "+(count1+1 ) + ":");
System.out.prin t (responses[count][count1]);
if ((count1+1) == responses[count].length){
System.out.prin tln(" ");
}
}
}
}
}[/code]
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!
[code=java]
import java.beans.XMLD ecoder;
import java.beans.XMLE ncoder;
import java.io.Buffere dInputStream;
import java.io.Buffere dOutputStream;
import java.io.FileInp utStream;
import java.io.FileOut putStream;
import java.io.IOExcep tion;
import java.util.Scann er;
import java.util.Vecto r;
import java.util.Compa rator;
import java.util.*;
import java.math.BigDe cimal;
class MainOutput{
private static int lessthan25 = 0, twentysixto45 = 0, biggerthan46 = 0, male = 0, female = 0, age;
private static char sex;
private static int answers[];
private static int format [];
private static int responses[][];
private static BigDecimal average;
public static void printQuestionna ire(Questionnai reFields questionnaire){
System.out.prin tln("SerialNumb er: " + questionnaire.g etSerialNumber( ));
System.out.prin tln("Sex: "+ questionnaire.g etSex());
System.out.prin tln("Age: " + questionnaire.g etAge());
System.out.prin tln("Postcode: " +questionnaire. getPostcode());
System.out.prin tln("Answers");
int[] answers = questionnaire.g etAnswers();
for(int count=0; count<answers.l ength; count++){
System.out.prin t(" Answer "+(count+1) + ": " + answers[count]);
}
System.out.prin tln();
System.out.prin tln();
}
public static void getStatistics (QuestionnaireF ields questionnaire)t hrows IOException{
Vector<Question naireFields> loaded = null;
XMLDecoder decoder = null;
try {
decoder = new XMLDecoder(new BufferedInputSt ream(
new FileInputStream ("Questionnaire Data.xml")));
loaded = (Vector<Questio nnaireFields>) decoder.readObj ect();
} catch (Exception e) {
loaded = new Vector<Question naireFields>();
} finally {
if (decoder != null) {
decoder.close() ;
}
}
age = questionnaire.g etAge();
if (age <= 25){
lessthan25 ++;
}else if (age >25 && age <45){
twentysixto45++ ;
}else{
biggerthan46++;
}
Vector averageAge = new Vector();
averageAge.add (age);
Iterator iter = averageAge.iter ator();
BigDecimal bdSum = new BigDecimal(0);
while(iter.hasN ext()){
bdSum = bdSum.add((BigD ecimal)iter.nex t());
}
BigDecimal average = bdSum.divide(Bi gDecimal.valueO f(averageAge.si ze()));
sex = questionnaire.g etSex();
if(sex == 'm' | sex == 'M'){
male++;
}else{
female++;
}
answers = questionnaire.g etAnswers();
for(int count=0; count<responses .length; count++){
for(int count1=0; count1<response s[count].length; count1++){
if (answers[count] == (responses[count].length)){
responses[count][count1]++;
}
}
}
}
public static void main (String [] args) throws IOException{
Vector<Question naireFields> loaded = null;
XMLDecoder decoder = null;
try {
decoder = new XMLDecoder(new BufferedInputSt ream(
new FileInputStream ("Questionnaire Data.xml")));
loaded = (Vector<Questio nnaireFields>) decoder.readObj ect();
} catch (Exception e) {
loaded = new Vector<Question naireFields>();
} finally {
if (decoder != null) {
decoder.close() ;
}
}
Collections.sor t (loaded, new Comparator<Ques tionnaireFields >(){
public int compare (QuestionnaireF ields one, QuestionnaireFi elds two){
return one.getSerialNu mber()- two.getSerialNu mber();
}
});
for (int count = 0; count < loaded.size(); count ++){
printQuestionna ire (loaded.get (count));
}
format = CheckingStructu reSetting.loadi ng();
responses = new int [format.length][];
for (int count = 0; count<format.le ngth; count++){
responses[count] = new int [format[count]];
}
for (int count = 0; count < loaded.size(); count ++){
getStatistics (loaded.get (count));
}
System.out.prin tln ("Age: <= 25: "+lessthan2 5+" 26-45: "+twentysixto45 +" >=46: "+biggerthan46) ;
System.out.prin tln("Male: "+male+" Female: "+female+" Total: "+(male+female) );
System.out.prin tln("Average age" + average);
for (int count=0; count<responses .length; count++){
System.out.prin tln ("Question: "+(count+1) );
System.out.prin t ("Responses: ");
for(int count1=0; count1<response s[count].length; count1++){
System.out.prin t (" "+(count1+1 ) + ":");
System.out.prin t (responses[count][count1]);
if ((count1+1) == responses[count].length){
System.out.prin tln(" ");
}
}
}
}
}[/code]