Download Lab0

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

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

Document related concepts
no text concepts found
Transcript
CS 115
LAB 0
THIS LAB CARRIES NO CREDIT POINTS
The purpose of this exercise is to:
1. Familiarize you with using the text Editor Notepad++ to write a very
basic Java program.
2. How to upload an assignment on Blackboard.

FOLLOW STEP BY STEP THE INSTRUCTIONS BELOW AND IMPLEMENT THE
SAMPLE PROGRAM DISCUSSED.

MUST DEMOSTRATE IT TO THE TA BEFORE SUBMISSION ON BLACKBOARD.

MUST BE SUBMITTED AND TIMESTAMPED BY BLACKBOARD NO LATER THAN
THE TIME THE CLASS IS OVER.

DO NOT HESITATE TO HELP THE STUDENT NEXT TO YOU IF YOU KNOW THE
ANSWER. YOU ARE ALLOWED TO DO THATDURING THE EXTRA CREDIT
EXERCISES.
-----------------------------------------------------------------------------------------------------------------To create a java program we will need:
1
1. A text editor program (application). The one we are going to use is called
Notepad++ . The text editor is where we write the program using java keywords
and symbols.
2. The java compiler (available on the computer station).
3. The java interpreter (available on the computer station)
TASK: WRITE A BASIC JAVA PROGRM THAT DISPLAYS THE MESSGE “This is a Java
program”.
STEP 1: Open the text editor program called Notepad++ by clicking on the icon representing
the program on your Desktop. From the menu toolbar of Notepad++ choose
FILE->New
STEP 2:
Let us type the program (java instructions) shown below in italics:
Note: In typing the program I would like you to follow the indents as shown. Advance the
tabs to maintain the position of the curly brackets and the typing of the programming
instructions as shown.
In addition to the programming instructions, we will add some comments.
Comments are notes that we make that are not part of the programming instructions.
These notes could be the: date the program was written, the name of the person who
wrote it, or explanatory notes about the program. The compiler disregards those
2
comments if they are preceded with the two slashes. Make sure that you type the
comments as shown preceded by //)
BEFORE YOU TYPE THE PROGRAM CREATE ON THE DESKTOP A NEW FOLDER AND
NAME IT extracredit1. NOTE: THIS DOCUMENT SHOULD ALSO BE SAVED IN THE
SAME
FOLDER
BY
DOWNLOADING
IT
FROM
THE
WEB
SITE
:
http://www.cs.iit.edu/~cs201/lab0.html
ON THE NOTEPAD++ SCREEN TYPE THE FOLLOWING PROGRAM EXACTLY AS SHOWN
BELOW (DO NOT CUT AND PASTE!!):
public class MyFirstProgram
{
//double slashes are used for comments
//anything after the double slashes is disregarded by the compiler
//the name of the class is: MyFirstProgram
public static void main (String[] args)
{
System.out.println(“This is a Java program”);
}
}
MAKE SURE THAT YOU USED THE CURLY BRACKETS AFTER THE NAME OF THE CLASS
AND AT THE END OF THE PROGRAM. SOLID PARENTHESIS AFTER main.
3
MAKE SURE ALSO THAT INSIDE THE PARENTHESIS (AFTER main) THE BRACKETS
AFTER THE KEYWORD String ARE SOLID BRACKETS.
Definitions:
The name after the keyword class (MyFirstProgram in this example) is called the
class name.
The file that you create by typing the above code is called the source code file.
The file has to be saved with the name: the class name that you provided followed
by the extension .java
In this example the source code file will be saved as: MyFirstProgram.java
STEP 3: Now, we are ready to save the program in a file WHICH NEEDS TO BE IN THE
FOLDER lab0. Choose from the Notepad++ menu File->SaveAs
MAKE SURE THAT THE PROPER DIRECTORY (FOLDER) IS SHOWN WHERE YOU WANT TO
SAVE THE SOURCE CODE FILE. YOU WANT TO SAVE THE SOURCE CODE FILE YOU TYPED IN
THE FOLDER extracredit1.
NAME THE FILE (REFERRED TO AS THE SOURCE CODE FILE) THAT YOU CREATED USING THE
SAME NAME AS THE NAME OF THE CLASS , AND THEN ADD THE FILE EXTENSION .java AFTER
THE NAME.
i.e. MyFirstPorgram.java for our program that we are working on.
It should be typed using the same capitalization as the name of the class in the source code
file. In the example picture below the program is saved directly on the Desktop (that is not
what you want, you want to save it in the folder you created:lab0).
4
CLICK save.
STEP 4: IN WINDOWS EXPLORER (OR THE DESKTOP) GO TO THE FOLDER lab0 AND VERIFY THAT
THE FILE MyFirstProgram.java IS THERE INSIDE THE FOLDER.
Definitions:
The next step will create the code that the computer can understand with the help of the java
compiler. The file created when the compiler tool compiles the source code file is called the
“compiled file” or “the .class file” or “the bytecodes file”.
5
The compiler is a software tool that we will use to compile the source code file in order to create
the bytecodes (compiled) file. The compiler will save the bytecodes file in the same folder as the
source code file using the same name as the source code file except that it changes the
extension from .java to .class (i.e. MyFirstProgram.class will be the new file created by the
compiler).
The compile will not produce the bytecodes file (MtFirstProgram.class in our example) unless
everything is correct. If there is a mistake (a misspelling or the wrong symbol etc), the compiler
will produce error messages at the bottom of the Notepad++ screen. An error message will
identify the line by number where the compiler found an error as well as some message about
the error.
STEP 5: WE ARE GOING TO USE THE JAVA COMPILER TO COMPILE THE SOURCE CODE FILE WE
CREATED. THERE ARE A COUPLE OF DIFFERENT WAYS WE CAN DO IT.
1.
WE COULD DO IT DIRECTLY FROM Notepad++ TOOL BAR (AT THE TOP OF THE SCREEN) BY
GOING TO “MACRO” MENU AND CHOOSING THE “COMPILER JAVA” MENU ITEM. .
2.
WE COULD DO IT BY OPENING A DOS WINDOW AND TYPING THE PROPER COMMAND. THIS
IS CALLED COMMAND LINE. THE OUTPUT WILL APPEAR ON THE DOS WINDOW (SOMETIMES
REFERRED TO AS THE DOS PANE).
WE WILL COMPILE BY USING METHOD 1 FIRST:
With your source code file open in Notepad++ go to the tool bar and choose macro->compile
java. As you have noticed the Notepad++ screen is split into two parts. The bottom part displays
6
the result of the compilation process. If everything was typed correctly the bottom part will
display the message shown in the picture below.
IN THIS PICTURE THE COMPILER INDICATES THAT EVRYTHING WAS O.K. WITH OUR PROGRAM
(Process finished exit(0)).
STEP 6: IN THE NEXT PICTURE A MISTAKE WAS ON PUPRPSE MADE IN ORDER TO DEMONSTRATE
WHAT THE COMPILE WILL SHOW AT TH EBOTTOM OF THE SCREEN (THE KEYWORD class was
misspelled as clas).
7
SIMULATE A MISTAKE IN THE PROGRAM AND RECOMPILE. OBSERVE THE MESSAGE AT THE
BTTOM OF THE SCREEN.
STEP 7: SECOND METHOD OF COMPILING BY USING COMMAND LINE DOS COMMANDS
FROM Start Menu-> All Programs CHOOSE Assessories->Command Prompt
A BLACK SCREEN WINDOW WILL APPEAR ON THE SCREEN WHICH CALLED DOS WINDOW. THE
PICTURE BELOW IS A DOS WINDOW.
8
NOTICE THAT THE SCREEN SHOWS A PATH ON YOUR COMPUTER LIKE:
C:\Users/George> _
OR IT MAY BE A DIFFERENT PATH DEPENDING ON YOUR USER ACCOUNT. WE NEED TO
NAVIGATE TO WHERE THE FOLDER WITH OUR FILE MyFirstProgram.java IS LOCATED. WE CAN
MOVE UP OR DOWN THE DIRECTORY PATH BY USING DOS COMMANDS.
i.e. C\User/George> cd..

TYPING cd FOLLOWED BY TWO DOTS AND PRESSING Enter KEY, MOVES THE CURSOR TO
THE LOWER DIRECTORY.
C\User>
9

TYPING THE SAME AGAIN AND PRESSING THE Enter KEY, MOVES THE CURSOR DOWN
ONE MORE DIRECTORY
C\User>cd..
NOW THE CURSOR IS AT C:\>_
a) THE COMMAND cd FOLLOWED BY SPACE AND THEN THE NAME OF THE DIRECTORY
(FOLDER) THAT WE WANT MOVES THE CURSOR TO THAT DIRECTORY.
C:\>cd PRACTICEEX1
PRESS Enter KEY.
RESULTS IN
C:\PRACTICEEXE1>_
b) TYPE THE PATH TO YOUR DESKTOP FROM USER PATH AND THEN TO YOUR
FOLDER extracredit1 . ASK THE TA FOR HELP IF YOU DON’T UNDERSTAND THE
ISNTRUCTIONS.
c) THE COMMAND dir WILL DISPLAY THE FOLDERS AND FILES IN A SPECIFIC PATH
I.E
C:/users/myloginname/Desktop/extracredit1>dir
SHOWS ON THE DOS WINDOWS ALL THE FILES IN FOLDER extracredit1 (note that
the [path could be different in your computer). YOU SHOULD TRY TO VERIFY
ONCE AGAIN THAT THE FILE MyFirstProgram.java IS THERE BY TYPING THE DOS
COMMAND dir.
NOW WE ARE READY TO USE THE DOS COMMAND THAT WILL ALLOW US TO
COMPILE OUR SOURCE CODE.
10
(YOU SHOULD DELETE THE COMPILED FILE FROM THE PREVIUOS STEP 5 ATTEMPT
BEFORE YOU RE COMPILE IN THIS STEP).
TYPE THE COMMAND AS FOLLOWS:
C:/somepath/Extracredit1>javac MyFirstProgram.java
NOTICE THAT THERE IS SPACE AFTER javac (which is the command for the java
compiler)
PRESS Enter KEY.
THE FILE MyFirstProgram.class IS CREATED . THIS FILE IS CALLED THE BYTECODES FILE.
d) TYPING THE DOS COMMAND dir WILL DISPLAY ALL FILES AGAIN. YOU SHOULD BE ABLE
TO VERIFY THAT THIS NEW FILE WAS CREATED.
e) HERE IS AN EXAMPLE SCREEN THAT SHOWS HOW I GOTTO THE DESKTOP ON MY
LAPTOP (the path may be a little different in your computer):
C:\Users\George>cd Desktop
C:\Users\George\Desktop>cd Practice1
C:\Users\George\Desktop\Practice1>
Typing the DOS command dir after the above prompt will display all the file residing in
folder Practice1 (that is the file we created and saved there under the name
MyFirstProgram.java)
11
STEP 7:
Definitions:
A java program is executed ( OR OTHER TERMS FOR THE SAME THING ARE : RUN OR
INTERPRETED) by using the java interpreter software tool. The java interpreter
translates the bydecode symbols in the bytecodes file into machine instructions that
the computer understands.
We can interpret the program we wrote from the Notepad++ toolbar by clicking on
MACRO-> java run MENU ITEM. THE OUTPUT WILL APPEAR ON THE BOOT PART OF
THE NOTEPAD++ SCREEN.
Observe the output by following the is method of running the program.
STEP 8: Let us now interpret (execute) the program we created by using the DOS
window as an alternative way. In the same DOS window as before type the
command to call the java interpreter:
….extracredit1>java MyFirstProgram
NOTICE THAT THE COMMAND IS java (not javac) THEN SPACE AND THEN THE NAME OF THE
BYTECODES FILE WITHOUT THE EXTENSION .class.
THE OUTPUT OF THE PROGRAM SHOULD APPEAR ON THE DOS WINDOW.
THE PICTURE BELOW ILLUSTRATES THE PROCESS DESCRIBED. THE DIRECTORY PATHS IN YOUR
CASE MAYBE DIFFERENT THAN THE PICTURE.
12
COMPILER GENERATED ERROR
OUTPUT OF PROGRAM
BECAUSE THE .java EXTENSION WAS NOT
TYPED AFTER THE NAME OF THE SOURCE
CODE FILE
13
STEP 7: NEXT I WOULD LIKE YOU TO UPLOAD THIS EXTRA CREDIT PRACTICE EXERCISE ON
BLACKBOARD. BEFORE YOU DO THIS STEP YOU MUST SHOW TO YOUR TA OR THE
INSTRUCTOR THAT YOU COMPLETED THE WORK CORRECTLY.
THE TA OR THE
INSTRUCTOR MUST APPROVE IT FIRST BEFORE YOU CAN UPLOAD YOUR WORK ON
BLACKBOARD.
a) Zip the two files MyFirstProgram.java and MyFirstProgram.class as well as
this WORD document with the answers to the above questions, into a zipped
file named yourfirstname_yourlastnam_PracticeEx1.zip
Ask myself or your TA assigned to you for help IF YOU DON’T KNOW HO WTO
ZIP GROUP OF FILES.
b) Open your Blackboard account for this class and upload the zipped file on
Blackboard under the assignment folder Extra Credit # 1 on Blackboard.
PLEASE MAKE SURE THAT YOU UPLOADED THE FILE ON THE CORRECT FOLDER> TAKE
YOUR TIME/ASK FOR HELP.
NOTE: YOU NEED TO CLICK ON SUBMIT BUTTON IN YOUR BLACKBOARD SCREEN.
STOP BY THE TA TO VERIFY CORRECT SUBMISSION OF THE FILE.
THIS PROCESS WILL BE REPAETED A NUMBER OF TIMES THROUGHOUT THE SEMESTER
IN DOING SUBMISSIONS. IT HAS TO BE DONE CORRECTLY OTHERWISE WE CAN”T
RETRIEVE YOUR FILES FOR GRADING!
STEP 8: FINALLY, I WOULD LIKE YOU TO REPEAT THE ABOVE PROCESS (STEPS 1-6 BUT
NOT THE SUBMISSION STEP) AT YOUR OWN TIME AFTER THE CLASS IS OVER, IN YOUR
14
OWN LAPTOP (OR DESKTOP) PROVIDED THAT YOU HAVE INSTALLED THE JDK IN YORU
LAPTOP (see Lecture2_2 for installation instructions OR SEE ME IF YOU NEED HELP
INSTLLING IT).
QUESTIONS SECTION: AFTER COMPLITIG THE ABOVE 8 STEPS SHOW YOUR WORK TO THE
TA AND RECEIVE APPROVAL TO PROCEED WITH THE REST OF THE EXERCISE:
NOW, ANSWER THE FOLLOWING QUESTIONS BY TYPING THE ANSWERS ON THIS DOCUMENT.
(NOTE: IT IS ASSUMED THAT YOU HAVE DOWNLOADED AND SAVED THE DOCUMENT IN YOUR
FOLDER: extracredit1)
1.
A Java program (source code file) is saved in a file named:
a) java.myprogram always.
b) The same name that you gave to the class in the source code, followed by the
extension .java
c) Any name that you want to save the source code file followed by the extension
.java
d) Just the name of the class that you typed in the source code without any
extension.
Answer: _____________________
2. In order to execute a java program
a) The java interpeter tool has to be used on the bytecodes file
b) The compiler tool has to be used on the bytecodes file.
15
c) The compiler interprets the source code file and executes it.
d) The interpreter is used on the file that has the .java extension.
Answer: ________________
3. Answer the following exercises from your text Chapter 1 (pages 35 – 37)
exercises 1:_________________
exercise 2:_____________________
exercise 3:________________________
exercise 9:____________________________
exercise 11:____________________________
exercise 13:___________________________
exercise 14:_______________________________
THE SUBMISSION IS VALID IF IT IS DONE BY TEHE ND OF THE CLASS PERIOD!!
SUBMISSION INSTRCUTIONS: AFTER THE TA CHECKS THE ACCUARCY OF YORU ANSWERS TO THE
ABOVE QUESTIONS DO THE FOLLOWING:
1.
CHECK O MAK ESURE THATHE FOLLOWING FILES ARE INSIDE YORU FOLDER extracredit1:
MyFirstProgram.java
MyFirstProgram.class
Practice1.doc (this document with the answers to the questions).
2.
ZIP (COMPRESS) THE FOLDER extracredit1. RIGHT CLICK ANDCHOOSE COMRESS USING .zip
CHOICE. THE RESULT WILL BE A NEW FILE NAMED extracredit.zip ON YOUR DESKTOP.
16
3.
OPEN BLACKBOARD AND GO TO YOUR STUDENT ACCOUNT. SUBMIT THE FILE
extracredit1.zip ON YOUR BLACKBOARD ASSIGNMENTS SCREEN BY CHOOSING THE
ASSIGNMENT “EXTRA CREDIT # 1”. ASK FOR HELP IF YOU NEED IT.
Copyright: Fall 2016 George Koutsogiannakis, IIT.
17