CSC3104 - Lab 2
CSC3104 - Lab 2
Activity 1 Objective Writing a class with simple attributes, constructor and methods.
Problem Description Write a program to display the message Welcome to OOP class, person_name. The name of person must be assigned through the constructor. In addition, the program should prompt message CSE3101 Object Oriented Programming, when you execute the default constructor. Run your program using default constructor and your pre-defined constructor. Evaluate the output. You should the sample output as below:
Solution Activity 1 Step 1: Go to SCE3101s folder and create sub-folder called as Lab Week 2. Step 2: Go to CSE3101 > Lab Week 2s folder and create sub-working folder called Activity 1. Step 3: Open IDE Netbeans or JCreator. JASKOM, UMT | CSE3101 OBJECT ORIENTED PROGRAMMING 11
CHAPTER 1
Step 4: Create new file/class called Welcome.java. Step 5: Define the class and instance variables as below:
Step 10: Save the file in sub-working folder called Activity 1. Step 11: Compile the program. Step 12: Create new file/class called WelcomeTest.java JASKOM, UMT | CSE3101 OBJECT ORIENTED PROGRAMMING 12
CHAPTER 1
Step 17: Compile the program. Step 18: Run and evaluate the output.
13
CHAPTER 1
Activity 2 Objective How to instantiate an object of the class. Problem Description Vehicles class shows in figure 1 will be used to display details information for specific vehicle. Create main program and instantiate each constructor, and display the information for each of the attribute.
14
CHAPTER 1
15
CHAPTER 1
Activity 3 Objective Initialize the constructor and introduce getter and setter methods. Problem Description The Student()s class consists of attribute such as student id, name, and status. The status can be assigned either active or suspend. Your program must have minimum three (3) constructors, that will behave differently when the program is executed at runtime. Create the main program and display the student name, course and his/her status. [Estimated Time: 35 minutes]
Solution Activity 3 Step 1: Go to CSE3101 > Lab Week 2s folder and create Sub-working folder called Activity 3. Step 2: Open IDE Netbeans or JCreator. Step 3: Create new file/class called Student.java. Step 4: Define the class and instance variables as below:
16
CHAPTER 1
Step 8: Save the file in sub-working folder called Activity 3. Step 9: Compile the program. Step 10: Create new file/class called StudentTest.java
17
CHAPTER 1
Step 14: Display the student name, course and his/her status.
Step 14: Compile the program. Step 15: Run and evaluate the output.
18
CHAPTER 1
Activity 4 Objective Using primitive data type in class. Problem Description Write a program to convert string number into integer, integer number into double. Prompt the user at main program to enter two (2) numbers that representing string number and integer number. The program should validate the string input key-in by user is number. If the input is string (a,b,c, etc), then assigned the input as 0. Otherwise, display the error message. Finally, display the out of conversion.
19
CHAPTER 1
Lab Exercises
Objective: Writing a small object oriented program using attributes, constructor and methods with primitive data type.
Objective: To passing a parameters and details implementation of constructor in computing ticket sales of Sciences exhibition. Problem Description: You need to write the program to calculate the total ticket sales of a concert. There are three types of seating: A, B and C. The program must accept the number of tickets sold and the price of a ticket for each of the three types of seats. The total sales are computed as follow: totalSales = numberofA_Seats * pricePerA_Seat + numberofB_Seats * pricePerB_Seat + numberofC_Seats * pricePerC_Seat
2. Write a called Ticket Sale and finally used class called TicketSaleTest to run the program and display total sales for ticket A, B and C. Finally display total sales of the ticket
20