Sie sind auf Seite 1von 16

Tutorial: A Second Cup of Java

To visit my site
[HOME] [Consulting] [Design] [Maintenance] [Project] [Testing] [Training] [Turnkey] [Java]

To contact us
2002 - 2003 All Rights Reserved Total Application Works

q q q q q q q

Introduction What is a Template? What is a Class? Putting It Together: Build an Application I Putting It Together: Build an Applet I Putting It Together: Build an Application II Putting It Together: Build an Applet II

Introduction This tutorial assumes that you have read and completed the following two tutorials.
1. Your First Cup of Java 2. Getting Started

The rest of this tutorial will build on and reinforce what you learned from The First Cup of Java tutorial. This tutorial will expand on:
1. What is a class? 2. The Hello World application.

3. The Hello World applet.

We will discuss the term template as we ease into this tutorial. top What is a Template? If you look in a dictionary, template is defined as a pattern, usually in the form of a thin plate of metal, wood, plastic, etc., for forming an accurate copy of an object or shape In C++, a template is a keyword that allows you to create generic functions and/or classes. Java has a keyword interface that is similar, but for our purposes, we will say they are not the same. Based on what we have seen in the tutorials Getting Started and First cup of Java, how does the term template apply? Remember that a template is a pattern; therefore, the application template for Hello World looks like:

/** * The HelloWorldApp class implements an application that * simply displays "Hello World!" to the standard output. */ class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello World!"); //Display the string. } } and the template for the Hello World applet looks like: import java.applet.*; import java.awt.*;

/** * The HelloWorld class implements an applet that * simply displays "Hello World!". */ public class HelloWorld extends Applet { public void paint(Graphics g) { // Display "Hello World!" g.drawString("Hello world!", 50, 25); } }
What's Wrong with This Picture?

The problem is that a template is supposed to be a generic pattern and the two examples above are not generic. To make our application and applet generic, we will make some changes and so our templates in pseudocode format look like:

/** * The MyApp class implements an application. */ class MyApp { public static void main(String[] args) { code ... } } and the template for the applet looks like: import java.applet.*; import java.awt.*; /** * The MyApplet class implements an applet. */ public class MyApplet extends Applet {

public void paint(Graphics g) { // Display "something!" g.drawString("something!", 50, 25); } }


The Templates Are Not Quite Generic Enough

In many text books, these beginning sections start off with much of the initialization taking place in the main() method. In reality, much of the initialization takes place in a piece of code called a constructor. A constructor is a method that has the same names as the class. At this point our application template looks like:

/** * The MyApp class implements an application. */ class MyApp { // MyApp constructor public MyApp() { initialization code ... } public static void main(String[] args) { minimal code ... } }

The applet API also has an initialization method called init(). Therefore, our applet template would look like:

import java.applet.*; import java.awt.*; /** * The MyApplet class implements an applet. */ public class MyApplet extends Applet { public void init() { // initialization code initialization code... ... } public void paint(Graphics g) { // Display "something!" g.drawString("something!", 50, 25); } }

A template allows us to create generic patterns. If we want a blueprint for something more specific, then we would consider a class. top What is a Class? The dictionary defines a class as a number of people or things grouped together because of certain likenesses or common traits; kind; sort; category The class of automobiles contains:
q

Acura TL

q q q q q q q q q

BMW 5 series Buick Century Ford Explorer Honda Pilot Mazda6 Toyota Avalon Toyota Highlander Volvo S60 etc.

When you buy a car, you get an instance of a brand and a model e.g., Toyota Highlander. We will modify the MyApp so we can get instances. This is done with the following line of code:

MyApp app = new MyApp() ; and the template looks like: /** * The MyApp class implements an application. */ class MyApp { /** MyApp constructor */ public MyApp() { initialization code ... } public static void main(String[] args) { MyApp app = new MyApp() ; /** creates an instance of MyApp */ } }

top Putting It Together: Build an Application I Using the application template, build an application that:
1. Asks the user for their name. 2. Use JOptionPane to ask the user for their name.
r

JOptionPane - makes it easy to pop up a standard dialog box that prompts users for a value or informs them of something.

showInputDialog(Component parentComponent, Object message, String title, int messageType)

Where showInputDialog shows a dialog requesting input from the user parented to parentComponent with the dialog having the title title and message type messageType. r Parameters: s parentComponent - the parent Component for the dialog s message - the Object to display s title - the String to display in the dialog title barmessage s Type - the type of message that is to be displayed: s ERROR_MESSAGE, s INFORMATION_MESSAGE, s WARNING_MESSAGE, s QUESTION_MESSAGE, or s PLAIN_MESSAGE. 3. Writes "Hello username" to the screen.
r

showMessageDialog(Component parentComponent, Object message,

String title, int messageType)

4. Writes "My name is TAW" to the screen. 5. The solution follows.

/** * The MyApp1 class implements an application. */ import javax.swing.JOptionPane ; class MyApp1 { String name ; // name of user

/** MyApp1 constructor */ public MyApp1() { /** read in the name of the user as a string */ name = JOptionPane.showInputDialog( "Please enter your name." ); JOptionPane.showMessageDialog( null, "Hello " + name, "Hello", JOptionPane.PLAIN_MESSAGE ); JOptionPane.showMessageDialog( null, "My name is TAW", "My name", JOptionPane.PLAIN_MESSAGE );

} public static void main( String args[] ) { MyApp1 app = new MyApp1() ; /** creates an instance of MyApp */ System.exit( 0 ); } } /** end the program */

top Putting It Together: Build an Applet I Using the applet template, build an applet that:
1. 2. 3. 4. 5. 6.

Extends JApplet. Asks the user for their name. Use JOptionPane to ask the user for their name. Writes "Hello username" to the screen. Writes "My name is TAW" to the screen. The solution follows.

/** *************************************************** * File: The MyApplet class implements an applet. */ import javax.swing.JOptionPane ; import javax.swing.* ; import java.awt.Graphics ; public class MyApplet1 extends JApplet {

String name;

// name of user

/** MyApp1 init() */ public void init() { name = JOptionPane.showInputDialog( "Enter your name" ); } public void paint( Graphics g) { /** show the results */ g.drawRect( 15, 20, 200, 105 ) ; g.drawString( "Hello " + name, 25, 45 ) ; g.drawString( "My name is TAW ", 25, 75 ) ; } }

top top Putting It Together: Build an Application II Build an application using the application template that:
1. Requests three floating point numbers from the user.

Use JOptionPane class to request the user input. 2. Adds these numbers together. 3. Multiplies the three numbers e.g., prod = num1 * num2 * num3 4. Determines the average of these three numbers.
r

/**

* The MyApp class implements an application. */ import javax.swing.JOptionPane ; class MyApp { String firstNum, secondNum, thirdNum; double num1, num2, num3, sum, prod, avg; // first string entered by user // second string entered by user // second string entered by user // first number to add // second number to add // second number to add // sum of num1, num2 and num3 // prod of num1, num3 and number2 // average of num1, num3 and number2

/** MyApp constructor */ public MyApp() { getNumbers(); convertNumbers(); sumNumbers(); prodNumbers(); avgNumbers(); outputNumbers(); } public void getNumbers() { /** read in first number from user as a string */ firstNum = JOptionPane.showInputDialog( "Enter first integer" ); /** read in second number from user as a string */ secondNum = JOptionPane.showInputDialog( "Enter second integer" );

/** read in third number from user as a string */ thirdNum = JOptionPane.showInputDialog( "Enter third integer" ); } public void convertNumbers() { /** convert numbers from type String to type int */ num1 = Double.parseDouble( firstNum ); num2 = Double.parseDouble( secondNum ); num3 = Double.parseDouble( thirdNum ); } public void sumNumbers() { /** add the numbers */ sum = num1 + num2 + num3; } public void prodNumbers() { /** add the numbers */ prod = num1 * num2 * num3; } public void avgNumbers() { /** add the numbers */ avg = ( num1 + num2 + num3 ) / 3; } public void outputNumbers() { /** display the results */ JOptionPane.showMessageDialog( null, "The sum is " + sum + "\n" + "The product is " + prod + "\n" + "The average is " + avg, "Results", JOptionPane.PLAIN_MESSAGE ); }

public static void main( String args[] ) { MyApp app = new MyApp() ; /** creates an instance of MyApp */ System.exit( 0 ); } } /** end the program */

top Putting It Together: Build an Applet II Build an application using the application template that:
1. Requests three floating point numbers from the user.

Use JOptionPane class to request the user input. 2. Adds these numbers together. 3. Multiplies the three numbers e.g., prod = num1 * num2 * num3 4. Determines the average of these three numbers.
r

/** * The MyApplet class implements an applet. */ import javax.swing.JOptionPane ; import javax.swing.* ; import java.awt.Graphics ; public class MyApplet extends JApplet { String firstNum, // first string entered by user

secondNum, thirdNum; double num1, num2, num3, sum, prod, avg;

// second string entered by user // second string entered by user // first number to add // second number to add // second number to add // sum of num1, num2 and num3 // prod of num1, num3 and number2 // average of num1, num3 and number2

/** MyApp constructor */ public void init() { getNumbers(); convertNumbers(); sumNumbers(); prodNumbers(); avgNumbers(); } public void getNumbers() { /** read in first number from user as a string */ firstNum = JOptionPane.showInputDialog( "Enter first integer" ); /** read in second number from user as a string */ secondNum = JOptionPane.showInputDialog( "Enter second integer" ); /** read in third number from user as a string */ thirdNum = JOptionPane.showInputDialog( "Enter third integer" ); } public void convertNumbers() { /** convert numbers from type String to type int */ num1 = Double.parseDouble( firstNum );

num2 = Double.parseDouble( secondNum ); num3 = Double.parseDouble( thirdNum ); } public void sumNumbers() { /** add the numbers */ sum = num1 + num2 + num3; } public void prodNumbers() { /** add the numbers */ prod = num1 * num2 * num3; } public void avgNumbers() { /** add the numbers */ avg = ( num1 + num2 + num3 ) / 3; } public void paint( Graphics g) { /** show the results */ g.drawRect( 15, 20, 200, 105 ) ; g.drawString( "The sum is " + sum, 25, 45 ) ; g.drawString( "The product is " + prod, 25, 75 ) ; g.drawString( "The average is " + avg, 25, 95 ) ; }

} The HTML file needed is: <html> <applet code="MyApplet.class" width=400 height=200> </applet> </html>

If I enter the following numbers:


1. 15.0 2. 15.0 3. 30.0

I get the following output:


The sum is 60.0 The product is 6750.0 The average is 20.0

top

[HOME] [Consulting] [Design] [Maintenance] [Project] [Testing] [Training] [Turnkey] [Java]

2002 - 2003 All Rights Reserved Total Application Works

Das könnte Ihnen auch gefallen