content-Description: // Program - 3 class threenum3 { public static void main(String argv[]) { threenum t_num_ref; t_num_ref = new threenum(10,20,30); System.out.println("The Average of the 3 numbers : " + t_num_ref.avg()); System.out.println("The Sum of the 3 numbers : " + t_num_ref.sum() ); } } content-Description: // Program - 2 class threenum2 { public static void main(String argv[]) { int n1 = 10; int n2 = 20; int n3 = 30; int sum = 0; float avg; System.out.println("The average of the 3 numbers : " + avg(n1,n2,n3)); System.out.println("The sum of the 3 numbers : " + sum(n1,n2,n3)); } public static float avg(int n1, int n2, int n3) { return (n1 + n2 + n3)/3; } public static int sum(int n1, int n2, int n3) { return (n1 + n2 + n3); } } content-Description: // Program - 1 class threenum1 { public static void main(String argv[]) { int n1 = 10; int n2 = 20; int n3 = 30; int sum = 0; float avg; avg = (n1 + n2 + n3)/3; System.out.println("The average of the 3 numbers : " + avg); sum = n1 + n2 + n3; System.out.println("The sum of the 3 numbers : " + sum); } } content-Description: // Program - 4 class threenum { private int n1,n2,n3; public threenum(int num1, int num2, int num3) { n1 = num1; n2 = num2; n3 = num3; } public float avg() { return (n1 + n2 + n3)/3; } public int sum() { return (n1 + n2 + n3); } } content-Description: CS116/504 Introduction to Computer Science for Majors II Spring 99 Lab - 1 Duration : Week of January 25 Points : 15 ------------------------------------------------------------------------------ Important : This lab should be completed within one week after it was assigned. Incase you don't complete your work within the recitation hour contact the respective TA's who handle your recitation as to how you need to submit your work to him for getting it graded. Late submissions will not be accepted. Clarify your doubts with your TA. ------------------------------------------------------------------------------ Lab1 is for familiarizing you with some basic unix commands, using emacs editor for java program development and also getting a start on Object oriented programming using Java. 1. Copy the .emacs file from ~vijays/ directory. Note : ~ represents your home directory. ~loginname represents the home directory of the user with that loginname. Here ~vijays denotes the home directory of the user whose login name is vijays. Also . represents your current directory. To know what your current directory is type the command pwd followed by return key. This will print on the screen the current working directory you are in and which . represents. > cp ~vijays/.emacs . Syntax : cp source destination 2. Create a subdirectory .emacs_dir. Now copy all the contents of the directory ~vijays/.emacs_dir into you .emacs_dir directory. Note : * represents everything. For eg. the command ls .* will list you all the files in your current directory that starts with a . and has zero or more characters after that. > mkdir .emacs_dir > cp ~vijays/.emacs_dir/* ~/.emacs_dir/ Note : Command for removing a file is rm filename. Removing a directory is rmdir directoryname. For viewing the contents of the file use the command more filename or less filename. Press q for returning to the command prompt for both more or less. For changing directory use cd directoryname. For copying the contents of one file to another and also destroying the source file use mv source destination. 3. Type emacs threenum1.java. emacs not only can be used for developing programs, but as you will see has a lot of modes and functions. > emacs threenum1.java A plain window should now open on the screen. Now your emacs program is running foreground. So goto the terminal window and press ^z you will see that the command prompt > appears. Now type bg in the command prompt. emacs will now run in the background. 4. Your emacs is now configured to work in the java mode. emacs can be configured to work in a lot of modes depending on the file/directory that needs to be processed. As you will see, when you type the java program keywords get highlighted. Note : Proper indendation of programs is very much stressed in this course. emacs editor helps you in this. It automatically sets the indendation needed whenever you press the tab key. Type in the program given to you titled program - 1. 5. Now click Open in the menu bar and click Save. Now once again goto the Open option and click Make a new frame. Another emacs window is now created with the same contents as in the original window. Now in the new window click Open and in that Open Directory. The new window will now list the directory contents of the current directory. Click on the file threenum1.java which will be listed in that and then click Operate on the menubar. In that click Shell Command. Now at the bottom most line of the new emacs window you will get the following prompt. ! on threenum1.java: Type javac followed by the return key. javac is the command for invoking the java compiler. If you have typed everything correctly you should receive no errors. Java Compiler would then have generated threenum1.class. Inorder to view that on the new emacs window, press g. This will update all the changes made to the directory that's open in the emacs window. 6. Type the following command in your command prompt. java threenum1.java The program outputs the average and sum on the screen. 7. Now in the original window click Open followed by Open File. emacs will prompt you to enter the file name. Type threenum2.java followed by return key. A blank emacs window appears before you. Now click Buffers and in that click the option Buffers. You will see the list of buffers that are currently open. Click threenum1.java. Your emacs window now contains contents of threenum1.java. Highlight the entire text by pressing the left button of your mouse and the use the Copy option in Edit. Now goto the Buffers option and there click threenum2.java. Goto Edit and click Paste. The contents of threenum1.java will now get pasted threenum2.java. Now modify the contents of threenum2.java so that it is the same given in your Program - 2 handout. 8. Repeat steps 5 & 6 described above for compiling threenum2.java and then executing it. The program outputs the average and sum on the screen. Note : In threenum2.java you get the same output as threenum1.java, but in threenum2, the calculation of sum and average are done in separate methods, which are invoked by the main() method. 9. Now we are going to develop an application threenum3 which does the same as the other two programs but invokes class methods. Type in program - 4 in the file threenum.java. This class has two methods sum() and average() which operate on it's three data memeber. Then type Program - 3 in the file threenum3.java. 10. Compile threenum3.java and run it.