You are on page 1of 5

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){
System.out.println("name is : "+na);
System.out.println("age is : "+ag);
}
void setinfo(String na,int ag,String adr){
System.out.println("name is: "+na);
System.out.println("age is: "+ag);
System.out.println("address is: "+adr);
}
}
public class studentdetail{
public static void main(String[] args){
student obj1=new student();
student obj2=new student();
obj1.setinfo("shanila",19);
obj2.setinfo("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=obj1.area();
System.out.println("area is: "+area);
area=obj2.area();
System.out.println("area is: "+area);
area=obj3.area();
System.out.println("area is: "+area);
}
}
Input:

Output:

You might also like