Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
42 views8 pages

Java Lab 123

Java

Uploaded by

ramu34080
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views8 pages

Java Lab 123

Java

Uploaded by

ramu34080
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

1Q))METHOD OVERLOADING.............

import java.io.*;
class MethodOverloadingEx
{
static int add(int a, int b)
{
return a + b;
}
static int add(int a, int b, int c)
{
return a + b + c;
}
public static void main(String args[])
{
System.out.println("add() with 2 parameters");
System.out.println(add(4, 6));
System.out.println("add() with 3 parameters");
System.out.println(add(4, 6, 7));
}
}

1Q))CONSTRUCTOR OVERLOADING.......
public class Student {
int id;
String name;
Student()
{
System.out.println("this a default constructor");
}
Student(int i, String n)
{
id = i;
name = n;
}
public static void main(String[] args) {
Student s = new Student();
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");
Student student = new Student(10, "Kalpana");
System.out.println("Student Id : "+student.id + "\nStudent Name : "+student.name);
}
}

2Q))STATIC BLOCK& VARIABLE& METHODS......

public class StaticDemo


{
static int x = 10;
static int y;
static void func(int z) {
System.out.println("x = " + x);
System.out.println("y = " + y);
System.out.println("z = " + z);
}
static {
System.out.println("Running static initialization block.");
y = x + 5;
}
public static void main(String args[]) {
func(8);
}
}

3Q)ARRAY FRAMEWORK.....

import java.util.*;
public class ArrayListExample1{
public static void main(String args[]){
ArrayList<String> list=new ArrayList<String>();//Creating arraylist
list.add("Mango");//Adding object in arraylist
list.add("Apple");
list.add("Banana");
list.add("Grapes");
//Printing the arraylist object
System.out.println(list);
}
}

3Q)VECTOR FRAMEWORK....

import java.io.*;
import java.util.*;
class VectorDemo {
public static void main(String[] args)
{
int n = 5;
Vector<Integer> v = new Vector<Integer>(n);
for (int i = 1; i <= n; i++)
v.add(i);
System.out.println(v);
v.remove(3);
System.out.println(v);
for (int i = 0; i < v.size(); i++)
System.out.print(v.get(i) + " ");
}
}

4Q))SUPER AND FINAL KEYWORD.....

class Animal {
String color = "White";
void eat() {
System.out.println("Eating...");
}
}
final class Dog extends Animal {
String color = "Black";
void eat() {
System.out.println("Eating dog food...");
}
void displayColor() {
System.out.println("Dog color: " + color);
System.out.println("Animal color: " + super.color);
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.displayColor();
}
}

5Q))MULTI LEVEL INHERITANCE........

class Shape {
public void display() {
System.out.println("Inside display");
}
}
class Rectangle extends Shape {
public void area() {
System.out.println("Inside area");
}
}
class Cube extends Rectangle {
public void volume() {
System.out.println("Inside volume");
}
}
public class Tester {
public static void main(String[] arguments) {
Cube cube = new Cube();
cube.display();
cube.area();
cube.volume();
}
}

5Q))MULTIPLE INHERITANCE....

interface Shape {
void area();
}
interface Rectangle {
void width();
void height();
}
class Square implements Shape, Rectangle {
private double width;
private double height;
public Square(double width, double height) {
this.width = width;
this.height = height;
}
public void width() {
System.out.println("Width of s1 : " + width);
}
public void height() {
System.out.println("Height of s1 : " + height);
}
public void area() {
System.out.println("Area of s1 : " + width * height);
}
}
public class Main {
public static void main(String[] args) {
Square s1 = new Square(10.0, 20.0);
Square s2 = new Square(10.0, 10.0);
s1.width();
s1.height();
s1.area();
s2.width();
s2.height();
s2.area();
}
}

5Q))SINGLE INHERITANCE....

class Animal {
String color = "White";
void eat() {
System.out.println("Eating...");
}
}
class Dog extends Animal {
String breed = "German Shepherd";

void bark() {
System.out.println("Barking...");
}
void displayInfo() {
System.out.println("Color: " + color);
System.out.println("Breed: " + breed);
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.bark();
dog.displayInfo();
}
}

6Q))METHOD OVERRIDING....

class Animal {
void sound() {
System.out.println("The animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("The dog barks");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("The cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Animal();
Animal dog = new Dog();
Animal cat = new Cat();
animal.sound();
dog.sound();
cat.sound();
}
}

7Q))THREAD AND RUNNABLE INTERFACE...

class MyThread extends Thread {


public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("MyThread: " + i);
}
}
}

public class Main {


public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();

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


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

8Q))ADAPTER CLASS......

interface Shape {
void draw();
void resize();
}
abstract class ShapeAdapter implements Shape {
@Override
public void draw() {
}
@Override
public void resize() {
}
}
class Circle extends ShapeAdapter {
@Override
public void draw() {
System.out.println("Drawing Circle");
}
}
class Rectangle extends ShapeAdapter {
@Override
public void draw() {
System.out.println("Drawing Rectangle");
}
}
public class Main {
public static void main(String[] args) {
Shape circle = new Circle();
circle.draw(); // Output: Drawing Circle
circle.resize(); // Default implementation
Shape rectangle = new Rectangle();
rectangle.draw(); // Output: Drawing Rectangle
rectangle.resize(); // Default implementation
}
}

9Q))JAVA GRID LAYOUT....


import java.awt.*;
import javax.swing.*;

public class GridLayoutExample {


JFrame frameObj;

// constructor
GridLayoutExample() {
frameObj = new JFrame();
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
JButton btn6 = new JButton("6");
JButton btn7 = new JButton("7");
JButton btn8 = new JButton("8");
JButton btn9 = new JButton("9");

frameObj.setLayout(new GridLayout(3, 3)); // Specify rows and columns


frameObj.add(btn1);
frameObj.add(btn2);
frameObj.add(btn3);
frameObj.add(btn4);
frameObj.add(btn5);
frameObj.add(btn6);
frameObj.add(btn7);
frameObj.add(btn8);
frameObj.add(btn9);

frameObj.setSize(300, 300);
frameObj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Add this line
frameObj.setVisible(true);
}

// main method
public static void main(String[] args) {
new GridLayoutExample();
}
}

9q))BORDER LAYOUT.....
import java.awt.*;
import javax.swing.*;
public class Border {
JFrame f;
Border() {
f = new JFrame();
f.setLayout(new BorderLayout());
// Creating buttons
JButton b1 = new JButton("NORTH");
JButton b2 = new JButton("SOUTH");
JButton b3 = new JButton("EAST");
JButton b4 = new JButton("WEST");
JButton b5 = new JButton("CENTER");
f.add(b1, BorderLayout.NORTH);
f.add(b2, BorderLayout.SOUTH);
f.add(b3, BorderLayout.EAST);
f.add(b4, BorderLayout.WEST);
f.add(b5, BorderLayout.CENTER);
f.setSize(300, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public static void main(String[] args) {
new Border();
}
}

10Q))JBUTTON......

import javax.swing.*;
import java.awt.event.*;
public class ButtonExample {
public static void main(String[] args) {
JFrame f = new JFrame("Button Example");
JButton b = new JButton("Click Here");
b.setBounds(50, 100, 95, 30);
f.add(b);
f.setSize(400, 400);
f.setLayout(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Add this line
f.setVisible(true);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked!");
}
});
}
}

10Q))JLABEL.....

import javax.swing.*;
class LabelExample {
public static void main(String args[]) {
JFrame f = new JFrame("Label Example");
JLabel l1, l2;
l1 = new JLabel("First Label.");
l1.setBounds(50, 50, 100, 30);
l2 = new JLabel("Second Label.");
l2.setBounds(50, 100, 100, 30);
f.add(l1);
f.add(l2);
f.setSize(300, 300);
f.setLayout(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Add this line
f.setVisible(true);
}
}

You might also like