Saturday, 7 September 2013

Totals not printing correctly and displaying zeros in java program

Totals not printing correctly and displaying zeros in java program

Hi everyone I have almost finished this program except that my totals are
not displaying their correct values they are only displaying zeros. I've
looked through for ages and find why this is happening. Can someone please
help me here? The totals are calculated in the getTotals() method near the
bottom. Thanks!
import java.util.Scanner;
public class Assignment1Q2 {
int age = 0;
char gender;
char show;
char choice = 'y';
int over30MY = 0, over30FY = 0, under30MY = 0, under30FY = 0;
int over30MN = 0, over30FN = 0, under30MN = 0, under30FN = 0;
int totalCalls = 0;
int totalWatchers = 0;
float perTotalWatchers = 0.0f;
float perU30M = 0.0f, perU30F = 0.0f, perO30M = 0.0f, perO30F = 0.0f,
perTotalF = 0.0f, perTotalM = 0.0f;
float perTotalU30 = 0.0f, perTotalO30 = 0.0f, perTotal = 0.0f;
public static void main(String[] args) {
Assignment1Q2 a = new Assignment1Q2(); //creates an objects to call
non-static methods
a.Info(); //Prints student info
System.out.println("");
System.out.println("Thanks for your call\nPlease answer some questions");
System.out.println("");
while(a.choice == 'y') {
a.collectData();
} // end of while loop
a.getTotals();
a.printTotals();
}//end of main
public void Info() { //Prints student Info
System.out.println("Name:");
System.out.println("Student Id:");
System.out.println("");
System.out.println("Tutor:");
System.out.println("Tutorial:");
}
public void collectData() {
Scanner userInput = new Scanner(System.in); //collects data from user
//ask questions
System.out.println("What is your age?\n");
age = userInput.nextInt();
System.out.println("Male or Female (Enter M or Y)");
gender = userInput.next().charAt(0);
gender = Character.toLowerCase(gender);
System.out.println("Do you watch the show regularly? (Enter Y or N)");
show = userInput.next().charAt(0);
show = Character.toLowerCase(show);
System.out.println("Do you want to enter another person's details?
(Enter Y or N)");
choice = userInput.next().charAt(0);
choice = Character.toLowerCase(choice);
//store data
if((age >= 30) && (gender == 'm') && (show == 'y')) {
over30MY++;
}
else if((age >= 30) && (gender == 'f') && (show == 'y')) {
over30FY++;
}
else if((age < 30) && (gender == 'm') && (show == 'y')) {
under30MY++;
}
else if((age < 30) && (gender == 'f') && (show == 'y')) {
under30FY++;
}
else if((age >= 30) && (gender == 'm') && (show == 'n')) {
over30MN++;
}
else if((age >= 30) && (gender == 'f') && (show == 'n')) {
over30FN++;
}
else if((age < 30) && (gender == 'm') && (show == 'n')) {
under30MN++;
}
else if((age < 30) && (gender == 'f') && (show == 'n')) {
under30FN++;
}//end of if else
}//end of collectData
public void getTotals() {//calculate totals for printing
totalCalls = over30MY + over30FY + under30MY + under30FY +
over30MN + over30FN + under30MN + under30FN;
totalWatchers = over30MY + over30FY + under30MY + under30FY;
perTotalWatchers = ((totalWatchers / totalCalls) * 100); //rounds
percentage to nearest whole number
if(totalWatchers > 0) {
perU30F = ((under30FY / totalWatchers) * 100); //Total Under
30 Females
perU30M = (under30MY / totalWatchers) * 100; //Total Under 30
Males
perO30F = ((over30FY / totalWatchers) * 100); //Total Over 30
Females
perO30M = ((over30MY / totalWatchers) * 100); //Total Over 30
Males
perTotalF = Math.round(perU30F + perO30F); //Total Females
perTotalM = Math.round(perU30M + perO30M); //Total Males
perTotalU30 = Math.round(perU30F + perU30M); //Total Under 30
perTotalO30 = Math.round(perO30F + perO30M); //Total Over 30
perTotal = Math.round(perTotalF + perTotalM); //Total
}
else
System.out.println("There was no-one who watched the show");
}//end of getTotals
public void printTotals () { //Prints the Totals
System.out.println("Total People called is "+totalCalls);
System.out.println("Number of people who watch the show regularly
is "+totalWatchers);
System.out.println("Percentage of those who watch the show is
regularly is "+perTotalWatchers+"%");
System.out.println("");
final Object[][] table = new String[4][];
table[0] = new String[] { "Gender", "%Under 30", "%30 or Over",
"%Total" };
table[1] = new String[] { "Female", " "+perU30F, " "+perO30F, "
"+perTotalF };
table[2] = new String[] { "Male", " "+perU30M, " "+perO30M, "
"+perTotalM };
table[3] = new String[] { "Total", " "+perTotalU30, "
"+perTotalO30, " "+perTotal };
for (final Object[] row : table) {
System.out.format("%15s%15s%15s%15s\n", row);
}
}//end of printTotals
}// end of class

No comments:

Post a Comment