Readme

Assignment 1: This assignment is based on materials contained in the slide decks and labs in the first 4 topics, namely, Introduction to Java programming language, Object Interaction and Grouping Objects. In this assignment, we will combine all these topics into a multi-class gym app.

Similar(ish) Project

In your previous lab, you worked on a Shop project. Your assignment will be similar(ish) to this project but will incorporate more concepts and require you to develop your own algorithms to solve some of the requirements given to you.

The solution to the Shop project can be found here.

Figure 1: Class Diagram for Shop Project

The responsibility of the Product class is to manage a single product.

The responsibility of the Store class is to manage an ArrayList of Products.

The Assignment

You are tasked with developing a GymApp.

This GymApp will have three classes:

  • Member: The responsibility for this class is to manage a single Member.
  • Gym: The responsibility for this class is to manage a collection of members.
  • MenuController: The responsibility for this class is to manage the menu, including the user input/ouput.

Figure 1: Class Diagram for GymApp Project

The instructions for developing the app and the above classes are on the following tabs.

Some key points to remember when coding your assignment

  • You must use the names given for the Classes and Methods; failure to do so will result in reduced marks.

  • For all variables, methods and classes used, please adhere to the naming standards discussed in class.

  • Use internal, helper methods (i.e. private access methods) where appropriate. The interface for your class must match the interface given in the assignment brief. Deviating from the interface will result in reduced marks. Note: the interface here refers to the fields, constructors and methods that are visible outside the class.

  • Your classes should be Javadoc commented in the same way that the assignment brief has them done.

Member class - overview

In your workspace folder, create a new folder called assignments. Within this folder, create a new folder called assignment 1:

Figure 1: Folder structure

Within this directory, create a new Project and call it GymApp.

Create a new class called Member. The responsibility for this class is to manage a single Member.

Reverse engineer the Member class from the JavaDoc screen shots below and those on the next tab. Note: there is sufficient information in these screen shots to build the class.

Class Description

Figure 2: Member Class Description

Constructor

Figure 3: Constructor Summary for the Member Class

Figure 4: Constructor Detail for the Member Class

Method Overview

Figure 5: Method Overview for the Member Class

Member class - specific method details

Below you will find more information on specific methods of the Member class.

Getters for the Member class

Figure 1: Getters for the Member Class

Setters for the Member class

Figure 3: Setters for the Member Class, part 1

toString() for the Member class

Figure 5: toString() for the Member Class

calculateBMI() for the Member class

Figure 6: calculateBMI() for the Member Class

convertHeightMetresToInches() for the Member class

Figure 7: convertHeightMetresToInches() for the Member Class

convertWeightKGtoPounds() for the Member class

Figure 7: convertWeightKGtoPounds() for the Member Class

determineBMICategory() for the Member class

Figure 8: determineBMICategory() for the Member Class

isIdealBodyWeight() for the Member class

Figure 9: isIdealBodyWeight() for the Member class

Gym class - overview

In your GymApp project, create a new class called Gym. The responsibility for this class is to manage a collection of Members.

Reverse engineer the Gym class from the JavaDoc screen shots below and those on the next tab. Note: there is sufficient information in these screen shots to build the class.

Class Description

Figure 1: Gym Class Description

Constructor

Figure 2: Constructor Summary for the Gym Class

Figure 3: Constructor Detail for the Gym Class

Method Overview

Figure 4: Method Overview for the Gym Class

Gym class - specific method details

Below you will find more information on specific methods of the Gym class.

Getters for the Gym class

Figure 1: Getters for the Gym Class

Setters for the Gym class

Figure 2: Setters for the Gym Class

toString() for the Gym class

Figure 3: toString() for the Gym Class

add(Member member) for the Gym class

Figure 4: add(Member member) for the Gym Class

listBySpecificBMICategory(String category) for the Gym class

Figure 5: listBySpecificBMICategory(String category) for the Gym Class

listMemberDetailsImperialAndMetric() for the Gym class

Figure 6: listMemberDetailsImperialAndMetric() for the Gym Class

listMembers() for the Gym class

Figure 7: listMembers() for the Gym Class

listMembersWithIdealWeight() for the Gym class

Figure 8: listMembersWithIdealWeight() for the Gym Class

numberOfMembers() for the Gym class

Figure 9: numberOfMembers() for the Gym Class

remove(int index) for the Gym class

Figure 10: remove(int index) for the Gym Class

MenuController class - overview

In your GymApp project, create a new class called MenuController. The responsibility for this class is to run the app and it's associated menu.

Reverse engineer the MenuController class from the JavaDoc screen shots below and those on the next tab. Note: there is sufficient information in these screen shots to build the class.

Class Description

Figure 1: MenuController Class Description

Constructor

Figure 2: Constructor Summary for the MenuController Class

Figure 3: Constructor Detail for the MemberController Class

Figure 4: Screen shot of entering gym details (1)

Figure 5: Screen shot of entering gym details (2)

Method Overview

The implementation of the class is hidden (encapsulated). However, the information and screen shots following tabs will give you sufficient information to complete the class.

MenuController class - menu choices

Once the user has entered the gym details, the main menu is displayed

Figure 1: The main menu

Below you will find information on each of the menu choices listed in the main menu.

Menu Option 1 - Add a member

Figure 2: Adding a member

Menu Option 2 - List all members

Figure 3: Listing all members when one member exists

Figure 4: Listing all members when multiple members exist

Figure 4: Listing all members when NO members exist

Menu Option 3 - Remove a member (by index)

Figure 5: Removing a member that doesn't exist

Figure 6: Removing a member that exists

Menu Option 4 - Number of members in the gym

Figure 4: Number of members in the gym

Menu Option 5 - List gym details

Figure 4: Listing gym details when NO members exist

Menu Option 6 - List members with ideal starting weight

Figure 1: List members with ideal starting weight

Menu Option 7 - List members with a specific BMI category

Figure 1: List members with a specific BMI category, when a category exists

Figure 1: List members with a specific BMI category, when NO category exists

Menu Option 8 - List all members stats imperically and metrically

Figure 1: List all members stats imperically and metrically

Menu Option 9 - or any invalid menu option

Figure 1: Invalid menu option entered

Menu Option 0 - Exit

Figure 1: Exiting system

Formatting decimal output

  • In the assignment, you were asked to truncate output to two decimal places.

  • Add the following helper method to the Member class:

    private double toTwoDecimalPlaces(double num){
        return (int) (num *100 ) /100.0; 
    }
  • This method:
    • takes in a parameter (called num) e.g. 45.76899765
    • multiplies it by 100 e.g. 4576.899765
    • casts it as an int e.g. 4576
    • divides it by 100 e..g 45.76
    • and returns this value truncated to two decimal places.
  • Given the following sample code:

    public double calculateSomething(){
        return ( value / anotherValue );
    }
  • We can use the toTwoDecimalPlaces method to truncate the result to two decimal places:

    //When returning the result of a calculation, we want to call our 
    //new toTwoDecimalPlaces method to truncate the result to two decimal places:
    public double calculateSomething(){
        return toTwoDecimalPlaces(  value / anotherValue );
    }
  • Apply this approach to the methods in the Member class whose returned result should be truncated to two decimal places.

JUnit

NOTE: YOU DO NOT HAVE TO DO THIS STEP IF YOU DON'T WANT TO.

For correcting your assignments, I have written two automated test classes:

  • MemberTest: This class exhaustively tests the methods in the Member class.
  • GymTest: This class exhaustively tests the methods in the Gym class.

I will be running these classes over your submitted assignment and correcting based on the results of the tests. I am releasing them to you so that, prior to submitting your work, you can pre-correct your assignment and make changes / fix your code based on the output of the tests.

Setting up the MemberTest Class

  • In your GymApp, create a new class called MemberTest, select the Unit Test option and click the OK button.

  • Delete all of the generated code in your new class and paste this code into it instead.

Setting up the GymTest Class

  • In your GymApp, create a new class called GymTest, select the Unit Test option and click the OK button.

  • Delete all of the generated code in your new class and paste this code into it instead.

Enabling BlueJ to run JUnit Tests

  • In BlueJ, you need to turn on the Unit Testing tools.

  • Select Tools from the menu bar, followed by Preferences...:

Figure 1: Selecting the preferences...

  • When the Preferences window appears, select the Interface tab and check the box Show unit testing tools and click the OK button:

Figure 2: Selecting the unit testing tools...

  • The Unit Testing tools should now appear on the left hand side of your BlueJ app.

Figure 3: The Unit testing tools...

Running the Tests

  • Now that you have your test class created and enabled the JUnit Testing tools in BlueJ, you are ready to test your code!

  • To do this, click on the Run Tests button.

  • If all of the tests ran successfully, you will end up with output like so (all green ticks for the 19 tests):

Figure 4: All tests ran successfully

  • If a test fails, you will end up with a red bar and a red X beside the failing test:

Figure 5: One test failed

  • Notice, in the above screen shot, that when we click on the failing test, more details are about the error in the window below it.

  • Click on the Show Source button. You will be brought to the line of code that failed in the test class. Try to figure out why the test failed on this particular line of code and make the changes in your classes to try to fix it. Note that you should not make any changes in the test class to fix a failing test; the changes should be made in your code.

Devine Method Pseudocode

isIdealBodyWeight() for the Member class

Figure 9: isIdealBodyWeight() for the Member class

Pseudocode

  • Assume that the calculation only applies to members of five feet and over. If a member is exactly five feet or under, their ideal body weight is set to exactly 50 for males and 45.5 for females.

1. Convert the members height from metres to inches.

2. If the members height is less than or equal to five feet (i.e. 60 inches) then
           2.1 if the member is a male, their ideal body weight is 50
           2.2 if the member is a female, their ideal body weight is 45.5
   else 
           2.3 if the member is male, calculate the ideal body weight based on 50 + 2.3 for every inch over 5 ft.
        2.4 if the member is female, calculate the ideal body weight based on 45.5 + 2.3 for every inch over 5 ft.

3. Check that ideal body weight (calculated above) is within a +-2 tolerance of the member's startingWeight.
    3.1. if within or equal to the tolerance, return true
    3.2. else return false

Submitting your assignment

When you are ready to submit your assignment:

  • Rename your project folder using the naming convention firstname_surname e.g. siobhan_drohan.

  • Zip this folder ensuring that it is called firstname_surname.zip. No WINRARs please!

  • Submit this assignment into the central area for submitting assignments, by Sunday 26th Feb, 5PM sharp.