Classes Objects Methods
Classes Objects Methods
9
Classes, Objects, and
Methods
class circle1{
Public static void main(String[] a){
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..
Example 5: Method returns integer that contains the sum of two numbers.
Exercises:
Exercise 1:
Write a class Book with three data members BookId, Pages and Price. It also contains the follow-
ing member function:
• The get() method is used to input values
• The show() method is used to display values
Exercise 2:
Create a class building that has the public member floors,area, and occupants and a method
areaperperson()respectively that display the area per person for building. In the main() meth-
od create two instance of building called house and office and display the area per person by di-
vision of area/occupants.
Homework:
1) Write a class Result that contains roll no, name and marks of three subjects. The marks
are stored in an array of integers. The class also contains the following member functions.
• The input() method is used to input values
• The show() method is used to display values
• The total() returns the total marks a student
• The avg() method returns the average marks of a student
2) Create a class rectangle. The class has attributes length and width each of
which defaults to 1. It has member function that calculates the perimeter and the area of
the rectangle. It has set() and get() functions for both length and width.
The set() function should verify that length and width are each floating point
numbers larger than 0.0 and less than 20.0.