You are on page 1of 7

COMSATS University Islamabad, Lahore Campus

Department of Computer Science

Assignment 2 – Semester Fall 2022


Course Title: Object Oriented Programming Course Code: CSC241 Credit Hours: 4(3,1)
Course Instructor/s: Mr. Imran latif Program Name: BSECE
Semester: 3rd Section: A, B Batch FA21
Total Marks: 10 Obtained Marks: 60 Minutes
Date:
Student’s Name: Abdullah Tariq Reg. No. Fa20-bce-029
Important Instruction:

 Student is himself/herself responsible for successful submission of assignment on CU-Online


 Your submission must include the following in a single pdf file.
1. Code of all classes
2. Snapshot of the output of submitted code.
 Copied assignment will get zero credit.
 Deadline: November 7, 2021 till 11:30 PM

Question 1:
Recall the concept of Constructors and Composition and write down the code according to
requirements.
Learning Outcome: PLO2→CLO2 (C2, C3, C4)

Question 1:

For each class/attributes described below, choose appropriate data type. All attributes of each class should
be private and exposed via get/set methods. Also define at least one constructor shall take and initialize 2-
3 attributes of the object.

1. Define a class Course with courseCode and courseTitle attributes.


2. Define a class PhoneNumber with countryCode, cityCode and lineNumber attributes.
3. Define a class Address with streetAddress, city, country and phoneNumber attributes. Attribute
phoneNumber shall be of type PhoneNumber.
4. Define a class Student with name, email, cnic, course1, course2, postalAddress and
contactNumber attributes. Where course1 and course2 should be of type Course, postal Address
shall be of type Address; contactNumber should be of Type PhoneNumber. Define a constructor
in Student class that takes cnic, name and address only.
Create a StudentTest class, in its main method, create a Student object i.e. student1. Fully initialize its’
all attributes. cnic, name and address shall be initialized by constructor, other attributes shall be initialized
using setter methods. All attributes values shall be taken from user. After the object is fully initialized,
print all, attribute values from student object reference.

Make another object student2, assume the student live at same address as student1. Reuse the address
object of student1 to initialize student2 address. You do not need to take attributes from user input for

student2 object. Change some attribute of address from student1 and check, does it also change for
student2, understand why and why not?

SOLUTION:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

class Course{
private int cCode;
private String cTitle;
Course(int cCode,String cTitle){
this.cCode=cCode;
this.cTitle=cTitle;
}
@Override
public String toString() {
return "cCode=" + cCode + ", cTitle=" +
cTitle;
}

}
class PhoneNumber{
private int countryCode;
private int cityCode;
private int lineNumber;
PhoneNumber(int countryCode,int cityCode,int
lineNumber ){
this.countryCode=countryCode;
this.cityCode=cityCode;
this.lineNumber=lineNumber;
}
@Override
public String toString() {
return "countryCode=" + countryCode + ",
cityCode=" + cityCode + ", lineNumber=" + lineNumber
;
}

}
class Address{
private String sAddress;
private String town;
private String city;
private String country;
private PhoneNumber p;
Address(String sAddress,String town,String
city,String country,PhoneNumber p){
this.sAddress=sAddress;
this.town=town;
this.city=city;
this.country=country;
this.p=p;
}
@Override
public String toString() {
return "sAddress=" + sAddress + ", town=" +
town + ", city=" + city + ", country=" + country;
}
}
class Student{
private String name;
private String email;
private String cnic;
private Course c;
private Address a;
private PhoneNumber p;
Student(String name,String email,String
cnic,Course c,Address a,PhoneNumber p){
this.name=name;
this.email=email;
this.cnic=cnic;
this.a=a;
this.c=c;
this.p=p;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getCnic() {
return cnic;
}
public void setCnic(String cnic) {
this.cnic = cnic;
}
public Course getC() {
return c;
}
public void setC(Course c) {
this.c = c;
}
public Address getA() {
return a;
}
public void setA(Address a) {
this.a = a;
}
public PhoneNumber getP() {
return p;
}
public void setP(PhoneNumber p) {
this.p = p;
}
@Override
public String toString() {
return "Student [name=" + name + ", email="
+ email + ", cnic=" + cnic + ", c=" + c + ", a=" + a
+ ", p=" + p
+ "]";
}

public class StudentTest{


public static void main(String args[]) {
String name;
String email;
String q;
Scanner sc=new Scanner(System.in);
System.out.println("Enter student1
deatils");
name=sc.next();
email=sc.next();
q=sc.next();
System.out.println("Enter course details");
String Course=sc.next();
int code=sc.nextInt();
Course c=new Course(code,Course);
System.out.println("Enter Number details");
int ccnumber=sc.nextInt();int
cnumber=sc.nextInt();int lnumber=sc.nextInt();
PhoneNumber p=new
PhoneNumber(ccnumber,cnumber,lnumber);
System.out.println("Enter Address details");
String sa=sc.next();String
t=sc.next();String city=sc.next();String
country=sc.next();
Address a=new Address(sa,t,city,country,p);
Student s=new Student(name,email,q,c,a,p);
System.out.println("Enter student2
details");
String name1=sc.next();String
email1=sc.next();String cninc=sc.next();
System.out.println("Enter Address details");
String sa1=sc.next();String
t1=sc.next();String city1=sc.next();String
country1=sc.next();
Address a1=new
Address(sa1,t1,city1,country1,p);
Student s2=new
Student(name1,email1,cninc,c,a1,p);
System.out.println(s);
System.out.println(s2);
}
}

OUTPUT:

You might also like