Java Word File..
Java Word File..
SESSION : 24-25
PRACTICAL FILE
String class.
StringBuffer class.
Built in packages
User defined packages.
URL class.
URLConnection.
25 *Write program that demonstrates connection oriented communication using
socket.
Insert record.
Update record.
Delete record.
package javaapplication1;
import java.util.Scanner;
Output :
Practical 2 : Write programs to evaluate different types of
expressions.
:
package javaapplication1;
import java.util.Scanner;
int a = 10;
int b = 5;
int sum = a + b;
int difference = a - b;
int product = a * b;
int remainder = a % b;
System.out.println("Arithmetic Expressions:");
}
Output :
Practical 3 : Write programs to demonstrate use of: if
statements (all forms of
if statement
Switch – Case statement
Different types of Loops(for,while and do..while).
:
:
package javaapplication1;
import java.util.Scanner;
} else {
}
}
}
Output:
Output : 3
3) Write a program to check switch-case using character datatype.
:
package javaapplication1;
import java.util.Scanner;
public class JavaApplication1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a character (A, B, C): ");
char ch = scanner.next().charAt(0);
switch (ch) {
case 'A':
System.out.println("You entered A.");
break;
case 'B':
System.out.println("You entered B.");
break;
case 'C':
System.out.println("You entered C.");
break;
default:
System.out.println("Invalid character.");
break;
}
scanner.close();
}
}
Output :
4) Write a program to display 1 to 20 numbers using for, while and do-while loop.
:
package javaapplication1;
import java.util.Scanner;
public class JavaApplication1 {
public static void main(String[] args) {
System.out.println("Using for loop:");
for (int i = 1; i <= 20; i++) {
System.out.print(i + " ");
}
System.out.println("\n\nUsing while loop:");
int j = 1;
while (j <= 20) {
System.out.print(j + " ");
j++;
}
System.out.println("\n\nUsing do-while loop:");
int k = 1;
do {
System.out.print(k + " ");
k++;
} while (k <= 20);
}
}
Output :
:
package javaapplication1;
import java.util.Scanner;
public class JavaApplication1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
do {
System.out.print("Enter a number (1-10) to continue or 0 to exit: ");
number = scanner.nextInt();
if (number >= 1 && number <= 10) {
System.out.println("You entered: " + number);
} else if (number != 0) {
System.out.println("Invalid input. Please enter a number between 1 and 10.");
}
}
while (number != 0);
System.out.println("Exiting the program.");
scanner.close();
}
}
Output :
Practical 4: Write programs for implementation of different
methods of:
String class.
StringBuffer class.
:
1) Write a program to show the use of all methods of String class.
package javaapplication1;
import java.util.Scanner;
System.out.println("Split: ");
for (String word : words) {
System.out.println(word);
Output :
2) Write a program to implement all methods of StringBuffer class.
Output :
Practical 5: Write programs to demonstrate:
Use of Array.
Use of Vectors.
:
1) Write a program to implement multidimensional array.
: package javaapplication1;
import java.util.Scanner;
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
System.out.println();
Output :
2) Write a program to display array elements using for-each loop.
: package javaapplication1;
import java.util.Scanner;
System.out.println("Array Elements:");
System.out.println(fruit);
Output :
3) Write a program to insert different elements in the Vector & display them
: package javaapplication1;
import java.util.Scanner;
import java.util.Vector;
vector.add("Hello");
vector.add(123);
vector.add(45.67);
vector.add(true);
System.out.println(element);
Output :
4) Write a program to use different methods of Vector class.
: package javaapplication1;
import java.util.Scanner;
import java.util.Vector;
vector.add("Apple");
vector.add("Banana");
vector.add("Cherry");
vector.add(1, "Orange");
vector.remove("Banana");
vector.clear();
vector.add("Grapes");
System.out.println("Vector after adding 'Grapes': " + vector);
Output :
Practical 6: Write programs using Wrapper Class:
to convert primitive into object.
to convert object into primitive.
package javaapplication1;
import java.util.Scanner;
if (comparisonResult < 0) {
} else {
Output :
package javaapplication1;
import java.util.Scanner;
Output :
import java.util.Scanner;
public class JavaApplication1 {
char ch = 'A';
Character charObj = Character.valueOf(ch);
Output :
4) Write a program to convert Integer object value into primitive datatype byte, short and
double value
:
package javaapplication1;
import java.util.Scanner;
Output :
Practical 7 : Develop a program for implementation of
different types of constructors.
:
1) Write a program to implement different types of constructors to perform addition of
complex numbers?
:
package javaapplication1;
import java.util.Scanner;
class Complex {
private double real;
private double imaginary;
public Complex() {
this.real = 0;
this.imaginary = 0;
}
Output :
Practical 8 : Develop program to implement:
Single inheritance.
Multilevel inheritance
import java.util.Scanner;
Output :
2) Write a program to implement multilevel inheritance
: class Animal {
public void eat() {
System.out.println("This animal eats food.");
}
}
Output :
: class Person {
private String name;
private int age;
4) Develop a program to calculate he room area and volume to illustrate the concept of single
inheritance.
package javaapplication1;
import java.util.Scanner;
class Room {
this.length = length;
this.width = width;
super(length, width);
this.height = height;
Output :
Practical 9 : Develop program for implementation of
interface.
1) Demonstrate the use of interfaces to implement the concept of multiple inheritance.( Attach
the code at the end).
:
package javaapplication1;
import java.util.Scanner;
interface Animal {
void eat();
void sleep();
}
interface Pet {
void play();
void groom();
}
dog.eat();
dog.sleep();
dog.play();
dog.groom();
}
}
Output :
import java.util.Scanner;
class Rectangle implements Shape { private double length; private double width;
@Override
public double calculateArea() {
return length * width;
}
}
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
Output :
Practical 10 : *Write programs to demonstrate use of:
Built in packages
User defined packages.