Objectives

  • Define a new class from specifications
  • Read input from the keyboard
  • Use methods defined in the String class
  • Use math operators and Math methods

Overview

This exercise is designed to give you more practice developing a class from specifications. It will also give you a chance to write an informal tester program that will read numeric input from the keyboard, as well as practice using some of the String methods.

While the metric system is handy because everything is in terms of units of 10's, the English (or Imperial) System for weight can be somewhat difficult to work with, because there are 16 ounces in one pound. In the exercise this week, we'll try to overcome some of those problems by developing a class to keep track of an English weight and provide some methods to help make calculations easier.

The Problem Statement - EnglishWeight

The class you will develop is named EnglishWeight, which will keep track of the whole number of pounds and the whole number of ounces. The EnglishWeight class is helpful in that it can be created from a String value given to the constructor. You can assume that the String will always be in the format "X pounds and Y ounces" where X and Y are each a whole number greater than or equal to 0. You can also assume that the number of ounces will be less than 16. You'll then provide information about these values and even do a few computations. Specifically, it can provide:

  • The number of pounds as a number
  • The number of ounces as a number
  • The value of the weight in terms of a total number of ounces
  • The sum of this EnglishWeight object and another EnglishWeight object
  • The heavier object between this EnlishWeight object and another EnglishWeight object
  • The value of the weight as a String in the format "X lbs, Y oz"

Getting Started

Be sure to read through all items before you begin. You will notice that these steps encourage the process known as incremental development'. That is, write just a little bit of code and test it. Only once you get correct results do you move on to the next item.

HINT: You might find it useful to print a hard copy of this document and cross out the items as you complete them

1. Create a new Java project named YourLastNameLab04

2. Add a new package named edu.westga.cs6311.weights.controller with three classes (each with their own appropriate Javadoc comments):

  • WeightDemo (this class will *not* have a main method)
  • InteractiveWeightDemo (this class will *not* have a main method)
  • WeightDriver (this class will have a main method)

3. Create a second package named edu.westga.cs6311.weights.model that contains a class called EnglishWeight. This class will not define main, but should have appropriate Javadoc comments.

4. Consider the EnglishWeight class specifications and determine the variable(s) that are necessary to perform the tasks in this class. As a hint, consider what things you would need to completely describe an EnglishWeight object (according to the Problem Statement).

5. Write the Javadoc specification comments for the EnglishWeight constructor. Recall that the purpose of the constructor is to initialize the instance variable(s). Be sure to reread the Problem Statement and think about what the constructor will need to accept in order to complete this task. Once the comments are written, write the Java code for this constructor.

6. Java convention says that when you write a class, you should also include a method with the signature (we'll learn why later in the curriculum):

public String toString()

This method will return a String representation of the object. Typically, this means that you'll return a String value that includes the values of the instance variables. To keep things simple, have this String always be in the format: "X lbs, Y oz", where X is the number of pounds and Y is the number of ounces.

Include the Javadoc comments and Java code for this method in your EnglishWeight class.

7. Once you feel that these methods are correct, go to the WeightDemo class and define

  • An instance variable of type EnglishWeight
  • A constructor that will not accept any parameters. Inside it will instantiate the instance variable using the any weight you choose.
  • A method called testSingleWeight. This method should accept no parameters and should not return a value. Inside the method body write code to use the EnglishWeight instance variable to call its toString method and display the results on the console. Be sure to include the expected value as well so that we can compare the two to see if the code produces what you expect.

8. Now it's time to turn to the driver's code. Go to the WeightDriver class' main method and include code to create a WeightDemo object and use that object to call the testSingleWeight method.

9. Run the program and confirm that the output is correct. If it is not, go back and fix the code so that it is.

10. So far our EnglishWeight class doesn't do much (OK, it doesn't really do anything yet). Let's turn our attention to the EnglishWeight's getPounds() method.

Write the Javadoc specification comments and Java code for the accessor (getter) method:

public int getPounds()

11. Let's test this code out by revisiting the WeightDemo class. Inside the testSingleWeight method, include the code to print:

  • A blank line of output (to make it easier to read)
  • A line listing the expected value of the pounds (based on the numbers you hardcoded)
  • A line listing the value returned by calling getPounds().

12. Run the program and confirm that the output is correct. If not, go back and fix the code so that it is.

13. Repeat this same process that you just did for the pounds, but this time include code for the number of ounces.

14. Once you have a getPounds() and a getOunces(), follow the same process that will compute and return the total number of ounces stored.

15. The EnglishWeight code now works correctly for our informal test, let's turn our attention to interacting with the user - we'll let them enter some values to create an EnglishWeight and then work with that object.

Go to the InteractiveWeightDemo class. Because we'll be getting input values from the user, we'll need an instance variable for the Scanner object.

16. Write a 0parameter constructor to initialize the Scanner instance variable.

17. Our InteractiveWeightDemo class will need a method called runWeight. This method should accept no parameters and have no return value. Inside the method body, write code to:

  • Display an appropriate line of output as a header to note that this is the start of the userinput code.
  • Prompt the user to enter the weight - this is just a System.out.print statement telling the user what it is they are to enter. Be sure to give them the expected format of 'X pounds and Y ounces' so that they will know. (Again, we'll assume that they will always follow these instructions)
  • Read in that value as a String using the Scanner object's nextLine() method.
  • Use these inputted values to create a new EnglishWeight object, and then call the toString and getter methods to get the results for this weight.

Note: there's no way you can print out an 'expected' value because you don't know what values will be entered by the user!

18. Finally, return to the driver class and inside main create an instance of InteractiveWeightDemo and then use that object to call the runWeight method. Try running the application (multiple times): enter some values, and check the output to confirm that the results are correct. Fix any errors that you detect.

Although it is not required to look this way, output from your application might look something like: see image.

19. Next we'll turn our attention to adding two different EnglishWeight objects. For this we'll go to the EnglishWeight class and start off our getSum method. Note that this method will need to be passed another EnglishWeight object in order to do its work. And because it is getting a sum of the weights, this method will need to return an EnglishWeight object. This means that this method header will be:

public EnglishWeight getSum(EnglishWeight anotherWeight)

Write the method body here so that it returns a new EnglishWeight object that would be result from adding the two EnglishWeight's together. Hint: you should make use of the existing methods available inside the EnglishWeight class, as well as math operators, including %. Also know that you are not allowed to use any type of decision structure for this exercise (we haven't covered them yet!)

20. Go back to the WeightDemo class and write a new method testTwoWeights. This method will:

  • Create a second EnglishWeight object, but make this one a local variable inside testTwoWeights. Use whatever values you like for this EnglishWeight also.
  • Write a line of code that prints on the console the result of calling the local EnglishWeight 's toString method
  • Print the expected sum of the two EnglishWeight objects (the instance variable and the local variable)
  • Use the two EnglishWeight objects to print the actual sum of the two EnglishWeights.

21. Modify the code inside the WeightDriver so that testTwoWeights is called after testSingleWeight.

22. Now we can go back to the InteractiveWeightDemo class and get the user to input information for a second EnglishWeight, then use these objects display the sum by calling getSum.

So now the output might look like: see image.

23. Finish out the EnglishWeight class by writing and testing the following methods:

getLighterWeight(EnglishWeight anotherWeight)
Hint: Use the Math.min method to help here

getHeavierWeight(EnglishWeight anotherWeight)
Hint: Use the Math.max method to help here

Be sure to follow the steps we've been teaching here. That is, write the code inside of EnglishWeight, then visit WeightDemo to write code to display expected and actual values, then go to InteractiveWeightDemo and add the code to show the results of this method (that we have now confirmed will work correctly).

Important:

Although it might seem more obvious to use some kind of selection structure for the last two methods, we have not covered this yet in our course, therefore I will ask that you please not use this. Instead, just use the Math class methods to help us out for now.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.