CS335
Fall, 2008
Solution Set of Homework 1 (20 points)
Due: September 9, 2008 (Tuesday)
************************************************************************
1. Describe in a few sentences the steps that are necessary to compile
and run a Java applet and a Java application (stand-alone program).
(2 pts)
SOL.
Both applets and standalone applications are compiled with the "javac"
command, such as "javac file_name.java". This produces a bytecode file
called file_name.class. The bytecode is executed with the "java" command
for a standalone application. For an applet, create an HTML file with
'< applet >' tags and include the class file, then use either a web browser
or the "appletviewer" to view the applet. The command lines are as
follows:
For a Java application, use
javac file_name.java
java file_name
For a Java applet, use
javac applet_name.java
appletviewer applet_name.html
2. Define each of these terms: applet, API, UVM, class.
(4 pts)
SOL.
Applet - Java code designed to run in a World Wide Web browser
API - Application Program Interface
JVM - Java Virtual Machine
class - object defintion, includes data and functions
3. Write a Java application to output the following message in the
standard output window. (7 pts)
* * * * * * * * * * * * * * * * * * * *
* * This is my first Java program!
* * * * * * * * * * * * * * * * * * * *
SOL.
import java.io.*;
public class CS335_HW1_3 {
public static void main (String args[]) throws IOException {
System.out.println("* * * * * * * * * * * * * * * * * * * *");
System.out.println("");
System.out.println("* * This is my first Java program!");
System.out.println("");
System.out.println("* * * * * * * * * * * * * * * * * * * *");
}
}
4. Write a Java applet that shows the above message in a web page.
(7 pts)
SOL.
import java.applet.Applet;
import java.awt.Graphics;
public class CS335_HW1_4 extends Applet {
public void paint (Graphics g) {
g.drawString("* * * * * * * * * * * * * * * * * * * *", 25, 30);
g.drawString("* * This is my first Java proram!", 25, 50);
g.drawString("* * * * * * * * * * * * * * * * * * * *", 25, 70);
}
}
< HTML >
< BODY >
< APPLET CODE=CS335_HW1_4.class WIDTH=200 HEIGHT=100 >
< /APPLET >
</BODY >
</HTML >
************************************************************************
Put you solution set in a text file with the following header and
email that text file before midnight of the due day to me
("cheng@cs.uky.edu").
CS335 (Fall 2008)
Solution of Homework 1
Due: 9/9/08
Name: xxxxxx xxxxxx
The answer to Problem 3 (and 4 as well) should be the exact content of
a .java file so we can easily edit it and test it.