Lab 002 (1)

Page 1

Department of Computer Science

OOP Fall 2014 LAB 2: 29-Sep to 03-Oct 2014 Chapter 2: Understanding Class Definition

Lab Tasks: Use naïve-ticket-machine project in BlueJ for the following tasks. Perform tasks in the given order. 1. Create an instance of TicketMachine class and observe the difference as compared to Circle Class in previous lab. Examine the newly created instance from object bench of BkueJ. Which Methods are there? 2. Call getPrice method on the above created instance not what happened on execution of this method 3. Call insertMoney method 4. Call getBalance method 5. Now repeat task 3 and 4 multiple times and see what happened every time you call getBalance method 6. Call printTicket method 7. What happens if you call getBalance after printTicket method. 8. Observe the behavior of machine by inserting multiple amounts and calling getBalance and printTicket method. 9. Now create multiple instance of TicketMachine, repeat above tasks on all instances simultaneously with different amounts, and observe the behavior. Following exercise, show you the code of a Class 10. Explain the main components of a class. 11. Double click on the TicketMachine icon in class diagram of BlueJ. You will see the source code of TicketMachine class. 12. Change the code statement public class TicketMachine to class public TicketMachine and compile by clicking on compile button. Note Does the sequence of words in statement matter or not? 13. What happened if the word public or class is skipped from the outer wrapper of TicketMachine class? 14. Identify the type and name of following fields private int age; private Circle circle1; private boolean status; 15. What happened if you write the word ‘int’ prior to ‘private’ in field declaration or if you omit a semicolon at the end of statement? 16. Add a data field discount of type int in class TicketMachine. Make sure that the discount value is initialized in constructor of class. Write a method setDiscount to change the discount amount. Change the ticket price after discount. Now call getPrice method. 17. What is the meaning of Scope and lifetime? Define scope and lifetime differences for “instance variable”, “Parameters” and “Local variables”. 18. What is the purpose of a Constructor in a class and how it is different from other methods? Can you define the define the difference between constructor space and method space. 19. You have a constructor in class TicketMachine that takes a single parameter. Write two more constructors one without parameter and sets the price value to


default value of your choice. Second constructor with two parameters one amount that sets the value of price and other disc_amount that sets the value of discount. User appropriate data types for these parameters. 20. If you have a field with name age in class Student. How will you assign value to the age in the following constructor. public Student(int studentAge) { } 21. What is the difference between “Accessor” and “Mutator Methods”? List the Accessor and Mutator methods in the TicketMachine class. 22. Write a new method getTotal in Ticketmachine class. The method show print the value of total field. 23. Write a method called prompt to the TicketMachine class. The body of the method should print thefollowing single line of output: Please insert the correct amount of money. Use appropriate return type. 24. Modify the insertMoney method. If user enter an amount less then or equals to zero then call prompt method to display message. 25. What is the error the following methods public int refundBalance() { return balance; balance = 0; } public void getAge() { return age; } 26. Open the code Pad of Bluej and type following lines of code. Observe the results carefully. This will help you to understand what happened inside the code when you perform acticities though diagrams. TicketMachine t1 = new TicketMachine(1000); t1.getBalance() t1.insertMoney(500); t1.getBalance()

Challenge exercises: This means that the following exercises might not be solved quickly. We do not expect everyone to be able to solve these at the moment. If you do, great. If you don’t, then don’t worry. Things will become clearer as you read on. Come back to this exercises later. 27. Challenge exercise: The following object creation will result in the constructor of the Date class being called. Write Header and Body of the Constructor for class Date. new Date("September", 29,2014 ) 28. Create a new Class “Student. The Class will have fields (sname, fname, age, address). Write Constructor of the class. Use appropriate methods to set and get the values of sname, fname, age and address. Use a printStudent method that will display the student data in following shape ########### STUDENT DATA #########


Name: Muhammad Ali Father Name: Ali Sher Age: 20 Address: Lahore ##################################

Things to Bring with you in Lab (on USB), if you are going to use Lab computer: • • •

This lab task sheet. BlueJ projects that are required to perform lab tasks. Text book: Object First.., fifth edition 2012.

Remember: • •

A good programmer never stops learning. Be interested and enjoy your lab! Most importantly give time and be patient! Work in a pair. Both members should participate actively i.e. one member analyzes the problem/task while the other member type/perform the task. Both members should do the both activities alternatively.

****************


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.