Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Java Programming (1)

The document outlines a series of Java programming experiments, each focusing on different concepts such as printing text, variable scope, command line arguments, typecasting, and class structures. It includes code examples and explanations for each experiment, covering topics like method overloading, polymorphism, interfacing, AWT for GUI, multithreading, and database connectivity. The document serves as a practical guide for learning Java programming through hands-on coding exercises.

Uploaded by

Armaan Saxena
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Java Programming (1)

The document outlines a series of Java programming experiments, each focusing on different concepts such as printing text, variable scope, command line arguments, typecasting, and class structures. It includes code examples and explanations for each experiment, covering topics like method overloading, polymorphism, interfacing, AWT for GUI, multithreading, and database connectivity. The document serves as a practical guide for learning Java programming through hands-on coding exercises.

Uploaded by

Armaan Saxena
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Experiment No.

01

Aim :- Write a Program in Java to print text on screen.


Software Required :- VS Code / JDK .

Theory :- In Java, use System.out.println() to print text to the console. It belongs


to the System class and outputs text with a newline, making it essential for displaying
messages.
Code:-

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:

1. Local Scope – Variables declared inside methods or blocks, accessible only


within them.
2. Instance Scope – Non-static variables inside a class, accessible via objects.
3. Static (Class) Scope – Declared with static, shared across all instances.

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

Aim :- Write a Program in Java to show concepts of Command Line Example.

Software Required :- VS Code / JDK .

Theory:- A command-line example in Java involves passing arguments to the program


via the terminal. The main method accepts parameters as a String[] args array.
These arguments can be accessed and processed within the program. This approach is
useful for dynamic input, automation, and script-based execution without user
interaction.
Code:-
Part - 1
class CommandLineExample{
public static void main(String args[]){
System.out.println("Your First argument is:"+args[0]);
}
}

Output:-

Part - 2
class Aclass{
public static void main(String args[]){
for (String arg : args) {
System.out.println(arg);
}
}
}
Output:-
Experiment No. 04

Aim :- Write a Program in Java to show concepts of Typecasting.


Software Required :- VS Code / JDK .
Theory:- Type Casting in Java is converting one data type into another. There are two
types:

1. Implicit (Widening) Typecasting – Automatically done by Java when converting a


smaller data type to a larger one, e.g., int to double.
2. Explicit (Narrowing) Typecasting – Done manually by the programmer when
converting a larger data type to a smaller one using (type), e.g., double to
int.

Code:-

public class TypeCastProg{

public static void main(String args[]){

double x;

int y;

x=25.50;

System.out.println("Value of [double] x: " + x);

System.out.println("conversion of double to integer");

y=(int)x;

System.out.println("Value of [integer] y: " + y);

int m;

double n;

m=10;

n=(double)m;

System.out.println("Value of [integer] m: " + m);

System.out.println("Conversion of integer to double");

System.out.println("Value of [double] n: " + n);


}

Output:-

Experiment No. 05

Aim :- Write a Program in Java to print the sum of two numbers.


Software Required :- VS Code / JDK .
Theory:- In this program i have taken input of two numbers from the user then sum of
two numbers into another variable and print the sum of two numbers.
Code:-
import java.util.Scanner;
class Sum{
public static void main(String args[]){
int a;
int b;
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number: ");
a = sc.nextInt();
System.out.println("Enter second number: ");
b = sc.nextInt();

// int c = a+b;
System.out.println("Sum is: " + (a+b) );

}
}
Output:-

Experiment No. 06

Aim :- Write a program to show concepts of class in java.


Software Required :- VS Code / JDK .
Theory:- In this program we create a person class in which we create object using
different types of constructor like default constructor , parameterized constructor then
use set Person function to set values of name and age of person object then use print
person function to print values of person.
Code:-
class Person {
private String name;
private int age;

// Default constructor
Person() {
this.name = "";
this.age = 0;
}

// Parameterized constructor
Person(String name, int age) {
this.name = name;
this.age = age;
}

// Method to set person details


void setPerson(String name, int age) {
this.name = name;
this.age = age;
}

// Method to print person details


void printPerson() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}

public static void main(String[] args) {


// Creating a person object using default constructor
Person person1 = new Person();
person1.printPerson();

// Creating a person object using parameterized constructor


Person person2 = new Person("Armaan", 19);
person2.printPerson();
// Setting person details using setPerson method
person1.setPerson("John Nolan", 40);
person1.printPerson();
}
}
Output:-

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;

void setValues(double l, double w) {


length = l;
width = w;
}

double getArea() {
return length * width;
}

double getPerimeter() {
return 2 * (length + width);
}
}

public class RectangleMain {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
sc.close();
System.out.print("Enter length: ");
double length = sc.nextDouble();

System.out.print("Enter width: ");


double width = sc.nextDouble();

Rectangle rect = new Rectangle();


rect.setValues(length, width);

System.out.println("Area: " + rect.getArea());


System.out.println("Perimeter: " + rect.getPerimeter());
}
}

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);
}

public double sum(double x , double y){


return (x+y);
}
public static void main(String args[]){
Sumover s = new Sumover();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.3));

}
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");
}
}

class Father extends Person{

@Override
void role(){
System.out.println("I am Father");
}
}

public class main{


public static void main(String[] args) {
Person p = new Father();
p.role();
}
}
Output:-

Single inheritance
Code:-
// Parent class
class Animal {
void sound() {
System.out.println("Animals make sounds");
}
}

// Child class (inherits from Animal)


class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}

// 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.*;

public class TCPServer {


public static void main(String[] args) {
int port = 5000; // Port number
try (ServerSocket serverSocket = new ServerSocket(port)) {
System.out.println("TCP Server is running on port " + port);

// Wait for a client connection


Socket socket = serverSocket.accept();
System.out.println("Client connected!");

// Read data from client


BufferedReader input = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);

String clientMessage = input.readLine();


System.out.println("Received from client: " + clientMessage);

// Send response
output.println("Hello from Server!");

// Close connection
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Output:-

You might also like