Sie sind auf Seite 1von 7

Lovely Professional University

Home Work 1
CAP 310: Modern Programming Tools and Techniques -I

Homework Title / No. : __1_________________________Course Code : CAP310_________

Course Instructor : Lect. Balraj Kumar____________ Course Tutor (if applicable) : ____________

Date of Allotment : _____________________ Date of submission : __22nd Feb_____________

Student’s Roll No._RTb805A01_______ Section No. : _TB805________________________

Declaration:
I declare that this assignment is my individual work. I have not copied from any other student’s work
or from any other source except where due acknowledgment is made explicitly in the text, nor has
any part been written for me by another person.

Student’s Signature : _Arun Khaju ria____

Evaluator’s comments:
_____________________________________________________________________

Marks obtained : ___________ out of ______________________

Content of Homework should start from this page only:

Part A
Q:1 Define abstraction, encapsulation, modularity and hierarchy in your own
terms.

Ans

Abstraction

Abstraction is the techinique by virtue of which we can represent the essential features of a
program excluding the details of program. It involves only what the system
performs and excluding the internal details of its functioning (what and how it
happens in the background is hidden)
E.g. consider the case of a train .driver only drive the train without considering what
goes under the body or engine of the train to move. Thus abstraction hides the basic
details in performing a particular function.

Encapsulation: The process of wrapping up variables and methods into a single unit
called class is known as encapsulation it is done to protect the variables and
methods from unauthorised access and misuse.

E.g.The human body is encapsulated by skin

Modularity: The process of dividing a sortware into modules is known as modularity


it makes the program easily understandable, easy to make and easy to modify. Using
modularity technique the program can be easily managed.

Hierarchy: The concept of arranging the variables and methods in a program in a top-
down order.

Example of the train. From the outside train is a single object, once inside one can
see various parts arranged together to work in a co-ordinated manner to run the train.

Q:2 Sketch the object-oriented design or the Card game Black-Jack. What are
the key objects? What are the properties and behaviours of these objects?
How does the object interact?

The object-oriented design of the Card game Black-Jack is shown below with class
associated with data and methods.

Following are the classes in card game black jack

Class hand:

( hand which holds various cards and cards that it gives and gets and shows cards)

Class player:

( player holds card and plays with cards)

Class deck:
(vector cards, shuffledcard, next card to be suffed, next item)

Class card :

(type of card ,value( of card e.g. a,2,3.......),type of suits, )

Methods : string getName()

int getValue()

string getSuit(),

Void addToHand

Void showHand()

Boolean morecard(), void getcard(), void showHand(), int getvalueOfHand().

void completeDeck(), void createHearts(), void createSpades(), void createClubs(), void


createDiamonds().

Various constants are declared according to the situation

With the help of objects these functions are called

They are defined in various classes

Q:3 Sketch the object-oriented design of a system to control a Soda


dispensing machine. What are the key objects? What are the properties and
behaviours of these objects? How does the object interact?

The object-oriented design of the Soda dispensing machine is shown below with class
associated with data and methods.

Following are the classes in Soda dispensing machine

Class type

( type of Soda dispensing machine various types of cold drinks that it can dispatch)

Class company:

(describes company that sponsor that machine)


Class quatity

(describes the quantity that is poured in each glass of soda)

Class flavour

(type of flavour, that is poured and how many flavour are there in a machine )

Class alert

( this is real time class which alerts when container of either water and flavour are empty )

Class no_of cups

(class which tells no of cup to be poured)

Methods : void getsoda()

void poursoda()

Void alert()

void no_ofcups( int )

Boolean moresoda(),void inputflavour(), void inputwater(),

Various constants are declared according to the situation

With the help of objects these functions are called

They are defined in various classes

Part B

Q:4 In an object oriented inheritance hierarchy, the objects at each level are
more specialized than the objects at the higher levels. Give three real
world examples of a hierarchy with this property.

Ans example 1: grand parent is a base class and father inherits some of the
grand parent class and son inherits the features of father class
example 2 vechiles ->car->maruti800

example 3: animals->mammals ->dog->bulldog

example 4: UGC->LPU->LIM

Q:5 How do methods System.out.print() and System.out.println() differ?


Define a java constant equal to 2.9979 X 108 that approximates the
speed of light in meters per second.

Ans

System.out.print() System.out.println()

1. prints the items or elements as System.out.println() prints the output in


output in a single line the next line.
2. It is same to printf function It is same to printf (“\n “)function
3. System.out.print(“Welcome in”); System.out.println(“Welcome in”);
System.out.print(“home ”);
System.out.println(“home ”);

Output
Welcome in home
Output

Welcome in

home

Class arun

public static void main(string args[])

{
final double s=2.9979E8;

}
}

Q:6 Write a code segment that defines a Scanner variable stdin that is
associated with System.in. The code segment should than define to int
variables a and b, such that they are initialized with the next two input
values from the standard input stream.

Ans: import java.util.Scanner;

class arun

public static void main(String args[])

Scanner stdin= new Scanner(System.in);

int x,y,z;

System.out.println(“Enter the first no”);

x=stdin.nextInt();

System.out.println(“Enter the second no”);

y= stdin.nextInt();

z=x+y;

System.out.println(“the sum is”+y);

Q:7 Separately identify the keywords, variables, classes, methods and


parameters in the following definition:
import java.util.*;
public class test
{
public static void main(String[] args)
{
Scanner stdin = new Scanner(System.in);
System.out.print(“Number:”);
double n = stdin.nextDouble();
System.out.println(n + “ * “ + n + “ = “ + n *
n);
}
}

Method: public static void main(), System.out.print(), System.out.println().

Classes: Scanner, test

Variables: stdin, n

Keywords: import, public, class, static, void, new, double

Parameters: String[] args , system.in

Das könnte Ihnen auch gefallen