Weeks 6-8 notes

Download Report

Transcript Weeks 6-8 notes

Packages, Javadoc, Testing, GUIs
Chapter 5
Chapter 6
Class notes
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 */
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
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.
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
Chapter 6
Testing
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
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
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
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
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
Testing, writing our own
• To Test findSpace method
• Record a test
–Start Recording
–Perform the test on the object
bench
–End Recording
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)
Perform a Test on the Object Bench
• Red Circle means recording
• Create objects
– Create a day object
– Create an Appointment objects with a onehour duration
– Call the findSpace method in Day
• Check that the test is correct
– right answer
– assert box checked
End Recording
• End recording
• Examine source code in test class
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.
Graphical User Interfaces
class notes (in book chapter 11)
Note ‘x’
GUIs: using Sun’s javadoc
• 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
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.
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)
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’
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)
Building a Window
• Everything is in a
JFrame
• JFrame has
– Title bar
– Menu bar (optional)
– Content Pane
menu
bar
content
pane
Frame
(whole
window)
title bar
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);
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);
Some Components
• JButton
• JPanel
• JLabel
• Lots, look at:
http://java.sun.com/j2se/1.5.0/docs/api/javax
/swing/package-summary.html
title
bar
component
(JPanel)
minimize
maximize
close
come for
free
{
Also in lab3, what to do on close (click on red X):
windowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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.
FlowLayout
BorderLayout
default for top level containers
(JFrame)
GridLayout
BoxLayout
Note: no component
resizing.
Layout Manager Lab 3
• 4 JPanels
gameWordPanel,
gameGuessPanel,
gameButtonPanel,
gameStatusPanel
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);
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/e
vent/package-summary.html)
“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();
}
}
);
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();
}
}
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( );
Create a Frame with Questions
part 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_CL
OSE);
No action listener
• No action listeners yet