0% found this document useful (0 votes)
402 views5 pages

Java Classes for Student and Rectangle

Create a class 'Student' with three data members which are name, age and address. The constructor of the class assigns default values name as "unknown", age as '0' and address as "not available". It has two members with the same name 'setInfo'. First method has two parameters for name and age and assigns the same whereas the second method takes has three parameters which are assigned to name, age and address respectively.

Uploaded by

Arman Afaq
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
402 views5 pages

Java Classes for Student and Rectangle

Create a class 'Student' with three data members which are name, age and address. The constructor of the class assigns default values name as "unknown", age as '0' and address as "not available". It has two members with the same name 'setInfo'. First method has two parameters for name and age and assigns the same whereas the second method takes has three parameters which are assigned to name, age and address respectively.

Uploaded by

Arman Afaq
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

NATIONAL UNIVERSITY OF MODERN LANGUAGES

Shanila Afaq (11769)


Assignment no 2
Submitted To
Miss Bushra khan

NATIONAL UNIVERSITY OF MODERN LANGUAGES


ISLAMABAD

12th,OCTOBER, 2018
Question 1
Create a class 'Student' with three data members which are name, age and
address. The constructor of the class assigns default values name as "unknown",
age as '0' and address as "not available". It has two members with the same
name 'setInfo'. First method has two parameters for name and age and assigns
the same whereas the second method takes has three parameters which are
assigned to name, age and address respectively.

package javaapplication13;
class student{
String name;
int age;
String address;
student()
{
String n="unknown";
int a=0;
String ad="not available";
}
void setinfo(String na,int ag){
[Link]("name is : "+na);
[Link]("age is : "+ag);
}
void setinfo(String na,int ag,String adr){
[Link]("name is: "+na);
[Link]("age is: "+ag);
[Link]("address is: "+adr);
}
}
public class studentdetail{
public static void main(String[] args){
student obj1=new student();
student obj2=new student();
[Link]("shanila",19);
[Link]("shanila afaq",19,"wah cantt");
}
}

Input:
Output:

Question 2
Create a class named 'Rectangle' with two data members- length and breadth
and a method to claculate the area which is 'length*breadth'. The class has three
constructors which are :
1 - having no parameter - values of both length and breadth are assigned zero.
2 - having two numbers as parameters - the two numbers are assigned as length
and breadth respectively.
3 - having one number as parameter - both length and breadth are assigned that
number.
Now, create objects of the 'Rectangle' class having none, one and two
parameters and print their areas.

package javaapplication14;
class rectangle{
int length;
int breadth;
rectangle(){
length=0;
breadth=0;
}
rectangle(int l,int b){
length=l;
breadth=b;
}
rectangle(int len){
length=len;
breadth=len;
}
int area(){
return length*breadth;
}
}
public class rectangles{
public static void main(String[] args){
rectangle obj1=new rectangle();
rectangle obj2=new rectangle(20,30);
rectangle obj3=new rectangle(10);
int area;
area=[Link]();
[Link]("area is: "+area);
area=[Link]();
[Link]("area is: "+area);
area=[Link]();
[Link]("area is: "+area);
}
}
Input:

Output:

Common questions

Powered by AI

Method overloading enhances flexibility by allowing multiple methods with the same name within a class, differentiated by parameter type or count, thereby accommodating various input scenarios without altering method names. In the 'Student' class, the 'setInfo' method is overloaded to take either two or three parameters. This flexibility lets the class handle different data initialization scenarios: one version for name and age, and the other for name, age, and address, facilitating code usability and readability .

Constructors initialize objects, setting initial values for data members. The 'Rectangle' class exemplifies this through three constructors: one initializes with default values (length and breadth as zero), another with specified values from parameters (providing flexibility for precise dimensions), and a third constructor sets the same value for both length and breadth. These constructors enable object instantiation with different initial configurations, enhancing class usability .

Encapsulation, by packaging data and behavior within a single unit and restricting access through specified methods, ensures object integrity. In the 'Student' class, data members (name, age, and address) are manipulated through 'setInfo' methods, controlling data assignment and maintaining consistency. Similarly, the 'Rectangle' class encapsulates data members and behaviors by providing constructors and an 'area' method for controlled data manipulation and use. Encapsulation in both classes prevents unintended interaction with internal data, promoting robustness and reliability .

While method overloading in the 'Student' class allows flexibility, it may introduce complexity if misused, such as unintentionally calling an incorrect version due to similar method signatures. Overloading requires careful handling to ensure that method calls are intuitive, reflecting intended parameter use. Additionally, maintaining overloaded methods can increase maintenance overhead due to similar yet distinct implementations, potentially leading to inconsistencies if updates are not uniformly applied .

Default values ensure that objects have a predictable initial state even when no explicit data is provided at creation. In the 'Student' class, the parameters initialize with 'unknown' for name, '0' for age, and 'not available' for address by default. Parameterized initialization allows setting specific values, providing flexibility and specificity in object creation. The 'Student' class uses the 'setInfo' methods for parameterized initialization, one for name and age, and another for name, age, and address, showcasing flexibility in setting object state beyond its defaults .

Constructors facilitate object initialization, ensuring data integrity by enforcing initial conditions for data members. In the 'Rectangle' class, different constructors set specific initial values for length and breadth, thereby preventing uninitialized data issues that could lead to undefined behaviors. By providing default and parameterized constructors, the class maintains flexibility and adherence to initialization integrity, allowing objects to be created in a consistent, predictable state .

Default constructors provide a baseline object state, beneficial when specific values are not immediately available. In the 'Rectangle' class, the default constructor sets length and breadth to zero, ensuring that every rectangle object has a valid, albeit minimal, state. Parameterized constructors allow for more precise initialization, as seen in the 'Rectangle' class's constructors that accept one or two parameters. These enable varying object configurations and enhance the class's utility in different contexts without modifying existing code structures .

Object-oriented programming promotes code reuse through encapsulation, inheritance, and polymorphism, enabling modular and maintainable code segments. Classes like 'Student' and 'Rectangle' demonstrate encapsulation by bundling data and functionality, simplifying component reuse in different programs with minimal changes. Encapsulated methods and constructors provide specific functionality, allowing objects to be reused across differing contexts while maintaining easy maintenance and enhancement through internal abstraction, ensuring ease of updates without pervasive code changes .

Method overloading and constructors both enhance a class's flexibility by accommodating diverse scenarios of data provision and object state. The 'Student' class utilizes overloaded 'setInfo' methods to allow initialization with varying details, enhancing adaptability. Constructors in the 'Rectangle' class offer flexibility through multiple initialization paths: no parameters, identical parameters, or distinct dimensions. This synergy supports varying object instantiation and operational contexts, reducing code duplication and enhancing class resilience against changing requirements .

The 'area' method in the 'Rectangle' class encapsulates a critical functionality – calculating the rectangle's area based on its dimensions. By housing this function within the class, the design ensures that the computation is inherently linked to the object, promoting cohesion. This method abstracts the calculation process, providing a clear interface for this functionality without exposing internal data or requiring external computation, aligning with OOP principles of encapsulation and abstraction .

You might also like