Spring 2025 [Bono]
Note: in this lab where we say implement so-and-so method, it means to implement it so its functionality matches the method comment already written for it (i.e., you will have to read the method comment to know exactly what the method is supposed to do), and it would also mean you are not changing the method header.
Remember to switch roles with your partner from what you were last time. For more information on lab partnerships, see Lab 2.
The class that encapsulates our array list is called Nums. It stores a sequence of ints and allows us to do some computations with the sequence. When you implement a non-trivial class, you want to write and test the minimal amount of code to make sure the data was put in it correctly, before implementing any other methods. That normally means implementing (1) at least one of the constructors and (2) an accessor method or print method that would allow us to see that it got created correctly.
For the Nums class you're going to implement the constructor and the printVals method initially, although you'll will be doing more than that by the end of the exercise. Since the constructor just creates an empty Nums object, the results of this won't be too interesting. Read on through Step 3 before you start this task.
Step 1. Read the code and comments in the following files. First, here's a summary of what they contain, and some changes you will be making to them:
Step 3. Implement the Nums constructor and printVals method. This, of course, also involves creating the instance variable(s) that are private to the class. You can run NumsTester with this code, to make sure an empty Nums object gets created and printed correctly. Look at the first few lines of main; that's where this code gets tested.
Step 4. Write the code necessary to to test building and printing a non-empty Nums object. To do that, you need to implement the readNums method in NumsTester, then the Nums add method itself (more about that process in the next few paragraphs).
More about the static method, readNums in NumsTester: (for more information about static methods, see the sidebar below). You're going to test our Nums class with input from a file (more about that below), so readNums should not print out any prompts for the user. We wrote a similar program, ScoreCounts.java in a recent lecture (available in the Vocareum Lecture Code area under 01-30). We'll review what's involved here:
java NumsTester < nums.inNote: running the program this way it will no longer wait for input from the keyboard.
Once you finish readNums and add will be the first time you'll be able to test that a non-empty Nums object gets created and printed correctly. Once you've completed the implementation your program should print out the list of numbers entered in the exact format described in the printVals comments (that includes no space after the last number printed). Note: NumsTester will still not get correct results for the tests of the other two Nums methods: minVal and valuesGT, since you haven't implemented those methods yet.
Step 5. Create at least two other test files to test boundary cases of the code you have written. Call them test1.in and test2.in. (And, of course, fix your program as necessary if it doesn't work properly on the boundary cases.)
To get checked-off, show the TA the answer to question 1.1, your working NumsTester subset program, running on regular and boundary cases; also show him or her the source code you wrote for this exercise, and your test cases.
For Exercise 2 keep working with the the version of the files that's in your work (i.e., home) directory.
Read the comments for the minVal method of the Nums class so you know what it's supposed to do.
Complete the minVal method of the Nums class. Once you test your code, answer the question below.
Read the comments for valuesGT method of the Nums class so you know what it's supposed to do. Note: valuesGT is short for "values greater than".
Then look at the hard-coded test cases for valuesGT in NumsTester. The method is called from the testFilter method in NumsTester. (testFilter just packages up the computation and output for a particular test of valuesGT, so you don't have to keep repeating that code to do multiple tests.) These test cases are designed to go with the data in nums.in.
Implement the valuesGT method of the Nums class. Check that your actual results match the expected results when you run it on each of the given test files. Remember, sometimes a mismatch is because of a mistake in the expected results.
To get checked-off, show the TA your working valuesGT code, including the source code; show it running on all four of the test input files plus additional ones if necessary; and show him or her the answers to the questions.
What are static methods? You've been introduced to static methods in context of calling them in the Java library, such as in the Math class (described in Special Topic 4.4 of the textbook), and you've been using static in your main headers even though you may not have really known why. In this lab we have a few more static method definitions, namely readNums and testFilter in NumsTester, so this is a good opportunity to look at this in more detail.A better name for a static method would be a class method; it's is inside the scope of a particular class, but it is not associated with an individual instance of the class (i.e., object). As you have seen, from outside the enclosing class they are called with the class name instead of an object name (e.g., Math.round(...)). When you call a static (or non-static) method from another method in the same class, you don't have to use the dot notation (e.g., calling testFilter(...) from main). They are also analogous to regular functions in languages like C/C++, Python, and Matlab; in contrast, in Java all functions must appear inside classes: when we want a function that's does not operate on an object instance we use a static method.
The methods in NumsTester are also static because they are helper methods for a static method (namely, main). In Java, main is always static because it is called (by the run-time system) with no associated instance of its enclosing class (i.e., no NumsTester object, in this case). In virtually all the Java programs you've seen and will write for this course all the variables used by main will be local variables (or the args parameter, which we'll get to later in the semester). Generally, static methods cannot access instance variables of their class, because there is no instance involved (i.e., no this), and thus classes with only static methods, such as NumsTester, don't have instance variables at all. The two other methods in NumsTester are helper functions for main to reduce the complexity of main and the amount of code you have to write overall. Any data they use are passed via explicit parameters.
To summarize, to make a procedure-oriented design in Java you make a main class that has a static main method, plus helper methods that are static methods in that same class using parameter passing and return values to communicate information between these functions. Our test programs, including NumsTester, use such a design.
You added coded that used hasNextInt() as part of the readNums method you implemented as part of Exercise 1. hasNextInt will return false on more than just end-of-file.
Look at the second line of code in the testFilter method in NumsTester which contains two "." (dot) operators:
nums.valuesGT(threshold).printVals();
Our Nums class uses an array list. You could have used an array instead. (Note: Sections 7.7.4 and 7.7.8 of the text may be helpful in answering the the following two questions related to this.)
To put it another way, do you need to save all the numbers to do the task? (why or why not)
Make sure you put your name and NetID in all the files you submit.
When you click the Submit button, it will be looking for
and compiling (for source code) the files
README,
test1.in,
test2.in,
test3.in,
Nums.java,
NumsTester.java,
ex1/Nums.java,
ex1/NumsTester.java,
ex2/Nums.java, and ex2/NumsTester.java.
As usual, the lab is due
by 11:59pm on Sunday Pacific Time at the end of the week for this lab.