School of Sciences Department of Computer Science and Engineering Programming Principles 1 (CSC131) & 2 (CSC132) Software usage guide WHAT SOFTWARE AM I GOING TO NEED/USE?... 3 WHERE DO I FIND THE SOFTWARE?... 4 HOW DO I INSTALL THE SOFTWARE?... 5 DO I NEED TO CONFIGURE ANYTHING?... 6 HOW DO I USE SCITE FOR MY JAVA PROGRAMS... 8 WHEN THINGS GO WRONG... 9 APPENDIX A - PROGRAMMING TIPS... 10 Andreas Grondoudis for DCSE
Page has been left blank intentionally 2 Last updated by Andreas Grondoudis on: Thursday, 02 February 2012
What software am I going to need/use? The following assumes that you already have a Microsoft Windows operation system already installed. There are two pieces of software that you will require. The listing below explains 1. You will need the Java Development Kit (or JDK) version 7 (and later). JDK is a free kit containing a collection of tools needed to compile and run your programs. Please note you want the JDK not the JRE 2. You will use a program editor called SciTE. This is a free versatile text editor enabling the creation of java files for the course. I think that you should also download and install 3. The Java Documentation. [Optional but recommended (especially for CSC132)] It is a set of HTML pages that are the specification of the language. Java contains literally thousands of methods, classes, tools and things that you can use. It is impossible to remember all of them by heart. The documentation is a point of reference for everything that is included in the kit. The documentation is an invaluable source of information. Frequently Asked Questions Q: Do I have a choice in any of this? A: Not really when it comes to 1 and 3. With the editor you can choose to use something else but please note: - this document focuses on assistance with SciTE - you are not allowed to use any application that automatically generates code Last updated by Andreas Grondoudis on: Thursday, 02 February 2012 3
Where do I find the software? No need to do anything if you have decided to work in the university s labs. If you are working at home (or on your portable) then download the software from the relevant websites. Software: Java SE 7 Download: http://www.oracle.com/technetwork/java/javase/downloads Directions: Scroll down a little bit and look for Java SE 7 and click the JDK download button. Select the correct version for your platform Software: Download Directions: Software: Download: Directions: Java SE 7 Documentation http://www.oracle.com/technetwork/java/javase/downloads Scroll down on the page and look for Java SE 7 Documentation and click the download button WSciTE 2.28 or later http://scintilla.sourceforge.net/scitedownload.html Locate the Windows section and find the full download link Frequently Asked Questions Q: What size are the downloads? A: At last writing: JDK 80MB, JavaDOC 58MB and SciTE 1MB Q: What if I use Linux or Win64? A: The JDK comes in a various versions including Solaris, Linux and Windows. Just select the appropriate platform when you download. Please note this guide focuses on Windows based development. 4 Last updated by Andreas Grondoudis on: Thursday, 02 February 2012
How do I install the software? 1. You can install the JDK by simply starting up the file you downloaded. I suggest that you change the installation directory to: C:\Program Files\Java. In a couple of clicks you should have everything ready. 2. The documentation is a.zip file that needs to be extracted in your JDK installation folder (as above). When extracted you should have a new folder C:\Program Files\Java\docs 3. The editor. SciTE does not come with an installation wizard. All you get is a.zip folder containing all the required files to start using the program. Extract the archive and you should get a folder called <wscite>. Inside this folder you will find a <SciTE.exe>. This is the editor; create a shortcut to it on your desktop and you are finished Frequently Asked Questions Q: What is a ZIP file and how to I use it? A: A zip file is an archive that contains one or more other files compressed together to economise in space. Windows XP (and later) has inbuilt support for.zip files; just right-click on a zip file to see the available commands. Earlier version of windows need archive software 1 installed. Q: What happens if I want to install (say the Java SE) in another location? A: That is fine as long as you know where everything is going so that you know where to extract the documentation and later on configure SciTE 1 http://www.7zip.com/ provides a comparison and download links Last updated by Andreas Grondoudis on: Thursday, 02 February 2012 5
Do I need to configure anything? Nothing is required for Java SE and the documentation (provided you extracted it ok). After installation and extraction everything is in place and you can use the tools. SciTE needs to be configured. You need to let SciTE know where is the Java SE installed so it can find the compiler and interpreter tools. Don t worry this is fairly straightforward. All you are required to do is type some information in a file. Just follow the steps below and you should be done in no time at all. 1. Make a note of the location that you installed Java SE. Say for example if you installed in c:\program files\java. This is your root-jdk directory. 2. Run SciTE. 3. From the menu select <Options> and then <Open cpp.properties> to open the file. 4. From the menu select <Search> and then <Find >. In the dialog that comes up type the following (exactly as you see it below) command.compile.*.java 5. Click on <Find Next> and your SciTE will look through the document to locate the phrase. It should look something like 6. Here is the only editing you are required to do. a. Edit the 1 st and 3 rd lines and b. just after the = sign enter your root-jdk directory followed by \bin\. c. Remember you are supposed to use YOUR root-jdk directory. In my example the root-jdk directory is C:\program files\java\ d. So I had to insert the following just after the 1 st and 3 rd equal signs C:\program files\java\bin\ e. Then the cpp.properties file (while being edited in SciTE) should look like 7. Don't edit anything else; save and close cpp.properties. 8. That is it. SciTE is configured and now knows where the Java SE tools are located. 6 Last updated by Andreas Grondoudis on: Thursday, 02 February 2012
Frequently Asked Questions Q: My Java SE is installed somewhere else, what do I do? A:You need to write down where you Java SE is installed and follow the steps above but don t type what is on these pages, type your Java SE path instead. If you are not able to find it manually, do a search in windows and try locating a file called 'javac.exe' or 'java.exe' Q: Don t we need to change anything else, there is a java before the = and java and javac after somewhere? A: No Q: Do I need to do anything to the second line? A: No Q: I don t know where SciTE is installed, what now? A: The default location is c:\program files\wscite\scite.exe. If you installed/extracted somewhere else and you forgot where, then you need to use your Windows search tools and look for a file called scite.exe Q: I am looking at the Java directory now and there is no bin folder there. What is wrong? A: Sometimes the machine might also include a Java Runtime Environment installation and that is usually installed in C:\program files\java\jre6 or C:\program files\java\jre7 or something similar. Please make sure that you are not looking in that folder. You should make a note of the folder you installed the java development kit. Q: I have completed the configuration but when I try to compile I still get nowhere? Or I get the following errors >The system cannot find the file specified... or >'c:\program' is not recognized as an internal... A: Something is wrong with your configuration retry the steps above making certain that you are actually using the correct folder for your java SE. Last updated by Andreas Grondoudis on: Thursday, 02 February 2012 7
How do I use SciTE for my java programs Run SciTE and type the following code in a new file Then select <File> and <Save> the file under the name Test.java. Now that the file is ready you move on to the compilation of the file. You compile a java program by selecting <Tools> and <Compile Ctrl+F7>. Provided that the configuration was correct; the compiler checks the file and creates a class that can be executed (or run as we say). The output of the compilation is shown on the right hand side of the SciTE window (see below). When you get an >Exit code: 0 this means that everything is OK and the compile operation was successful. Having the file compiled means that we can now run it (or execute it).you execute a file by selecting <Tools> and <Go F5> (or just press the F5 key). The result will be as indicated below The Hello that you see is the execution of the program. The >Exit code: 0 means that execution terminates normally and no errors were reported. This is it. You have compiled and executed a java program called Test. 8 Last updated by Andreas Grondoudis on: Thursday, 02 February 2012
When things go wrong If something was wrong; say you missing the last curly bracket '}' then the compiler will complain by bringing up an error. The following figure illustrates. Notice that line 7 (on the left) is now empty and that the compiler output on the right includes details of 1 error. In the second line of the complier output the compile is showing you: 1) the file in error (Test.java) 2) the line of the error (:6:) and 3) a short error explanation (reached end of file while parsing). Also note that the Exit code is now 1 which means that something went wrong and errors were reported. By double clicking on the error reporting line the editor will take you there (see below) After you correct the error; save the file, compile again and you should be ready to execute. Frequently Asked Questions Q: When I try to compile I get the following: >The system cannot find the file specified... or >'c:\program' is not recognized as an internal... A: Your SciTE does not know where the J2SE is installed. You need to configure your SciTE, please refer to the relevant section above. Q: When I try to compile I get too many mistakes. The right side of SciTE fills up A: When you type a program you have to be very careful of what you are typing. Java is a case sensitive language so System.out is not the same as system.out. It is common for (new) users to have quite a few mistakes. All you have to do is go through your code and correct them Last updated by Andreas Grondoudis on: Thursday, 02 February 2012 9
Appendix A - Programming tips 1. You have to be patient and persistent. Programs can take a while to complete. Don t give up 2. The compiler has nothing against you, as do the keyboard, monitor and case. They are just doing their job; don t take your frustration out on them. 3. Java is case sensitive. This means that JOptionPane is not the same as joptionpane, and String is not the same as string. 4. Remember to save.java files with the same name as the class name that you used inside the file. If inside the file you wrote public class My_first_class then you should save this file as My_first_class.java. If you use any other name, the compiler will report error(s). 5. The compiler is very rarely wrong. If it is reporting a mistake then it means that you do have a mistake somewhere (see point 7 below) 6. When you do get 10 or even 100 scrolls worth of mistakes do NOT despair. Usually mistakes at the beginning of the program can cause many mistakes later on. What you should do is scroll-back at the top of the compiler output window and try to correct the mistakes starting from the top. 7. Quite often mistakes are reported on lines that when you see them no mistake exists. This, 99% of the times, means that there is mistake somewhere above in the program. If the code is in a loop somewhere; look at the loop first and then look and the method that the loop was in and so on. Try and double-check related code (close and sometimes far) to the mistake reporting line. 8. When you do copy and paste code that you don t want to retype you have to take care to make any minor required changes. Quite often, especially when we have to paste the code in more than one place what you do is: paste everywhere first and then go back and adjust the code. The problem here is that you might have actually copied the code in, say, 10 places and went to make the adjustments in only 9 of them. Then the one place that you didn t change will: 1) Not report an error and 2) Run OK, but quite possibly not as you want it. So, what you need to do is, paste the code and make the changes there and then (so that you don t forget later on). The clipboard holds the text so you can then carry on to the next paste and change. 9. When you are starting a method or a loop or anything else that might require the use of start and end braces what most people do is start with the open brace and carry on to type the code inside the brace. Then (at the end) type the closing brace. This is fine but, if you have to type many lines inside the brace, you tend to forget to put the closing brace at the end. This leads to the compiler reporting (numerous sometimes) } expected. A very handy tip to use is: Write both the opening and closing brace at the same time and then take your cursor back in the middle of the two to complete the code. This way, anything you write in there (no matter how many lines it is) will be inside the braces. 10. Use the Java documentation. You might be doing something wrong without knowing it. 11. Using Netbeans or Eclipse as your editor: o o CSC131 students: should avoid it. When typing they offer help or autocomplete. That is ok when typing but when writing an exam you will NOT have that and you might find yourself not remembering how to write something simple. CSC132 students: you are not allowed to (especially for GUI development) 10 Last updated by Andreas Grondoudis on: Thursday, 02 February 2012