JAVA FILE - PFD 2
JAVA FILE - PFD 2
TECHNICAL CAMPUS
OUTPUT
Ques6. Write a Java program to implement constructor overloading.
CODE
class Box {
double length, breath, height;
Box() {
length = breath = height = 20;
}
Box(double w, double b, double h) {
length = w;
breath = b;
height = h;
}
double volume() {
return (length * breath * height);
}
}
class construct_demo {
public static void main(String args[]) {
Box obj1 = new Box();
Box obj2 = new Box(2, 5, 7);
double vol;
vol = obj1.volume();
System.out.println("volume is :" + vol);
vol = obj2.volume();
System.out.println("volume is :" + vol);
}
}
OUTPUT
Ques7. Write a Java program to count the no. of objects created in a program.
CODE
class object_counter {
static int count = 0;
object_counter() {
count++;
}
static int get_count() {
return count;
}
}
class object_demo {
public static void main(String args[]) {
object_counter obj1 = new object_counter();
object_counter obj2 = new object_counter();
object_counter obj3 = new object_counter();
object_counter obj4 = new object_counter();
System.out.println("no. of object:" + object_counter.get_count());
}
}
OUTPUT
Ques8. Write a Java program to show call by value & call by reference.
CODE
class MyClass {
int value;
MyClass(int value) {
this.value = value;
}
}
public class qestion9 {
public static void main(String[] args) {
int x = 10;
MyClass obj = new MyClass(20);
System.out.println("Before callByValue - x: " +
x); callByValue(x);
System.out.println("After callByValue - x: " + x);
System.out.println("Before callByReference - obj.value: " + obj.value);
callByReference(obj);
System.out.println("After callByReference - obj.value: " + obj.value);
}
public static void callByValue(int a)
{ a = 100;
}
public static void callByReference(MyClass obj) {
obj.value = 200;
}
}
OUTPUT
Ques9. Write a Java program to implement method over ridding & method
overloading.
CODE
//method overloading
class Method {
static void add(int a, int b) {
int c = a + b;
System.out.println("the sum is" + c);
}
static void add(double a, double b) {
double c = a + b;
System.out.println("the sum is" + c);
}
static void add(double a) {
double c = a + 50.2;
System.out.println("the sum is" + c);
}
public static void main(String args[]) {
Method.add(4, 5);
Method.add(4.5, 6.7);
Method.add(7.5);
}
}
// method overriding
class Bike {
void run() {
System.out.println("Bike is running");
}
}
class Hero extends Bike {
void run() {
System.out.println("Hero bike is running");
}
}
public class Runtime {
public static void main(String[] args) {
Bike h = new Hero();
h.run();
}
}
OUTPUT
Ques10. Create a class box having height, width, depth as the instance variables
& calculate its volume. Implement constructor overloading in it. Create a
subclass named box_new that has weight as an instance variable. Use super in
the box_new class to initialize members of the base class.
CODE
class box {
int height;
int width;
int depth;
box() {
System.out.println("The defaukt constructor has been called");
}
box(int w, int h, int d) {
width = w;
height = h;
depth = d;
}
void volume() {
int v = height * depth * width;
System.out.println("Volume of the box:" + v);
}
}
class box_new extends box {
int weight;
box_new(int w, int h, int d, int we)
{ super(we, h, d);
weight = we;
}
void display() {
System.out.println("Weight:" + weight);
}
}
public class Ques10 {
public static void main(String[] args) {
box_new b = new box_new(23, 62, 20, 10);
b.volume();
b.display();
}
}
OUTPUT
Ques11. Write a Java program to implement run time
polymorphism. CODE
class A {
void show_me() {
System.out.println("show inside A");
}
}
class B extends A {
void show_me() {
System.out.println("show inside B");
}
}
class C extends B {
void show_me() {
System.out.println("show inside C");
}
}
class abc {
public static void main(String args[])
{ A a = new A();
B b = new B();
C c = new C();
A r;
r = a;
r.show_me();
r = b;
r.show_me();
r = c;
r.show_me();
}
}
OUTPUT
Ques12. Write a Java program to implement interface. Create an interface
named shape having area () & perimeter () as its methods. Create three classes
circle, rectangle & square that implement this interface.
CODE
interface Shape {
void area();
void perimeter();
}
class Circle implements Shape {
int r = 5;
public void area() {
double area = (3.14) * r * r;
System.out.println("Area of circle:" + area);
}
public void perimeter() {
double per = 2 * 3.14 * r;
System.out.println("Perimter of circle:" + per);
}
}
class Square implements Shape {
int a = 10;
public void area() {
int area = a * a;
System.out.println("Area of sqaure:" + area);
}
public void perimeter() {
int per = 4 * a;
System.out.println("Perimeter of square" + per);
}
}
class Rectangle implements Shape {
int l = 10;
int b = 5;
public void area() {
int area = l * b;
System.out.println("Area of rectangle:" + area);
}
public void perimeter() {
int per = 2 * (l + b);
System.out.println("Perimeter of rectangle:" + per);
}
}
public class ques12 {
public static void main(String[] args) {
Circle c = new Circle();
Rectangle r = new Rectangle();
Square s = new Square();
c.area();
c.perimeter();
s.area();
s.perimeter();
r.area();
r.perimeter();
}
}
OUTPUT
Ques13. Write a Java program to show multiple inheritance.
CODE
interface Vehicle {
void exists();
}
interface car {
void wheels();
}
class Transport implements Vehicle, car
{ public void exists() {
System.out.println("It is existing");
}
public void wheels() {
System.out.println("It has four wheels");
}
}
public class mulinher {
public static void main(String[] args) {
Transport t1 = new Transport();
t1.exists();
t1.wheels();
}
}
OUTPUT
Ques14. Write a Java program to implement exception handling. Use try, catch
& finally.
CODE
class exceptiondemo {
public static void main(String args[]) {
int a[] = { 2, 6 };
int b = 5;
try {
int x = a[2] / b - a[1];
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("array index error");
} finally {
System.out.println("finally always executed");
}
}
}
OUTPUT
Ques15. Write a Java program to implement matrix multiplication by 2d array.
CODE
class matrices {
class vec {
v.removeElement("Banana");
int s = v.size();
System.out.println("Size of vector: " + s);
System.out.println(v);
}
}
OUTPUT
Ques17. Create a user defined exception named “nomatchexception” that is fired
when the string entered by the user is not “india”
CODE
class NomatchException extends Exception {
public NomatchException() {
super("String does not match 'india'");
}
}
public class ex {
public static void main(String[] args) {
try {
String userInput = "example";
if (!userInput.equalsIgnoreCase("india")) {
throw new NomatchException();
}
System.out.println("String matches 'india'");
} catch (NomatchException e) {
System.out.println(e.getMessage());
}
}
}
OUTPUT
Ques18. Write a Java program to show even & odd numbers by thread.
CODE
public class EvenOddThread {
public static void main(String[] args) {
Printer printer = new Printer();
Thread evenThread = new Thread(new EvenRunnable(printer,
10), "EvenThread");
Thread oddThread = new Thread(new OddRunnable(printer, 10), "OddThread");
evenThread.start();
oddThread.start();
}
OUTPUT
Ques22. Write a Java program to demonstrate the use of equals() and == in Java.
CODE
class eq {
public static void main(String args[]){
String str1="Hello";
String str2="world";
String str3="Hello";
System.out.println("string 1=" +str1 );
System.out.println("string 2=" +str2 );
System.out.println("string 3=" +str3 );
System.out.println("using equals()= " + str1.equals(str3));
System.out.println("using double" + str1==str3 );
}
}
OUTPUT
Ques23. Write a Java program to implement all mouse events and mouse motion
events.
CODE
import java.awt.*;
import java.awt.event.*;
public class MouseEventsDemo extends Frame implements MouseListener,
MouseMotionListener {
public MouseEventsDemo() {
setTitle("Mouse Events Demo");
setSize(400, 300);
addMouseListener(this);
addMouseMotionListener(this);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse Clicked at (" + e.getX() + ", " + e.getY() + ")");
}
public Kkeyevent() {
setTitle("Keyboard Events
Demo"); setSize(400, 300);
addKeyListener(this);
setFocusable(true);
setVisible(true);
}
public void keyPressed(KeyEvent e) {
System.out.println("Key Pressed: " + KeyEvent.getKeyText(e.getKeyCode()));
}