Lab 3 Class Object Methods
Lab 3 Class Object Methods
Objective:
Understanding the concept of Object-Oriented programming.
Introducing classes and objects and differences between them.
Understanding class members and their properties.
Calling methods.
Scope:
In JAVA, programmers concentrate on creating their own user defined classes and
objects. Each class contains data and data members and methods to facilitate the user
how to kept data encapsulated manner for the protection from the unauthorized access.
Circle
+ radius: double
+ Circle(r:double)
+ getRadius(): double
+ getArea():double
+ getCircumference(): double
+ getDiameter(): double
Example 2: This program demonstrates the private member of a class can’t be
accessed from the outside of the class which reports compile-time error..
A
- value = 10 : int
+ getValue(): int
+ setValue(value:int): void
Example 3: The following example shows the difference between static and non-static
variables.
.
Example 4: The following example shows how to add two numbers using method calling.
Method returns integer that contains the sum of two numbers
Exercise 3:
Result
Write a class Result that contains rollNo, name and ??? rollNo: ???
marks of three subjects. The marks are stored in an array ??? name: ???
of integers. The class also contains the following member ??? mark: ???
functions.
??? input(): ???
The input() method is used to input values
??? show(): ???
The show() method is used to display values
??? total(): ???
The total() returns the total marks a student
??? avg(): ???
The avg() method returns the average marks of a
student
Exercise 4:
Rectangle
Create a class Rectangle. The class has attributes length
and width each of which defaults to 1. It has member ??? length: ???
function that calculates the perimeter and the area of the ??? width: ???
Rectangle. It has set() and get() functions for both ??? set(): ???
length and width. The set() function should verify that ??? get(): ???
length and width are each floating point numbers larger
than 0.0 and less than 20.0.