Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Programming: Command Line, Exceptions, and GUI with Vassar College's CMPU-101 - Prof., Assignments of Computer Science

Instructions on running and compiling java code at the command line, dealing with exceptions, and creating a simple graphical user interface (gui) using vassar college's cmpu-101 course material. It covers writing a main method, compiling code, running code with arguments, printing to the terminal, static methods, and handling exceptions. Additionally, it introduces the swing library for creating a gui and the swing interactor hierarchy.

Typology: Assignments

Pre 2010

Uploaded on 08/18/2009

koofers-user-iyw
koofers-user-iyw 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Wrap-up:
Where do we go from here?
CMPU-101: Problem-Solving and
Abstraction
Jennifer Walter
Vassar College
Spring 2008
Running and compiling code at the command line (i.e. the
"system prompt"):
1. Need to write main method in a public class:
public static void main (String[] args)
args is an array of Strings that is space-delimited and
whose elements are typed on the same line as the
command to execute the file.
2. To compile code at command line:
javac -classpath .:acm.jar
filename
.java
where filename is the file with the .java extension to
be compiled.
3. To run code at command line:
java -classpath .:acm.jar
filename
leave off the .java extension when running code.
The -classpath specifies location of acm.jar file.
4. To include arguments to the main method on the
command line when running code:
java -classpath .:acm.jar
filename 20 hi there
Note:
No extension used when running a program.
In this case, args.length = 3 and args[0] = "20", args[1]
= "hi", and args[2] = "there"
Printing to terminal without using acm.jar commands
Use System.out.println("String to print");
System.out.print("String to print");
These methods are the same as the print and
println in acm.jar except they print to terminal
window.
System
is a class defined in the
java.lang
package.
This package is always imported by default and
includes commonly used classes like:
String,
Character, Math, Integer, Double, Boolean
To read from the keyboard without using acm.jar,
you must declare a new Scanner and import
java.util.*, as was done in Assignment 10.
Static methods
We did not cover the definition and use of static
methods in this class. A method is
static
if it
includes the keyword "static" in its signature.
The main method is an example of a static method.
Other static methods we used were in the Math
class, the Character class, the RandomGenerator
class, and the System class. These methods are
called using the class name as a receiver rather
than using an object as the receiver.
One limitation of static methods is that they cannot
call any non-static methods in their own class
directly. An object must be created on which to
call non-static methods.
Dealing with Exceptions in Java
1. Lines that generate exceptions when doing file input:
a. Instantiating a new Scanner for an input file.
b. Instantiating a new FileWriter for file output.
2. To deal with Exceptions:
a. Include a "throws Exception" clause on method
signature
b. Include a try{ } block around line that may cause
exception and follow it with a catch(Exception e){ }
to take appropriate action if an Exception occurs.
pf3

Partial preview of the text

Download Java Programming: Command Line, Exceptions, and GUI with Vassar College's CMPU-101 - Prof. and more Assignments Computer Science in PDF only on Docsity!

Wrap-up:

Where do we go from here?

CMPU-101: Problem-Solving and

Abstraction

Jennifer Walter

Vassar College

Spring 2008

Running and compiling code at the command line (i.e. the "system prompt"):

  1. Need to write main method in a public class: public static void main (String[] args) args is an array of Strings that is space-delimited and whose elements are typed on the same line as the command to execute the file.
  2. To compile code at command line: javac -classpath .:acm.jar filename.java where filename is the file with the .java extension to be compiled.
  3. To run code at command line: java -classpath .:acm.jar filename leave off the .java extension when running code. The -classpath specifies location of acm.jar file.
  4. To include arguments to the main method on the command line when running code: java -classpath .:acm.jar filename 20 hi there Note: No extension used when running a program. In this case, args.length = 3 and args[0] = "20", args[1] = "hi", and args[2] = "there" Printing to terminal without using acm.jar commands
  • Use System.out.println("String to print"); System.out.print("String to print"); These methods are the same as the print and println in acm.jar except they print to terminal window.
  • System is a class defined in the java.lang package. This package is always imported by default and includes commonly used classes like: String, Character, Math, Integer, Double, Boolean
  • To read from the keyboard without using acm.jar, you must declare a new Scanner and import java.util.*, as was done in Assignment 10. Static methods
  • We did not cover the definition and use of static methods in this class. A method is static if it includes the keyword "static" in its signature.
  • The main method is an example of a static method. Other static methods we used were in the Math class, the Character class, the RandomGenerator class, and the System class. These methods are called using the class name as a receiver rather than using an object as the receiver.
  • One limitation of static methods is that they cannot call any non-static methods in their own class directly. An object must be created on which to call non-static methods. Dealing with Exceptions in Java
  1. Lines that generate exceptions when doing file input: a. Instantiating a new Scanner for an input file. b. Instantiating a new FileWriter for file output.
  2. To deal with Exceptions: a. Include a "throws Exception" clause on method signature b. Include a try{ } block around line that may cause exception and follow it with a catch(Exception e){ } to take appropriate action if an Exception occurs.

Creating a Simple GUI

  • In addition to mouse and keyboard events, application programs may include a graphical user interface or GUI (pronounced gooey ) consisting of buttons and other on-screen controls. Collectively, these controls are called interactors.
  • Java defines many types of interactors, most of which are part of a collection called the Swing library , described in section 10.6. You create a GUI by constructing the Swing interactors you need and then arranging them appropriately in the program window. You need to import javax.swing.* to use the Swing components.

The Control Strip

  • When you create an instance of any Program subclass, Java divides the window area into five regions as follows:
  • The CENTER region is typically where the action takes place. A ConsoleProgram adds a console to the CENTER region, and a GraphicsProgram puts a GCanvas there. CENTER NORTH SOUTH W E S T E A S T
  • The other regions are visible only if you add an interactor to them. In other regions, the interactors are laid out from left to right in the order in which they were added.

Creating a GUI with a Single Button

Please Please dodo notnot presspress thisthis buttonbutton again.again. Arthur listened for a short while, but being unable to understand the vast majority of what Ford was saying he began to let his mind wander, trailing his fingers along the edge of an incomprehensible computer bank, he reached out and pressed an invitingly lit up with the words “Please do not press this button again. large red button on a nearby panel. The panel” — Douglas Adams, Hitchhiker’s Guide to the Galaxy, 1979 The HitchhikerButton program on the next slide uses this vignette from Hitchhiker’s Guide to the Galaxy to illustrate the process of creating a GUI without focusing on the details. The code creates a single button and adds it to the SOUTH region. It then waits for the user to click the button, at which point the program responds by printing a simple message on the console. HitchhikerButton Red

The HitchhikerButton Program

import acm.program.; import java.awt.event.; import javax.swing.; /

  • This program puts up a button on the screen, which triggers a
  • message inspired by Douglas Adams's novel. / public class HitchhikerButton extends ConsoleProgram { / Initializes the user-interface buttons / public HitchhikerButton() { this.add(new JButton("Red"), SOUTH); this.addActionListeners(); this.start(); } / Responds to a button action / public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Red")) { this.println("Please do not press this button again."); } } }*

The Swing Interactor Hierarchy

The following diagram shows the Swing classes used in this text. With the exception of IntField and DoubleField , all of these classes are defined in the javax.swing package. JComponent AbstractButton JSlider JLabel JComboBox JButton JToggleButton JTextComponent JCheckBox JRadioButton JTextField IntField DoubleField acm.gui ButtonGroup

The JButton Class

  • The most common interactor in GUI-based applications is an on-screen button, which is implemented in Swing by the class JButton. A JButton object looks something like
  • When you click on a button, Java generates an action event , which in turn invokes a call to actionPerformed in any listeners that are waiting for action events.
  • The constructor for the JButton class is where label is a string telling the user what the button does. The button shown earlier on this slide is therefore created by new JButton( label ) JButton pushMeButton = new JButton("Push Me"); Push Me