0% found this document useful (0 votes)
23 views

Java OOPs

OOPS presentation For 5th sem exam , MAKAUT University

Uploaded by

shamoonmallick12
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Java OOPs

OOPS presentation For 5th sem exam , MAKAUT University

Uploaded by

shamoonmallick12
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

OOPs

(PCC CS593)

ABDULLAH SHAMOON
ROLL NO: 28100120005
DEPT: CSE
SEM:5TH
GARGI MEMORIAL INSTITUTE OF
TECHNOLOGY
INDEX
S.N TOPIC Page REMARK
o No. S
1 Assignment - 1-16
1
2 Assignment - 17-
2 21
3 Assignment - 22-
3 26
4 Assignment - 27-
4 28
5 Assignment - 29-
5 34
6 Assignment - 35
6
7 Assignment - 36-
7 42
8 Assignment - 43
8
Laboratory Assignment 1
1) Find the Biggest of Three Integers :-

import java.util.Scanner;

public class biggest {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int x=sc.nextInt();
int y=sc.nextInt();
int z=sc.nextInt();

// int biggest;
if(x>y)
{
if(x>z){
System.out.println(“Biggest is " + x);
}
else{
System.out.println(“Biggest is " +z);
}
}
else{
if(y>z){
System.out.println(“Biggest is " + y);
}
else{
System.out.println(“Biggest is " +z);
}
}
sc.close();

}
}

Output :
6 15 12
Biggest is 15
Pg-1
2) Checking a leap year :-
import java.util.Scanner;

public class Leap {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a valid Year : ");
int n=sc.nextInt();

if(n%400 ==0 )
{
System.out.println("It's a Leap Year ");
}
else if(n%4 ==0 && n%100!=0)
{
System.out.println("Its a Leap Year !");
}
else{
System.out.println("It's not a Leap Year !");
}
sc.close();
}
}

Output :
Enter a valid Year :
2006
It’s not a Leap Year !

Pg-2
3)Arithmetic calculator using
switch case statement :-
import java.util.Scanner;

public class cal {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("Enter 1st value : ");


int x=sc.nextInt();
System.out.println("Enter 2nd value : ");
int y=sc.nextInt();
System.out.println("Enter what operation you want to
perform : ");

// String z= sc.next();
char z = sc.next().charAt(0);

switch(z)
{
case '+' :
System.out.println(x+y);
break;
case '-' :
System.out.println(Math.abs(x-y));
break;
case '*' :
System.out.println(x*y);
break;
}
sc.close();
}
}

Output :
Enter 1st value : 9
Enter 2nd value : 20
Enter what operation you want to perform : +
ans = 29
Pg-3
4) Binary search using array :-
import java.util.Scanner;

public class BinarySearch {


static int Binary(int a[], int t) {
int l = 0;
int h = a.length - 1;
while (l <= h) {
int mid = (l + h) / 2;
if (a[mid] > t) {
h = mid - 1;
} else if (a[mid] < t) {
l = mid + 1;
} else {
return mid;
}
}
return -1;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter array size : ");
int n = sc.nextInt();
int[] a = new int[n];
System.out.println("Enter values in array : ");
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
System.out.println("Enter the value want to
Search : ");
int t = sc.nextInt();
int ans = Binary(a, t);
System.out.println("Value is at index " + ans);
sc.close();
}
}

Output :
Enter array size : 7
Enter values in array : 3 5 4 1 7 9 8
Enter the value want to Search : 7
Value is at index 4 Pg-4
5) WAP to illustrate for-each
statement :-
public class for_each {
public static void main(String[] args)
{
int[] x = { 1, 3, 4, 6, 9 };
for(int i : x){
// x[i]+=1;
System.out.println(i);
}
}
}

Output :
1
3
4
6
9

Pg-5
6)Reverse an array without
coping :-
public class reverse {
public static void main(String[] args) {
int a[] ={ 2, 5, 1, 7, 9, 3 };
int n = a.length;

for (int i : a) {
System.out.print(i + " ");
}
System.out.println();
System.out.println("After reversing array");
for (int i = 0; i < n / 2; i++) {
int temp = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = temp;
}
for (int i : a) {
System.out.print(i + " ");
}
}
}

Output :
Array : 2 5 1 7 9 3
After reversing
Array : 3 9 7 1 5 2
Pg-6
7) Matrix Multiplication
public class matrix_multi {
public static void main(String[] args) {
int[][] a = { { 2, 2, 1 },
{ 9, 4, 2 },
{ 3, 5, 1 } };
int[][] b = { { 1, 4, 4 },
{ 3, 4, 8 },
{ 1, 5, 1 } };

int[][] result = { { 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 } };
// int x=0;

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
result[i][j] += a[i][k] * b[k]
[j];
}
}
}
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
System.out.print(result[j][k] + " ");
}
System.out.println();
}
}
}
Output :
Matrix Multiplication :
9 21 24
23 62 68
19 37 52
Pg-7
8) Transpose of Matrix
public class Transpose {
public static void main(String[] args) {
int[][] x = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };

for (int i = 0; i < 3; i++) {


for (int j = i; j < 3; j++) {
int temp = x[i][j];
x[i][j] = x[j][i];
x[j][i] = temp;
}
}
System.out.println("Transpose : ");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(x[i][j]);
}
System.out.println();
}
}
}

Output :
Matrix : 123
456
789
Transpose :147
258
369 Pg-8
9) Reverse a Number
import java.util.Scanner;

// Reverse a number

class Reversed{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Number : ");
int x= sc.nextInt();
int ans=0;
while(x!=0)
{
int l=x%10;
ans=ans*10+l;
x=x/10;
}
System.out.println("Reverse of the number:
"+ans);
sc.close();
}
}

Output :
Enter a Number :
638
Reverse of the number: 836
Pg-9
10) Find the Armstrong number.
import java.util.Scanner;

public class Armstrong {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Number : ");
int x = sc.nextInt();
int y = x;
int ans = 0;
while (y != 0) {
int l = y % 10;
ans = ans + (l * l * l);
y = y / 10;
}
if (ans == x) {
System.out.println("It is an Armstrong
Number !");

} else {
System.out.println("Its not an Armstrong number
: ");
}
sc.close();
}
}

Output :
Enter a Number :
407
It is an Armstrong Number !
Pg-10
11) Generate prime number
from 2 to 1000
import java.util.Scanner;

public class GeneratePrime {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter range : ");
// int x =sc.nextInt();
int y = sc.nextInt();
int arr[] = new int[y + 2];
for (int i = 0; i < y; i++) {
arr[i] = 0;
}
for (int i = 2; i <= y; i++) {
if (arr[i] == 0) {
for (int j = i * i; j <= y; j = j +
i) {
arr[j] = 1;
}
}
}
for (int i = 3; i < y; i++) {
if (arr[i] == 0) {
System.out.print(i + " ");
}
}

sc.close();
}
}
Output :
3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67
71 73 79 83 89 97 101 103 107 109 113 127 131 137
139 149 151 157 163 167 173 179 181 191 193 197
199 211 223 227 …….
Pg-11
12) Factorial using recursion
import java.util.Scanner;

public class FactRec {


public static int Fact(int x)
{
if(x==0 || x==1)return 1;
else
return x*Fact(x-1);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
int x = sc.nextInt();
System.out.println("Factorial of the no :
"+Fact(x));
sc.close();
}
}

Output :
Enter a number : 6
Factorial of the no: 720

Pg-12
13) Fibonacci using recursion &
loop

import java.util.Scanner;

public class FiboRec {


public static int Fibonacci(int x)
{
if(x==1)return 0;
if(x==2)return 1;
else
return Fibonacci(x-1)+Fibonacci(x-2);
}
public static void main(String[] args) {
System.out.println("Enter the number : ");
Scanner sc = new Scanner(System.in);
int x=sc.nextInt();
System.out.println(x+"th Fibonacci is
"+Fibonacci(n));
sc.close();
}
}

Output :
Enter the number :
8
10th Fibonacci is 13
Pg-13
14) HCF using recursion
import java.util.Scanner;

public class Hcfactor {


public static int hcf(int x, int y) {
while (y != 0) {
int m = x % y;
x = y;
y = m;
}
return x;
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.print("Enter numbers : \
n");
int x = sc.nextInt();
int y = sc.nextInt();
System.out.print("HCF is " + hcf(x,
y));
sc.close();
}
Output :
}

Enter numbers :
42
24
HCF is 6 Pg-14
15)Write a program to take
user input with the help of
Scanner class.
import java.util.Scanner;

public class InputScanner {


public static void main(String[]
args) {
Scanner sc = new
Scanner(System.in);
System.out.print("Enter two
numbers : ");
int x=sc.nextInt();
int y=sc.nextInt();
int z= x + y;
System.out.println("Sum is "+
(z));
sc.close();
}
}
Output :
Enter two numbers : 10 6
Sum is 16

Pg-15
16) Write a program to
take user input with the
help of Console class.
import java.io.*;

public class InputConsole {


public static void main(String[]
args) throws IOException {
int x =
Integer.parseInt(args[0]) +
Integer.parseInt(args[1]);
System.out.println("Sum is " +
x);
}
}

Output :
>javac InputConsole.java
PS > java InputConsole 10 20
Sum is 30
Pg-16
Laboratory Assignment 2
Q) Write program which includes a method inside
the box class. Take the method as void volume().

class BoxDemo {
double height;
double breadth;
double width;

public void volume(double h , double b ,double w)


{
double vol=(h*b*w);
System.out.println("Volume is : "+vol);
}
}

class Box{
public static void main(String[] args) {

BoxDemo box = new BoxDemo();


box.height = 20;
box.width = 10;
box.breadth = 30;

box.volume(box.height, box.breadth, box.width);


}

Output :
Volume is : 6000.0
Pg-17
Q) Develop a method that will read an
array of strings and display the
occurrences of each word in a decreasing
order.

import java.util.Scanner;

public class Array_stringd {


public static void read_array(String arr[]) {
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i].compareTo(arr[j]) < 0) {
String t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
}
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.print("Enter number of strings : ");
int x = sc.nextInt();
System.out.println("Enter string : ");
String arr[] = new String[x];
for (int i = 0; i < x; i++) {
arr[i] = sc.next();
}
read_array(arr);
System.out.println("Strings in reverse order : ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
sc.close();
}
}

Output :
Enter number of strings : 5
Enter string :
My name is Abdullah Shamoon
Strings in reverse order :
Shamoon name my is Abdullah Pg-18
5)Develop a method that
calculates the square root of a
number (use Newton Rapson’s
Method).
class SqaureNewton {
static double squareRoot(double n, double l) {
double x = n;
double root;
while (true) {
root = 0.5 * (x + (n / x));
if (Math.abs(root - x) < l)
break;
x = root;
}
return root;
}

public static void main(String[] args) {


double n = 154;
double l = 0.00001;
System.out.println(squareRoot(n, l));
}
}

Output :
12.40967364599086

Pg-19
6) Develop a method that counts,
blanks, tabs and new lines in a given
string.
import java.util.Scanner;

public class CountWhiteSpace {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s=sc.nextLine();
int blank=0;
int tab=0;
int newLine=0;
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)==' ')
{
blank++;
}
if(s.charAt(i)=='\t')
{
tab++;
}
if(s.charAt(i)=='\n')
{
newLine++;
}
}
System.out.println("Blank space : "+blank+"\n"+"Tab
: "+tab+"\
Line : "+newLine);
sc.close();
}
}

Output :
What are you doing
Blank space : 2
Tab : 1
New Line : 0 Pg-20
7) Develop a program to illustrate array of objects.

import java.io.*;

class Student {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public int rollno;
String name;
String year;
float a, b, c, d;

public void getdata() throws IOException {


rollno = Integer.parseInt(in.readLine());
name = in.readLine();
year = in.readLine();
a = Float.parseFloat(in.readLine());
b = Float.parseFloat(in.readLine());
c = Float.parseFloat(in.readLine());
d = Float.parseFloat(in.readLine());
}

public void display() {


System.out.println("Roll no = " + rollno);
System.out.println("Name = " + name);
System.out.println("Year = " + year);
System.out.println("marks = " + a + "," + b + "," + c + "," + d);
}

public void calc_result() {


String result = "fail";
if (a >= 40 && mb >= 40 && c >= 40 && d >= 40)
result = "pass";
System.out.println("Result = " + result);
}
}

public class StudentTest2 {


public static void main(String args[]) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("how many students?:");
int max = Integer.parseInt(in.readLine());
Student s[] = new Student[max];
for (int i = 0; i < max; i++)
s[i] = new Student();
for (int i = 0; i < max; i++) {
System.out.println("Enter details of students" + i);
s[i].getdata();
}
for (int i = 0; i < max; i++) {
System.out.println("Result of student " + i);
s[i].display();
s[i].calc_result();
}
}
}

Output : Result of student 0 Result of student 1


how many students?: 2 Roll no = 05 Roll no = 16
Name = Abdullah Name = Shamoon
Enter details of students 0 : 05
Abdullah 3 59 70 74 69 Year = 2022 Year = 4
marks = marks =
Enter details of students 1 : 16 59.0,70.0,74.0,69.0 63.0,55.0,39.0,10.0
Shamoon 4 63 55 39 10 Result = pass Result = fail
Pg-21
Laboratory Assignment 3
1) Writea program to illustrate parameterized
constructor.

class BoxDemo {
private double height;
private double breadth;
private double width;

BoxDemo(double h, double b, double w) {


height = h;
width = w;
breadth = b;
}

public void volume() {


double vol = (height * breadth * width);
System.out.println("Volume is : " + vol);
}
}

class Box {
public static void main(String[] args) {

BoxDemo box = new BoxDemo(10, 15, 20);

box.volume();
}

Output :
Volume is : 3000.0
Pg-22
2) Design a class Prism to represent a prism.
• Data members: private double length(l), width(w), height(h)
• public void setPrism(): to assign value to l,w,h.
• public double topArea(): returms top area as l*w
• public double bottomArea(): returms bottom area as l*w
• public double leftArea(0: returns left area of the prism as h*w
• public double rightArea(0: returns right area of the prism as h*w
• public double frontArea(): returns front area of the prism h*l
• public double backArea(): returns back area of the prism h*l
• public double area(): returns a sum of the areas of all six sides as 2(l*w + h*w + h*l)
• Develop a public class TestPrism that tests all these methods.
import java.util.*;
class Prism{
private Double l;
private Double w;
private Double h;
public void setPrism(Double l,Double w,Double h){
this.l=l;
this.w=w;

}
this.h=h;
Output :
public double topArea(){
return l*w; Enter the length:
}
public double buttomArea(){ 3
return l*w;
} Enter the width:
public double leftArea(){
return h*w;
}
4
public double rightArea(){
return h*w; Enter the height:
}
public double frontArea(){ 5
return l*h;
} Total Top area is 12.0
public double backArea(){
return l*h; Total buttom area is 12.0
}
public double area(){
return 2*(l*w + h*w + h*l);
Total left area is 20.0
}
}
Total right area is 20.0
public class TestPrism{
public static void main(String args[]){ Total front area is 15.0
Scanner sc =new Scanner(System.in);
System.out.println("Enter the length: "); Total back area is 15.0
Double l=(Double)sc.nextDouble();
System.out.println("Enter the width: "); Total area is 94.0
Double w=(Double)sc.nextDouble();
System.out.println("Enter the height: ");
Double h=(Double)sc.nextDouble();
Prism p=new Prism();
p.setPrism(l,w,h);
System.out.println("Total Top area is "+p.topArea());
System.out.println("Total buttom area is "+p.buttomArea());
System.out.println("Total left area is "+p.leftArea());
System.out.println("Total right area is "+p.rightArea());
System.out.println("Total front area is "+p.frontArea());
System.out.println("Total back area is "+p.backArea());
System.out.println("Total area is "+p.area());
}
} Pg-23
3) Replace setPrism() method with a constructor to initialize
the data members of Prism class.
import java.util.*;
class Prism{
private Double l;
private Double w;
private Double h;
Prism(Double l,Double w,Double h){
this.l=l;
this.w=w; Output :
this.h=h;
} Enter the length:
public double topArea(){
return l*w; 3
}
public double buttomArea(){ Enter the width:
return l*w;
} 4
public double leftArea(){
return h*w; Enter the height:
}
public double rightArea(){ 5
return h*w;
} Total Top area is 12.0
public double frontArea(){ Total buttom area is 12.0
return l*h;
} Total left area is 20.0
public double backArea(){
return l*h; Total right area is 20.0
}
public double area(){ Total front area is 15.0
return 2*(l*w + h*w + h*l);
} Total back area is 15.0
}
public class TestPrism1{ Total area is 94.0
public static void main(String args[]){
Scanner sc =new Scanner(System.in);
System.out.println("Enter the length: ");
Double l=(Double)sc.nextDouble();
System.out.println("Enter the width: ");
Double w=(Double)sc.nextDouble();
System.out.println("Enter the height: ");
Double h=(Double)sc.nextDouble();
Prism p=new Prism(l,w,h);
System.out.println("Total Top area is "+p.topArea());
System.out.println("Total buttom area is "+p.buttomArea());
System.out.println("Total left area is "+p.leftArea());
System.out.println("Total right area is "+p.rightArea());
System.out.println("Total front area is "+p.frontArea());
System.out.println("Total back area is "+p.backArea());
System.out.println("Total area is "+p.area());
}
}

Pg-24
4) Develop a class hierarchy for Food as shown below.

Food

Non veg food


Veg Food

Fruit Vegetable Fish Chicken

class Food {
public int calories;
int fat;
int protein;
int carb;
int sugar;

public int getCalories() {


return calories;
}

public int getFood() {


return fat + protein + carb;
}
}

class VegFood extends Food {


int iron;

int getIron() {
return iron;
}

void isLeafy() {
System.out.println("This fruit is a leafy food");
}

void isWatery() {
System.out.println("This fruit is a watery food");
}
}

class NonVegFood extends Food {


int protein;
int fat;

public int getProtein() {


return protein;
}
}

class Fruit extends VegFood {


String dry;
String water;

void isDry() {
System.out.println("This fruit is a dry fruit.");
}
Pg-25
void isWatery() {
System.out.println("This fruit is a watery fruit.");
}
}

class Vegetable extends VegFood {


String leafy;
String water;
int vitamins;

public void getLeafy() {


System.out.println("Its a Leafy Vagetable");
;
}

public int getMinerals() {


return fat + protein + vitamins;
}
}

class Fish extends NonVegFood {


int vitamins;
}

class Chicken extends NonVegFood {


int vitamins;

public int getEnergy() {


return protein + vitamins;
}
}

class Test1 {
public static void main(String args[]) {
Fruit apple = new Fruit();
apple.calories = 50;
apple.fat = 5;
apple.protein = 10;
apple.carb = 20;
System.out.println("calories in apple " + apple.getCalories());
System.out.println("Minerals in apple " + apple.getFood());
Vegetable potato = new Vegetable();
potato.vitamins = 5;
potato.calories = 78;
System.out.println("calories in potato : " + potato.getCalories());
Fish hilsa = new Fish();
hilsa.fat = 54;
hilsa.calories = 88;
hilsa.protein = 98;
System.out.println("Proteins in hilsa Fish is " + hilsa.getProtein());
System.out.println("calories in hilsa Fish is " + hilsa.getCalories());
Chicken hybrid = new Chicken();
hybrid.protein = 125;
hybrid.fat = 84;
System.out.println("Proteins in chicken : " + hybrid.getProtein());

}
}

Output :
calories in apple 50
Minerals in apple 35
calories in potato : 78
Proteins in hilsa Fish is 98
calories in hilsa Fish is 88
Proteins in chicken : 125 Pg-26
Laboratory Assignment 4
1) Write a program to illustrate call by
value in Java
class CallByValue{
int data=50;
void change(int data){
data=data+100;
}
public static void main(String args[]){
CallByValue op=new CallByValue();
System.out.println("before change
"+op.data);
op.change(500);
System.out.println("after change
"+op.data);
}
}

Output :
before change 50
after change 50

Pg-27
2) Write a program to illustrate call by
reference in java
class CallByRefference{
int data=50;
void change(CallByRefference op){
op.data=op.data+100;
}
public static void main(String args[]){
CallByRefference op=new CallByRefference();
System.out.println("before change "+op.data);
op.change(op);
System.out.println("after change "+op.data);

}
}

Output :
before change 50
after change 150

Pg-28
Laboratory Assignment 5
1) Illustrate super at variable level.
class Vehicle{
int maxSpeed = 100;
}
class Car extends Vehicle{
int maxSpeed = 180;
void display(){
System.out.println("Maximum
Speed: " + super.maxSpeed);
}
}
class SuperVariable{
public static void main(String[] args){
Car small = new Car();
small.display();
}
}

Output :
Maximum Speed: 100

Pg-29
2) Illustrate super at method level.
class Person{
void message(){
System.out.println("This is person class");
}
}
class Student extends Person{
void message(){
System.out.println("This is student class");
}
void display(){
message();
super.message();
}
}
class SuperVariable1{
public static void main(String args[]){
Student s = new Student();
s.display();
}
}

Output :
This is student class
This is person class
Pg-30
3) Illustrate super at constructor level.
class Person{
Person(){
System.out.println("Person class
Constructor");
}
}
class Student extends Person{
Student(){
super();
System.out.println("Student class
Constructor");
}
}
class SuperVariable2{
public static void main(String[] args){
Student s = new Student();
}
}

Output :
Person class Constructor
Student class Constructor

Pg-31
4)Illustrate super and this key word in Inheritance.
class Parent{
Parent(){
System.out.println("Parent class no argument constructor");
}
Parent(String s){
System.out.println("Parent class parameterized "+s);
}
}
class Child extends Parent{
Child(){
this("constructor.");
System.out.println("Child class no-argument constructor.");
}
Child(String s){
super("constructor.");
System.out.println("Child class parameterized "+s);
}
}
class SuperVariable3{
public static void main(String[] args) {
Child obj = new Child();
}
}

Output :
Parent class parameterized constructor.
Child class parameterized constructor.
Child class no-argument constructor.
Pg-32
5) Illustrate method overriding.

class Animal {
public void display(){
System.out.println("I am an animal");
}
}
class Dog extends Animal {
public void display(){
System.out.println("I am a dog");
}
public void printMessage(){
display();
}
}
class OverrideMethod{
public static void main(String[] args) {
Dog dog1 = new Dog();
dog1.printMessage();
}
}

Output :
I am a dog

Pg-33
6)Illustrate Dynamic method dispatch.

class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal {
public void move() {
System.out.println("Dogs can walk and bark");
}
}
public class Dynamic{
public static void main(String args[]) {
Animal a = new Animal();
Animal b = new Dog();
a.move();
b.move();
}
}

Output :
Animals can move
Dogs can walk and bark
Pg-34
Laboratory Assignment 6
Q)Write a program in Java to
implement command-line argument that
prints all the values.

class cmd
{
public static void main(String[] args)
{
for(int i=0;i< args.length;i++)
{
System.out.println(args[i]);
}
}
}
Output :
23 24 76 15 29
23
24
76
15
29
Pg-35
Laboratory Assignment 7
1)Demonstrate method overloading.

class Adder{
static int add(int a,int b){
return a+b;
}
static int add(int a,int b,int c){
return a+b+c;
}
}
class TestOverloading{
public static void main(String[] args){
System.out.println(Adder.add(23,5));
System.out.println(Adder.add(11,4,6));
}
}
Output :
27
21

Pg-36
2)Demonstrate Overloading Constructors.

class TestOverloading1{
int id;
String name;
TestOverloading1(){
System.out.println("this a default constructor");
}
TestOverloading1(int i, String n){
id = i;
name = n;
}

public static void main(String[] args) {


TestOverloading1 s = new TestOverloading1();
System.out.println("\nDefault Constructor values: \n");
System.out.println("Student Id : "+s.id + "\nStudent Name :
"+s.name);
System.out.println("\nParameterized Constructor values: \n");
TestOverloading1 student = new TestOverloading1(10,
"David");
System.out.println("Student Id : "+student.id + "\nStudent
Name : "+student.name);
}
}
Output :
this a default constructor
Default Constructor values:
Student Id : 0
Student Name : null
Parameterized Constructor values:
Student Id : 10
Student Name : David Pg-37
3)Methods with differing type signatures are overloaded –
not overridden demonstrate with an example.

class OverloadDemo{
void test() {
System.out.println("No parameters");
}
void test(int a, int b) {
System.out.println("a and b: " + a + " " + b);
}
void test(double a) {
System.out.println("Inside test(double) a: " + a);
}
}
class TestOverloading3 {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
int i = 88;
ob.test();
ob.test(2, 7);
ob.test(i);
ob.test(123.2);
}
}

Output :
No parameters
a and b: 2 7
Inside test(double) a: 88.0
Inside test(double) a: 123.2
Pg-38
4)Create a super class called Figure that stores the dimensions of a two-
dimensional object. It also defines a method called area( ) that computes
the area of an object. The program derives two subclasses from Figure. The
first is Rectangle and the second is Triangle. Each ofthese subclasses
overrides area( ) so that it returns the area of a rectangle and a
triangle,respectively.

class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
void show() {
System.out.println("i and j: " + i + " " + j);
}
}
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
void show(String msg) {
System.out.println(msg + k);
}
}
class TestOverloading4 {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show("This is k: ");
subOb.show();
}
}

Output :
This is k: 3
i and j: 1 2 Pg-39
5) Demonstrate nested try.
class NestedTry{
public static void main(String args[]){
try{
try{
System.out.println("dividing by 0");
int b =39/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 statement");
}
catch(Exception e)
{
System.out.println("handled the exception (outer catch)");
}
System.out.println("normal flow..");
}
}
Output :
dividing by 0
java.lang.ArithmeticException: / by zero
java.lang.ArrayIndexOutOfBoundsException: 5
other statement
normal flow.. Pg-40
6)Demonstrate multi-catch statement.
import java.util.*;
class MultiCatch{
public static void main(String args[]){
Scanner scn = new Scanner(System.in);
try{
int n = Integer.parseInt(scn.nextLine());
if (99%n == 0)
System.out.println(n + " is a factor of 99");
}
catch (ArithmeticException ex){
System.out.println("Arithmetic " + ex);
}
catch (NumberFormatException ex){
System.out.println("Number Format Exception " +
ex);
}
}
}
Output :
HI,i am shamoon
Number Format Exception
java.lang.NumberFormatException: For
input string: "HI,i am shamoon"
Pg-41
7) Demonstrate throw.

class Throw{
public static void validate(int age) {
if(age<18) {
throw new ArithmeticException("Person is
not eligible to vote");
}
else {
System.out.println("Person is eligible to
vote!!");
}
}
public static void main(String args[]){
validate(13);
System.out.println("rest of the code...");
}
}

Output :
Exception in thread "main"
java.lang.ArithmeticException: Person is not eligible to
vote
at Throw.validate(Throw.java:4)
at Throw.main(Throw.java:11)

Pg-42
Laboratory Assignment 8
1) Demonstrate static variables, methods, and blocks.

class StaticKey{
static int n;
static String mystr;
static{
n = 25;
mystr = “This is a Static block";
}
static void meth(int a){
System.out.println("Hi , This is a static method");
}
public static void main(String args[])
{
System.out.println("Value of num: "+n);
System.out.println(mystr);
int a=10;
meth(a);
}
}

Output :
Value of num: 25
This is a Static block
Hi , This is a Static method

Pg-43

You might also like