Java Programming (1)
Java Programming (1)
01
class Hello{
public static void main(String args[]){
System.out.println("Hello world");
}
}
Output:-
Experiment No. 02
Aim :- Write a Program in Java to show concepts of varscope.
Software Required :- VS Code / JDK .
Theory:- In Java, variable scope defines the region where a variable can be accessed.
There are three main types:
Code:-
class Varscope{
public static void main(String args[]){
int A = 10;
{
int B = 20;
System.out.println("Block A: " + A);
System.out.println("Block B: " + B);
}//scope of B ends here
{
int B = 30;
System.out.println("Block A: " + A);
System.out.println("Block B: " + B);
}//Scope of b ends here
int B = 40; //Scope of b starts here
{
System.out.println("Block A: " + A);
System.out.println("Block B: " + B);
}
}//Scope of all the variables ends here
}
Output:-
Experiment No. 03
Output:-
Part - 2
class Aclass{
public static void main(String args[]){
for (String arg : args) {
System.out.println(arg);
}
}
}
Output:-
Experiment No. 04
Code:-
double x;
int y;
x=25.50;
y=(int)x;
int m;
double n;
m=10;
n=(double)m;
Output:-
Experiment No. 05
// int c = a+b;
System.out.println("Sum is: " + (a+b) );
}
}
Output:-
Experiment No. 06
// Default constructor
Person() {
this.name = "";
this.age = 0;
}
// Parameterized constructor
Person(String name, int age) {
this.name = name;
this.age = age;
}
Experiment No. 07
Aim :- Write a program to print the area and perimeter of rectangle class in java.
Software Required :- VS Code / JDK .
Theory:- In this program we have created a class name rectangle in which we create
two variables length and width. We take input using the utility scanner library then use
two functions to return the area and perimeter of the rectangle . Then we create a r
object then take input from the user for length and width then print the perimeter and
area of the rectangle
Code:-
import java.util.Scanner;
class Rectangle {
double length, width;
double getArea() {
return length * width;
}
double getPerimeter() {
return 2 * (length + width);
}
}
Output:-
Experiment No. 08
Aim :- Write a program to print Method overloading in java.
Software Required :- VS Code / JDK .
Theory:- In this program i had created a class named Sumover in which there are three
functions of the same name but in each function parameters are different. So when we
pass two numbers function with 2 parameters run then when we pass 3 numbers then
function with 3 parameters run and when we pass two double numbers then function
with two double parameters.
Code:-
public class Sumover{
public int sum(int x , int y){
return (x+y);
}
public int sum(int x , int y , int z){
return (x+y+z);
}
}
Output:-
Experiment No. 09
Aim :- Write a program to Polymorphism and Single Inheritance in java.
Software Required :- VS Code / JDK .
Theory:- In this program we created a two classes person and father in which we
created a role function in person class then same function override in father class.
When we call role function of father class then function of father class run not person
class.
Polymorphism
Code:-
class Person{
void role(){
System.out.println("I am Student");
}
}
@Override
void role(){
System.out.println("I am Father");
}
}
Single inheritance
Code:-
// Parent class
class Animal {
void sound() {
System.out.println("Animals make sounds");
}
}
// Main class
public class SingleInheritanceExample {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.sound(); // Calling method from parent class
myDog.bark(); // Calling method from child class
}
}
Output:-
Experiment - 10
Aim:- Write a program to show Interfacing between two classes.
Software Required :- VS Code / JDK .
Theory:- Interfacing allows multiple classes to implement a common structure using an
interface. It promotes abstraction, loose coupling, and polymorphism, enabling flexible
and scalable code with independent class implementations.
Code:-
// Interface definition
interface Vehicle {
void start(); // Abstract method
}
// Implementing the interface in Car class
class Car implements Vehicle {
public void start() {
System.out.println("Car is starting with a key...");
}
}
// Implementing the interface in Bike class
class Bike implements Vehicle {
public void start() {
System.out.println("Bike is starting with a self-start button...");
}
}
// Main class demonstrating interfacing
public class InterfaceDemo {
public static void main(String[] args) {
Vehicle myCar = new Car(); // Interface reference for Car
Vehicle myBike = new Bike(); // Interface reference for Bike
myCar.start(); // Calls Car's start() method
myBike.start(); // Calls Bike's start() method
}
}
Output:-
Experiment - 11
Aim:- Write a program to demonstrate AWT.
Software Required :- VS Code / JDK .
Theory:- Abstract Window Toolkit (AWT) is a Java package for creating graphical user
interfaces (GUIs). It provides components like buttons, labels, and text fields to build
interactive desktop applications.
Code:-
import java.awt.*;
import java.awt.event.*;
// AWT Example
public class AWTExample extends Frame implements ActionListener {
Button button;
AWTExample() {
button = new Button("Click Me");
button.setBounds(100, 100, 80, 30); // x, y, width, height
// Add action listener to the button
button.addActionListener(this);
// Add button to frame
add(button);
// Frame properties
setSize(300, 200); // Frame size
setLayout(null); // No layout manager
setVisible(true); // Show frame
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
}
public void actionPerformed(ActionEvent e) {
System.out.println("Button Clicked!");
}
public static void main(String[] args) {
new AWTExample();
}
}
Output:-
Experiment - 12
Aim:- Write a program to demonstrate multithreading using Java.
Software Required :- VS Code / JDK .
Theory:- Multithreading in Java allows concurrent execution of multiple threads,
improving performance and responsiveness. It is implemented using the Thread class
or runnable interface for parallel task execution.
Code:-
// Thread using Thread class
class NumberThread extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Number: " + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
class LetterThread implements Runnable {
public void run() {
for (char ch = 'A'; ch <= 'E'; ch++) {
System.out.println("Letter: " + ch);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
public class MultithreadingDemo {
public static void main(String[] args) {
NumberThread thread1 = new NumberThread(); // Using Thread class
Thread thread2 = new Thread(new LetterThread()); // Using Runnable interface
thread1.start();
thread2.start();
}
}
Output:-
Experiment - 13
Aim:- Write a Program to show Database Connectivity Using JAVA.
Software Required :- VS Code / JDK .
Theory:- Multithreading in Java allows concurrent execution of multiple threads,
improving performance and responsiveness. It is implemented using the Thread class
or runnable interface for parallel task execution.
Code:-
Experiment - 14
Aim:- Create a network TCP/UDP socket.
Software Required :- VS Code / JDK .
Theory:- Multithreading in Java allows concurrent execution of multiple threads,
improving performance and responsiveness. It is implemented using the Thread class
or runnable interface for parallel task execution.
Code:-
import java.io.*;
import java.net.*;
// Send response
output.println("Hello from Server!");
// Close connection
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:-