Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
BILAL AHMED
BT428808
PL-2 (3411)
AUTUMN 2019
BSCS
Structured Programming
class Car(object):
def __init__(self, model, passengers, color, speed):
self.model = model
self.passengers = passengers
self.color = color
self.speed = speed
def accelerate(self):
self.speed = self.speed + 2
print (self.speed)
bmw.accelerate()
print (bmw.color)
ferrari.accelerate()
print (ferrari.color)
ferrari.accelerate() ]
print (ford.passengers)
ford.accelerate()
“Difference Between structure in structure programming language and
Class in object oriented programming language”
Structured Programming:
1. Structured Programming is designed which focuses on process.
2. Structured programming follows top-down approach.
3. In Structured Programming, Programs are divided into small self-
contained functions.
4. Structured Programming provides less reusability, more function
dependency.
5. Less abstraction and less flexibility.
Question#2
Why inheritance is needed in object-oriented programming
languages? Write a program in java to demonstrate the concept
of inheritance and polymorphism.
“The Importance of Inheritance Within OOP”:
In an object-oriented programming language that is well designed,
a function should be able to have functions that reside inside it. In addition to
this, other functions should be processed as input and output as well. When an
OOP language uses these features, it will utilize a design that is simple and
consistent.
This is an important concept that can make the difference between a
good programming language and a great programming language. When code is
written, the subroutine should be capable of returning a function. When this is
done, the argumentcan be raised to a higher nth power. Once this has occurred, it
can be used in a number of different ways.
For example, when two arguments are taken, one can be used as a
function. It can be applied to different arguments many times over, and the result
will stay the same. It will be computed by utilizing a mathematical notion. It can
also be used to implement a number of different systems, and it can help you find
solutions to problems that deal with iteration. It may also be possible for
elements or variables to have a third range of the true or false function. It could
be used as an additional method to decide when the nesting process should be
terminated. When it is viewed in this format, it is the same as being in the "while
loop" that is often viewed in procedural programming languages.
It is also important for you to understand compositions. The
function composition is a mathematical concept. If you use two functions within a
sequence, you can look at it as being a single function that is a composition of the
elements or variables. In mathematics, it may be seen as being viewed in a
number of different ways. In a nutshell, the function will take a set number of
arguments, and it will return one function of the composition. A subroutine will
need to be defined. For example, even if the function is a power of two, the name
and the function will be two different elements. In OOP languages that are well
designed, the definition of a function and a name of a function will not be
inseparable.
Because OOP languages use such a strong linguistic system, classes
can be written by simply extending older subroutines. This can be done in a way
that will allow the new subroutine to hold all the variables and classes of a simple
subroutine with having to deal with the old code that may appear in the body of
the class. This is the foundation of inheritance. Inheritance will occur when an
object inherits data or traits from its parent. Each superclass will be composed of
subclasses, and plays an important role in the object-oriented programming
paradigm.
Program:
Main Class:-
package file
public class File {
public static void main (String [] args) {
Cal obj = new Cal ();
int z=7;
obj.sum();
Cal obj1 = new Cal();
obj1.sum();
}
}
Sub Class:-
public class Cal {
}
public void sum(int a){
int b=7;
System.out.println(a+b);
}
}
Extend Class:-
package file;
Question no 03:
What are exceptions? How exceptions are handled in Java? Write
a program in java to demonstrate the handling of exception and
use try/catch block.
Definition: An exception is an event, which occurs during the execution
of a program, that disrupts the normal flow of the program's instructions.
The runtime system searches the call stack for a method that contains
a block of code that can handle the exception. This block of code is called
an exception handler. The search begins with the method in which the error
occurred and proceeds through the call stack in the reverse order in which the
methods were called. When an appropriate handler is found, the runtime system
passes the exception to the handler. An exception handler is considered
appropriate if the type of the exception object thrown matches the type that can
be handled by the handler.
Program: -
class Example1 {
try {
*/
num1 = 0;
num2 = 62 / num1;
System.out.println(num2);
catch (ArithmeticException e) {
*/
catch (Exception e) {
*/
System.out.println("Exception occurred");
Output:
Question no 04:
Why packages are used in Java programming language? Write a
program in java to describe the procedure of creating a package
in Java.
Packages are used in Java in order to prevent naming conflicts, to
control access, to make searching/locating and usage of classes, interfaces,
enumerations and annotations easier, etc.
Since the package creates a new namespace there won't be any name
conflicts with names in other packages. Using packages, it is easier to provide
access control and it is also easier to locate the related classes.
“Creating a Package”
While creating a package, you should choose a name for the package
and include a package statement along with that name at the top of every source
file that contains the classes, interfaces, enumerations, and annotation types that
you want to include in the package.
The package statement should be the first line in the source file. There
can be only one package statement in each source file, and it applies to all types
in the file.
To compile the Java programs with package statements, you have to use
-d option as shown below.
Java c -d Destination_folder file_name.java
Then a folder with the given package name is created in the specified
destination, and the compiled class files will be placed in that folder.
Example:
Package animals;
Interface Animal {
Question no 05:
What is the difference between CLI and GUI? Briefly discuss few
of the GUI packages used in java with a suitable programming
example.
“What is GUI”
GUI stands for Graphical User Interface. It takes the advantage of
computer graphics. It allows the user to interact with the computer using
components such as windows, icons, labels, text boxes, and radio buttons. It is
easy for the user to perform tasks using GUI as it does not require remembering
commands. He can easily click on icons, drag and drop objects using the mouse.
Programming examples: -
import java.awt.Frame; // Using Frame class in package java.awt
// This subclass inherits all properties from Frame, e.g., title, icon, buttons,
content-pane
......
// methods
new MyGUIProgram();
Example#2
...