Core Java Practicals
Core Java Practicals
Code:
import java.util.Scanner;
public class Practs1 {
public static void main(String[] args) {
int m, n, c, d;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows of matrix");
m = in.nextInt();
System.out.println("Enter the number of columns of matrix");
n = in.nextInt();int first[][] = new int[m][n];
int second[][] = new int[m][n];
int sum[][] = new int[m][n];
System.out.println("Enter the elements of first matrix");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
{
first[c][d] = in.nextInt();
}
}
System.out.println("Enter the elements of second matrix");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
{
second[c][d] = in.nextInt();
}
}
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
{
sum[c][d] = first[c][d] + second[c][d]; //replace '+' with '-' to subtract matrices
}
}
System.out.println("Sum of entered matrices:-");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
{
System.out.print(sum[c][d]+"\t");
}
System.out.println();
}
}
}
3 Create a package: animals. In the package animals create interface Animal with
suitable behaviours. Implement the interface Animal in the same package animals.
Code:
Interface:
package animals;
public interface Animal {
public void name(String nm);
public void color(String cl);
}
Class:
package animals;
public class Animals implements Animal {
public static void main(String[] args) {
Animals an=new Animals();
an.name("Cat");
an.color("Black");
}
Method OverRiding:
package methodoverriding;
public class MethodOverriding {
public static void main(String[] args) {
Base b1=new Base();
b1.add(4, 5);
Base b2=new Derived();
b2.add(6,7);
}
}
class Base
{
void add(int a,int b)
{
int c=a+b;
System.out.println("addition of base class="+ c);
}
}
class Derived extends Base
{
void add(int a,int b)
{
int c=a+b;
System.out.println("addition of derived class="+ c);
}
}
Code:
package practs7;
class MyException extends Exception
{ String str;
MyException(String s)
{
str=s;
}
// Access an element
String firstFruit = fruits.get(0);
System.out.println("First fruit: " + firstFruit);
// Modify an element
fruits.set(2, "Strawberry");
System.out.println("List after modifying element at index 2: " + fruits);
Code:
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Practical10 extends Frame implements ActionListener{
Label no1,no2,result;
TextField tno1,tno2,tresult;
Button badd,bsub,bmul,bdiv,bclr;
public Practical10() {
setLayout(new FlowLayout());
no1=new Label("Enter 1 st no");
no2=new Label("Enter 2 nd no");
result=new Label("Result");
tno1=new TextField(10);
tno2=new TextField(10);
tresult=new TextField(10);
badd= new Button("Add");
bsub= new Button("Sub");
bmul= new Button("Mul");
bdiv= new Button("Div");
bclr= new Button("Clear");
add(no1);
add(tno1);
add(no2);
add(tno2);
add(result);
add(tresult);
add(badd);
add(bsub);
add(bmul);
add(bdiv);
add(bclr);
badd.addActionListener(this);
bsub.addActionListener(this);
bmul.addActionListener(this);
bdiv.addActionListener(this);
bclr.addActionListener(this);
setSize(120,300);
setVisible(true);
}
public static void main(String[] args) {
Practical10 p=new Practical10();
}
try {
// Copy the file content from source to destination
Files.copy(sourcePath, destinationPath,
StandardCopyOption.REPLACE_EXISTING);
System.out.println("File copied successfully!");
} catch (IOException e) {
// Handle potential I/O errors
System.err.println("Error occurred while copying the file: " + e.getMessage());
}
}
}
10 Write a TCP client-server program; the client accepts a number from the user &
sends it to the server, the server returns the factorial of that number to the client.
Server code:
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
while (true) {
try (Socket socket = serverSocket.accept();
BufferedReader input = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter output = new PrintWriter(socket.getOutputStream(), true)) {
System.out.println("Client connected");
// Calculate factorial
long factorial = calculateFactorial(number);
} catch (IOException e) {
System.err.println("Client communication error: " + e.getMessage());
}
}
} catch (IOException e) {
System.err.println("Server error: " + e.getMessage());
}
}
System.out.println("Connected to server");
} catch (IOException e) {
System.err.println("Client error: " + e.getMessage());
}
}
}