Oop
COMSATS UNIVERSITY
ISLAMABAD
DEPARTMENT:
SOFTWARE ENGINEERING
ASSIGNMENT:
OBJECT ORIENTED PROGRAMMING
SUBMITTED BY:
AREEBA SUNDAL(CIIT-SP22-BSE-021WAH)
SUBMITTED TO:
MS. SAMIA ZAFFAR
Oop
Q1: Rectangle Class
CODE:
package javaapplication30;
import java.util.Scanner;
class rectangle{
private int length;
private int width;
private String color;
void setlength(int l){
length = l;
}
void setwidth(int w){
width = w;
}
void setcolor(String c){
color = c;
Oop
}
int getlength(){
return length;
}
int getwidth(){
return width;
}
String getcolor(){
return color;
}
void calcArea(int l , int w ){
double area = l*w;
System.out.println("Area is : "+area);
}
public rectangle(){
}
public rectangle(int l , int w , String c){
length = l;
width = w;
color = c;
Oop
}
public rectangle(rectangle r1){
System.out.println("This is copy constructor");
length = r1.length;
width = r1.width;
color = r1.color;
}
}
public class JavaApplication30 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter length , width and color");
int l = sc.nextInt();
int w = sc.nextInt();
Scanner s = new Scanner(System.in);
String c = s.nextLine();
rectangle r = new rectangle();
System.out.println("this is simple constructor");
r.setlength(l);
r.setwidth(w);
Oop
r.setcolor(c);
System.out.println("details : "+r.getlength() +" "+r.getcolor() +"
"+r.getwidth());
r.calcArea(l, w);
rectangle r1 = new rectangle(l,w,c);
System.out.println("details : "+r.getlength() +" "+r.getcolor() +"
"+r.getwidth());
r1.calcArea(l, w);
rectangle r2 = new rectangle(r1);
System.out.println("details : "+r.getlength() +" "+r.getcolor() +"
"+r.getwidth());
r2.calcArea(l, w);
Q2: Date Class
CODE:
package javaapplication31;
import java.util.Scanner;
Oop
class Date{
private int day;
private int year;
private String month;
void setday(int d){
day = d;
}
void setmonth(String m){
month = m;
}
void setyear(int y){
year = y;
}
int getday(){
return day;
}
int getyear(){
return year;
}
String getmonth(){
return month;
}
Oop
void display(int d , int y , String m ){
System.out.println("Date is : "+d + m +y);
}
public Date(){
}
public Date(int d , int y , String m){
System.out.println("This is parameterized constructor");
day = d;
year = y;
month = m;
}
public Date(Date d1){
System.out.println("This is copy constructor");
day = d1.day;
year = d1.year;
month = d1.month;
}
}
public class JavaApplication31 {
Oop
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter day , year and month");
int d = sc.nextInt();
int y = sc.nextInt();
Scanner s = new Scanner(System.in);
String m = s.nextLine();
Date d0 = new Date();
System.out.println("this is simple constructor");
d0.setday(d);
d0.setmonth(m);
d0.setyear(y);
System.out.println("details : "+d0.getday() +" "+d0.getmonth() +"
"+d0.getyear());
d0.display(d,y,m);
Date d1 = new Date(d,y,m);
System.out.println("details : "+d1.getday() +" "+d1.getmonth() +"
"+d1.getyear());
d1.display(d,y,m);
Date d2 = new Date(d1);
System.out.println("details : "+d2.getday() +" "+d2.getmonth() +"
"+d2.getyear());
d2.display(d,y,m); }
}
Oop