Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS170 Fall 2013 Lab 2 Basic File Editing and Java Programming Objectives of this lab: • • • • Practice some basic file operations Write, compile and execute a simple Java program Turn in your work using a “turnin” command (not email!!) You can probably finish this during lab period. If not, you have until midnight tonight. Pre-lab Reading Material: You should read the material before coming to the lab. If you have not done so, you will need to read it while the TA is explaining the lab and you can read them after the lab to re-enforce what you have learned. • Manipulating files. • Editing files using gedit. Review: • • • • Shell: the program which interprets what you type at the prompt on the command line in UNIX. A terminal application is running a shell program Editor: a computer application that lets you edit files Review of special directories: ◦ ~/ means /home/UserID/ (this is your home directory) ◦ ~cs170004/ means /home/cs170004 (the grader home directory). Each section has a different grader account ending in 000, 001, 002, 003, or 004. Know which section you are in. Exercise Preparation: • Start a terminal application (see previous lab or ask the TA) and follow the next steps by typing the appropriate commands: ◦ Create the lab2 directory inside your cs170 directory mkdir ~/cs170/lab2 ◦ Copy the files you will need cp ~cs170004/share/lab2/* ~/cs170/lab2 ◦ Move into the lab2 directory (ie make it the working directory) cd ~/cs170/lab2 ◦ List the contents of the current directory ls ◦ You should see 2 files: Convert.java and Cylinder.java. If you do not see these files, ask the TA for help. Execute the "Convert" Java program: • • The Convert Java program reads from the terminal a distance in kilometers and prints the corresponding measure converted in miles. Take a look at the Convert.java program by typing: • cat Convert.java ◦ Note: cat is a Unix command which causes the content of a file to be displayed in the terminal. You should see this output: import java.util.Scanner; /* This program converts km to miles. */ public class Convert { public static void main(String [] args) { // declaration of the variables double km, miles; // create a Scanner Scanner input; input = new Scanner(System.in); // Enter distance in kilometer System.out.print("Please enter a distance in km: "); km = input.nextDouble(); // Read in input from terminal // and stores it in the variable "km" // Calculate distance in miles miles = 0.62 * km; // print out the result System.out.print(km); System.out.print(" kilometers = "); System.out.print(miles); System.out.println(" miles"); } } Compiling the Convert.java program: • Remember that a Java program must first be translated into executable code using a Java compiler. • To translate (compile) the Convert.java program, execute this command in a terminal window: javac Convert.java • Now list the contents of the directory with the ls command. You should see: Convert.java Convert.class Cylinder.java Executing the compiled Convert.class program • To execute the compiled program, type the command java Convert • Sample execution (the >>> indicates a prompt in the terminal window): >>> java Convert Please enter a distance in km: 2.3 2.3 km = 1.426 miles Lab Assignment : You should have a copy of the Java program called Cylinder.java in your lab2 directory. The Cylinder.java file represents the starting point for your program. Tasks: • • • • • • • • • Edit the Cylinder.java file and write the code to compute the volume and surface area of a cylinder. Use this command to start the gedit editor to edit your Cylinder.java file: gedit Cylinder.java & gedit should start and the file should be displayed. Add the following to the Cylinder.java program: (the tasks are also given inside the Cylinder.java program with comments (//)) ◦ The program must use a Scanner (just like in the Convert.java program) to read from the terminal: ▪ the radius of the cylinder ▪ the height of the cylinder ▪ Note: both of these numbers can be fractional numbers (i.e. of double type) ◦ Define the variables needed to store the inputs (radius and height) provided by the user. Use meaningful variable names! ◦ Define a variable of type double called pi and initialize it to 3.14. ◦ Compute the volume and surface area of the cylinder and store the results in variables with meaningful names. ▪ The formula to compute the volume and surface area of a cylinder are: • volume = baseArea * height • surfaceArea = 2 * baseArea + (2 * radius * pi) * height • where baseArea = pi * radius * radius ◦ Using System.out.print and System.out.println, print out the volume and the surface area, similar to the Convert.java program. When you have entered the necessary Java statements, save your file. From the terminal window, compile your file using the command: javac Cylinder.java Remove the bugs (errors) if necessary. If you can't figure one out, ask the TA for help. ◦ Remember to save each time you edit your file ◦ Save and recompile your file until there are no bugs After a successful compilation, in a terminal window, run the program using the command: java Cylinder The program will prompt you for two inputs and then print the volume and surface area. An example of the interaction you should see is: >>> java Cylinder Please enter the radius: 4.5 Please enter the height: 12.2 The volume of the cylinder = 775.737 The surface of the cylinder = 471.942 Turning in your work: • When you are done, turn-in lab 2 using this command: /home/cs170xxx/turnin-lab Cylinder.java lab2 ◦ Note: you will need to replace 'xxx' with your section number. • This makes a copy of your file (Cylinder.java) in the grader's account. • Your turnin is successful when you see this message: Program `Cylinder.java' has been turned in by YOUR_ID as lab2