1005ICT Object Oriented Programming 2015-2 Laboratory 2 School of Information and Communication Technology Griffith University August 2, 2015 When Teaching week 3 Goals In this laboratory class we use the Java API and create programs with multiple classes. Marks 2 1 Preparation Before your lab class: • Print these lab notes. You need to refer to them a lot before the lab class and during it. • Make sure you are properly enrolled and have access to the PC lab where your class is held and that you can log into those PCs. • Read all of this document. • Review the lecture notes, at least, up to section 5. • If you have not already done so, complete the Griffith Sciences Laboratory Induction test on Learning@Griffith. Print the certificate and bring it to your lab class. • Complete the pre-laboratory questions. 2 Pre-laboratory questions (0.5 marks) Consult the Java API to answer the following questions in the space provided, before your laboratory class. 1. What package contains class Math? 2. What packages contain classes named Date? 3. What method can be used to find the square root of a number ( √ x) (Give the class and method name)? 4. What method can be used to raise a number by a power (xy) (Give the class and method name)? 3 Laboratory activities 3.1 Program 1 (0.5 marks) • This program is to do exactly the same task as program 2 from laboratory 1: 1 $ java TriArea 6.5 3.0 Area = 9.75 $ • This time create two classes: 1. A class that contains only a function that returns the area of a triangle given the length of the base of the triangle and height, as doubles. base height 2. A Main class that contains only a main method. • The Main class must call the function from the other class to perform the calculation. • Test whether your program works correctly by using the MaSH Online Judge. Use the command: javaj 0500-TriArea *.java 3.2 Program 2 (1 mark) • Make a new program (in another folder) with 2 classes: 1. A class that contains only a function that returns the area of a triangle given the lengths of all three sides, as doubles. c a b Use this formula: area = 1 4 √ (a2 + b2 + c2)2 − 2(a4 + b4 + c4) 2. A Main class that contains only a main method, that inputs the three sides as command line arguments and prints the area. • Class 2 must call the function from class 1 to perform the calculation. • Example output: $ java Main 3.0 4.0 5.0 Area = 6.0 $ • Test whether your program works correctly by using the MaSH Online Judge. Use problem id: 0500-TriArea2. 2 3.3 Program 3 (no marks, just kudos) An apartment building has small triangular balconies outside every apartment. They are to be tiled with 30 cm square tiles. Write a program that counts how many tiles are needed for each balcony. One corner of the balcony is always a right angle, and a whole tile is placed in that corner. Tiles on the opposite edge may have to be cut. The pattern on the tiles is not symmetric. So the off-cuts can not be used. The dimensions x and y are to be supplied as command line arguments, in metres. (Use MaSH Online Judge problem id: 0501-triangularBalcony to check your answer.) x y 4 After the Laboratory • Organise the work you have done into folders on your network drive. • Please answer these feedback questions. – What was the most difficult aspect of this laboratory? – Did you find an error in these lab notes? 3