Uploaded by Abc

OOP document

advertisement
Pir Mehr Ali Shah
Arid Agriculture University, Rawalpindi
Office of the controller of Examinations
Final Exam / Spring 2021 (Paper Duration 12 hours)
To be filled by Teacher
Course No.:
CS-423
Total Marks:
Course Title:
_______150_(30)__
Object Oriented Programming
9th July 2021_______
Date of Exam: ___
Degree: _____BSCS, BSIT___ Semester: ___Second, Third_____ Section:
Q.No.
1
2
3
4
5
6
7
8
9
.
10
Marks
Obtained
Total Converted Marks in Words:
Name of the teacher: Mr. Aftab, Mr. Zahid, Mr. Afrasiab
Who taught the course: Signature of teacher / Examiner:
A, B, C, D, E________.
Marks Obtained
/
Total Marks
/150
_____.
Converted Marks
Obtained /
Total Marks
/30
To be filled by Student
Reg No.: …..…-arid-…..… Name:…………………………………….…………………….. Semester:…….……Section:…....
Notes:
1. By filling out your name and registration number above, you Pledge that: “I affirm
that I have not given or received any unauthorized help on this exam/assignment,
and that this work is my own.”
2. Any student found breaching the “Regulations Relating to the Examinations of
PMAS-Arid Agriculture University, Rawalpindi”, will face severe penalties.
3. Any form of Cheating, Unauthorized Help, and/or Plagiarism found in the Answers
given below will have severe penalty for the student, ranging from cancellation of
paper to suspension from the University rolls for up to 2 years.
Answer the following questions.
Q.No.1.
(Marks: 15)
The atoms of different elements have different numbers of protons, neutrons and electrons. Electrons
are negatively charged; protons are positively charged and neutrons have no charge.
1. Write a definition for an atom class that contains:
a. Fields for storing the numbers of protons, neutrons and electrons with appropriate
visibility;
b. Setter and getter methods for manipulating these fields, ensuring that the minimum
value for electrons and protons is 1, and the minimum value for neutrons is 0;
c. A constructor that initializes new objects of atom to be the smallest element
(Hydrogen), for which the number of protons is 1, the number of neutrons is 0, and
the number of electrons is 1.
2. Write a new method for the atom class called ision that will return true or false, depending
upon whether the atom is an ion. An atom is an ion if it is charged (i.e., if the number of
electrons ≠ the number of protons).
3. Write a new method for the atom class called getAtomicMassNumber that will calculate and
return the atomic mass number of the atom. Atomic mass number of an atom (often denoted
A) is defined as the number of protons plus the number of neutrons.)
Answer:
Q.No.2.
(Marks: 15)
Create a class (in JAVA) for FileHandling with following members
1. Final and Static field named as “bookPath”, which holds the address for the file, i.e;
“Data\book.txt”
2. Final and Static field named as “authorPath”, which holds the address for the file, i.e;
“Data\author.txt”
3. Static method to write data in file. It takes two arguments: data and file path. Data is
inserted at the end of the file while previous data remains. Data is single line to write in the
file.
4. Static method to read data from file. It takes file path and returns whole file data.
Create Main class containing main method and show the working of FileHandling class without
creating the object.
Answer:
Q.No.3.
(Marks: 15)
Consider you are given a very specialized string with a unique format as:
Quiz_(no) # (q.no) : (question statement) $ (option1) , (option 2) , … * (right option number) … \n
String starts with Quiz_ (quiz number like 1, 2, … ), then hash #, then question number (like qno_1,
qno_2 …), then colon :, then question statement, then dollar $, then options (like option1, option 2
…), then asterisk *, and then right option for the given question statement. If more questions are there
for the same quiz, then pattern after hash # is repeated (means every question is separated by #
symbole). Each quiz ends with new line (\n), and then new quiz starts with same pattern as before. A
sample pattern is given below.
Quiz_1# qno_1: What comes next to First $first, second, third, fourth *2 # qno_2: What comes
before second $first, second, third, fourth, fifth *1 \nQuiz_2# qno_1: What is liquid $water,
bread, smoke *1 # qno_3: What is hot $ice-cream, candies, spices *3
You are required to create a program in JAVA, in such a way that if your program receives any string
with this pattern (or format) it should provide output as given below. (Hint: Use split, trim method
to do the job)
Quiz_1
qno_1:What comes next to First
1. frist
2. second
3. third
4. fourth
right option is 2
qno_2:What comes before second
1. first
2. second
3. third
4. fourth
5. fifth
right option is 1
Quiz_2
qno_1:What is liquid
1. water
2. bread
3. smoke
right option is 1
qno_3:What is hot
1. icecream
2. candies
3. spices
right option is 3
Answer:
Q.No.4.
(Marks: 15)
Write the JAVA code for the following UML Diagram
Answer:
Q.No.5.
(Marks: 15)
Following is the Student class in JAVA. You are required to create StudentMatch class to inherit
the Student class. You must override the following methods:
1. hasGreaterAge: returns true if calling object has greater age value then passed object.
2. hasGreaterCGPA: returns true if calling object has greater cgpa value then passed object.
3. hasEqualName: returns true if calling object has exactly equal name then passed object.
4. hasEqualAge: returns true if calling object has equal age then passed object.
5. hasEqualCGPA: returns true if calling object has equal cgpa then passed object.
public class Student {
protected String name;
protected int age;
protected double cgpa;
public Student() {
name = "NULL";
age = 0;
cgpa = 0.0;
}
public Student(String name, int age, double cgpa)
{
this.name = name;
this.age = age;
this.cgpa = cgpa;
}
public Student (Student student)
{
this(student.name,student.age,student.cgpa);
}
public void set(String name,int age, double cgpa)
{
this.set(name);
this.set(age);
this.set(cgpa);
}
public Student getCopy()
{
Student student = new Student(this);
return student;
}
public Student getClone()
{
return this;
}
public boolean hasGreaterAge(Student student)
{
return false;
}
public boolean hasGreaterCgpa(Student student)
{
return false;
}
public boolean hasEqualName(Student student)
{
return false;
}
public boolean hasEqualAge(Student student)
{
return false;
}
public boolean hasEqualCGPA(Student student)
{
return false;
}
public void display()
{
System.out.println("Name : "+ this.name + ", Age : "+ this.age + ",
CGPA : "+ this.cgpa);
}
public String toString() {
String outputString =
this.name+","+this.age+","+this.cgpa+"\n";//"Ali,28,3.21\n"
return outputString;
}
}
Answer:
Q.No.6.
(Marks: 15)
Create an abstract class named Course that can be used with course allocation mechanism of an
enterprise. The Course class should track the course Id Number, course Title and course Credit Hours
with appropriate accessor/getter and mutator/setter methods. Define an equals (Course obj)
method, that returns true if caller and input argument course object have identical course Id
Number.
Also define an abstract method double getCourseEnrollmentFee(). Next, create two additional
classes named ElectiveCourse and CoreCourse that are derived from Course. Finally, create an
overridden method named getCourseEnrollmentFee() calculate and returns the total fee for that
course. Elective course has Rs. 10,000 per credit hours and Core has Rs. 13,000 per credit hours.
Answer:
Q.No.7.
(Marks: 15)
A universal remote has a lot of similarities to an interface. By itself a universal remote does nothing.
It defines a set of buttons for standard operations, but the operation of each button must be
programmed specifically to suit each kind of device that you want to control. You can represent the
TV and VCR by classes, each of which will make use of the same remote-control interface. Universal
remote controls perform common functionality on devices like TV and VCR: power on/off, volume
up/down, Channel up/down, set Specific channel and mute.
Define an interface to model the common functionality of universal remote. Define TV and VCR
classes that use the universal remote. Demonstrate polymorphic behavior with these classes and
create at least two objects of each class in main method. Write some code in main to show the
sample output as well.
Answer:
Q.No.8.
(Marks: 15)
Create an interface named as “TeaService” having following method abstract methods
1. Boling Tea
2. Serving Tea
3. Is Tea Hot
Create another interface named as “BiscuitService” having following method abstract methods
1. Bake Biscuit
2. Serving Biscuit
3. Is Biscuit Baked
Create another interface named as “TeaParty” having only one default method but inherit both
interfaces: “TeaService” and “BiscuitService”. Default method name is “partyStarted” and displays
“Party is started”.
Create a class named “SchoolTeaParty” having two boolean attributes: bakedBiscuit and hotTea.
Complete the class such that for the following main method it produces output as show below.
Main Class with main method
public class Main {
public static void main(String[] args) {
SchoolTeaParty schoolTeaParty = new SchoolTeaParty();
schoolTeaParty.serveBiscuit();
schoolTeaParty.servingTea();
schoolTeaParty.bakeBiscuit();
schoolTeaParty.boilingTea();
schoolTeaParty.serveBiscuit();
schoolTeaParty.servingTea();
schoolTeaParty.partyStarted();
}
}
Output of the main method
We are waiting for baking biscuit
We are waiting for Tea to boil
We are baking biscuit
We are boiling the Tea
We are serving biscuit
We are Serving Tea
Party is started
Answer:
Q.No.9.
(Marks: 15)
You are given a file stored in “D” drive named “input.txt” consist of valid email addresses. The email
account belongs to either Yahoo or Gmail or Hotmail email service only. Each line contains only one
email address. Write a java program which reads the file line by line, creates an output file and writes
down the percentage of yahoo accounts.
Answer:
Q.No.10.
(Marks: 15)
Suppose you have a text file named admission.txt that consists of applicants who have applied for
admission in an IT institute. Data of file consist of multiple rows and each row represents one
record separated by comma character in the following order.
1.
First Name
2.
Last Name
3.
CNIC
4.
Inter marks Percentage
5.
Age
6.
Phone Number
7.
Subjects in Inter (Major Subjects)
There are 200 available seats for current session and first come first served model will be followed.
Admission Eligibility criteria is:
1.
Percentage must be greater than 60
2.
Age must be less than 30
3.
Candidate must have studied Math/Stat or Computer in Inter.
You are required to read and process the text file in a way that data of qualified candidates should
be copied in a file qualified.txt. The data of new file must be comma separated and in the following
format:
1.
Full Name
2.
Phone Number
3.
Eligibility (Yes or NO)
4.
Rejection Reason (Over Age, Low percentage, Missing Prerequisite, All seats reserved or
NA)
Sample admission.txt file format
FirstName,LastName,CNIC,InterPercentage,Age,PhoneNo,Subjects
Ali,Khan,12345-1234561-7,55,25,370-884724,CS-Phy-Math
Ahmed,Ali,12345-1234561-9,70,20,371-818734,ENG-BIO-CHM
Ashraf,Khan,10345-1234211-8,65,22,388-828672,CS-Phy-Math
Sajid,Sheikh,23455-1234301-8,90,34,377-814794,CS-Stat-Math
Sample qualified.txt file format
Name,PhoneNo,IsEligible,Rejection Reason
Ali Khan,344-8184724,No,Low Percentage
Ahmed Ali,341-8184734,No,Missing Prerequisites
Ashraf Khan,344-8184724,Yes,NA
Sajid Sheikh,344-8184724,No,Over Age
Answer:
Download