1 / 40

Packages, Javadoc, Testing, GUIs

Learn how to write comprehensive JavaDoc documentation for your Java class, including explanations for each method using @param and @return when appropriate. View the documentation through BlueJ, print it out with the source code, and use the API for guidance.

cpetry
Download Presentation

Packages, Javadoc, Testing, GUIs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Packages, Javadoc, Testing, GUIs Chapter 5 Chapter 6 Class notes

  2. JavaDoc Documentation • View in BlueJ through Interface • View on web through API documentation (application programming interface): • http://java.sun.com/j2se/1.5.0/docs/api/ • Write your own /** Here is what my Class does * can go over multiple lines */

  3. JavaDoc: write your own • Write in html (text IS html) /** Here is my <b>important</b> part */ • Use tags: @author your name @version date or 1.0 @param name description @return description • For a full list of tags, see: http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/javadoc.html

  4. Quiz 6 • Add javadoc to your mastermind program. Add an explanation for each method using @param and @return when appropriate. Add a comment at the beginning of the program. Use @version and put the date. View your javadoc through the BlueJ interface. Print out your source code with the javadoc comments.

  5. Using the API • Use API whenever you want to do something, but don’t know how Example: How do I use Random numbers? • Look at API for Random • Or, Look at documentation for classes: • http://java.sun.com/j2se/1.5.0/search.html • (note: search isn’t always that helpful) • Or, look at: http://java.sun.com/j2se/1.5.0/docs/ • This has a more graphical representation than the API; most people use the API • Look at top description and each method summary

  6. Chapter 6 Testing

  7. Testing/Debugging Syntax errors: Initially fixing syntax errors the hard part. After that, fixing logic errors: • Testing: Ensuring that your code works • Debugging: finding where code is incorrect

  8. Levels of Testing • Unit testing • Application testing • Always start at the lowest level • Test after every method is written • Use these tests throughout. after each method, run all of the old tests

  9. Types of Testing (in BlueJ) • Inspection • System.out.println • Regression Testing • Create tests and rerun them for each new development • To do that, Test Harness/Test Rig • Class whose sole purpose is to test another class • To do that, Automatic Testing using junit • To do that, Record Tests

  10. Testing, version 1(textbook: 6.4) • Write a method that will test different parts of a class. • Example: day (simple blackberry) • in chapter 6, projects • diary-testing • OneHourTests class

  11. Testing, version 2 • Downsides of version 1 • results have to be checked by eye and checker has to know what results should be • automate process • diary-testing-junit-v1 • green boxes are tests classes • supports unit testing, regression testing • part of BlueJ (not Java, but in the spirit of Java) • Run tests button • Test classes contain • source code to test class, AND • assert.equals( ) to test whether test was successful

  12. Testing, writing our own • To Test findSpace method • Record a test • Start Recording • Perform the test on the object bench • End Recording

  13. Start Recording • Show unit testing • Under tools, preferences, miscellaneous, check "Show unit testing tools" checkbox • Compile all classes • Create Test Class (if one is not there) • Right click on class • Choose "Create Test Class" • Create Test Method • Right click on test class, chose "Create Test Class Method" • Put a name in (findSpace9)

  14. Perform a Test on the Object Bench • Red Circle means recording • Create objects • Create a day object • Create an Appointment objects with a one-hour duration • Call the findSpace method in Day • Check that the test is correct • right answer • assert box checked

  15. End Recording • End recording • Examine source code in test class

  16. Quiz 7 Write a test unit class for Mastermind and write two unit tests for the same method (test it using two different inputs). If you have no methods with parameters, then write two test for two different methods.

  17. Graphical User Interfaces class notes (in book chapter 11)

  18. GUIs: using Sun’s javadoc Note ‘x’ • http://java.sun.com/j2se/1.5.0/docs/ • Graphical User Interfaces implemented in Java in javax.Swing and AWT packages (Swing is newer, better, but some stuff only in AWT) • For general information, click on library • Do tutorial

  19. Designing Program with Graphical User Interfaces • GUI Programs have two parts/classes: • the User Interface • the engine • In Mastermind • User interface is board and methods that use can call • The engine is what does the calculations: calculates black and whites/win/lose/generates answer, etc.

  20. Implementating GUIs:GUI parts • Components • buttons, menus, textfields, etc. • Layout • Where on the screen to put the components. use LayoutManager • Event Handling • instead of executing methods when called (by BlueJ or in code), execute methods when an event occurs (e.g., mouse click on a button)

  21. Swing • Java has AWT (old, Abstract Window Toolkit) and Swing (based on AWT) • Swing prefixes components with J • e.g., Button is from AWT, jButton is Swing • We’ll use Swing • To use Swing: import java.awt.*; // * means all packages in import java.awt.event.*; import javax.swing.*; // note ‘x’

  22. Swing Specifics • Everything in a container • Two top level containers: JFrame, JApplet • Other containers can be embedded within each other: Jbutton on JPanel on another JPanel, etc. • Lots of parts to a JFrame, mostly ContentPane • content panes can hold any number of components • (4 components in windowFrame in lab3) • contentPane (only one) vs JPanel (can have lots)

  23. Building a Window Frame (whole window) • Everything is in a JFrame • JFrame has • Title bar • Menu bar (optional) • Content Pane title bar menu bar content pane

  24. Code for a Frame To build a JFrame, do 4 things (From 150 lab3 GuiInterface.java) 1. Create new JFrame and put a title in private JFrame windowFrame = new JFrame(“Word Jumble 1.0”); 2. Tie parts to JFrame (component, menubar, etc) // if gameWordPanel already exists – type JPanel Container cp = WindowFrame.getContentPane( ); cp.add(gameWordPanel); OR windowFrame.getContentPane( ).add(gameWordPanel); OR (in Java5): windowFrame.add(gameWordPanel);

  25. Code for a JFrame 3. pack frame. arrange components windowFrame.pack( ) 4. make the frame visible (can also make it invisible, as in quitWord) windowFrame.setVisible(true);

  26. Some Components • JButton • JPanel • JLabel • Lots, look at: http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/package-summary.html

  27. minimize maximize close come for free title bar { component (JPanel) Also in lab3, what to do on close (click on red X): windowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  28. Layout managers • Manage limited space for components. • FlowLayout, BorderLayout, GridLayout, BoxLayout, GridBagLayout (most flexible, hardest) • Manage Container objects, e.g. a content pane. • Each imposes its own style.

  29. FlowLayout

  30. BorderLayoutdefault for top level containers (JFrame)

  31. GridLayout

  32. BoxLayout Note: no component resizing.

  33. Layout Manager Lab 3 • 4 JPanels gameWordPanel, gameGuessPanel, gameButtonPanel, gameStatusPanel

  34. Layout Managers – Lab 3 Container contentPane = windowFrame.getContentPane ( ); contentPane.setLayout(new GridLayout(4, 1)); OR windowFrame.getContentPane().setLayout(new GridLayout(4,1)); OR windowFrame.setLayout(new GridLayout(4, 1)); // add panels in order windowFrame.getContentPane().add(gameWordPanel); windowFrame.getContentPane().add(gameGuessPanel); windowFrame.getContentPane().add(gameButtonPanel); windowFrame.getContentPane().add(gameStatusPanel);

  35. Events • We can now put items on a JFrame and arrange them. We need to be able to do something with them. • Action Events • mouse click, mouse over, window action, menu choice, etc. • General, ActionListener • Something happened • in java.awt.event • (http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/event/package-summary.html)

  36. “Check” button in Lab 3:add ActionListener event //Associate actionlistener with button and write actionPerformed b = new JButton("Check"); b.addActionListener(new ActionListener( ) { public void actionPerformed(ActionEvent e) { System.out.println("Check is " + e.getActionCommand()); checkWord(); } } );

  37. Action Listeners: a different way b = new JButton("Check"); b.addActionListener(new checkAction( )); private class checkAction implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("Check is " + e.getActionCommand()); checkWord(); } }

  38. Create a Frame with Questions • //Create the frame: • JFrame frame = new JFrame( ); // layout is BorderLayout • //Create the button panel • JPanel buttonPanel = new JPanel(); // layout is flowLayout • //Create the buttons: • JButton b1 = new JButton("yes"); • JButton b2 = new JButton("no"); • JButton b3 = new JButton("maybe"); • //Create the Question Label: • JLabel question = new JLabel( );

  39. Create a Frame with Questionspart 2 • // Add the buttons to the Panel • buttonPanel.add(b1); • buttonPanel.add(b2); • buttonPanel.add(b3); • // Add the question and panel to the frame • frame.add(question, BorderLayout.CENTER); • frame.add(buttonPanel, BorderLayout.SOUTH); • // Pack the frame and display it • frame.setSize(200, 100); // instead of frame.pack( ); • frame.setVisible(true); • frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  40. No action listener • No action listeners yet

More Related