0

I'm trying to debug this project and during testing, in the Test class, although the criteria for the checkModuleSelection() method to return True has been met and this is not happening. I want the driver class to run the checkModuleSelection() method using the test data supplied and return a true.

Test class

public class Test {
    public static void main (String [] args)
    {

        Module csc8001 = new Module("CSC8001", "Programming and data structures", 15, 0, 0, 6);//1: 60, 2: 60 
        Module csc8002 = new Module("CSC8002", "Test Module", 0, 15, 0, 7);
        Module csc8003 = new Module("CSC8001", "Programming and data structures", 0, 0, 0, 7);
        Module csc8004 = new Module("CSC8002", "Test Module", 15, 0, 0, 7);
        Module csc8005 = new Module("CSC8001", "Programming and data structures", 0, 15, 0, 7);
        Module csc8006 = new Module("CSC8002", "Test Module",  0, 0, 0, 7);
        Module csc8007 = new Module("CSC8001", "Programming and data structures", 15, 0, 0, 7);
        Module csc8008 = new Module("CSC8002", "Test Module",  0, 15, 0, 7);
        Module csc8009 = new Module("CSC8001", "Programming and data structures", 0, 0, 0, 7);
        Module csc8010 = new Module("CSC8002", "Test Module", 15, 0, 0, 7);//lvl 7 150
        Module csc8011 = new Module("CSC8001", "Programming and data structures",  0, 15, 0, 7);
        Module csc8012 = new Module("CSC8002", "Test Module", 0, 0, 60, 7);
        Module csc8013 = new Module("CSC8001", "Programming and data structures", 0, 0, 0, 7);

        Student chris = new Student("Chris", "1"); 
        System.out.println(chris.getName());


        chris.addModule(csc8001);
        chris.addModule(csc8002);
        chris.addModule(csc8003);
        chris.addModule(csc8004);
        chris.addModule(csc8005);
        chris.addModule(csc8006);
        chris.addModule(csc8007);
        chris.addModule(csc8008);
        chris.addModule(csc8009);
        chris.addModule(csc8010);
        chris.addModule(csc8011);
        chris.addModule(csc8012);
        chris.addModule(csc8013);

       System.out.println(chris.checkModuleSelection());



    }

}

Student class

public boolean checkModuleSelection() {

        int sumLevel6Credits = 0;
        int sumLevel7Credits = 0;
        int sumOtherCredits = 0;
        int sem1Credits = 0;
        int sem2Credits = 0;
        int sem3Credits = 0;
boolean projectModule = false;

        for (int i = 0; i < MAX_NUMBER_OF_MODULES; i++) {
            System.out.println("The value of sem1Credits is: " + sem1Credits);
            if (moduleRecords[i] != null) {

                if (moduleRecords[i].getModule().getLevel() == Module.MSC_MODULE_LEVEL) {
                    sumLevel7Credits += moduleRecords[i].getModule()
                            .getSemesterOneCredits();
                    System.out.println();
                } else if (moduleRecords[i].getModule().getLevel() == Module.STAGE_3_MODULE_LEVEL) {
                    sumLevel6Credits += moduleRecords[i].getModule()
                            .getTotalCredits();
                }
                projectModule = moduleRecords[i].getModule().getTotalCredits() >= MINIMUM_PROJECT_CREDITS;
                sem1Credits += moduleRecords[i].getModule()
                        .getSemesterOneCredits();
                sem2Credits += moduleRecords[i].getModule()
                        .getSemesterTwoCredits();
                sem3Credits += moduleRecords[i].getModule()
                        .getSemesterThreeCredits();
            }
        }
        // check that there is at least one project module, there isn't too many
        // level 6 credits, there is enough total credits and enough credits in
        // each semester
        return !((sumOtherCredits > 0)
                || !projectModule
                || (sumLevel6Credits < MAXIMUM_LEVEL_6_CREDITS)
                || (sumLevel6Credits + sumLevel7Credits != VALID_NUMBER_OF_REGISTERED_CREDITS)
                || sem1Credits < 50 || sem1Credits > 70 || sem2Credits < 50
                || sem2Credits > 70 || sem3Credits < 50 || sem3Credits > 70);

    }

EDIT As requested, the Module class is below

package Project1;
//This class represents a module
public class Module {

      public final static int MSC_MODULE_PASS_MARK = 50;

      public final static int UG_MODULE_PASS_MARK = 40;
      public final static int MSC_MODULE_LEVEL = 7;
      public final static int STAGE_3_MODULE_LEVEL = 6;
      private String moduleCode;
      private String moduleTitle;
      private int sem1Credits;
      private int sem2Credits;
      private  int sem3Credits;
      private  int moduleLevel;


      public Module(String code, String title, int sem1, int sem2, int sem3, int level)
      {

          moduleCode = code;
          moduleTitle = title;
          sem1Credits = sem1;
          sem2Credits = sem2;
          sem3Credits = sem3;
          moduleLevel = level;

      }

      //method to return the module code
      public String getCode()
      {

          return moduleCode;

      }

    //method to return the module title
      public String getTitle()
      {

          return moduleTitle;

      }
    //method to return the Semester 1 credits
      public int getSemesterOneCredits()
      {

          return sem1Credits;

      }
    //method to return the Semester 2 credits
      public int getSemesterTwoCredits()
      {

          return sem2Credits;

      }
    //method to return the Semester 3 credits
      public int getSemesterThreeCredits()
      {

          return sem3Credits;

      }

    //method to return the total credits across all the semesters
      public int getTotalCredits()
      {

         // return sem2Credits + sem3Credits;
          return sem1Credits + sem2Credits + sem3Credits;

      }

    //method to return the module level
      public int getLevel()
      {

          return moduleLevel;

      }

}
  • Could you post the values of the variables from the return statement just before the return statement? – Alexander_Winter Mar 28 '14 at 06:30
  • Tested with breakpoint, projectModule is true until it reaches if (moduleRecords[i] != null) { – Sebastian Smolorz Mar 28 '14 at 06:36
  • I think this is another XY problem, it's either you're not passing values correctly or you're missing something. Can you try to trace both values of your variables to be compared just before you compare them? – Mc Kevin Mar 28 '14 at 06:50
  • Changed the sum "Level7Credits += moduleRecords[i].getModule() .getSemesterOneCredits();" so that now it adds semester 2 and 3 results. When I is 11 in the for loop, sem3Credits are added and SumLevel7 is 180 and projectModule is true (as wanted). I turn to 12 and projectModule is false. Why is this – Sebastian Smolorz Mar 28 '14 at 07:16

1 Answers1

2

Use .equals() method to compare, do not use == for comparing objects.

== will check both references are referring same object in the memory, while .equals() used for comparing both objects are meaninfully equal.

if (moduleRecords[i].getModule().getLevel().equals(Module.MSC_MODULE_LEVEL)) {}
Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105