You are on page 1of 3

CS 111T - Lab

Programming Language 2
Computer Science Department
2023

Lab Objectives:
In this lab, a student will practice

• Creating a class declaration. • Using final members


• Declaring instance variables. • Declaring accessors and mutators
• Declaring a constructor. methods.
• Overloading constructor. • Writing a test application to
• Constructor’s chaining. demonstrate the capabilities of another
• Using static members class
Lab Exercise 1:

Shop
- ShopID: int
- ShopName: String
- NbProduct: int
-ShopCount: static int
-ShopDiscount : final int
+ <<Constructor>> Shop()
+ <<Full Constructor>>
+ checkShop(int NbProduct): void
+ PrintShopInfo(): void

Problem Description
1) Based on the UML class diagram above, Implement Shop class considering the following:
• create the needed instance variables, constructors and set and get methods then writing a test
application to demonstrate the class capabilities.
• Use the variable ShopCount as a counter to count the number of Shops.
• Use the variable ShopDiscount to assign the amount of discount for each shop.
• Declare a suitable method to return and show the counter.
• Use constructor call chaining to refer the default constructors to the full argument constructor using
the keyword this.
• In the full argument constructor use the set methods to initialize the variables.
• The method "CheckShop" will printout messages depending on the number of Product:
o 0→100: Small shop
o 101→200: Market
o More than 200: HyperMarket
• The method PrintShopInfo printout all the Shop information and the number of existing shops.
2) Write a test program that:
• Create Shop objects from each constructor.
• Call getShopCount method before creating the objects and after.
• Call CheckShop method for the second object.
• Finally print information of any Shop object using PrintShopInfo.
Assignment

1) Write a Java program that calculates the average of a student grades in multiple courses by creating a
class named Course. The class contains:
• Data fields: a course name, a numeric grade and a maximum grade for each course. (all fields are
private)
• A two parameters constructor that initialize the name and the grade.
• An overloaded constructor that initializes all data field.
➢ The first constructor must call the second constructor (Constructor Chaining).
➢ If the maximum grade is not initialized in the object instantiation, you should set its value to
100.
➢ The second constructor should use set methods to assign properties values.
➢ The class should calculate the grades total so each time an object is created the class should
increment the total by the grade of this course. However, if the grade is greater than the
maximum grade for this object then you should increment the total by the maximum grade.
For example: if the maximum grade is 100 and the student’s grade is 115 then increment the
total by 100. Moreover, you should print the following message:
"New Course added. Grades Total= ", then print the current total.
• Accessor and Mutator methods for all data fields
• Define and use three methods:
➢ A method that returns the number of created courses.
➢ A method that calculates the average by dividing grades total by the number of courses.
➢ A method that prints all course information.

Consider the following points:

• To calculate the average, you must declare and use appropriate static variables and
methods.
• You should declare one of the instance variables as final. (Choose the right one).

2) In your main class you should do the following:


• print the initial number of courses
• create at least 4 Courses objects
• print the updated number of courses.
• display each course information
• print the grades average

You might also like