Вы находитесь на странице: 1из 8

GAGE COLLEGE FACULTY OF TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE


ASSIGNMENT ON
Object Oriented Programming (CSC 231)

Date:____________________

Total Weight:- 20 %

Name __________________________________ID__________ Department ___________________


Section______________ Entry year _____________

GENERAL DIRECTIONS

1. Read the instructions carefully and do accordingly.


2. Submission deadline will decide by the instructor or the college.
3. The assignment should submit with a hard copy.
4. All students should collect the assignment at www.elearningportal.gagecollege.net by
follow the login steps.
5. Use of pencils and red pens are not allowed.
6. For further activities or questions each student can communicate the instructor through
email or telegram and other communication media.
7. Write your answers on the space provided at the end of all questions /or on a separate
sheet of paper/.

Good Luck !!!

RYTHM: Raise yourself To Help Mankind !!!

1
GAGE COLLEGE DEPARTMENT OF COMPUTER SCIENCE OBJECT ORIENTED
PROGRAMMING FINAL EXAM

Name: _______________________________________ID NO:_________ Dept & Sec:_________________

Part One: Say true if the statement is correct false if the statement is incorrect. (10 pts.)

False
_______ 1.A way of simplifying complex reality is called encapsulation
True
_______ 2.A class is a blue print or template that defines states and behavior common to all objects of a
certain kind.
True
_______ 3.A block statement is a group of zero or more statement between balanced braces and can be
used anywhere a single statement is allowed.
True 4. Object is instance of a class
_______
True 5. Java provides a default constructor automatically if the programmer does not define it.
_______
True 6. Default, parameterized and copy constructors are the three types of constructor.
_______
True
_______ 7. In the concept of inheritance the super class does not know the existence of the sub class but
the sub class knows the existence of the super class
False 8. Java does not support multiple inheritances that a derived class can have only one parent class.
_______
True 9. Constructors are used for initializing the instance, variables of an object
_______
True 10. Interface is useful when there is a parent and child class relationships to inherit the property of
_______
parent class.

Part two: Workout questions (10 pts.)

1. Compare and contrast constructor and methods (1pts)


2. Discuss about abstract class.(2 pts)
3. Discuss about object in object oriented programming and write the syntax to create objects.(2 pts)
4. Discuss the three types of access modifiers with examples by creating code fragment of a class
(2pts)
5. Write java program that can guess the date of birth. (3pts)

2
Part two: Workout questions
1) Following are the difference between constructor and method.

 Constructor is used to initialize an object whereas method is used to exhibits functionality of an


object.

 Constructors are invoked implicitly whereas methods are invoked explicitly.

 Constructor does not return any value where the method may/may not return a value.

 In case constructor is not present, a default constructor is provided by java compiler. In the case
of a method, no default method is provided.

 Constructor should be of the same name as that of class. Method name should not be of the
same name as that of class.

2) Abstract Class
An abstract class is a class that is declared abstract — it may or may not include abstract methods.
Abstract classes cannot be instantiated, but they can be subclassed. An abstract class may have static
fields and static methods. When an abstract class is subclassed, the subclass usually provides
implementations for all of the abstract methods in its parent class. However, if it does not, then the
subclass must also be declared abstract.

An abstract method is a method that is declared without an implementation (without braces and
followed by a semicolon)

3) Object − Objects have states and behaviors. Example: A dog has states - color, name,
breed as well as behaviors – wagging the tail, barking, eating. An object is an instance of a
class.

New ClassName (parameters) // to create object


Eg Book b= new Book()

4) three types of access modifiers with examples by creating


code fragment of a class
1) Private
The private access modifier is accessible only within the class.

Simple example of private access modifier


In this example, we have created two classes A and Simple. A class contains private data
member and private method. We are accessing these private members from outside the
class, so there is a compile-time error.

1. class A{
2. private int data=40;
3. private void msg(){System.out.println("Hello java");}
4. }
5.
6. public class Simple{
7. public static void main(String args[]){
8. A obj=new A();
9. System.out.println(obj.data);//Compile Time Error
10. obj.msg();//Compile Time Error
11. }
12. }
R e f Private C struct r

If you make any class constructor private, you cannot create the instance of that class from
outside the class. For example:

1. class A{
2. private A(){}//private constructor
3. void msg(){System.out.println("Hello java");}
4. }
5. public class Simple{
6. public static void main(String args[]){
7. A obj=new A();//Compile Time Error
8. }
9. }
Note: A class cannot be private or protected except nested class.

2) Pr tected
The protected access modifier is accessible within package and outside the package but
through inheritance only.

The protected access modifier can be applied on the data member, method and constructor.
It can't be applied on the class.

It provides more accessibility than the default modifer.


Example of protected access modifier

In this example, we have created the two packages pack and mypack. The A class of pack
package is public, so can be accessed from outside the package. But msg method of this
package is declared as protected, so it can be accessed from outside the class only through
inheritance.

1. //save by A.java
2. package pack;
3. public class A{
4. protected void msg(){System.out.println("Hello");}
5. }
1. //save by B.java
2. package mypack;
3. import pack.*;
4.
5. class B extends A{
6. public static void main(String args[]){
7. B obj = new B();
8. obj.msg();
9. }
10. }
Output:Hello

3) Pub ic
The public access modifier is accessible everywhere. It has the widest scope among all
other modifiers.

Example of public access modifier

1. //save by A.java
2.
3. package pack;
4. public class A{
5. public void msg(){System.out.println("Hello");}
6. }
1. //save by B.java
2.
3. package mypack;
4. import pack.*;
5.
6. class B{
7. public static void main(String args[]){
8. A obj = new A();
9. obj.msg();
10. }
11. }
Output:Hello

5) Write java program that can guess the date of birth.

import java.util.Scanner;

public class GuessBirthday


{
public static void main( String [] args )
{

String set1 =
" 1 3 5 7\n" +
" 9 11 13 15\n" +
" 17 19 21 23\n" +
" 25 27 29 31";

String set2 =
" 2 3 6 7\n" +
" 10 11 14 15\n" +
" 18 19 22 23\n" +
" 26 27 30 31";

String set3 =
" 4 5 6 7\n" +
" 12 13 14 15\n" +
" 20 21 22 23\n" +
" 28 29 30 31";

String set4 =
" 8 9 10 11\n" +
" 12 13 14 15\n" +
" 24 25 26 27\n" +
" 28 29 30 31";

String set5 =
" 16 17 18 19\n" +
" 20 21 22 23\n" +
" 24 25 26 27\n" +
" 28 29 30 31";

int day = 0;

// Create a scanner

Scanner input = new Scanner(System.in);

// Prompt the user to answer questions

System.out.print("Is your birthday in set1?\n");


System.out.print(set1);
System.out.print("\nEnter (Y) for yes or (N) for no: ");
String answer = input.next();

if (answer.equals("Y"))
day += 1;

System.out.print("\nIs your birthday in set2?\n");


System.out.print(set2);
System.out.print("\nEnter (Y) for yes or (N) for no: ");
answer = input.next();

if (answer.equals("Y"))
day += 2;

System.out.print("\nIs your birthday in set3?\n");


System.out.print(set3);
System.out.print("\nEnter (Y) for yes or (N) for no: ");
answer = input.next();

if (answer.equals("Y"))
day += 4;

System.out.print("\nIs your birthday in set4?\n");


System.out.print(set4);
System.out.print("\nEnter (Y) for yes or (N) for no: ");
answer = input.next();

if (answer.equals("Y"))
day += 8;

System.out.print("\nIs your birthday in set5?\n");


System.out.print(set5);
System.out.print("\nEnter (Y) for yes or (N) for no: ");
answer = input.next();

if (answer.equals("Y"))
day += 16;
System.out.println("\nYour birthday is " + day + "!");

}
}

Вам также может понравиться