250 likes | 377 Views
This guide explains how to effectively test object-oriented programs by coding a main method that tests various class methods before their completion. It discusses the importance of using method stubs for unfinished methods to facilitate iterative development. The UserEmail application is highlighted, detailing the UserEmailFrame and UserEmailPanel classes, including how they manage window actions and user input through button clicks, and how user data is handled with the User and UserIO classes. This approach encourages early testing and enhances overall program reliability.
E N D
How to test an object –oriented program How to code a main method that tests a class Description • When you want to test a class before the classes that are going to use it are finished, you can write a main method in the same class. The main method can then test the other methods of the class by sending typical arguments to them and printing the results on the console • The main method in the example above works because the BookOrder class has set and get methods for all instance variables and a toString method that overrides the one in the Object class
When and how to code method stub • Description • When you’re writing a class or method that calls another method that hasn’t been coded yet, it sometimes makes sense to quickly write a method stub for that method. Then, you can complete the method that you’re working on • A method stub can be written at any of the levels shown in the next slide. But when the stub gets too elaborate, it’s often best to write and test the entire method instead of a stub
The User Email application • The UserEmailFrame class defines the JFrame onject. This object includes a windowClosing method that’s executed whenever the frame is closed • The UserEmailPanel class defines the Jpanel object that’s displayed within the JFrame.
The User Email application • This object includes an actionPerformed method that’s executed whenever a button on the frame is clicked. If the Add button is clicked, this method creates a new User object from the User class and calls the addRecord method of the UserIO class to add the data in the User object to the end of a file • The User class defines the User object • The UsetIO class con tains a static addRecord method that adds the data in a User object to the end of a file.