Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Lab 3 Class Object Methods

The document outlines Lab No. 3 focusing on Object-Oriented programming concepts in JAVA, including classes, objects, and methods. It provides examples of class implementations and exercises for creating user-defined classes such as Book, Building, Result, and Rectangle. The objective is to understand class members, encapsulation, and method calling in programming.

Uploaded by

hieuldse180535
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab 3 Class Object Methods

The document outlines Lab No. 3 focusing on Object-Oriented programming concepts in JAVA, including classes, objects, and methods. It provides examples of class implementations and exercises for creating user-defined classes such as Book, Building, Result, and Rectangle. The objective is to understand class members, encapsulation, and method calling in programming.

Uploaded by

hieuldse180535
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab No.

3: Classes, Objects, and 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.

Example 1: This program demonstrates a simple class implementation.

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

Example 5: Demonstration of methods with BufferedReader class


Exercises:
Exercise 1: Book
Write a class Book with three data members bookId, ??? bookId: String
pages and price. It also contains the following member ??? pages: int
function: ??? price: double
 The get() method is used to input values
??? get() : ???
 The show() method is used to display values
??? show(): ???
 The set() method is used to set the values of data ??? set(): ???
members using parameters ??? getPrice():???
 The getPrice() method is used to return the
value of price
Building
??? floors: ???
Exercise 2:
??? area: ???
 Create a class Building that has the public ??? occupants: ???
member floors, area and occupants and a
??? areaPerPerson(): ???
method areaPerPerson()respectively that display the area per person for
Building.

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.

You might also like