Vous êtes sur la page 1sur 9

CHANDIGARH UNIVERSITY Gharuan, Mohali

Institute/Department:AIT-CSE
Division: AIML

Subject Name: Programming in Java

Subject Code:208

Assignment No.:2

Max. Marks:15

Date of Allotment:15/9/19

Last date of Submission:22/9/19

Name :- Jaskaran Singh

UID :- 18BCS6031

SET –C
Q1 :- Write a program to achieve multiple inheritance in Java.

Ans :-

Java does not support multiple inheritance by default, as it causes diamond ambiguity, to
prevent that , there is an alternative to use interface class, which can even be multiple
implemented , and their behavior can be set to default or override as desired.
Source Code :-

interface Audi

int pace = 160;

void distance();

interface BMW
{

int distance = 1000;

void pace();

class Cars implements Audi,BMW

int distanceTravel;

int avgPace;

public void distance()

distanceTravel=pace*distance;

System.out.println("Total Distance Travelled is : "+distanceTravel);

public void pace()

int avgPace= distanceTravel/pace;

System.out.println("Average Speed maintained is : "+avgPace);

public class assign2{

public static void main(String args[])

Cars c1=new Cars();

c1.distance();

c1.pace();

Output :-
Ques 2:- Explain the purpose of super and final ?

Ans:- SUPER keyword :-

 Used to access variables and functions/methods of main or super class from which the
present class is extended , when these are overridden by variables and methods of
subclasss.

FINAL keyword :-

 With variables :- Its values cannot be reassigned once done, either hard coded or using
class constructor.
 With methods :- The methods having keyword FINAL cannot be rewritten or overridden
, they can only be called in class objects.
 With classes :- A class declared as FINAL cannot be inherited by other classes, but their
methods can be used by the objects made from that class.

Ques 3 :- With the help of a program explain thread synchronization

Answer :-

Many a times inside a program , multiple threads try to access the same resource which can produce an
unforeseen result due to continuous overwriting of data, as at one time where one thread may close the
file , the other might open it , leading to a huge set of uncertainty.

So to prevent this from happening, we use monitor to control the access , ensuring only one thread
accesses files once.

The class being used shall extend methods from

Source Code :-

class PrintResult {

public void Count() {

try {

for(int i = 5; i > 0; i--) {

System.out.println("Counter : " + i );
}

} catch (Exception e) {

System.out.println("Thread interrupted.");

class ThreadResult extends Thread {

private Thread thread;

private String threadName;

PrintResult PR;

ThreadResult( String name, PrintResult pr) {

threadName = name;

PR = pr;

public void run() {

synchronized(PR) {

PR.Count();

System.out.println("Thread " + threadName + " is exiting.");

public void start () {

System.out.println("Starting " + threadName );

if (thread == null) {

thread = new Thread (this, threadName);

thread.start ();
}

public class assign {

public static void main(String args[]) {

PrintResult PR = new PrintResult();

ThreadResult T1 = new ThreadResult( "Thread 1 : ", PR );

ThreadResult T2 = new ThreadResult( "Thread 2 : ", PR );

T1.start();

T2.start();

try {

T1.join();

T2.join();

} catch ( Exception e) {

System.out.println("Interrupted");

OUTPUT :-
Ques 4:- Write a brief note on Iterator? Illustrate it with an Example

Ans :-

Iterator is a Collection framework of Java which is used to retrieve items one by one.

It is universal and could be applied to any Collection object , and can perform both read and remove
operations. It is an improved version of Enumeration , it is applied on collection like List, Set, queue etc.

The iterator object can be called by iterator method.

It usually has 3 methods :-

.hasNext() : returns Boolean true if another element exists;

.next() : to refer to present element

.remove : removes next element of iterations.

Source Code :-

Removing odd elements from list.

import java.util.ArrayList;

import java.util.Iterator;

public class assign2

public static void main(String[] args)

ArrayList listlist = new ArrayList();


for (int i = 0; i < 10; i++)

listlist.add(i);

System.out.println(listlist);

Iterator iterate = listlist.iterator();

while (iterate.hasNext())

int i = (Integer)iterate.next();

System.out.print(i + " ");

if (i % 2 != 0)

iterate.remove();

System.out.println();

System.out.println(listlist);

OUTPUT :-

Ques 5 :- Differentiate throw and throws. Explain it with a Program

Ans :- Usage :-

 Throw :- it is used inside a function , and is required when a logical exception is to be


thrown.
 Throws :- It is used when , the function is having statements that might lead to some
exceptions.

Number :-

 Throw :- Can only give exception explicitly , throwing one exception at a time.
 Throws :- It can be used to declare multiple exceptions , if any case matches, it is
automatically thrown.

Syntax :-

 Throw :- throw newArithmeticException();


 Throws :- void base() throws ArithmeticException, NullPointerException { }

Motion :-

 Throw :- Only used to propagate non declared exceptions by throws keyword in the
beginning.
 Throws :- Only used to propagate the declared exceptions .

Example :-

 Throw :-

public class test {

public static void main(String[] args)

try {

double x=3/0;

throw new ArithmeticException();

catch (ArithmeticException e)

e.printStackTrace();

 Throws :-

import java.io.IOException;
public class Throws {

public static void main(String[] args)

throws IOException

Vous aimerez peut-être aussi