Advance - Java - File - 1-5 - (1) (1) Anshu
Advance - Java - File - 1-5 - (1) (1) Anshu
Advance - Java - File - 1-5 - (1) (1) Anshu
Faculty name: Ms. Zameer Fatima Student name: Anshu Kumar Pathak
Roll No.: 004148227222
Semester: 6th
Group: FSD – II B
CODE:
public class Demo { public static void
main(String args[]) { int arr[][] = new
int[2][2]; int n = 0;
System.out.println("\nOriginal Array");
for (int i = 0; i < arr.length; i++) { for
(int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
System.out.println("\nReversed Array");
for (int i = 0; i < arr.length; i++) { for
(int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
OUTPUT:
Practical 1(b)
AIM: Write a java program to produce the tokens from a given long string.
CODE:
import java.util.StringTokenizer;
import java.util.Scanner;
class demo{
System.out.println(st.nextToken());
}}}
OUTPUT:
Practical 1(c)
AIM: Using the concept of method overloading, Write method for calculating the area of triangle, circle
and rectangle.
CODE:
class demo{
s3)/2;
return Math.sqrt(s*(s-s1)*(s-s2)*(s-s3));
}
CODE:
class Box{ double width; double length; double height;
void show(){
System.out.println("Dimenstions of box are: "); System.out.println("Width: " + width);
System.out.println("Length: " + length); System.out.println("Height: " + height);
} } class demo{ public static void main(String args[]){ Box b =
}
}
OUTPUT:
Practical 1(e)
AIM: Write a program that can count the number of instances created for the class.
CODE:
class Box{ static int objects; static{ objects
= 0; } double width; double length; double
height;
demo{
}
}
OUTPUT:
Practical 1(f)
AIM: Java Program to get the cube of a given number using the static method.
CODE:
import java.util.Scanner; class demo{ static
double cube(double n){ return n*n*n;
} public static void main(String args[]){ Scanner sc = new Scanner(System.in);
System.out.print("Enter
the number: "); double n = sc.nextDouble();
System.out.println("Cube of " + n + " is " + cube(n));
}
}
OUTPUT:
Lab 2
Practical 2(a)
AIM: Create a base class Fruit which has name ,taste and size as its attributes. A method called eat() is created
which describes the name of the fruit and its taste. Inherit the same in 2 other class Apple and Orange and
override the eat() method to represent each fruit taste. (Method overriding).
CODE:
// Base class Fruit class
Fruit { protected String
name; protected String
taste; protected String
size;
OUTPUT:
Practical 2(b)
AIM: Write a program to create a class named shape. It should contain 2 methods- draw() and erase() which
should print “Drawing Shape” and “Erasing Shape” respectively. For this class we have three sub classes-
Circle, Triangle and Square and each class override the parent class functions- draw () and erase (). The draw()
method should print “Drawing Circle”, “Drawing Triangle”, “Drawing Square” respectively. The erase()
method should print “Erasing Circle”, “Erasing Triangle”, “Erasing Square” respectively. Create objects of
Circle, Triangle and Square in the following way and observe the polymorphic nature of the class by calling
draw() and erase() method using each object. Shape c=new Circle(); Shape t=new Triangle(); Shape s=new
Square(); (Polymorphism)
CODE:
// Base class Shape
class Shape {
// Method to draw the shape
public void draw() {
System.out.println("Drawing Shape");
}
c.draw();
c.erase();
t.draw();
t.erase();
s.draw();
s.erase();
}
}
OUTPUT:
Practical 2(c)
AIM: Write a Program to take care of Number Format Exception if user enters values other than integer for
calculating average marks of 2 students. The name of the students and marks in 3 subjects are taken from the
user while executing the program. In the same Program write your own Exception classes to take care of
Negative values and values out of range (i.e. other than in the range of 0-100)
CODE:
import java.util.Scanner;
try {
System.out.println("Enter name of first student:");
String student1Name = scanner.nextLine();
OUTPUT:
Practical 2(d)
AIM: Write a program that takes as input the size of the array and the elements in the array. The program
then asks the user to enter a particular index and prints the element at that index. Index starts from zero.This
program may generate Array Index Out Of Bounds Exception or NumberFormatException . Use exception
handling mechanisms to handle this exception.
CODE:
import java.util.Scanner;
try {
System.out.println("Enter the size of the array:");
int size = Integer.parseInt(scanner.nextLine());
getElementAtIndex(array, index);
} catch (NumberFormatException e) {
System.out.println("NumberFormatException: Please enter valid integer values.");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException: Index is out of bounds.");
}}
private static int getElementAtIndex(int[] array, int index) throws ArrayIndexOutOfBoundsException {
if (index < 0 || index >= array.length) { throw new ArrayIndexOutOfBoundsException("Index is out
of bounds.");
} return
array[index];
}}
OUTPUT:
Lab 3
Practical 3(a)
AIM: Write a Java program to demonstrate the concept of socket programming.
CODE:
Server.java
import java.io.BufferedReader;
import
java.io.InputStreamReader;
import java.io.PrintWriter; import
java.net.ServerSocket; import
java.net.Socket;
clientSocket.close();
serverSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Client.java
import java.io.BufferedReader;
import
java.io.InputStreamReader;
import java.io.PrintWriter; import
java.net.Socket; public class
Client {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 1234);
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
OUTPUT:
Practical 3(b)
AIM: Implement Datagram UDP socket programming in java.
CODE:
Server.java
import java.io.BufferedReader;
import
java.io.InputStreamReader;
import java.io.PrintWriter; import
java.net.ServerSocket; import
java.net.Socket;
clientSocket.close();
serverSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Client.java
import java.io.BufferedReader;
import
java.io.InputStreamReader;
import java.io.PrintWriter; import
java.net.Socket;
public class Client { public static void
main(String[] args) { try {
Socket socket = new Socket("localhost", 1234);
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
OUTPUT:
Practical 3(c)
AIM: Implement Socket programming for TCP in Java Server and Client Sockets.
CODE:
Server.java
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import
java.io.InputStreamReader;
import java.net.*; public class
Server {
public static void main(String arg[]) throws Exception {
ServerSocket ss = new ServerSocket(8080);
Socket s = ss.accept();
DataInputStream din = new DataInputStream(s.getInputStream());
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
OUTPUT:
Lab 4
Practical 4(a)
AIM: Implement a program Java Bean to represent person details.
CODE:
Person.java
package lab4; import
java.io.Serializable;
// Default constructor
public Person() {
}
person.setLastName("Pathak");
person.setEmail("anshu@gmail.com");
OUTPUT:
Practical 4(b)
CODE:
Car.java
package lab4; import java.io.Serializable;
public class Car implements Serializable
{
private String model;
private int year; private
double mileage; //
Default constructor
public Car() {
}
// Parameterized constructor public Car(String
model, int year, double mileage) {
this.model = model;
this.year = year;
this.mileage = mileage;
}
// Getter and Setter methods
public String getModel() {
return model; } public void
setModel(String model) {
this.model = model;
}
EncapsulationDemo.java
OUTPUT:
Lab 5
Practical 5(a)
AIM: Write a JSP program to display your name.
PROCEDURE:
1. Create a new file with a .jsp extension, here it is “index.jsp.”
2. Write JSP code to display your name and save the file.
3. Start your Tomcat server by right clicking configured Tomcat Server.
4. Now your JSP code will be visible on the web browser.
CODE: index.jsp
CODE: formValidation.jsp
OUTPUT: