0% found this document useful (0 votes)
115 views12 pages

Lab Task 2 Oop

This document contains 7 lab tasks related to object-oriented programming concepts in Java. Each task involves defining a class for a real-world object like Student, Time, Car, etc. For each class, the tasks identify appropriate data members and methods. Solution code and runner files are provided to demonstrate creating objects and using methods.
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)
115 views12 pages

Lab Task 2 Oop

This document contains 7 lab tasks related to object-oriented programming concepts in Java. Each task involves defining a class for a real-world object like Student, Time, Car, etc. For each class, the tasks identify appropriate data members and methods. Solution code and runner files are provided to demonstrate creating objects and using methods.
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
You are on page 1/ 12

LAB NO 2:

NAME: SHAYAN ZAMEER

REG NO: SP21 BCS 088

SUBMITTED TO:
MAM SNEHA AMIR

OBJECT ORIENTED PROGRAMMING

COMSATS UNIVERSITY ISLAMABAD


(CUI)
Lab Task 1

A Student is an object in a university management System. Analyze the concept and


identify the data members that a Student class should have. Also analyze the
behavior of student in a university management System and identify the methods that
should be included in Student class.

SOLUTION CODE:
import java.util.Scanner;
public class student{
String studentname;
String fathername;
String regnumber;
int age;
double cgpa;
int rollno;

public void display(){


System.out.println("Student name is" + studentname);
System.out.println("fathername is" + fathername);
System.out.println("Registration Number is" + regnumber);
System.out.println("Age of student is" + age);
System.out.println("cgpa of student is" + cgpa);
System.out.println(" Roll No of Student is" + rollno);
}

RUNNER FILE:

import java.util.Scanner;

public class runnerstudent{


public static void main(String[] args) {
Scanner input = new Scanner (System.in);
student obj = new student();

obj.studentname="Shayan";
obj.fathername="Zameer Ahmad";
obj.regnumber ="SP21bcs088";
obj.age = 20;
obj.cgpa=3.00;
obj.rollno = 88;

obj.display();

Lab Task 2

Time is an intangible concept. Analyze the concept and identify the data members
and methods that should be included in Time class.

SOLUTION CODE:

public class time{


int hours;
int minutes;
int seconds;

public void display(){


System.out.println("HOURS IN TIME ARE " + hours);

System.out.println("Seconds IN TIME ARE " + seconds);


System.out.println("Minutes IN TIME ARE " + minutes);

}
}

RUNNER FILE:

import java.util.Scanner;
public class runnertime{
public static void main(String[] args) {

Scanner input = new Scanner(System.in);

time obj1= new time();

obj1.hours =2;
obj1.minutes=25;
obj1.seconds= 53;
obj1.display();
}
}

Lab Task 3
Car is an object that helps us in transportation. Analyze the concept and
identify the data members and methods that should be included in Car class.

SOLUTION CODE:

public class car{

String carname;
int model;
int enginenumber;
double enginecapacity;
String companyname;
public void display(){
System.out.println("car name is "+ carname);
System.out.println("model number is "+ model);
System.out.println("engine number is "+ enginenumber);
System.out.println("engine capacity is "+ enginecapacity);
System.out.println("company name is "+ companyname);
}

RUNNER FILE:

import java.util.Scanner;

public class runnercar{


public static void main(String[] args) {

Scanner input=new Scanner(System.in);

System.out.println("*****************************");
System.out.println("NEXT QUESTION");

car obj2= new car();

System.out.println("ENTER CARNAME");
obj2.carname= input.next();
System.out.println("ENTER MODEL Number ");
obj2.model= input.nextInt();
System.out.println("ENTER ENGINE NUMBER");
obj2.enginenumber= input.nextInt();
System.out.println("ENTER ENGINE CAPACITY");
obj2.enginecapacity= input.nextDouble();
System.out.println("ENTER COMPANY NAME");
obj2.companyname= input.next();

System.out.println(" RESULT IS GIVEMN BELOW");


System.out.println("");
obj2.display();

}
Lab Task 4

Rectangle is an object that represents a specific shape. Analyze the concept and
identify the data members and methods that should be included in Rectangle class.

SOLUTION CODE:

class rectangle{
double length;
double breath;
String colour;

public double area(double l,double b){


double a= 2*(l+b);
return a;
}

public void display(){


System.out.println("LENGTH OF RECTANGLE IS "+length);
System.out.println("BREATH OF RECTANGLE IS "+length);

System.out.println("COULOUR OF RECTANGLE IS "+length);


}

RUNNER FILE:

import java.util.Scanner;
public class runnerrectangle{
public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("*****************************");
System.out.println("NEXT QUESTION");

rectangle obj3= new rectangle();

System.out.println("ENTER LENGTH OF RECTANGLE");


obj3.length= input.nextDouble();
System.out.println("ENTER WIDTH OF RECTANGLE");
obj3.breath= input.nextDouble();
System.out.println("ENTER COLOUR OF RECTANGLE");
obj3.colour= input.next();

System.out.println("RESULTS ARE GIVEN BELOW");

obj3.display();
System.out.println("AREA IS AS FOLLOW");
System.out.println(obj3.area(20.2,2.0));
}
}

Lab Task 5

Distance is an object that represents a specific distance in feet and inches. Analyze
the concept and identify the data members and methods that should be included in
Distance class.

SOLUTION CODE:

class distance{
double distanceinkm;
double distanceinfeet;
double distanceinmeter;
double distanceininches;
double distanceinmiles;

public void display(){


System.out.println("DISTANCE IN KM "+distanceinkm);
System.out.println("DISTANCE IN FEET "+distanceinfeet);
System.out.println("DISTANCE IN METER "+distanceinmeter);
System.out.println("DISTANCE IN INCHES "+distanceininches);
System.out.println("DISTANCE IN MILES "+distanceinmiles);
}
}
RUNNER FILE:
import java.util.Scanner;
public class runnerdistance{
public static void main(String[] args) {

Scanner input = new Scanner(System.in);

distance obj4= new distance();

obj4.distanceinkm = 43.0;
obj4.distanceinmeter = 23000.0;
obj4.distanceinfeet = 2543.0;
obj4.distanceinmiles = 9.0;
obj4.distanceininches = 247563465837.0;

obj4.display();

}
}

Lab Task 6

Point is an object that represents a a specific location in x, y plane. Analyze the


concept and identify the data members and methods that should be included in Point
class.

SOLUTION CODE:
class point{
double pwxaxis;
double pwyaxis;
double pwzaxis;

public void display(){


System.out.println("POSITION WRT X- AXIS IS"+pwxaxis);
System.out.println("POSITION WRT Y- AXIS IS"+pwyaxis);
System.out.println("POSITION WRT Z- AXIS IS"+pwzaxis);
}
}
RUNNER FILE:

import java.util.Scanner;

public class runnerpoint{


public static void main(String[] args) {

Scanner input = new Scanner(System.in);

point obj5 = new point();

System.out.println("ENTER POSITION OF POINT WRT X-


AXIS");
obj5.pwxaxis = input.nextDouble();
System.out.println("ENTER POSITION OF POINT WRT Y-
AXIS");
obj5.pwyaxis = input.nextDouble();
System.out.println("ENTER POSITION OF POINT WRT Z-
AXIS");
obj5.pwzaxis = input.nextDouble();

System.out.println("RESULTS ARE GIVEN BELOW");


obj5.display();

}
}

Lab Task 7

Circle is an object that represents a specific shape. Analyze the concept and identify
the data members and methods that should be included in Circle class.

SOLUTION CODE:
class circle{
double radius;

String colour;

public double circumference(double r){


double circum = 2*3.14*3.14*r;
return circum;
}
public double area(double r){
double area = 3.14*r*r;
return area;
}

public void display(){


System.out.println("RADIUS OF CIRCLE IS "+ radius);

System.out.println("COLOUR OF CIRCLE IS "+ colour);

RUNNER FILE:

import java.util.Scanner;
public class runnercircle{
public static void main(String[] args) {

Scanner input = new Scanner(System.in);

circle obj6 = new circle();

System.out.println();

System.out.println(" RESULTS ARE AS FOLLOWS");

System.out.println("ENTER RADIUS OF CIRCLE");


double rr= obj6.radius= input .nextDouble();
System.out.println("ENTER COLOUR OF CIRCLE");
obj6.colour= input .next();

obj6.display();

System.out.println("CIRCUMFERENCE OF CIRCLE IS");


System.out.println(obj6.circumference(rr));
System.out.println("AREA OF CIRCLE IS");
System.out.println(obj6.area(rr));

You might also like