Extra Credit Practice Exercise # 1

Extra Credit Practice Exercise # 1

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

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    18 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us