Sunteți pe pagina 1din 3

U NIVERSITY OF THE W ITWATERSRAND , J OHANNESBURG

COMS2003: Application and Analysis of Algorithms II


A3 Laboratory 2 2012

Setup
1. First, make sure your folder structures make sense. It will become very difcult to nd your work if you do not maintain a level of organisation. 2. Connect to your home directory. Your username and password are the same as your moodle username and password. Do this by clicking on Places, and then Connect to Server. Then pick Windows Share from the dropdown menu. In the Server eld, type ug.ms.wits.ac.za, in the Share and User name elds, type your student number, then click Connect. In the resulting box, enter your moodle password. 3. Open a terminal in your home directory by right-clicking in the folder and selecting Open in Terminal. 4. Create a folder called AAA in your home directory by typing mkdir AAA (meaning make directory AAA). 5. Go into the AAA folder by typing cd AAA (meaning change directory to AAA). 6. In the AAA folder, make a folder called labs and go to that folder. 7. In the labs folder, make a folder called lab2 and go to that folder. 8. In the lab2 folder, type pwd to see the full path of the folder youre in.

Using Packages
1. In the lab2 folder, create a le called Lab2.java by typing touch Lab2.java 2. Edit the le called Lab2.java by typing gedit Lab2.java &. 3. In the le, make a program that outputs the text Hello World by typing the following:

package lab2; public class Lab2{ public static void main(String args[]){ System.out.println("Hello World"); } } 4. Make sure you save the le. 5. To compile the le, go back to the terminal and type javac Lab2.java 6. Try to run the le by typing java Lab2. Note that it does not work! This is because of the line package lab2, which tells the Java Virtual Machine that the class is inside a package called lab2. 7. So, in order to run the le, we need to go up one folder level by typing cd .. 8. Type pwd to see the full path of the folder youre in. 9. Run the program from here by typing java lab2/Lab2, which indicates that the Lab2 class is in the lab2 folder or package.

Creating jar packages to submit to the marking system


1. Since java is object oriented, its quite possible to use multiple les to represent one program. To make submission easier in this case, we need to package our les into a jar le. 2. Note that in order for the Java Virtual Machine to know which le to execute when there are many les, we need to dene which le is our main class, which is the one containing a main method. In our case, since there is only one class (Lab2), this would be our main class. 3. Go to the lab2 folder by typing cd lab2 4. In this folder, create a le called touch mainClass 5. Edit the le by typing gedit mainClass & 6. In this le, we will dene which class the main class is by typing: Main-Class: lab2.Lab2 7. Make sure you save the le. This le indicates that the main class is the Lab2 class in the lab2 package. 2

8. Now, in your terminal, go up to the labs folder by typing cd .. 9. From here, create your jar le by typing the following: jar cmf lab2/mainClass lab2.jar lab2 10. The lab2/mainClass part says which le contains the information about the main class. 11. The lab2.jar part indicates that a le called lab2.jar should be created. 12. The lab2 part says that the folder containing the source and compiled les is called lab2 13. Now try submitting your jar le to the marking system using moodle.

Lab2b
1. In your labs folder, create a folder called lab2b 2. In the lab2b folder, edit a le called Lab2b.java 3. Remember to use package lab2b; 4. This class needs to read an integer from the keyboard, using: Scanner in =new Scanner(System.in); int i = in.nextInt(); 5. It must output true if the number entered is a multiple of 3, and false otherwise. 6. (Hint : Use the modulus function (%) which returns the remainder of an integer division) 7. Package this up in a jar le and upload it to the marking system.

S-ar putea să vă placă și