Reputation: 69
I'm working on a homework assignment and I'm confused about what the instructor wants. Unfortunately, when I asked for clarification it created more questions, so I'm hoping I'm just missing something simple here and I'm overthinking the issue. Here is the assignment:
Create a class named Purchase. Each Purchase contains an invoice number, amount of sale, and amount of sales tax. Include set methods for the invoice number and sale amount. Within the set() method for the sale amount, calculate the sales tax as 7.5% (using a static field in the Purchase class) of the sale amount. Also include a display method that displays a purchase's details in a well formatted output display.
Save the file as Purchase.java. Compile and run the program until it works and the output looks nice, add the necessary documentation as described in Course Documents, and then attach your .java file to this assignment. Do not attach the .class file, attach only your source code. This assignment is due the last day of the academic week.
I created the Purchase class with all of the required set methods and display methods, but in the past when we've been required to create a class like this we've also been required to create and provide a test class. When I asked if we needed to submit our test class, the instructor said, "you do not need a separate class, however you still must include test code."
My question is it seems like the assignment was specifically asking for a unique class for creating Purchase objects, not an application. How can I provide test code without another class? I've asked for clarification, but I'm not sure if he'll get back to me before it's due.
Upvotes: 2
Views: 67
Reputation: 308001
I absolutely necessary you can have some "test code" in a simple main
method of the Purchase
class:
public class Purchase {
// ... your real code is here ...
public static void main(String[] args) {
Purchase p = new Purchase();
p.doSomeThings();
if (p.getSomeInfo() != 42) {
throw new IllegalStateException("Oh noes! This went wrong!");
}
System.out.println("All Tests ok!");
}
}
That's not really a clean solution and I wouldn't do that for any real production code, but if the goal is just to demonstrate that your class does what it's meant to do for a university course (and you've been explicitly told that you don't need a separate test class) then it might just be good enough.
Obviously a real test class using a proper unit testing framework (such as JUnit or TestNG) would be better.
Upvotes: 2