-1

This is my first CS project ever. After creating my method, I ran the code to see if it works so far, everything works correctly but it does not actually do the math inside the method. Been working on this forever and can't find the bug. Any help would be great. Its due tomorrow lol.

public static void main(String[] args) {

  int numberOfStudents = 0;
  int total = 0;
  int value = 0;
  int creditHours;
  double tuition;
  int classesMissed;

  System.out.println("Tuition Wasted Based on Student Absences and its effect on GPA.");
  Scanner keyboard = new Scanner(System.in);

  System.out.print("Enter the number of students to consider: ");
  value = keyboard.nextInt();
  while (value >= 5)
  {
    if (value > 5)
      System.out.println("Number of students must be between 1 and 5");
    System.out.print("Please re-enter a value number of students to consider: ");
    value = keyboard.nextInt();
  }

  System.out.print("Enter the student ID for student 1: ");
  value = keyboard.nextInt();

  System.out.print("For how many credit hours is the student registered: ");
  creditHours = keyboard.nextInt();

  System.out.print("Enter the amount of the tuition for the semester: ");
  tuition = keyboard.nextDouble();

  System.out.print("Enter the average number of classes the student misses in a week: ");
  classesMissed = keyboard.nextInt();
  while (classesMissed > creditHours)
  {
    if (classesMissed > creditHours)
      System.out.print("That is not possible, please re-enter the number of classes missed in a week: ");   
    classesMissed = keyboard.nextInt();
  }

  DetermineWastedTuition(creditHours, tuition, classesMissed);

}

public static void DetermineWastedTuition(int creditHours, double tuition, int classesMissed){

  double weeklyTuition;
  weeklyTuition = tuition / 10;
  double weeklyTuitionWasted;
  weeklyTuitionWasted = weeklyTuition *(classesMissed / creditHours);
  double semesterWasted;
  semesterWasted = weeklyTuitionWasted * 16;

  System.out.println("Tuition money wasted each week is " + weeklyTuitionWasted);
  System.out.println("Tuition money wasted each semester is " + semesterWasted);

}

With the sample output as:

Tuition Wasted Based on Student Absences and its effect on GPA.
Enter the number of students to consider: 1
Enter the student ID for student 1: 1234555
For how many credit hours is the student registered: 15
Enter the amount of the tuition for the semester: 7500
Enter the average number of classes the student misses in a week: 2
Tuition money wasted each week is 0.0
Tuition money wasted each semester is 0.0

Woodrow Barlow
  • 8,477
  • 3
  • 48
  • 86
Kesto
  • 1
  • 2
  • Have you tried stepping through the code with the debugger? – rrirower Oct 02 '14 at 18:00
  • You should really look at the indentation of your code as it hurts readability and might hide or obscure bugs. And there is really no need to declare all variables at the start of your method. – Mark Rotteveel Oct 02 '14 at 18:04

1 Answers1

2

The following calculation :

weeklyTuitionWasted = weeklyTuition * (classesMissed / creditHours);

would return 0.0 if classesMissed < creditHours, since you are dividing two int variables, and therefore the result will be an int.

Change it to :

weeklyTuitionWasted = weeklyTuition * ((double) classesMissed / creditHours);
Eran
  • 387,369
  • 54
  • 702
  • 768
  • ah. so you cannot divide 2 integers in java? I was not taught that yet. – Kesto Oct 02 '14 at 18:44
  • @Kesto Of course you can, but the result is an integer, so 3/4 would return 0, 4/3 would return 1, etc... – Eran Oct 02 '14 at 18:46