Karthikeya Java423 File Final

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 55

Amity University Madhya Pradesh,

Gwalior

JAVA Programming Lab


CSE 423
Submitted To: - Submitted By: -
Mrs. Divya Gautam Bilwal Karthikeya Maringanti
Assistant Professor A60205220180
ASET - CSE B.tech CSE, 4th semester
Section-C (C2)
Index
S.no Names of Program Signature Remark
1. Basic Programs
1.1 WAP to add two numbers provided by the user.
1.2 WAP to tell if the number is odd or even.
1.3 WAP to add two binary numbers.
1.4 WAP to find a Prime number.
1.5 WAP to find a Factorial of a number.
1.6 WAP to print a series of Armstrong number.
1.7 WAP to print the area of a right-angled triangle.
1.8 WAP to calculate the area of a circle.
1.9 WAP to demonstrate the switch case.
1.10 WAP to demonstrate if else statement.
2. Array
2.1 WAP to take input and print the array.
2.2 WAP to sum values of an array.
2.3 WAP to find the average values of an array.
2.4 WAP to find the largest element in an array.
3. Control Statement
3.1 WAP to check the season by if and else statement.
3.2 WAP to apply switch case.
3.3 WAP to understand the concept of break.
3.4 WAP to demonstrate working nested loop.
3.5 WAP to show while loop.
4. Classes and object
4.1 WAP to implement the use of class and object.
4.2 WAP to demonstrate the use of class and object.
4.3 WAP to implement the use of copy constructor.
4.4 WAP to show to hide the variable in java.
4.5 WAP to implement class and object.
5. Overloading
5.1 WAP to implement the overloading concept.
5.2 WAP to implement the overloading concept.
5.3 WAP on call by value.
5.4 WAP to show call by reference method.
5.5 WAP to find factorial through recursion.
5.6 WAP to show static method implementation.
5.7 WAP to show static method.
6. Inheritance
6.1 WAP to show concept of inheritance.
6.2 WAP to implement the concept overriding.
6.3 WAP to show the implementation of function overriding.
6.4 WAP to invoke overridden methods from sub-class.
7. Exception Handling
7.1 WAP to implement exception handling.
7.2 WAP to implement multiple catch statements.
7.3 WAP to show implementation of try catch.
7.4 WAP to show the use of finally keyword.
7.5 WAP to implement try catch.
8. JAVA Packages
8.1 WAP to demonstrate abstract class and methods.
8.2 WAP to create and implement package p1.
8.3 WAP to create and implement package p2.
8.4 WAP to show importing of a package.
9. JAVA Interfaces
9.1 WAP to show interface in JAVA.
9.2 WAP to show interface and inheritance.
9.3 WAP to implement multiple inheritance.
10.Multithreading
10.1 WAP to implement thread using thread class.
Introduction to JAVA.
JAVA is a purely object-oriented programming language. It is platform independent
(platform neutral) programming language. It is highly secure, robust and open-source
programming language which makes it popular, also it is easy to use as it is close to C and C+
+. Since, it is a purely object-oriented programming language, it gives a proper structure to
the program and makes it easy to understand and reusable.

History of JAVA.
JAVA was developed by James Gosling un the early 1990s. It was initially developed for
digital devices such as the televisions, etc. Initially it was named as “Oak”, but later it was
renamed as “JAVA”. It was created on the principles like robust, portable, platform
independent, high performance, multithreading, etc. Currently, it is being used in internet
programming, mobile devices, games, e-business solutions etc.

What are JVM, JDK, JRE, JIT?


JVM: - JVM (JAVA Virtual Machine) is an abstract machine that enable a computer to run a
JAVA program. When a JAVA program is executed, JAVA compiler first compiles the
JAVA code to bytecode. Then, the JVM translates bytecode into native machine code.
JDK: - JDK (JAVA Development Kit) is a software development kit required to develop
applications in JAVA. It allows developers to create JAVA programs that can be executed
and run by the JVM and JRE.
JRE: - JRE (JAVA Runtime Environment) is a software package that provides JAVA class
libraries, JAVA Virtual Machine (JVM), and other components that are required to run
JAVA applications.
JIT: - JIT (Just-In-Time) is an integral part of the JVM. It accelerates execution performance
many times over the previous level. It optimizes the performance of the JAVA application at
compile or run time.

Features of JAVA.
Feature of JAVA include:
 Simple
 Object-oriented
 Portable
 Platform independent
 Secured
 Robust
 Architecture neutral
 Interpreted
 High performance
 Multithreaded
 Distributed
 Dynamic

Usage of JAVA.
Usages of JAVA programming language are:
 Mobile App Development
 Desktop GUI Applications
 Web-based Applications
 Gaming Applications
 Big Data Technologies
 Distributed Applications
 Cloud-based Applications
 IoT Applications

Drawbacks of JAVA.
 Takes much longer time to run compared to C and C++.
 Consumes more memory.
 Hardware cost is high.
 Doesn’t support low level programming, like pointers.
 Programmer doesn’t have right to control the garbage collection.
1.Basic programs.
1.1 WAP to add two numbers provided by the user.

public class addtwo {

public static void main(String[] args) {


int num1 = 5, num2 = 15, sum;
sum = num1 + num2;
System.out.println("First number: "+num1);
System.out.println("Second number: "+num2);
System.out.println("Sum of these numbers: "+sum);

2 WAP to tell if the number is odd or even.


public class EvenOdd {

public static void main(String[] args) {


int num = 345;
System.out.println("The given number is: "+num);
if (num%2==0)
System.out.println(num+ " is even.");
else
System.out.println(num+ " is odd.");

1.3 WAP to add two binary numbers.


public class AddBinary {

public static void main(String[] args) {


long bin1 = 1011, bin2 = 1100;
int i = 0, carry = 0;
int[] sum = new int[10];
System.out.println("First binary number: "+bin1);
System.out.println("Second binary number: "+bin2);
while (bin1 != 0 || bin2 !=0)
{
sum[i++] = (int) ((bin1%10 + bin2%10 +carry)%2);
carry = (int) ((bin1%10 +bin2%10 +carry)/2);
bin1 = bin1/10;
bin2 = bin2/10;
}
if(carry != 0) {
sum[i++] = carry;
}
--i;
System.out.println("Output: ");
while(i>=0) {
System.out.print(sum[i--]);
}

1.4 WAP to find the prime number.

import java.util.Scanner;
public class PrimeNumber
{
public static void main(String args[])
{
int num,b,c;
Scanner s=new Scanner(System.in);
System.out.println("Enter A Number");
num =s.nextInt();
b=1;
c=0;
while(b<= num)
{
if((num%b)==0)
c=c+1;
b++;
}
if(c==2)
System.out.println(num +" is a prime number");
else
System.out.println(num +" is not a prime number");
}
}

1.5 WAP to find factorial of a number.

public class FactorialL {


public static void main(String[] args) {
int num = 8;
long fact = 1;
System.out.println("The given number is: "+num);
for(int i=1; i<=num; i++)
{
fact = fact*i;
}
System.out.println("Factorial of the given number is: "+fact);

1.6 WAP to print a series of Armstrong numbers.

public class ArmstrongNumber {


public static void main(String[] args) {
int num, i , rem, temp;
int start = 0, end= 200, counter = 0;
System.out.println("Range Starts from "+start+" to"+end+" .");
for(i=start+1;i<end;i++)
{
temp = i;
num = 0;
while(temp!=0)
{
rem = temp%10;
num = num + rem*rem*rem;
temp = temp/10;
}
if(i==num)
{
if(counter==0)
{
System.out.print("Armstrong numbers are: ");
}
System.out.println(i + " ");
counter++;
}
}
if(counter==0)
{
System.out.println("There are no armstrong number between th
given range.");
}

1.7 WAP to print the area of a right-angled triangle.

public class TriangleArea {


public static void main(String[] args) {
double base = 10.0;
double height = 20.5;
double area = 0;
area = (base*height)/2;
System.out.println("Base of the triangle: "+base);
System.out.println("Height of the trianlge: "+height);
System.out.println("Area: "+area);

1.8 WAP to calculate area of a circle.

public class CircleArea {


public static void main(String[] args) {
double radius = 15.5, area=0.0;
System.out.println("Radius of the circle: "+radius);
System.out.println("\n");
area = 3.14*radius*radius;
System.out.println("Area of the circle: "+area);

1.9 WAP to demonstrate the switch case.


public class switchex {

public static void main(String[] args) {


for(int i=0;i<5;i++)
switch(i) {
case 0:
System.out.println("a is zero.");
break;
case 1:
System.out.println("b is one.");
break;
case 2:
System.out.println("c is two.");
break;
case 3:
System.out.println("d is three.");
break;
default:

}
}
}

1.10 WAP to demonstrate if -else statement.


public class IfElse {

public static void main(String[] args) {


int num = 10;
System.out.println("The given number is: "+num);
if(num>0) {
System.out.println("The number is positive");
}
else
System.out.println("The number is negative.");

}
}

2.Array.
2.1 WAP to take and print an array.
public class PrintArray {

public static void main(String[] args) {


int[] arr = {1, 2, 5, 7, 8, 10, 15, 20};
System.out.println("Elemnts of the given array are: ");
for(int i=0;i<arr.length;i++) {
System.out.println(arr[i]+ " ");
}

2.2 WAP to sum values of an array.

public class ArraySum {


public static void main(String[] args) {
int[] arr = {1,2,3,4,5,6,7,8,9,10};
int sum=0;
System.out.println("Elemnts of the given array are: ");
for(int i=0;i<arr.length;i++) {
System.out.print(arr[i]+ " ");

}
for(int j = 0;j<arr.length;j++) {
sum=sum+j;
}
System.out.println("Sum of the array: "+sum);
}
}

2.3 WAP to find average of the elements of the array.


public class ArrayAverage {

public static void main(String[] args) {


int[] ar= {10,20,30,40,50,60,70,80,90};
int sum=0, avg=0;
System.out.println("Elemnts of the given array are: ");
System.out.println("\n");
for(int i=0;i<ar.length;i++) {
System.out.print(ar[i]+ " ");

}
for(int j = 0;j<ar.length;j++) {
sum=sum+j;
}
avg=sum/ar.length;
System.out.println("Sum of the array: "+sum);
System.out.println("Average of the array: "+avg);

}
}

2.4 WAP to find the largest element in the array.

public class Largest {


public static void main(String[] args) {
int[] arr = {10,50,40,30,20};
int max = arr[0];
System.out.println("Elements of the array are: ");
System.out.println("\n");
for(int i = 0;i<arr.length;i++) {
System.out.print(arr[i]+ " ");
}
for(int i =1;i<arr.length;i++)
{
if(arr[i]>max)
max=arr[i];
}
System.out.println("\n");
System.out.println("\nLargest element from the given array is:
"+max);

3.Control Statements.
3.1 WAP to check the seasons by if-else statements.
public class ifelse {

public static void main(String[] args) {


int month = 6;
String season;
if(month==12||month==1||month==2)
season = "winter";
else if(month==3||month==4||month==5)
season = "spring";
else if(month==6||month==7||month==8)
season = "summer";
else if(month==9||month==10||month==11)
season = "autumnn";
else
season = "NO SEASON";
System.out.println("JANUARY is in the "+season+".");

3.2 WAP to apply switch case.

public class VowelConsonant {

public static void main(String[] args) {


char alph='b';
boolean isVowel=false;
switch(alph)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U': isVowel = true;
}
if(isVowel == true)
System.out.println(alph+" is a Vowel");
else
System.out.println(alph+" is a consonant");

3.3 WAP to understand the concept of break.

public class breakloop {

public static void main(String[] args) {


for(int i=0;i<10;i++) {
if(i==5) break; //terminate loop if i is 5
System.out.println("i: "+i);
}
System.out.println("Loop Complete.");

3.4 WAP to demonstrate working of nested loop.

public class Nested {

public static void main(String[] args) {


int i,j;
for(i=0;i<10;i++) {
for(j=i;j<10;j++)
System.out.println(j+".");
System.out.println();
}

}
3.5 WAP to show while loop.

public class While {

public static void main(String[] args) {


int n = 10;
while(n>0) {
System.out.println("tick "+n);
n--;
}
}

4.Classes and Objects.


4.1 WAP to implement the use of class and object.

class Box{
double width;
double height;
double depth;
}
//this class declares an object of type Box.
public class Boxdemo {

public static void main(String[] args) {


Box mybox = new Box();
double vol;
//assign values to mybox's instance variables
mybox.width = 15;
mybox.height = 25;
mybox.depth = 20;
//compute volume of box.
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume is "+vol);

4.2 WAP to demonstrate the use of classes and objects.

class Box2{
double width;
double height;
double depth;
void volume() {
System.out.print("Volume: ");
System.out.println(width*height*depth);
}
}
public class Boxdemo2 {

public static void main(String[] args) {


Box2 mybox1 = new Box2();
Box2 mybox2 = new Box2();
//assign values to mybox1's instance variables
mybox1.width = 15;
mybox1.height = 10;
mybox1.depth = 25;
//assign different values to mybox2's instance variables
mybox2.width = 6;
mybox2.height = 5;
mybox2.depth = 10;
//display volume of first box
mybox1.volume();
//display volume of second box
mybox2.volume();

4.3 WAP to implement the use of copy constructor.

class Rect{
int length;
int breadth;
//constructor to intitialize length and breadth of rectangle
Rect(int l, int b)
{
length = l;
breadth = b;
}
Rect(Rect obj)
{
System.out.println("Copy constructor invoked.\n");
length = obj.length;
breadth = obj.breadth;
}
//method to calculate area of rectangle
int area() {
return length*breadth;
}
}
public class Rectangle {

public static void main(String[] args) {


Rect r1 = new Rect(10,15);
Rect r2 = new Rect(r1);
System.out.println("Area of first rectangle: "+r1.area());
System.out.println("Area of second rectangle: "+r2.area());

4.4 WAP to show to hide the variable in java.

class JBT2{
int variable = 10;
void method(int variable) {
variable = 15;
System.out.println("Value of Instance variable: "+this.variable);
System.out.println("Value of Local variable: "+variable);
}
void method() {
int variable = 35;
System.out.println("\n");
System.out.println("Value of Instance variable: "+this.variable);
System.out.println("Value of Local variable: "+variable);
}
}
class JBT3 {

public static void main(String[] args) {


JBT2 obj = new JBT2();
obj.method(25);
obj.method();

4.5 WAP to implement class and object.

class Box5{
double width;
double height;
double depth;
//This is the constructor for Box5
Box5(double w, double h, double d){
width = w;
height = h;
depth = d;
}
//compute and return volume
double volume() {
return width*height*depth;
}
}
public class Boxdemo5 {

public static void main(String[] args) {


Box5 mybox1 = new Box5(10,15,5);
Box5 mybox2 = new Box5(6,5,4);
double vol;
//get volume of first box
vol = mybox1.volume();
System.out.println("Volume is: "+vol);
//get volume of second box
vol = mybox2.volume();
System.out.println("Volume is: "+vol);

5.Overloading.

5.1 WAP to implement the overloading concept.

class OverloadDemo{
void test() {
System.out.println("No parameters");
}
//overload test for one integer parameter.
void test(int a) {
System.out.println("a: "+a);
}
//overload test for two integer parameters.
void test(int a,int b) {
System.out.println("a and b: "+a+" "+b);
}
//overload test for a double parameter
double test(double a) {
System.out.println("double a: "+a);
return a*a;
}
}
class Overload extends OverloadDemo{

public static void main(String[] args) {


OverloadDemo ob = new OverloadDemo();
double result;
//call all versions of test()
ob.test();
ob.test(15);
ob.test(20,30);
result = ob.test(234.56);
System.out.println("Result of ob.test(234.56): "+result);
}

5.2 WAP to implement the overloading concept.

class OverloadDemo1{
void test() {
System.out.println("No Parameters.");
}
//Overload test for two integer parameters.
void test(int a, int b) {
System.out.println("a and b: "+a+" "+b);
}
//Overload test for a double parameter
void test(double a) {
System.out.println("Inside test(double) a: "+a);
}
}
public class Overload1 {

public static void main(String[] args) {


OverloadDemo1 ob = new OverloadDemo1();
int i =90;
ob.test();
ob.test(20, 30);
ob.test(i); //this will invoke test(double)
ob.test(234.56);
}

5.3 WAP on call by value.

class Test {
void meth(int i, int j) {
i*=4;
j/=4;
}
}
public class CallByValue {

public static void main(String[] args) {


Test ob = new Test();
int a =20, b= 25;
System.out.println("a and b before call: "+a+" "+b);
ob.meth(a, b);
System.out.println("a and b after call: "+a+" "+b);

5.4 WAP to show call by reference method.

class Test1{
int a,b;
Test1(int i,int j){
a= i;
b= j;
}
//pass an object
void meth(Test1 o) {
o.a*= 4;
o.b/=4;
}
}
public class CallByRef {

public static void main(String[] args) {


Test1 ob = new Test1(20,25);
System.out.println("ob.a and ob.b before call: "+ob.a+" "+ob.b);
ob.meth(ob);
System.out.println("ob.a and ob.b after call: "+ob.a+" "+ob.b);

5.5 WAP to find factorial through recusion.

class Factorial{
//this is a recursive function
int fact(int n) {
int result;
if(n==1) return 1;
result = fact(n-1)*n;
return result;
}
}
public class Recursion {

public static void main(String[] args) {


Factorial f = new Factorial();
System.out.println("Factorial of 5 is: "+f.fact(5));
System.out.println("Factorial of 8 is: "+f.fact(8));
System.out.println("Factorial of 10 is: "+f.fact(10));

5.6 WAP to show static method implementation.

public class UseStatic {

static int a =5;

static int b;

static void meth(int x) {

System.out.println("x: "+x+"\n");

System.out.println("a: "+a+"\n");

System.out.println("b: "+b);

}
static {

System.out.println("Static block initialized.\n");

b=a*10;

public static void main(String[] args) {

meth(50);

5.7 WAP to show static method.

class StaticDemo{
static int a = 45;
static int b = 100;
static void callme() {
System.out.println("a: "+a+"\n");
}
}
class StaticByName {

public static void main(String[] args) {


StaticDemo.callme();
System.out.println("b: "+StaticDemo.b);
}

6.Inheritance.
6.1 WAP to show the concept of inheritance.

class A{
int i,j;
void showij() {
System.out.println("i and j: "+i+" "+j);
}
}

class B extends A{
int k;
void showk() {
System.out.println("k: "+k);
}
void sum() {
System.out.println("i+j+k: "+(i+j+k));
}
}
public class simpleinheritance {

public static void main(String[] args) {


A superob = new A();
B subob = new B();
//the super class may be used by itself.
superob.i=20;
superob.j=30;
System.out.println("Contents of superob: ");
superob.showij();
System.out.println();
//the subclass has access to all public members of its subclass.
subob.i=5;
subob.j=7;
subob.k=9;
System.out.println("Contents of subob: ");
subob.showij();
subob.showk();
System.out.println();
System.out.println("Sum of i, j and k in subob: ");
subob.sum();

6.2 WAP to implement the concept of overriding.

class Vehicle1{
void run() {
System.out.println("Vehicle is running.");
}
}
class Bike1 extends Vehicle1{
void run()
{
System.out.println("Bike is running safely.");
}

public static void main(String[] args) {


Bike1 obj = new Bike1();
obj.run();
}

6.3 WAP to show the implementation of function overriding.

class Bank{
int getRateOfInterest() {
return 0;
}
}
class SBI extends Bank{
int getRateOfInterest() {
return 5;
}
}
class ICICI extends Bank{
int getRateOfInterest() {
return 7;
}
}
class AXIS extends Bank{
int getRateOfInterest() {
return 6;
}
}
public class Test {

public static void main(String[] args) {


SBI s = new SBI();
ICICI i = new ICICI();
AXIS a = new AXIS();
System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());
System.out.println("ICICI Rate of Interest:
"+i.getRateOfInterest());
System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());

6.4 WAP to invoke overridden methods from sub-class.

class Animal1{
public void move() {
System.out.println("Animals can move.");
}
}
class Dog1 extends Animal1{
public void move() {
super.move();//invokes the super class method
System.out.println("Dogs can walk and run.");
}
}
public class TestDog1 {

public static void main(String[] args) {


Animal1 b = new Dog1(); //Animal reference but Dog object
b.move(); //runs the method in Dog class

7.Exception Handling.
7.1 WAP to implement exception handling.

public class Expec1 {


public static void main(String[] args)
{
try {
int data = 50/0;
}catch(ArithmeticException e)
{
System.out.println("Rest of the code.");
}
}
}

7.2 WAP to implement multiple catch statement.

import java.lang.Throwable;

public class MultiCatch {

public static void main(String[] args) {


try {
int a = args.length;
System.out.println("a = "+a);
int b = 42/a;
int c[]= {2};
c[42]=99;
}catch (ArithmeticException e) {
System.out.println("Divide by 0: "+e);
}catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array index: "+ e);
}
System.out.println("After try/catch blocks.");

7.3 WAP to show implementation of try-catch.

public class Expec2 {

public static void main(String[] args) {


try {
int data =50/0; //may throw exception
//if exception occurs, the remaining statement will not
execute
System.out.println("Rest of the code.");
}
//handling the exception
catch(ArithmeticException e) {
System.out.println(e);
System.out.println("Divide By Zero ERROR.!");
}
}

7.4 WAP to show the use of finally keyword.

public class TestFinallyBlock2 {

public static void main(String[] args) {


try {
int data = 30/0;
System.out.println(data);
}
catch(ArithmeticException e) {
System.out.println("finally block is always executed.");
}
finally {
System.out.println("finally block is always executed.");
}
System.out.println("rest of the code..");
}

7.5 WAP to implement try-catch.

public class Expec8 {

public static void main(String[] args) {


try {
try {
System.out.println("going to divide.");
int b=40/0;
}catch(ArithmeticException e) {
System.out.println(e);
}
try {
int a[]=new int[5];
a[5]=4;
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println(e);
}
System.out.println("Other statements.");
}catch(Exception e ) {
System.out.println("handled.");
}
System.out.println("Normal flow..");

8.JAVA Packages.
8.1 WAP to demonstrate abstract class and methods.

abstract class Figure{


double dim1;
double dim2;
Figure(double a, double b){
dim1=a;
dim2=b;
}
abstract double area();
}
class Rectangle extends Figure{
Rectangle(double a, double b){
super(a,b);
}
//override area for rectangle
double area() {
System.out.println("Inside Area for Rectangle.");
return (dim1*dim2);
}
}
class Triangle extends Figure{
Triangle(double a, double b){
super(a,b);
}
//overriden area for right triangle
double area() {
System.out.println("Inside area for triangle.");
return (dim1*dim2/2);
}
}
public class AbstractAreas {

public static void main(String[] args) {


Rectangle r = new Rectangle(4,7);
Triangle t = new Triangle(5,3);
Figure figref;
figref = t;
System.out.println("Area is "+ figref.area());

8.2 WAP to create and implement package p1.

Protection.java:

package p1;

public class Protection {


int n=1;
public int n_pri = 2;
protected int n_pro =3;
public int n_pub = 4;
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.java:
package p1;

public class Derived extends Protection{

public Derived() {
System.out.println("Derived 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);
}

SamePackage.java:
package p1;

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_pri= "+p.n_pri);
System.out.println("n_pro= "+p.n_pro);
System.out.println("n_pub= "+p.n_pub);
}

Demo.java:
package p1;

public class Demo {

public static void main(String[] args) {


Protection ob1 = new Protection();
Derived ob2 = new Derived();
SamePackage ob3 = new SamePackage();
}

}
8.3 WAP to create and implement package p2.
Protection2:

package p2;

public class Protection2 extends p1.Protection {

public Protection2() {
System.out.println("Derived other package constructor.");
System.out.println("n_pro= "+n_pro);
System.out.println("n_pub= "+n_pub);
}

OtherPackage:
package p2;

public class OtherPackage {

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

Demo2:
package p2;

public class Demo2 {

public static void main(String[] args) {


Protection2 ob1 = new Protection2();
OtherPackage ob2 = new OtherPackage();

8.4 WAP to show importing of a package.

MyPack:

package MyPack;

public class Balance {


String name;
double bal;
public Balance(String n, double b) {
name = n;
bal = b;
}
public void show() {
if(bal<0)
System.out.println("__>");
System.out.println(name + ":$" + bal);
}

TestBalance:
import MyPack.*;
public class TestBalance {

public static void main(String[] args) {


Balance test = new Balance("Adam ", 90.99);
test.show();
}

9.JAVA Interfaces.
9.1 WAP to show interfaces in JAVA.

interface MyInterface{
public void method1();
public void method2();
}
public class Inter1 implements MyInterface{
public void method1() {
System.out.println("Implements method1.");
}
public void method2() {
System.out.println("Implements method2.");
}
public static void main(String[] args) {
MyInterface obj = new Inter1();
obj.method1();

9.2 WAP to show interface and inheritance.

interface Inf1{
public void method1();
}
interface Inf2 extends Inf1{
public void method2();
}
public class Demo implements Inf2{
public void method1() {
System.out.println("method1.");
}
public void method2() {
System.out.println("method2.");
}

public static void main(String[] args) {


Inf2 obj = new Demo();
obj.method2();

9.3 WAP to implement multiple inheritance.

interface Moveable{
public boolean ismoveable();
}
interface Rollable {
public boolean isrollable();
}
public class Tyre implements Moveable{
int width;
public boolean ismoveable() {
return true;
}
public boolean isrollable() {
return true;
}
public static void main(String[] args) {
Tyre tr = new Tyre();
System.out.println(tr.ismoveable());
System.out.println(tr.isrollable());

10.Multithreading.
10.1 WAP to implement thread using thread class.

public class Multi extends Thread {


public void run() {
System.out.println("thread is running..");
}

public static void main(String[] args) {


Multi t1 = new Multi();
t1.start();

You might also like