COM6050 Lab Sheet 1 September 2010 Page 1 COM6050: Java and UML for programmers: Lab 1 Dr Richard Clayton The three aims of this practical are (i) to introduce you to the online resources of the module, (ii) to ensure you can edit and compile a simple Java program, and (iii) to make sure you understand the ideas introduced in the first lectures. You should work through this worksheet on your own. You should aim to complete the first three parts of the lab sheet in the Thursday session, leaving time to complete the final part on Monday. If you experience any problems then ask a Demonstrator or Lecturer to help you. If you don’t finish the practical during the lab sessions on Thursday and Monday, then try to complete it in your own time. You do not need to hand the completed sheet in, and this work will not be marked. Locating online resources Open a web browser and go to http://www.sheffield.ac.uk, click on the MUSE link at the top of the page. Log in to MUSE using your username. You should find a link to My Online Learning Environment (MOLE). Login to MOLE. If your MOLE page does not include a link to COM6050 then tell a Lecturer or Demonstrator. The COM6050 MOLE page has several links, and this is the medium we will use for delivering course materials. There will also be some example multiple choice questions already available for you to practice with in the Assessment folder. Follow the link to COM6050 Teaching Schedule. Here you will find links to the lecture notes, and lab exercises. A simple Java program Create a folder where your Java code for the COM6050 module will be stored. At this point you may wish to think about a suitable way to organize your code, so that you can find your work easily. From the Lab Sheets and Java Code folder, open the file HelloWorld.java. You may wish to create a new folder called COM6050, with a subfolder called Week1 in which you will store your work. You will need to copy the Java code from your browser window into a text editor, and then save the file as HelloWorld.java. You could use Windows Notepad, or any other text editor of your choosing. jEdit is an open source code editor written in Java, and can be downloaded from www.jedit.org. If you are comfortable with an IDE, then Eclipse is installed on the DCS desktop. Modify HelloWorld.java as indicated by the comments in the file, and save it. Compile the modified file using the java compiler in a command window (dos prompt> javac HelloWorld.java), and run the result (dos prompt> java HelloWorld). If you experience any problems at this stage then ask. A more detailed programming task Hopefully you will complete the first and second parts of the practical very quickly, and can get on to the more interesting part. Here you will modify a more complex program, and COM6050 Lab Sheet 1 September 2010 Page 2 implement some of the ideas about variables, types and operators that were covered in the first lecture. From the Lab Sheets and Java Code folder, download the file Quadratic.java, compile it, and run it. This program solves a quadratic equation ax2 + bx + c = 0, giving the two solutions for x, where the coefficients a, b, and c are known, using the formulae a acbb x 2 )4( 1 2 and a acbb x 2 )4( 2 2 The program Quadratic.java uses both integer and double types to store a, b, and c, and prints out the solution (x1 and x2) for the each case. The coefficients are initially set to be a=1, b=2, and c=1, and then we subtract 0.5 from a before working out the solution. Why are the solutions for integer and double types different, and which is correct? (Hint: you may wish to refer to the program Typecast.java). Remove (or comment out) the code for the incorrect variable type. Add some code to Quadratic.java to insert the solutions x1 and x2 back into the quadratic equation so we can check if x1 and x2 are indeed solutions to the initial quadratic equation, (i.e. calculate a*x12 + b*x1 + c, and a*x22 + b*x2 + c). These values should be exactly 0 if the solutions x1 and x2 are correct. Now change coefficient b to have the value 20,000 and obtain your solution for x1 and x2. Are these solutions still correct? If not, why not? Second programming task In the following task you will use the Scanner class to read data items from the file. We covered the Scanner class briefly in Lecture 1, and you should take a look at the lecture notes to remind yourself how Java handles input and output. See also http://download.oracle.com/javase/6/docs/api/. You should write a Java program that first reads data from the file DataFile.txt (available on the MOLE pages – see below for a description), which is a file of integers, into an array of integers and then calculates each of the following: (i) The average of all the numbers stored in the array. (ii) The standard deviation of all the numbers stored in the array, calculated using the equation on the right, where N is the number of values in the array, aj is the jth value in the array, and ā is the average. The symbol Σ means summation. Thus, the above summation could be rewritten as: Note: Java uses 0 for the first index of an array, so you will need to take this into account in your program. The data file for the program is DataFile.txt, which can be found in the folder for lab 1 linked from MOLE. This file contains a list of integers, with one integer on each line of the file. The first integer in the file is not part of the data and instead states how many integers there are to follow. Thus, in the very simple example to the right, the first line, containing the integer 4, indicates that there are 4 integer data items in the file (45, -56, 0 and 23), which should be read into an array 4 45 -56 0 23 N j j aa N aSD 1 2)( 1 )( 22 3 2 2 2 1 2 1 )...()()()()( aaaaaaaaaa N N j j COM6050 Lab Sheet 1 September 2010 Page 3 before calculations can begin. How to proceed The following is just one way that you might choose to develop your program. It suggests a cautious and controlled way to develop a small program such as the one in this exercise. (a) Assume that the data file exists and that the data in it is well formed. Thus there will be no need for explicit error checking in the program, except for the try-catch block we mentioned in Lecture 1. try { Scanner inputFile = new Scanner(new File("DataFile.txt")); // you will need some extra code here } catch (FileNotFoundException e) { e.printStackTrace(); } (b) Write a simple Java program that reads data from the file, one item at a time, and displays each item on the screen as it is read, using System.out.println(). You need a loop structure for this. Since the first line of the file states how many items are to follow, a for loop can be used. You can read an integer from a Scanner object using the nextInt() method using int numberReadFromFile = inputFile.nextInt(); Visually check that the display of data matches the data in the text file, and make sure that you have imported java.io.*, and java.util.Scanner. (c) Next, declare an array after you have read the first line of the file. The first line of the file will give the size of the array to be declared (but remember that array indexing starts at 0 in Java). Using the for loop, read each subsequent data item into the array. Then write a second for loop that displays each item in the array. Visually check that the display of data matches the data in the text file. Now you have successfully read the data into an array, you can complete the exercise. (d) Use a for loop (or for-each loop) to sum the items in the array and divide by the number of items in the array (i.e. the size of the array) to give the average as a real number. (Hint: Dividing one integer by another will not produce the correct result. Why not?) Make sure you display a message telling the user that you are displaying the average of the data. (e) Split the calculation of the standard deviation into parts. First, use another for loop to calculate the summation, storing this in a temporary variable. Then use this in the final calculation of the standard deviation. Java has a Math class that provides all of the static Math methods you are likely to need. You will need the Math.sqrt() method for calculating the standard deviation, so make sure you have imported java.math.*. Again be careful about mixing integers and floats in the same calculation. Also, make sure you tell the user what you are displaying using a string message when you display the results.