Docsity

Problem Solving and Programming Concept - Programming Lab 1 | CIS 120, Lab Reports of Computer Science

Toggle sidebar
bg1
CIS 120-101 / Overstreet / Fall 2007
Lab 1 - Due Week 1/Class Meeting 2 (Wed 8/22/07)
Objectives:
(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 “HelloWorld.java” 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
purposes).
1. // Author : Your Name here.
2. // Student Number : Your student number here.
3. // Date Submitted : August ?, 2007
4. // File : HelloWorld.java
5. // Instructor : My name here.
6. // Class : CIS 120 (time of class here)
7. // Identification : Lab 1.
8. // Purpose : fill in...
9. // fill in...
10.// fill in...
11. // Imported Packages/Classes
12. // None for this program.
13. public class HelloWorld {
14. public static void main (String[] args) {
15. System.out.print("Hello ");
16. System.out.println("World");
17. } // main
18. } // class: HelloWorld
pf3
pf4
pf5

Partial preview of the text

Download Problem Solving and Programming Concept - Programming Lab 1 | CIS 120 and more Lab Reports Computer Science in PDF only on Docsity!

CIS 120-101 / Overstreet / Fall 2007

Lab 1 - Due Week 1/Class Meeting 2 (Wed 8/22/07)

Objectives:

(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 “HelloWorld.java” 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

purposes).

  1. // Author : Your Name here.
  2. // Student Number : Your student number here.
  3. // Date Submitted : August ?, 2007
  4. // File : HelloWorld.java
  5. // Instructor : My name here.
  6. // Class : CIS 120 (time of class here)
  7. // Identification : Lab 1.
  8. // Purpose : fill in...
  9. // fill in... 10.// fill in...
  10. // Imported Packages/Classes
  11. // None for this program.
  12. public class HelloWorld {
  13. public static void main (String[] args) {
  14. System.out.print("Hello ");
  15. System.out.println("World");
  16. } // main
  17. } // class: HelloWorld

Program B : call this one “EasyMath.java” when you save it.

  1. // Author : Your name here.
  2. // Student Number : Your student number here.
  3. // Date Submitted : August ?, 2007.
  4. // File : EasyMath.java
  5. // Instructor : My name here.
  6. // Class : CIS 120 (Time of day here)
  7. // Identification : Lab 1 (second program).
  8. // Purpose : fill in...
  9. // fill in... 10.// fill in...
  10. // Imported Packages/Classes
  11. import javax.swing.JOptionPane;
  12. public class EasyMath {
  13. public static void main (String[] args) {
  14. // Local Variables :
  15. String userInput;
  16. int number1, number2;
  17. String output;
  18. // Ask user for his inputs and convert them to integers.
  19. userInput = JOptionPane.showInputDialog("Input the first integer: ");
  20. number1 = Integer.parseInt(userInput); // Convert String to int.
  21. userInput = JOptionPane.showInputDialog("Input the second integer: ");
  22. number2 = Integer.parseInt(userInput);
  23. // Produce the output string.
  24. output = "Sum of two integers is: " + (number1+number2) + "\n";
  25. output += "Product of two integers is: " + (number1*number2) + "\n";
  26. // Open a window and show the results.
  27. JOptionPane.showMessageDialog(null, output, "Easy Math",
  28. JOptionPane.PLAIN_MESSAGE);
  29. System.exit(0); // End the program.
  30. } // main
  31. } // EasyMath

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

eyes”.

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

imported!

(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”;