Software and Programming: Lab 1 Exercises (Part 1) WELCOME TO THE FIRST LAB for Software and Programming. In this lab, you will write, compile, and run your Java programs. If Java programming is of an issue for you, then we encourage you to work on the labs with a partner, but we do not require you to do this. It's a good idea to have someone to talk to as you work on the lab. At the end of the lab, you'll find a set of exercises. In general, you should not expect to finish all the exercises during the lab. Finishing the exercises is homework. In particular, we expect you to clean up any programs you write and take the time to give thoughtful answers to essay questions. Outline of the Lab: Your Computer Accounts Your First Java Programs Exercises Your Computer Accounts In the world of networked computers, you can sit at one computer and access computers from across campus and around the world. As a student at Birkbeck College, you have an account on the College computers. Having an account means that you have a user name and password that you can use to log on to the computer. You also have a directory on each computer where you can store files (which are stored centrally). You can also access files stored in other directories on the computers. Your username on each of these computers is the same. The main programs you should use for this course is JDK (Java Developers Kit) and BlueJ (an introduction of how to use BlueJ is given in part 2 of this lab sheet). Note: the following website gives a description of how to setup Java on your own computer http://www.ics.uci.edu/~thornton/ics22/LabManual/SettingUpJava.html. For more information on using your account, you should read the Web pages at http://www.bbk.ac.uk/ccs. In this lab, you will need to know a few commands for working with files and directories. You should know at least the concepts of file, directory, path name, and home directory. Your First Java Programs In these exercises, you will use the “TicketMachine” example and work with it to modify and improve it. You will also build Java programs from scratch. Exercise 1: NOTE: You can either use BlueJ to do the “HelloWorld” exercise below or use an editor such as Textpad, Notepad (if you choose this option, you need to type commands in the DOS console). If you decide to use BlueJ and are unfamiliar with this software, part 2 of this lab explains how to use BlueJ. You should type in the HelloWorld program (see Figure 1) into a file HelloWorld.java, which simply prints out the message "Hello World". To run the program you must first compile HelloWorld.java. If If you are using an editor, do this with the command javac HelloWorld.java After doing this, the compiler has created a file named HelloWorld.class. This is the compiled, Java bytecode version of the HelloWorld program. Now that the compiled program exists, you don't have to compile the program ever again, unless you want to edit it and change what it does. If you are using an editor, run the program with the command: java HelloWorld The computer will say hello to the world. (Note that you say "java HelloWorld" to execute the class file, and not "java HelloWorld.class".) public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } Figure 1: HelloWorld program Exercise 2: Before attempting the rest of the exercises in part 1 of lab 1, switch over to part 2 of the lab sheet and come back to this section once you have completed part 2. From now on, we recommend you use BlueJ. Exercise 3: Exploring the behavior of a naïve ticket machine Open the naive-ticket-machine project (http://www.dcs.bbk.ac.uk/~gngch01/sandp1) in BlueJ. When you create a TicketMachine instance, you will be asked to supply a number that corresponds to the price of tickets that will be issued by that particular machine. The price is taken to be a number of cents, so a positive whole number such as 500 would be appropriate as a value to work with. 3-1 Create a TicketMachine object on the object bench and take a look at its methods. You should see the following: getBalance, getPrice, insertMoney, and printTicket. Try out the getPrice method. You should see a return value containing the price of the tickets that was set when this object was created. Use the insertMoney method to simulate inserting an amount of money into the machine and then use getBalance to check that the machine has a record of the amount inserted. You can insert several separate amounts of money into the machine, just like you might insert multiple coins or notes into a real machine. Try inserting the exact amount required for a ticket. As this is a simple machine, a ticket will not be issued automatically, so once you have inserted enough money, call the printTicket method. A facsimile ticket should be printed in the BlueJ terminal window. 3-2 Experiment with inserting different amounts of money before printing tickets. Do you notice anything strange about the machine’s behavior? What happens if you insert too much money into the machine – do you receive any refund? What happens if you do not insert enough and then try to print a ticket? 3-3 Try to obtain a good understanding of a ticket machine’s behavior by interacting with it on the object bench before we start looking at how the TicketMachine class is implemented. Exercise 4: For your next exercise, you will write a program that does a computation and prints out a result. Here are some facts: Every year, 724 billion cubic yards of water flow out of the Mississippi. There are 1760 yards in a mile. There are 365 days in a year. Here is the question: How many cubic miles of water flow out of the Mississippi every day? Some Java commands that will get the job done are: double cubicYardsPerYear = 724000000000.0; double cubicYardsPerDay = cubicYardsPerYear / 365; double cubicMilesPerDay = cubicYardsPerDay / (1760*1760*1760); System.out.println("The answer is " + cubicMilesPerDay); For a complete program, you have to put this inside the standard Java program wrapper: public class Mississippi { public static void main(String[] args) { double cubicYardsPerYear = 724000000000.0; double cubicYardsPerDay = cubicYardsPerYear / 365; double cubicMilesPerDay = cubicYardsPerDay / (1760*1760*1760); System.out.println("The answer is " + cubicMilesPerDay); } } This should be in a file called Mississippi.java.