Download CIS 120 Lab 1 Fall 2007

yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
CIS 120-101 / Overstreet / Fall 2007
Lab 1 - Due Week 1/Class Meeting 2 (Wed 8/22/07)
(1) Make sure students have a laptop (they should bring it with them to the lab).
(2) Make sure students have JDK and JGrasp loaded onto their laptops.
(3) Have students create a “java” directory. Suggest: C:\java!
(4) Make sure students know how invoke Jgrasp.
(5) Make sure students know how to type in program, make editing changes, and
compile/edit program.
Two programs will be used to do this.
(A) traditional Hello World that uses System.out.print and println.
(B) A program with a JOptionPane (input of a number) and output.
Program A: call this one “” when you save it. (Note: do NOT type in the line
numbers shown below when you type in the program. They are added here only for discussion
1. //
2. //
3. //
4. //
5. //
6. //
7. //
8. //
9. //
Student Number
Date Submitted
11. //
12. //
Your Name here.
Your student number here.
August ?, 2007
My name here.
CIS 120 (time of class here)
Lab 1.
fill in...
fill in...
fill in...
Imported Packages/Classes
None for this program.
13. public class HelloWorld {
public static void main (String[] args) {
System.out.print("Hello ");
} // main
18. } // class: HelloWorld
Program B: call this one “” when you save it.
1. //
2. //
3. //
4. //
5. //
6. //
7. //
8. //
9. //
Student Number
Date Submitted
Your name here.
Your student number here.
August ?, 2007.
My name here.
CIS 120 (Time of day here)
Lab 1 (second program).
fill in...
fill in...
fill in...
11. // Imported Packages/Classes
12. import javax.swing.JOptionPane;
13. public class EasyMath {
public static void main (String[] args) {
// Local Variables :
String userInput;
number1, number2;
String output;
// Ask user
userInput =
userInput =
// Produce the output string.
output = "Sum of two integers is: " + (number1+number2) + "\n";
output += "Product of two integers is: " + (number1*number2) + "\n";
// Open a window and show the results.
JOptionPane.showMessageDialog(null, output, "Easy Math",
for his inputs and convert them to integers.
JOptionPane.showInputDialog("Input the first integer: ");
Integer.parseInt(userInput); // Convert String to int.
JOptionPane.showInputDialog("Input the second integer: ");
System.exit(0); // End the program.
} // main
32. } // EasyMath
CIS 120 Lab 1 Notes
These notes and comments apply to the two programs, A and B, that you entered as part of CIS
120 Lab 1. Please refer to those handouts as you read over these notes.
A comment is a piece of English text which we add to our program strictly to document
it. The purpose can be to add identification data to the program or to explain what the code in
the program is doing. The intent of a comment is to aid its readability by a human reader. The
java compiler (javac) totally ignores any comments that you add to your program.
Virtually all high level programming languages support the addition of comments to
programs that we write. In Java there are two kind of comments: (a) to-the-end-of-the-line
comments and (b) multi-line comments. Comments that go to the end of the current line start
with a “//” and end at the end of that same line. Two examples of such a comment:
Example 1: // Author: CIS 120 Faculty
Example 2: totalCost = cost + tax; // Update cost to reflect taxes.
In the second example we see that the comment can follow a piece of Java code.
Mulit-line comments start with “/*” and end with “*/”. We may have one-line comments
embedded within a multi-line comment. Example:
Author: CIS 120 Faculty
Date: ….
Note: programs A and B have only used the one-line comments. The programs A and B would
have run the exact same way if we had removed ALL of our comments.
Java code consists of class definitions that we save as file. The basic structure
of a Java class definition is:
public class OurClassName {
Code goes here…
} // OurClassName
The details of this basic structure will be explained more fully later. These lines can be seen in
programs A (lines13 and 18) and B (lines 13 and 32). The words “public” and “class” are java
keywords. As we shall see later the “public” in these programs was not really required. The
public-keyword controls who can access the code in the class definition. Each class definition
has a name that we make up. Above it is shown as “OurClassName” and in programs A and B it
is “HelloWorld” and “EasyMath”. Whatever name we give to our class we also use as the name
of the file when we save our class definition. As a result, the code for program A needs to be
saved as “” and the code for program B needs to be saved as “” –
with capitalization to match the class name used in the class definition (i.e do not save it as
“”!) The name for the class is a name which we make up, but there are
programming conventions that dictate that we “should” make up a name where each word in
the name is capitalized – thus “HelloWorld” not “helloWorld”, nor “hello_world”. Programming
conventions are “rules” that we as java programmers “agree” to follow to improve the readability
of our code by other java programmers. Bottom line, the javac compiler will compile our code
correctly whether we follow those conventions or not. As we move forward learning java we
will be exposed to more of these “programming conventions”. These conventions include
“rules” that suggest how we capitalize the names we use (for class names, method names,
variables, etc) and will suggest a style of indentation that will make our code “easier on the
Some class definitions allow us to use the class as a “program” – meaning to invoke it (maybe
from command line) and have some of its code executed on our behalf. However, some other
class definitions are written to be used as the provider of some kind of abstraction or service and
those classes do not offer themselves to be executed as a program. So… how can we tell which
can be run as a program and which cannot? Answer: if the class definition includes a method (a
chunk of code) whose name is “main” and whose structure looks like:
public static void main (String[] arguments) {
Code for main method goes in here.
} // main
then the class can be run as a program and when we do run it as a program we are in fact
invoking the main method from within that class definition. Class definitions can in fact include
many methods as we shall see later. In lab 1 both programs A and B can be used as programs
because both contain a main-method. In program A the main-method starts on line 14 and runs
to line 17. You note, by programming convention that the main method within the class
definition has its code “indented” to help us to spot the beginning and ending of that method
definition. In program B, the main-method starts on line 14 and runs to line 31. Thus in JGrasp
when we click on the “Run” icon we are in fact asking the system to run the main-method within
the class definition that we are currently editing. (Note: we must click on the “Compile” icon –
red plus, to successfully compile our class definition prior to being able to run it).
A package is a collection of “related” class definitions. The java programming system
gives us an enormous number of these packages (pre-written and tested) for use in our programs!
Thank you JDK!!! We express that we are going to be using one of these external classes or
packages by using an import statement in our program (Note: Program A does not import any
classes or packages, while Program B (on line 12) imports a class called
“javax.swing.JOptionPane”. The truth is that program A (and ALL programs for that matter)
does import one package called “java.lang”. The classes in java.lang are so widely used that
Java allows that one package to be imported without having to say so. But, any other
class/package that we wanted to bring into our program would have to be explicitly mentioned in
an import statement (more on this later!)
Comments specific to program A:
(1) Comments on lines 11 and12 say no packages imported… but in fact java.lang is
(2) “System” – mentioned in lines 15 and 16 is a class made available to us from the
java.lang package. “System.out” is an object that corresponds to the standard output
window. “System.out.print(…)” and “System.out.println(…)” are methods that we are
asking the System.out object to perform for us. Namely to print some text. In line 15,
with “print” we do not go to the start of the next line after printing desired text. With
“println” (line 16) we print the desired text and then move to the start of the next line.
Comments specific to program B:
(1) Line 12 tells the Java compiler (javac) that this program will be importing the class called
JOptionPane (which is in the package called javax.swing). Note that one can import ALL
of the classes of a given package by using the “.*” notation after the package name. For
example, line 12 could have been written as: import javax.swing.*; This wold have
caused all of the classes in javax.swing to be imported not just JOptionPane. This does
raise the question of does this “bloat” our code by importing classes that we do not really
need… and the answer is NO. Even if you import all of the classes of a given package,
javac is smarter enough to only bring in the class definitions for those we actually use.
As a result Java programmers often get a bit “lazy” in their typing and use the “.*”
instead of typing in the desired class name just because it is less typing.
(2) Lines 16 to 18 declare 3 local variables (i.e. local t the main-method) that are used to hold
values of different kinds. “userInput” and “output” will hold textual data and thus their
type is noted as “String”. “number1” and “number2” will hold numeric data (integers in
particular) so their type is declared to be of type “int”.
(3) Observe that all 4 of these variable names are spelled with a lower case letter for the first
word and a capital letter for the first letter of each subsequent word (e.g. “userInput”).
This clearly differs from the capitalization rule which we use for class names, namely
that each word starts with a capital letter. Again, a “rule” such as this is a programming
convention followed by programmers to improve code readability (and is NOT enforced
by the compiler).
(4) Consistent with the above point, method names follow the same capitalization rules as
those used for variables (which is why the main-method “main” is spelled with a lower
case letter). Another possible name for a method might be “computeCost”.
(5) In line 20, we invoke the method “showInputDialog” from the class called JOptionPane –
note that the capitalization of these two names is consistent with the coding conventions
already mentioned. This method call results in a String being returned and that String is
then stored into the variable called “userInput” (a slight lie… but it will do for now).
(6) In line 21, we invoke the method called “parseInt” from the class called “Integer” (more
on that later!). We invoke this method passing it the userInput value to work on. This
method returns an int-value that we then store into the variable called “number1”.
(7) In lines 25 and 26 we are “building” the String called “output” that we intend to display
at the end of this program. In line 25 the text “Sum of two integers is: “ is concatenated
to the computed value of number1 added to number2. To this result we concatenate a
“\n”. So what the heck is a “\n”?? Answer: it is a string which we can put into another
string where we want a line break to be! So where you see the “\n” being used, think
“next line”. In line 26 we are adding even more text to the output String. The “+=”
operator means append it to itself… so output += “More text”; is really like saying :
output = output + “More text”;
(8) In line 28 we are invoking the “showMessageDialog” method in the JOptionPane class.
The details of the 4 things we pass to that method will be discussed later. Note, again,
that our names are still staying consistent with the capitalization rules of our
programming convention! Capitalization Rules:
a. class names are spelled with each work starting with a capital letter. Examples:
“HelloWorld” and “EasyMath”.
b. method names are spelled with a lower case letter for the first word and each
subsequent word is capitalized. Examples: “main” and “showMessageDialog”.
c. variable names are spelled with a lowercase letter for the first word and each
subsequent word is capitalized. Examples: “userInput” and “output”.