You are on page 1of 10

STAMFORD UNIVERSITY

BANGLADESH

Assignment
COURSE CODE: CSI 233
COURSE NAME: Advanced Programming

Submitted To:
Name: Tanveer Ahamed
Designation: Senior Lecturer
Department of Computer Science & Engineering
Stamford University Bangladesh

Submitted By:
Name: Abu Sayeed Khudri
ID: CSE 06607682
Batch No:66(B)
Program: B.Sc. in Computer Science and Engineering
STAMFORD UNIVERSITY BANGLADESH

DATE OF SUBMISSION :11/06/2020


Answer to the question no 1(a):
Protection Class:
package pckg1;

public class Protection {


int n = 1;
private int n_pri = 11;
protected int n_pro = 22;
public int n_pub = 33;
public Protection()
{
System.out.println("base constructor");
System.out.println("n = " + n);
System.out.println("n_pri = " + n_pri);
System.out.println("n_pro = " + n_pro);
System.out.println("n_pub = " + n_pub);
}
}
Derived Class:
package pckg1;

public class Derived extends Protection


{
public Derived()
{
System.out.println("derived constructor");
System.out.println("n = " + n);
System.out.println("n_pro = " + n_pro);
System.out.println("n_pub = " + n_pub);
}
}
SamePackage Class:
package pckg1;

public class SamePackage


{
public SamePackage()
{
Protection p = new Protection();
System.out.println("same package constructor");
System.out.println("n = " + p.n);
System.out.println("n_pro = " + p.n_pro);
System.out.println("n_pub = " + p.n_pub);
}
}
Main Class:
package pckg1;

public class MainClass


{
public static void main(String[] args)
{
Derived d1 = new Derived();
SamePackage sp1 = new SamePackage();

}
}

Answer to the question no 2(a):


Protection2 Class:
package pckg2;

public class Protection2 extends pckg1.Protection{

public Protection2(){
System.out.println("derived package2 constructor");
System.out.println("n_pro = " + n_pro);
System.out.println("n_pub = " + n_pub);

}
}

OtherPackage Class:

package pckg2;

public class OtherPackage


{
public OtherPackage()
{
pckg1.Protection p = new pckg1.Protection();
System.out.println("Other package constructor");
System.out.println("n_pub = " + p.n_pub);
}
}

Main Class:

package pckg2;

public class MainClass2


{
public static void main(String[] args)
{
Protection2 p2 = new Protection2();
OtherPackage op = new OtherPackage();
}
}

Answer to the question no 3(a)-i:


IntStack Interface:
public interface IntStack
{
void push(int item);
int pop();
}

FixedStack Class:
public class FixedStack implements IntStack
{
private int stck[];
private int tos;

FixedStack(int size)
{
stck = new int[size];
tos = -1;
}
@Override
public void push(int item)
{
if(tos==stck.length-1)
System.out.println("Stack is full.");
else
stck[++tos] = item;
}

@Override
public int pop()
{
if (tos < 0)
{
System.out.println("Stack underflow.");
return 0;
} else
{
return stck[tos--];
}
}
}

Test Class:
public class Test

{
public static void main(String[] args)

{
FixedStack mystack1 = new FixedStack(5);
FixedStack mystack2 = new FixedStack(8);
for(int i=0; i<5; i++) mystack1.push(i);
for(int i=0; i<8; i++) mystack2.push(i);
System.out.println("Stack in mystack1:");
for(int i=0; i<5; i++)
System.out.println(mystack1.pop());
System.out.println("Stack in mystack2:");
for(int i=0; i<8; i++)
System.out.println(mystack2.pop());
}
}

Answer to the question no 3(a)-ii:


IntStack Interface:
public interface IntStack
{
void push(int item);
int pop();
}

DynStack Class:
public class DynStack implements IntStack
{
private int stck[];
private int tos;
DynStack(int size)
{
stck = new int[size];
tos = -1;
}
@Override
public void push(int item)
{
if(tos==stck.length-1)
{
int temp[] = new int[stck.length * 2];
for(int i=0; i<stck.length; i++) temp[i] = stck[i];
stck = temp;
stck[++tos] = item;
}
else
stck[++tos] = item;
}

@Override
public int pop()
{
if(tos < 0)
{
System.out.println("Stack underflow.");
return 0;
}
else
return stck[tos--];
}
}

Test2 Class:

public class Test2


{
public static void main(String[] args)
{
DynStack mystack1 = new DynStack(5);
DynStack mystack2 = new DynStack(8);
for(int i=0; i<12; i++) mystack1.push(i);
for(int i=0; i<20; i++) mystack2.push(i);
System.out.println("Stack in mystack1:");
for(int i=0; i<12; i++)
System.out.println(mystack1.pop());
System.out.println("Stack in mystack2:");
for(int i=0; i<20; i++)
System.out.println(mystack2.pop());
}
}

Answer to the question no 4(a):

import java.util.Random;

public class TryCatchStatement


{
public static void main(String args[])
{
int a=0, b=0, c=0;
Random random = new Random();

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


{
try
{
b = random.nextInt();
c = random.nextInt();
a = 12345 / (b/c);
}
catch (ArithmeticException e)
{
System.out.println("Division by zero.");
a = 0;
}
System.out.println("a: " + a);
}
}
}

Theory Assignment:
Answer to the question no 5(a):
public class TryCatchThrowsFinally
{
void procA()
{
System.out.println("Multi catch clauses:");
try
{
int a =5;
System.out.println("a = " + a);
int b = 42 / a;
int c[] = { 1 };
c[42] = 99;
}
catch(ArithmeticException e)
{
System.out.println("Divide by 0: " + e);
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Array index oob: " + e);
}
System.out.println("After try/catch blocks.");
}
void procB()
{
System.out.println("Nested try statement:");
try
{
int a =5;
int b = 42 / a;
System.out.println("a = " + a);
try
{
if(a==1) a = a/(a-a);
if(a==2) {
int c[] = { 1 };
c[42] = 99;
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array index out-of-bounds: " + e);
}
}
catch(ArithmeticException e)
{
System.out.println("Divide by 0: " + e);
}
}
void procC()
{
System.out.println("Finally:");
try
{
System.out.println("inside procC");
} finally
{
System.out.println("procC's finally");
}
}
public static void main(String args[])
{
TryCatchThrowsFinally obj = new TryCatchThrowsFinally();
obj.procA();
obj.procB();
obj.procC();
}
}

You might also like