Internet Programming I

Assignment 6

Introduction to Java

Due: October 24, 2006

 

Despite the names, Java and Javascript are different languages.  However, they have a number of things in common.  We will use Java to connect with a database and send queries to it.  But first we will look at a few simple programs.

 

1.       One of the first examples we had in Javascript displayed the current date and time on a web page.  The following program uses Java to display the date on a console screen.  Go to JCreator and type it in.

 

/* TodaysDate displays the current date on the console screen.  It uses the date

    stored in the computer on which the program is run.

*/

 

import java.io.*;

import java.util.*;

 

public class TodaysDate

{

       public static void main (String [] args)

       {

               GregorianCalendar today = new GregorianCalendar ();

               int day = today.get (Calendar.DATE);

               int month = today.get (Calendar.MONTH);

               month ++;

               int year = today.get (Calendar.YEAR);

               System.out.print ("Today's date is " + month + "/");

               System.out.print (day + "/");

               System.out.println (year + ".");

       }

} // TodaysDate

 

The following screen shows an example.

 

 
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Click on the icon with the red down arrow.  It is used to compile a program.  Compiling translates a Java program into Java bytecode.  This is an intermediate language that can be run on a number of different operating systems.

 

2.       It there are no syntax errors, click on the icon with the two blue circles.  This one is used to run your program.  It will open up a console window like the one below containing the output

 

 

 

 

 

 

 

 

 

3.       If there are syntax errors, you can correct them in the editor.  For example, suppose that you left off a semicolon on one of the lines.  After attempting to compile your program, you would see a box at the bottom with a description of the error.  You can double click on the line number to take you to the line where the compiler found the error.

 

4.       There are a number of things in this example that are different from Javascript.  The first thing is that the comments are enclosed by the symbols /* and /*, not the ones used in Html.  As in all programming languages, comments are there to help others understand the program and what it is to do.  They do not have any effect upon the way the program runs.

 

5.       The second thing is that in Java, certain packages have to be imported into the program.  These are packages of library routines needed by the program.  The package, java.io, contains code for input and output.  The package, java.util, is where the date and time methods can be found.

 

6.       Next all Java code is contained in a class.  This one is called TodaysDate.  We usually begin class names with upper case letters.  We can also use upper case to show where another word begins.  Spaces are not allowed in Java names, only letters, digits, and underscores.  The letters must always come first.

 

7.       Classes contain methods, the Java term for functions.  Programs that are to be viewed by the console have a method called main.  This is where execution of the program will begin.  We will also use classes that are accessed over the Internet.  Instead of having a main method, they have one whose name is listed in the Html form and recognized by the server.  The example program, however, is to be run on a local computer, and so it contains the standard code: public static void main (String [] args)

 

8.       Like functions in Javascript, all the code in a method must be included between pairs of curly braces, { and }.  But unlike Javascript, all lines of code must be terminated with a semicolon.  However, be careful, since lines naming methods, such as public static void main (String  [] args), do not end with semicolons.  Putting a semicolon there is an error, and a very difficult error to find.

 

9.       You may wonder why this program uses something as complicated as GregorianCalendar.  When Java was first written, it had a Date class that handled things like month, day and year.  But later this was changed because there really are a number of different calendars used throughout the world.  Now that Java is being used in many countries, the developers decided to make it more general.  So instead of just calling the class Date, as is done in Javascript, we have the longer, more descriptive name.  But as you can see, it is used very much the same way as in the earlier Javascript program.

 

10.   The lines starting with int day, int month, and int year all say that day, month and year are integer variables.  They name locations in memory that are large enough to store 32 bit integers.  Since this allows storage of numbers up to 2 billion, it is quite a bit more than needed, but for such a short program, it really makes little difference.

 

11.   The GregorianCalendar class in our example is called today.  This class contains a number of methods for accessing values stored in the class.  Accessor methods all begin with ‘get’ and are followed by the name of the value you want to get.  So we have today.get(Calendar.DATE), etc.  Java is case sensitive, so be careful of when you have to use upper and lower case. 

 

12.   The line, month++, says to add one to the variable month.  It is a short hand way to express month = month + 1.  This is a carry over from the C language and really is very convenient.

 

13.   Finally, the lines beginning with System.out.print and System.out.println all write something on the console screen.  The print statements display whatever is in the parentheses and then keep the cursor at the end.  The System.out.println statement displays the parentheses contents and then goes to the next line.  Anything in quotation marks is displayed exactly as is.  However, the variables listed display their values instead.  For example, System.out.println("Today's date is " + month + "/"); will display

               Today's date is 9/

while System.out.println("Today's date is month/"); will display

Today's date is month/

So be careful of where your quotation marks begin and end.  Also note that the pieces in the parentheses are joined using a plus (+) sign rather than a comma.

 

14.   Note that both curly braces are closed at the end and that the last one is followed by a comment with the name of the class.  This comment starts with // and tells the compiler to ignore everything following it up to the end of the line.  The next line will not be part of the comment.

 

15.   Next test the following program:

 

/* This program computes the area of a rectangle.*/

import java.io.*;

 

public class RectangleArea

{

       public static void main (String [] args)

       {

               int width = 25;

               int height = 30;

               int area = width * height;

               System.out.println ("The area of the rectangle is " + area);

       }

} // RectangleArea

 

16.   Change the program so that is does some other computation.  Again use fixed values for your data.  In the future we will get data from Html forms.  But for now just put numbers into your program.  If you want to compute something that involves fractions, such as dollars and cents, your line should read something like

double cost = 25.95;

 

17.   After you have tested this program in JCreator, make a program that will just display your name, email address, and telephone number.  These are all strings, so your variables will be similar to

String name = "Alice Lee";

String email = "alee@aol.com";

String phone = "212-123-4567";

 

18.   When you are finished, zip up all your programs in one archive and send them to me.