Lab 5: Java applets
Assignment due on Tuesday, June 10
Today, we will work on Java applets. As we have learned in class, writing applets is very similar to writing java
applications. Translating a java application to a java applet is even easier, especially with a correctly formatted code.
In this assignment you will be required to translate your "lab 3 - monetary change" java program into an applet. Here are the steps to
complete the assignment (Please reference Lecture 12 if you have any trouble):
- Create a new java project
- Import your lab3 code or add a new class to the project and copy paste the lab3 code
- If your class variables and methods contain keyword "static" remove all the "static" keywords
- Import java.applet.*, java.awt.*, and javax.swing.* to your project
- Add the following line to your class declaration: extends JApplet
- Rename your constructor method to init()
e.g. public lab3() becomes public init()
- Rename your main method to paint(Graphics g).
e.g. public static void main(String args[]) becomes public void paint(Graphics
g)
- Cut and paste all your code in the paint method to the end of the init method. Note: after this step your paint method should
have become empty
- Now we are finished with the cosmetic changes. So let's start adding applet features
- Use the following code to get input from the keyboard:
String s1 = JOptionPane.showInputDialog("Enter first floating-point value"
);
- To convert the inserted string into number, use:
number1 = Double.parseDouble( s1 );
- To output your result, add the following code in the paint() method (Use this code to output all the denominations):
g.drawString( "Quarters: " + quarters, 25, 25 );
-
We are finished coding. Now let's try to run it. Go to the menu Run -- Run as -- Java applet
-
If you have successfully ran your applet from Eclipse, it's time to add the applet into your web page.
- Open windows explorer and navigate to your project. (it is in your workspace directory)
- Go to the bin folder
- Copy the "program name".class file into P:/public_html
- To display your applet add the following code in your web page:
Submission: Name your program "your name"_applet.java, where you will need to replace "your name"
with your name. Email me your solutions before the deadline.