尊敬的 微信汇率:1円 ≈ 0.046166 元 支付宝汇率:1円 ≈ 0.046257元 [退出登录]
SlideShare a Scribd company logo
JYOTIRADIRYA BHATT 2102818 BCA A 38
1
1. WAP in java to simulate condition to generate Wi-Fi password. Take input as Name, City,
Age and Gender.
* import java.util.Scanner;
public class termwork1 {
public static void main(String[] args) {
String name,city,gender;
int age;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the name:");
name=sc.next();
if(name.length()<=3)
{
System.out.println("Name should be greater than 3 words:");
System.out.println("Enter the name:");
name=sc.next();
}
System.out.println("Enter the city:");
city=sc.next();
if(city.length()<=3)
{
System.out.println("City name should be grater than 3 words:");
System.out.println("Enter the city:");
city=sc.next();
}
System.out.println("Enter the gender(M/F):");
gender=sc.next();
System.out.println("Enter the age:");
age=sc.nextInt();
if(age==18)
{
System.out.println("Enter the age again:");
age=sc.nextInt();
}
PasswordGenerator pa=new
PasswordGenerator(name,city,gender,age);
}
}
class PasswordGenerator
{
String password="";
JYOTIRADIRYA BHATT 2102818 BCA A 38
2
int age,sum=0,counter=0,diff=0;
PasswordGenerator(){
}
PasswordGenerator(String n,String c,String g,int a) {
if (g.equals("F") && a < 18) {
for (int i = 0; i < 3; i++) {
password = password + String.valueOf(n.charAt(i));
}
String d = Integer.toString(a);
for (int i = 0; i < d.length(); i++) {
char ca = d.charAt(i);
sum = sum + Character.getNumericValue(ca);
}
password = password + Integer.toString(sum);
int i=c.length()-3;
while (counter != 3) {
password = password + String.valueOf(c.charAt(i));
i++;
counter++;
}
System.out.println(password);
}
else if(g.equals("F")&& a>18)
{
int i=n.length()-3;
while (counter != 3) {
password = password + String.valueOf(n.charAt(i));
i++;
counter++;
}
String d=Integer.toString(a);
for(int j=0;j<d.length();j++)
{
char ca = d.charAt(j);
if(ca>diff){
diff = Character.getNumericValue(ca)-diff;}
else if(ca<diff){
diff = diff- Character.getNumericValue(ca);
}
else
{
JYOTIRADIRYA BHATT 2102818 BCA A 38
3
diff=1;
}
}
password = password + Integer.toString(diff);
for (int j = 0; j < 3; j++) {
password = password + String.valueOf(c.charAt(j));
}
System.out.println(password);
}
else {
int i=0,j=0;
while( i<n.length()&& j<c.length()) {
password = password + String.valueOf(n.charAt(i));
password = password + String.valueOf(c.charAt(j));
i++;
j++;
}
password=password+a;
System.out.println(password);
}
}
}
*
OUTPUT
JYOTIRADIRYA BHATT 2102818 BCA A 38
4
2. WAP in Java to initialize a string in order to find that character which frequency is 2nd most in
that string.
*
import java.util.Scanner;
public class termwork2 {
public static void main(String[] args){
String s="";
Scanner sc=new Scanner(System.in);
System.out.println("enter the string");
s=sc.next();
int l=s.length();
int ar[]=new int[l];
for(int i=0;i< ar.length;i++)
ar[i]=1;
for(int i=0;i<l;i++)
{
for(int j=i+1;j<l;j++)
{
if(ar[j]!=-1) {
if (s.charAt(i) == (s.charAt(j))) {
ar[j]=-1;
++ar[i];
}
}
}
JYOTIRADIRYA BHATT 2102818 BCA A 38
5
}
int max=0,smax=0,n=0;
for(int i=0;i<ar.length;i++)
for(int j=i+1;j<ar.length-1;j++)
{
if(ar[j]!=-1)
{
if(ar[i]>=ar[j])
{
max = ar[i];
if(ar[j]<=max&&ar[j]>=smax)
{
n=j;
smax=ar[j];
}
}
}
}
System.out.println("the letter with second highest frequency is "+s.charAt(n)+" and no.of
times its came is "+ar[n]);
}
}
*
OUTPUT
JYOTIRADIRYA BHATT 2102818 BCA A 38
6
3.WAP to check longest sub sequence of a same character in an initialized string?[aaaabppppp,
p=5] [aabbcc, a=2].
*
import java.util.Arrays;
import java.util.Scanner;
public class termwork3 {
public static void main(String[] args) {
String s="";
Scanner sc=new Scanner(System.in);
System.out.println("enter the string");
s=sc.next();
int[] ar =new int[s.length()];
Arrays.fill(ar, 1);
for(int i=0;i<ar.length;i++)
for(int j=i+1;j<ar.length;j++)
{
if (ar[i] != -1||ar[j]!=-1) {
if (s.charAt(i) == s.charAt(j))
{
ar[j]=-1;
++ar[i];
JYOTIRADIRYA BHATT 2102818 BCA A 38
7
}
else
break;
}
}
int max=0,n=0;
for(int i=0;i<ar.length;i++)
if(ar[i]!=-1)
{
if(ar[i]>max)
{
max=ar[i];
n=i;
}
}
System.out.println("the letter with most occurrence is :: "+s.charAt(n)+" which repeats ::
"+max+" times");
}
}*
OUTPUT
JYOTIRADIRYA BHATT 2102818 BCA A 38
8
4.WAP to generate wifi key as user will enter form value Name,City,Age and Gender?
*
import java.util.Scanner;
public class termwork4 {
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
String name="";
String city="";
int age;
String gender="";
System.out.println("enter the gender ::");
gender=sc.next();
System.out.println("enter the name ::");
name=sc.next();
System.out.println("enter the city name ::");
city=sc.next();
System.out.println("enter the age ::");
age=sc.nextInt();
System.out.println("the key generated is :: "+passwordGenerator(name,city,gender,age));
}
static String passwordGenerator(String n,String c,String g,int a)
{
String key="";
if(g.equals("M")||g.equals("m"))
{
for(int i=0;i<3;i++)
JYOTIRADIRYA BHATT 2102818 BCA A 38
9
{
key=key+n.charAt(i);
}
String m=Integer.toString(a);
int i=0;
int k=(Character.getNumericValue(m.charAt(i))-
Character.getNumericValue(m.charAt(i+1)));
if(k>0)
key=key+k;
else if(k<0)
key=key+(-k);
else
key=key+0;
for(int j=2;j>=0;j--)
{
key=key+c.charAt((c.length()-1)-j);
}
}
else if(g.equals("F")||g.equals("f"))
{
for (int i=2;i>=0;i--)
{
key=key+n.charAt((n.length()-1)-i);
}
String m=Integer.toString(a);
int i=0;
int k=Character.getNumericValue(m.charAt(i))+Character.getNumericValue(m.charAt(i+1));
key=key+k;
JYOTIRADIRYA BHATT 2102818 BCA A 38
10
for(int j=0;j<3;j++){
key=key+c.charAt(j);
}
}
return key;
}
} *
OUTPUT
5.WAP to delete only those text file which are non empty in these folder.[E://MCA/BCA/DCA]
*
import java.util.*;
import java.io.*;
public class termwork5 {
public static void main(String[] args)throws Exception
{
File f = new File("E:/MCA/BCA/DCA");
File[] files=f.listFiles();
for(File f1:files) {
FileReader fr = new FileReader(f1);
int m;
JYOTIRADIRYA BHATT 2102818 BCA A 38
11
if ((m = fr.read())!= -1) {
fr.close();
boolean s = f1.delete();
if (s)
System.out.println("file deleted");
else
System.out.println("file not deleted");
} else System.out.println("file is empty");
}
}
}
*
OUTPUT
6.WAP to create a method check which returns two values.If first string having a character twice
as well second string also then return both the string by removing that character?
*
import java.util.Scanner;
public class termwork6 {
public static void main(String[] ar)
{
Scanner sc=new Scanner(System.in);
String s1="";
JYOTIRADIRYA BHATT 2102818 BCA A 38
12
String s2="";
System.out.println("enter the value of first string ");
s1=sc.next();
System.out.println("enter the value of second string ");
s2=sc.next();
System.out.println("enter the character that is needed to be checked");
String m=sc.next();
String s3[]=check(s1,s2,m);
System.out.println(s3[0]+" "+s3[1]);
}
static String [] check(String s1,String s2,String m)
{
String s []=new String[2];
int q=0,n=0;
for(int i=0;i<s1.length();i++)
{
if(m.charAt(0)==(s1.charAt(i)))
{
++q;
if(q==2)
{ s[0]="";
for(int j=0;j<s1.length();j++)
{
if(m.charAt(0)!=(s1.charAt(j)))
{
s[0]=s[0]+s1.charAt(j);
}
JYOTIRADIRYA BHATT 2102818 BCA A 38
13
}
}
}
else if(q!=2)
s[0]=s1;
}
for(int i=0;i<s2.length();i++)
{
if(m.charAt(0)==(s2.charAt(i)))
{
++n;
if(n==2)
{s[1]="";
for(int j=0;j<s2.length();j++)
{
if(m.charAt(0)!=(s2.charAt(j)))
{
s[1]=s[1]+s2.charAt(j);
}
}
}
}
else if(n!=2)
s[1]=s2;
}
return s;
}
JYOTIRADIRYA BHATT 2102818 BCA A 38
14
}
*
OUTPUT
7.WAP to print number of days gape in your age as enter DD-MM-YYYY with current system
date?
*
import java.text.*;
import java.util.*;
public class termwork7 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Get user input for birthdate
System.out.print("Enter your birthdate (DD-MM-YYYY): ");
String birthdateInput = scanner.next();
try {
// Calculate and print the age difference in days
int ageDifference = calculateAgeDifference(birthdateInput);
System.out.println("Number of days gap in your age: " +
ageDifference + " days");
} catch (ParseException e) {
JYOTIRADIRYA BHATT 2102818 BCA A 38
15
System.out.println("Invalid date format. Please enter the date in DD-MM-YYYY format.");
}
}
private static int calculateAgeDifference(String birthdate) throws
ParseException {
// Get the current system date
Date currentDate = new Date();
// Define the date format for parsing
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
// Convert the input birthdate string to a Date object
Date birthdateObj = dateFormat.parse(birthdate);
// Calculate the age difference in days
long ageDifferenceInMillis = currentDate.getTime() -
birthdateObj.getTime();
int ageDifferenceInDays = (int) (ageDifferenceInMillis / (24 * 60 *
60 * 1000));
return ageDifferenceInDays;
}
}
*
OUTPUT
JYOTIRADIRYA BHATT 2102818 BCA A 38
16
9.WAP in an Applet Make two Button one is Circle within Square and second is Square within
Circle. When user click on Circle within Square then draw Circle within Square. And when user
click on Square within Circle then draw Square within Circle on the panel window?
*
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class termwork9 extends Applet implements ActionListener
{
int i=0;
Button b1;
Button b2;
public void init()
{
b1=new Button("squareWithinCircle");
b1.addActionListener(this);
add(b1);
b2=new Button("cirleWithinSquare");
b2.addActionListener(this);
add(b2);
}
public void paint(Graphics g)
{
if (i==1){
g.drawRect(129,129,141,141);
g.drawOval(100,100,200,200);
}
else if(i==2)
JYOTIRADIRYA BHATT 2102818 BCA A 38
17
{
g.drawOval(100,100,200,200);
g.drawRect(100,100,200,200);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
i=1;
else if(e.getSource()==b2)
i=2;
repaint();
}
}
//<applet code="termwork9.java" width=1000 height=1000></applet>
*
OUTPUT
JYOTIRADIRYA BHATT 2102818 BCA A 38
18
10.WAP to put one Exception class in a package and to use this Exception class object in an
another package class method any how?
*
package Package1;
public class CustomException extends Exception
JYOTIRADIRYA BHATT 2102818 BCA A 38
19
{
public CustomException(String message) {
super(message);
}
*
*
package Package2;
import Package1.CustomException;
public class ExampleClass {
public void someMethod() throws CustomException {
throw new CustomException("This is a custom exception.");
}
*
*
import Package2.ExampleClass;
import Package1.CustomException;
public class termwork10{
public static void main(String[] args) {
ExampleClass example = new ExampleClass();
try {
example.someMethod();
} catch (CustomException e) {
System.out.println("Caught custom exception: " +
e.getMessage());
}
*
OUTPUT
JYOTIRADIRYA BHATT 2102818 BCA A 38
20
11.WAP to initialize 2D string array at runtime and to print reverse value of diagonal position
only?
*
import java.util.Scanner;
public class termwork11 {
public static void main(String[] args) {
String [][]a=new String[2][2];
Scanner sc=new Scanner(System.in);
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
a[i][j]=sc.next();
}
}
reverseValue(a);
}
static void reverseValue(String[][]x)
{
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
if(i==j)
JYOTIRADIRYA BHATT 2102818 BCA A 38
21
{
String s=x[i][j].toString();
StringBuffer st=new StringBuffer(s);
st.reverse();
System.out.println("The diagonal reverse
value of "+x[i][j]+" is:"+st);
}
}
}
}
}
*
OUTPUT
12.WAP to show the Working of multithreading . Make separate program by using Runnable
interface And by using Thread Class .also use their methods .
*
class MyRunnable implements Runnable {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Thread using Runnable: " + i);
try {
JYOTIRADIRYA BHATT 2102818 BCA A 38
22
Thread.sleep(500); // Simulate some work being
done
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class termwork12{
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
// Creating two threads using the same instance of
MyRunnable
Thread thread1 = new Thread(myRunnable);
Thread thread2 = new Thread(myRunnable);
// Starting the thread
thread1.start();
thread2.start();
}
}
*
OUTPUT
JYOTIRADIRYA BHATT 2102818 BCA A 38
23
USING Thread CLASS
*
class MyThread extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Thread using Thread class: " + i);
try {
sleep(500); // Simulate some work being done
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class termwork12 {
JYOTIRADIRYA BHATT 2102818 BCA A 38
24
public static void main(String[] args) {
MyThread thread1 = new MyThread();
MyThread thread2 = new MyThread();
// Starting the threads
thread1.start();
thread2.start();
}
}
*
OUTPUT
13.WAP using Applet to draw circle, line ,rectangle and fill them with a color given by the user.
*
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class termwork13 extends Applet {
TextField colorTextField;
JYOTIRADIRYA BHATT 2102818 BCA A 38
25
public void init() {
colorTextField = new TextField("#FF0000", 20);
Button fillButton = new Button("Fill Rectangle");
fillButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
repaint();
}
});
add(new Label("Enter color code (e.g., #FF0000):"));
add(colorTextField);
add(fillButton);
}
public void paint(Graphics g) {
g.drawLine(2, 3, 100, 100);
g.drawRect(200, 300, 400, 300);
g.drawOval(600,200,400,500);
String inputColor = colorTextField.getText();
try {
Color fillColor = Color.decode(inputColor);
g.setColor(fillColor);
g.fillRect(200, 300, 400, 300);
g.fillOval(600,200,400,500);
} catch (NumberFormatException ex) {
System.out.println("Invalid number format: " + inputColor);
System.out.println("Please enter a valid color code in the format #RRGGBB");
} catch (IllegalArgumentException ex) {
System.out.println("Invalid color code: " + inputColor);
JYOTIRADIRYA BHATT 2102818 BCA A 38
26
System.out.println("Please enter a valid color code in the format #RRGGBB");
}
}
}
//<applet code="termwork13.java" width=1000 height=1000></applet>
*
OUTPUT
15.WAP to show the use of KeyListener/MouseListener interface in order to generate and
process those events.
*import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Pt15 extends JFrame implements KeyListener, MouseListener {
public Pt15() {
setTitle("Event Demo");
setSize(300, 200);
JYOTIRADIRYA BHATT 2102818 BCA A 38
27
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(this);
addMouseListener(this);
setFocusable(true);
}
public void keyPressed(KeyEvent e) {
System.out.println("Key Pressed: " + e.getKeyChar());
}
public void keyReleased(KeyEvent e) {
System.out.println("Key Released: " + e.getKeyChar());
}
public void keyTyped(KeyEvent e) {
// Not used in this example
}
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse Clicked at (" + e.getX() + ", " + e.getY() +
")");
}
public void mousePressed(MouseEvent e) {
// Not used in this example
}
public void mouseReleased(MouseEvent e) {
// Not used in this example
}
public void mouseEntered(MouseEvent e) {
// Not used in this example
}
JYOTIRADIRYA BHATT 2102818 BCA A 38
28
public void mouseExited(MouseEvent e) {
// Not used in this example
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Pt15().setVisible(true);
}
});
}
}
*
OUTPUT
16.WAP to create TCP/IP Socket on both client and server side . and after socket creation
perform the Operation as done in chat server .
*
Client:
import java.io.*;
import java.net.*;
JYOTIRADIRYA BHATT 2102818 BCA A 38
29
public class ChatClient {
public static void main(String[] args) {
try {
// Connect to the server on the specified port
Socket socket = new Socket("localhost", 12345);
// Set up input and output streams
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(),
true);
// Create a BufferedReader to read messages from the console
BufferedReader consoleInput = new BufferedReader(new
InputStreamReader(System.in));
// Start a thread to continuously read messages from the server
new Thread(() -> {
String serverMessage;
try {
while ((serverMessage = in.readLine()) != null) {
System.out.println("Server: " + serverMessage);
}
} catch (IOException e) {
e.printStackTrace();
}
}).start();
// Start a loop to read and send messages
String clientMessage;
while (true) {
JYOTIRADIRYA BHATT 2102818 BCA A 38
30
// Read a message from the console and send it to the
server
System.out.print("Enter your message: ");
clientMessage = consoleInput.readLine();
out.println(clientMessage);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Server:
import java.io.*;
import java.net.*;
public class ChatServer {
public static void main(String[] args) {
try {
// Create a server socket and bind it to a specific port
ServerSocket serverSocket = new ServerSocket(12345);
System.out.println("Server is waiting for client
connection...");
// Wait for a client to connect
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected!");
// Set up input and output streams
BufferedReader in = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
JYOTIRADIRYA BHATT 2102818 BCA A 38
31
PrintWriter out = new
PrintWriter(clientSocket.getOutputStream(), true);
// Create a BufferedReader to read messages from the console
BufferedReader consoleInput = new BufferedReader(new
InputStreamReader(System.in));
// Start a loop to read and send messages
new Thread(() -> {
String clientMessage;
try {
while ((clientMessage = in.readLine()) != null) {
System.out.println("Client: " + clientMessage);
}
} catch (IOException e) {
e.printStackTrace();
}
}).start();
String serverMessage;
while (true) {
// Read a message from the console and send it to the
client
System.out.print("Enter your message: ");
serverMessage = consoleInput.readLine();
out.println(serverMessage);
}
} catch (IOException e) {
e.printStackTrace();
}
JYOTIRADIRYA BHATT 2102818 BCA A 38
32
}
}
OUTPUT
SERVER
CLIENT
JYOTIRADIRYA BHATT 2102818 BCA A 38
33
17.WAP to print all numeric digits sum of all database column values.
*
import com.sun.jdi.connect.Connector;
import java.sql.*;
public class termwork17 {
public static void main(String[] args) throws
ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/testDB","root","Tsdk123#");
PreparedStatement pt=con.prepareStatement("Select *from Student ");
ResultSet rs=pt.executeQuery();
String s="";
while(rs.next())
{
s=rs.getString(1);
s=s+rs.getString(3);
}
System.out.println(s);
int sum=0;
for(int i=0;i<s.length();i++)
{
char c=s.charAt(i);
sum=sum+Character.getNumericValue(c);
}
JYOTIRADIRYA BHATT 2102818 BCA A 38
34
System.out.println("Sum of column is :"+sum);
}
}
*
OUTPUT
18.WAP to update a table column value using stored procedures.
*
import java.sql.*;
public class termwork18 {
public static void main(String[] args) {
try {
// Load the JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/termwork", "root", "Tdsk123#");
CallableStatement callableStatement =
connection.prepareCall("{call update_employees(?, ?)}");
JYOTIRADIRYA BHATT 2102818 BCA A 38
35
callableStatement.setInt(1, 1); // Assuming 1 is the empid ofthe row you want to update
callableStatement.setString(2, "yogesh"); // New employee name
// Execute the stored procedure
callableStatement.execute();
System.out.println("Update successful!");
// Close resources
callableStatement.close();
connection.close();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace(); }}
}
OUTPUT
JYOTIRADIRYA BHATT 2102818 BCA A 38
36
19.WAP to show the use of transient keyword.
* import java.io.*;
class MyClass implements Serializable {
private int normalVariable;
transient private String transientVariable;
public MyClass(int normalVariable, String
transientVariable) {
this.normalVariable = normalVariable;
this.transientVariable = transientVariable;
}
public void printValues() {
System.out.println("Normal Variable: " +
normalVariable);
System.out.println("Transient Variable: " +
transientVariable);
}
}
public class termwork19 {
public static void main(String[] args) {
MyClass obj = new MyClass(42, "This is a transient
variable.");
try (ObjectOutputStream out = new
ObjectOutputStream(new
FileOutputStream("serialized_object.ser"))) {
out.writeObject(obj);
System.out.println("Object has been serialized.");
} catch (IOException e) {
JYOTIRADIRYA BHATT 2102818 BCA A 38
37
e.printStackTrace();
}
try (ObjectInputStream in = new ObjectInputStream(new
FileInputStream("serialized_object.ser"))) {
MyClass deserializedObj = (MyClass)
in.readObject();
System.out.println("Object has been
deserialized.");
deserializedObj.printValues();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}*
OUTPUT
JYOTIRADIRYA BHATT 2102818 BCA A 38
38
22.WAP for following OUTPUT String s="san12may4tya7yyy678rtb62tp"
Output 2 will 4*7=28
Output 1 will sanmytrbp
Output 3 will 12+678+62=690+62 =....
*
public class termwork22 {
public static void main(String[] args) {
String p = "san12may4tya7yyy678rtb62tp";
int count = 0, mul = 1, sum = 0;
String f = "";
for (int i = 0; i < p.length(); i++) {
char c = p.charAt(i);
if (Character.isDigit(c)) {
char d = p.charAt(i + 1);
char e = p.charAt(i - 1);
if (!Character.isDigit(d) && !Character.isDigit(e)) {
mul = mul * Character.getNumericValue(c);
} else if (Character.isDigit(d)) {
sum = sum + Character.getNumericValue(c) + Character.getNumericValue(d);
char g = p.charAt(i + 2);
if (Character.isDigit(g)) {
sum = sum + Character.getNumericValue(g);
}
}
} else if (Character.isLetter(c)) {
if (!f.contains("" + p.charAt(i))) {
JYOTIRADIRYA BHATT 2102818 BCA A 38
39
f = f + p.charAt(i);
}
}
}
System.out.println("Output 1 for:4*7" + mul);
System.out.println("Output 2 for:" + f);
System.out.println("Output 3" + sum);
}
} *
OUTPUT
23. Enter a no and than create array of its digits.ie.If no is 123456 thanStore it's digit in int array
of size 6 as If array is a than a[0] should 1 and a[1] should 2......Also after storing in array a then
reverse it's all value without using another array i.e After reversing a[0] should 6 and a[1] should
5 ......print common elemnts of both array.
*
import java.util.Scanner;
public class termwork23 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number:");
String s = sc.next();
int c = s.length();
int[] a = new int[c];
// Convert the input string to an array of integers
JYOTIRADIRYA BHATT 2102818 BCA A 38
40
for (int i = 0; i < c; i++) {
a[i] = Character.getNumericValue(s.charAt(i));
}
// Print the original array
System.out.print("Original Array: ");
for (int i = 0; i < c; i++) {
System.out.print(a[i] + " ");
}
// Reverse the array
int temp;
for (int i = 0; i < c / 2; i++) {
temp = a[i];
a[i] = a[c - 1 - i];
a[c - 1 - i] = temp;
}
// Print the reversed array
System.out.print("nReversed Array: ");
for (int i = 0; i < c; i++) {
System.out.print(a[i] + " ");
}
System.out.print("nCommon Elements: ");
for (int i = 0; i < c; i++) {
if (a[i] == a[c - 1 - i]) {
System.out.print(a[i] + " ");
}
}
}
JYOTIRADIRYA BHATT 2102818 BCA A 38
41
} *
OUTPUT
24.Wap to initlize two integer array of user enterd size .Then perform following operations1.add
elements in that array which is longest .I.e. if first array is of 3 size having elements 1,2,3Second
array is of size five and elements are 4,5,6,7,8 Than result array will second and now it's
elements will 5,7,9,7,8
*
import java.util.*;
public class termwork24 {
public static void main(String ss[])
{
int n=0,m=0;
Scanner sc=new Scanner(System.in);
System.out.println("enter the size of array 1");
n=sc.nextInt();
System.out.println("enter the value of array 2");
m=sc.nextInt();
int a[]=new int[n];
int b[]=new int[m];
System.out.println("enter the elements of array 1");
for(int i=0;i<n;i++)
{
JYOTIRADIRYA BHATT 2102818 BCA A 38
42
a[i]=sc.nextInt();
}
System.out.println("enter the elements of array 2");
for(int i=0;i<m;i++)
{
b[i]=sc.nextInt();
}
if(a.length>=b.length)
{
for(int i=0;i<b.length;i++)
a[i]=a[i]+b[i];
for(int j=0;j<a.length;j++)
System.out.print(a[j]+" ");}
else if(a.length<b.length)
{for(int i=0;i<a.length;i++)
b[i]=b[i]+a[i];
for(int j=0;j<b.length;j++)
System.out.print(b[j]+" ");}
}
} *
OUTPUT
JYOTIRADIRYA BHATT 2102818 BCA A 38
43
25. Initlize integer array of any size containg values 0 and 1 only. Now check which is longest
series of either 0 or 1 ,which is existing.I.e. if array values are{1,1,1,0,0,1,1,0,0,0,0,0,1,1,1}Ans is
0 and length is 5 If there is same series of 0 and 1 then ans will which is existing firstI.e. if array
values are {1,1,1,0,0,1,1,0,0,0,1,1,1} Ans is 1 and length is 3 As 1,1,1 comming first before 0,0 0
*
import java.util.*;
public class termwork25 {
public static void main(String[]sd)
{
int n=0;
Scanner sc=new Scanner(System.in);
System.out.println("enter the size of array");
n=sc.nextInt();
int[] a=new int[n];
System.out.println("enter the elements of array a");
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
int[] b =new int[n];
for (int i=0;i<n;i++)
{
b[i]=1;
}
for(int i=0;i<a.length-1;i++)
{
for (int j=i+1;j<a.length;j++) {
if (a[j] != -1||a[i]!=-1) {
JYOTIRADIRYA BHATT 2102818 BCA A 38
44
if (a[i] == a[j]) {
b[j]=-1;
++b[i];
}
else
break;
}
}
}
int max=0,m=0;
for(int i=0;i<b.length;i++)
if(b[i]!=-1)
{
if(b[i]>max)
{
max=b[i];
m=i;
}
}
System.out.println("ans is "+ a[m] +" which repeats :: "+max+" times");
}
} *
OUTPUT
JYOTIRADIRYA BHATT 2102818 BCA A 38
45

More Related Content

Similar to JAVA.pdf

Utility.ppt
Utility.pptUtility.ppt
Utility.ppt
BruceLee275640
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
Upender Upr
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptx
KimVeeL
 
java program assigment -2
java program assigment -2java program assigment -2
java program assigment -2
Ankit Gupta
 
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
Nithin Kumar,VVCE, Mysuru
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
MaruMengesha
 
Nested For Loops and Class Constants in Java
Nested For Loops and Class Constants in JavaNested For Loops and Class Constants in Java
Nested For Loops and Class Constants in Java
Pokequesthero
 
Quest 1 define a class batsman with the following specifications
Quest  1 define a class batsman with the following specificationsQuest  1 define a class batsman with the following specifications
Quest 1 define a class batsman with the following specifications
rajkumari873
 
java-programming.pdf
java-programming.pdfjava-programming.pdf
java-programming.pdf
Prof. Dr. K. Adisesha
 
java experiments and programs
java experiments and programsjava experiments and programs
java experiments and programs
Karuppaiyaa123
 
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian KomputerKOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
Aiman Hud
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_22-Feb-2021_L9-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_22-Feb-2021_L9-...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_22-Feb-2021_L9-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_22-Feb-2021_L9-...
MaruMengesha
 
JAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdfJAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdf
RohitkumarYadav80
 
Wap to implement bitwise operators
Wap to implement bitwise operatorsWap to implement bitwise operators
Wap to implement bitwise operators
Harleen Sodhi
 
Ann
AnnAnn
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
AkhilaaReddy
 
information Security.docx
information Security.docxinformation Security.docx
information Security.docx
SouravKarak1
 
.net progrmming part2
.net progrmming part2.net progrmming part2
.net progrmming part2
Dr.M.Karthika parthasarathy
 
Java programs
Java programsJava programs
Java programs
jojeph
 
Java Question---------------------------FacebookApp(Main).jav.pdf
Java Question---------------------------FacebookApp(Main).jav.pdfJava Question---------------------------FacebookApp(Main).jav.pdf
Java Question---------------------------FacebookApp(Main).jav.pdf
ambersushil
 

Similar to JAVA.pdf (20)

Utility.ppt
Utility.pptUtility.ppt
Utility.ppt
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptx
 
java program assigment -2
java program assigment -2java program assigment -2
java program assigment -2
 
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
 
Nested For Loops and Class Constants in Java
Nested For Loops and Class Constants in JavaNested For Loops and Class Constants in Java
Nested For Loops and Class Constants in Java
 
Quest 1 define a class batsman with the following specifications
Quest  1 define a class batsman with the following specificationsQuest  1 define a class batsman with the following specifications
Quest 1 define a class batsman with the following specifications
 
java-programming.pdf
java-programming.pdfjava-programming.pdf
java-programming.pdf
 
java experiments and programs
java experiments and programsjava experiments and programs
java experiments and programs
 
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian KomputerKOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_22-Feb-2021_L9-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_22-Feb-2021_L9-...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_22-Feb-2021_L9-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_22-Feb-2021_L9-...
 
JAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdfJAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdf
 
Wap to implement bitwise operators
Wap to implement bitwise operatorsWap to implement bitwise operators
Wap to implement bitwise operators
 
Ann
AnnAnn
Ann
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
 
information Security.docx
information Security.docxinformation Security.docx
information Security.docx
 
.net progrmming part2
.net progrmming part2.net progrmming part2
.net progrmming part2
 
Java programs
Java programsJava programs
Java programs
 
Java Question---------------------------FacebookApp(Main).jav.pdf
Java Question---------------------------FacebookApp(Main).jav.pdfJava Question---------------------------FacebookApp(Main).jav.pdf
Java Question---------------------------FacebookApp(Main).jav.pdf
 

Recently uploaded

QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
AlexanderRichford
 
ScyllaDB Tablets: Rethinking Replication
ScyllaDB Tablets: Rethinking ReplicationScyllaDB Tablets: Rethinking Replication
ScyllaDB Tablets: Rethinking Replication
ScyllaDB
 
Session 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdfSession 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdf
UiPathCommunity
 
New ThousandEyes Product Features and Release Highlights: June 2024
New ThousandEyes Product Features and Release Highlights: June 2024New ThousandEyes Product Features and Release Highlights: June 2024
New ThousandEyes Product Features and Release Highlights: June 2024
ThousandEyes
 
An All-Around Benchmark of the DBaaS Market
An All-Around Benchmark of the DBaaS MarketAn All-Around Benchmark of the DBaaS Market
An All-Around Benchmark of the DBaaS Market
ScyllaDB
 
Discover the Unseen: Tailored Recommendation of Unwatched Content
Discover the Unseen: Tailored Recommendation of Unwatched ContentDiscover the Unseen: Tailored Recommendation of Unwatched Content
Discover the Unseen: Tailored Recommendation of Unwatched Content
ScyllaDB
 
An Introduction to All Data Enterprise Integration
An Introduction to All Data Enterprise IntegrationAn Introduction to All Data Enterprise Integration
An Introduction to All Data Enterprise Integration
Safe Software
 
ScyllaDB Leaps Forward with Dor Laor, CEO of ScyllaDB
ScyllaDB Leaps Forward with Dor Laor, CEO of ScyllaDBScyllaDB Leaps Forward with Dor Laor, CEO of ScyllaDB
ScyllaDB Leaps Forward with Dor Laor, CEO of ScyllaDB
ScyllaDB
 
Call Girls Kochi 💯Call Us 🔝 7426014248 🔝 Independent Kochi Escorts Service Av...
Call Girls Kochi 💯Call Us 🔝 7426014248 🔝 Independent Kochi Escorts Service Av...Call Girls Kochi 💯Call Us 🔝 7426014248 🔝 Independent Kochi Escorts Service Av...
Call Girls Kochi 💯Call Us 🔝 7426014248 🔝 Independent Kochi Escorts Service Av...
dipikamodels1
 
Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...
Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...
Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...
manji sharman06
 
TrustArc Webinar - Your Guide for Smooth Cross-Border Data Transfers and Glob...
TrustArc Webinar - Your Guide for Smooth Cross-Border Data Transfers and Glob...TrustArc Webinar - Your Guide for Smooth Cross-Border Data Transfers and Glob...
TrustArc Webinar - Your Guide for Smooth Cross-Border Data Transfers and Glob...
TrustArc
 
Facilitation Skills - When to Use and Why.pptx
Facilitation Skills - When to Use and Why.pptxFacilitation Skills - When to Use and Why.pptx
Facilitation Skills - When to Use and Why.pptx
Knoldus Inc.
 
Introduction to ThousandEyes AMER Webinar
Introduction  to ThousandEyes AMER WebinarIntroduction  to ThousandEyes AMER Webinar
Introduction to ThousandEyes AMER Webinar
ThousandEyes
 
Automation Student Developers Session 3: Introduction to UI Automation
Automation Student Developers Session 3: Introduction to UI AutomationAutomation Student Developers Session 3: Introduction to UI Automation
Automation Student Developers Session 3: Introduction to UI Automation
UiPathCommunity
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
Pablo Gómez Abajo
 
ThousandEyes New Product Features and Release Highlights: June 2024
ThousandEyes New Product Features and Release Highlights: June 2024ThousandEyes New Product Features and Release Highlights: June 2024
ThousandEyes New Product Features and Release Highlights: June 2024
ThousandEyes
 
CNSCon 2024 Lightning Talk: Don’t Make Me Impersonate My Identity
CNSCon 2024 Lightning Talk: Don’t Make Me Impersonate My IdentityCNSCon 2024 Lightning Talk: Don’t Make Me Impersonate My Identity
CNSCon 2024 Lightning Talk: Don’t Make Me Impersonate My Identity
Cynthia Thomas
 
CTO Insights: Steering a High-Stakes Database Migration
CTO Insights: Steering a High-Stakes Database MigrationCTO Insights: Steering a High-Stakes Database Migration
CTO Insights: Steering a High-Stakes Database Migration
ScyllaDB
 
Multivendor cloud production with VSF TR-11 - there and back again
Multivendor cloud production with VSF TR-11 - there and back againMultivendor cloud production with VSF TR-11 - there and back again
Multivendor cloud production with VSF TR-11 - there and back again
Kieran Kunhya
 
Call Girls Chennai ☎️ +91-7426014248 😍 Chennai Call Girl Beauty Girls Chennai...
Call Girls Chennai ☎️ +91-7426014248 😍 Chennai Call Girl Beauty Girls Chennai...Call Girls Chennai ☎️ +91-7426014248 😍 Chennai Call Girl Beauty Girls Chennai...
Call Girls Chennai ☎️ +91-7426014248 😍 Chennai Call Girl Beauty Girls Chennai...
anilsa9823
 

Recently uploaded (20)

QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
 
ScyllaDB Tablets: Rethinking Replication
ScyllaDB Tablets: Rethinking ReplicationScyllaDB Tablets: Rethinking Replication
ScyllaDB Tablets: Rethinking Replication
 
Session 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdfSession 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdf
 
New ThousandEyes Product Features and Release Highlights: June 2024
New ThousandEyes Product Features and Release Highlights: June 2024New ThousandEyes Product Features and Release Highlights: June 2024
New ThousandEyes Product Features and Release Highlights: June 2024
 
An All-Around Benchmark of the DBaaS Market
An All-Around Benchmark of the DBaaS MarketAn All-Around Benchmark of the DBaaS Market
An All-Around Benchmark of the DBaaS Market
 
Discover the Unseen: Tailored Recommendation of Unwatched Content
Discover the Unseen: Tailored Recommendation of Unwatched ContentDiscover the Unseen: Tailored Recommendation of Unwatched Content
Discover the Unseen: Tailored Recommendation of Unwatched Content
 
An Introduction to All Data Enterprise Integration
An Introduction to All Data Enterprise IntegrationAn Introduction to All Data Enterprise Integration
An Introduction to All Data Enterprise Integration
 
ScyllaDB Leaps Forward with Dor Laor, CEO of ScyllaDB
ScyllaDB Leaps Forward with Dor Laor, CEO of ScyllaDBScyllaDB Leaps Forward with Dor Laor, CEO of ScyllaDB
ScyllaDB Leaps Forward with Dor Laor, CEO of ScyllaDB
 
Call Girls Kochi 💯Call Us 🔝 7426014248 🔝 Independent Kochi Escorts Service Av...
Call Girls Kochi 💯Call Us 🔝 7426014248 🔝 Independent Kochi Escorts Service Av...Call Girls Kochi 💯Call Us 🔝 7426014248 🔝 Independent Kochi Escorts Service Av...
Call Girls Kochi 💯Call Us 🔝 7426014248 🔝 Independent Kochi Escorts Service Av...
 
Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...
Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...
Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...
 
TrustArc Webinar - Your Guide for Smooth Cross-Border Data Transfers and Glob...
TrustArc Webinar - Your Guide for Smooth Cross-Border Data Transfers and Glob...TrustArc Webinar - Your Guide for Smooth Cross-Border Data Transfers and Glob...
TrustArc Webinar - Your Guide for Smooth Cross-Border Data Transfers and Glob...
 
Facilitation Skills - When to Use and Why.pptx
Facilitation Skills - When to Use and Why.pptxFacilitation Skills - When to Use and Why.pptx
Facilitation Skills - When to Use and Why.pptx
 
Introduction to ThousandEyes AMER Webinar
Introduction  to ThousandEyes AMER WebinarIntroduction  to ThousandEyes AMER Webinar
Introduction to ThousandEyes AMER Webinar
 
Automation Student Developers Session 3: Introduction to UI Automation
Automation Student Developers Session 3: Introduction to UI AutomationAutomation Student Developers Session 3: Introduction to UI Automation
Automation Student Developers Session 3: Introduction to UI Automation
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
 
ThousandEyes New Product Features and Release Highlights: June 2024
ThousandEyes New Product Features and Release Highlights: June 2024ThousandEyes New Product Features and Release Highlights: June 2024
ThousandEyes New Product Features and Release Highlights: June 2024
 
CNSCon 2024 Lightning Talk: Don’t Make Me Impersonate My Identity
CNSCon 2024 Lightning Talk: Don’t Make Me Impersonate My IdentityCNSCon 2024 Lightning Talk: Don’t Make Me Impersonate My Identity
CNSCon 2024 Lightning Talk: Don’t Make Me Impersonate My Identity
 
CTO Insights: Steering a High-Stakes Database Migration
CTO Insights: Steering a High-Stakes Database MigrationCTO Insights: Steering a High-Stakes Database Migration
CTO Insights: Steering a High-Stakes Database Migration
 
Multivendor cloud production with VSF TR-11 - there and back again
Multivendor cloud production with VSF TR-11 - there and back againMultivendor cloud production with VSF TR-11 - there and back again
Multivendor cloud production with VSF TR-11 - there and back again
 
Call Girls Chennai ☎️ +91-7426014248 😍 Chennai Call Girl Beauty Girls Chennai...
Call Girls Chennai ☎️ +91-7426014248 😍 Chennai Call Girl Beauty Girls Chennai...Call Girls Chennai ☎️ +91-7426014248 😍 Chennai Call Girl Beauty Girls Chennai...
Call Girls Chennai ☎️ +91-7426014248 😍 Chennai Call Girl Beauty Girls Chennai...
 

JAVA.pdf

  • 1. JYOTIRADIRYA BHATT 2102818 BCA A 38 1 1. WAP in java to simulate condition to generate Wi-Fi password. Take input as Name, City, Age and Gender. * import java.util.Scanner; public class termwork1 { public static void main(String[] args) { String name,city,gender; int age; Scanner sc=new Scanner(System.in); System.out.println("Enter the name:"); name=sc.next(); if(name.length()<=3) { System.out.println("Name should be greater than 3 words:"); System.out.println("Enter the name:"); name=sc.next(); } System.out.println("Enter the city:"); city=sc.next(); if(city.length()<=3) { System.out.println("City name should be grater than 3 words:"); System.out.println("Enter the city:"); city=sc.next(); } System.out.println("Enter the gender(M/F):"); gender=sc.next(); System.out.println("Enter the age:"); age=sc.nextInt(); if(age==18) { System.out.println("Enter the age again:"); age=sc.nextInt(); } PasswordGenerator pa=new PasswordGenerator(name,city,gender,age); } } class PasswordGenerator { String password="";
  • 2. JYOTIRADIRYA BHATT 2102818 BCA A 38 2 int age,sum=0,counter=0,diff=0; PasswordGenerator(){ } PasswordGenerator(String n,String c,String g,int a) { if (g.equals("F") && a < 18) { for (int i = 0; i < 3; i++) { password = password + String.valueOf(n.charAt(i)); } String d = Integer.toString(a); for (int i = 0; i < d.length(); i++) { char ca = d.charAt(i); sum = sum + Character.getNumericValue(ca); } password = password + Integer.toString(sum); int i=c.length()-3; while (counter != 3) { password = password + String.valueOf(c.charAt(i)); i++; counter++; } System.out.println(password); } else if(g.equals("F")&& a>18) { int i=n.length()-3; while (counter != 3) { password = password + String.valueOf(n.charAt(i)); i++; counter++; } String d=Integer.toString(a); for(int j=0;j<d.length();j++) { char ca = d.charAt(j); if(ca>diff){ diff = Character.getNumericValue(ca)-diff;} else if(ca<diff){ diff = diff- Character.getNumericValue(ca); } else {
  • 3. JYOTIRADIRYA BHATT 2102818 BCA A 38 3 diff=1; } } password = password + Integer.toString(diff); for (int j = 0; j < 3; j++) { password = password + String.valueOf(c.charAt(j)); } System.out.println(password); } else { int i=0,j=0; while( i<n.length()&& j<c.length()) { password = password + String.valueOf(n.charAt(i)); password = password + String.valueOf(c.charAt(j)); i++; j++; } password=password+a; System.out.println(password); } } } * OUTPUT
  • 4. JYOTIRADIRYA BHATT 2102818 BCA A 38 4 2. WAP in Java to initialize a string in order to find that character which frequency is 2nd most in that string. * import java.util.Scanner; public class termwork2 { public static void main(String[] args){ String s=""; Scanner sc=new Scanner(System.in); System.out.println("enter the string"); s=sc.next(); int l=s.length(); int ar[]=new int[l]; for(int i=0;i< ar.length;i++) ar[i]=1; for(int i=0;i<l;i++) { for(int j=i+1;j<l;j++) { if(ar[j]!=-1) { if (s.charAt(i) == (s.charAt(j))) { ar[j]=-1; ++ar[i]; } } }
  • 5. JYOTIRADIRYA BHATT 2102818 BCA A 38 5 } int max=0,smax=0,n=0; for(int i=0;i<ar.length;i++) for(int j=i+1;j<ar.length-1;j++) { if(ar[j]!=-1) { if(ar[i]>=ar[j]) { max = ar[i]; if(ar[j]<=max&&ar[j]>=smax) { n=j; smax=ar[j]; } } } } System.out.println("the letter with second highest frequency is "+s.charAt(n)+" and no.of times its came is "+ar[n]); } } * OUTPUT
  • 6. JYOTIRADIRYA BHATT 2102818 BCA A 38 6 3.WAP to check longest sub sequence of a same character in an initialized string?[aaaabppppp, p=5] [aabbcc, a=2]. * import java.util.Arrays; import java.util.Scanner; public class termwork3 { public static void main(String[] args) { String s=""; Scanner sc=new Scanner(System.in); System.out.println("enter the string"); s=sc.next(); int[] ar =new int[s.length()]; Arrays.fill(ar, 1); for(int i=0;i<ar.length;i++) for(int j=i+1;j<ar.length;j++) { if (ar[i] != -1||ar[j]!=-1) { if (s.charAt(i) == s.charAt(j)) { ar[j]=-1; ++ar[i];
  • 7. JYOTIRADIRYA BHATT 2102818 BCA A 38 7 } else break; } } int max=0,n=0; for(int i=0;i<ar.length;i++) if(ar[i]!=-1) { if(ar[i]>max) { max=ar[i]; n=i; } } System.out.println("the letter with most occurrence is :: "+s.charAt(n)+" which repeats :: "+max+" times"); } }* OUTPUT
  • 8. JYOTIRADIRYA BHATT 2102818 BCA A 38 8 4.WAP to generate wifi key as user will enter form value Name,City,Age and Gender? * import java.util.Scanner; public class termwork4 { public static void main(String []args) { Scanner sc=new Scanner(System.in); String name=""; String city=""; int age; String gender=""; System.out.println("enter the gender ::"); gender=sc.next(); System.out.println("enter the name ::"); name=sc.next(); System.out.println("enter the city name ::"); city=sc.next(); System.out.println("enter the age ::"); age=sc.nextInt(); System.out.println("the key generated is :: "+passwordGenerator(name,city,gender,age)); } static String passwordGenerator(String n,String c,String g,int a) { String key=""; if(g.equals("M")||g.equals("m")) { for(int i=0;i<3;i++)
  • 9. JYOTIRADIRYA BHATT 2102818 BCA A 38 9 { key=key+n.charAt(i); } String m=Integer.toString(a); int i=0; int k=(Character.getNumericValue(m.charAt(i))- Character.getNumericValue(m.charAt(i+1))); if(k>0) key=key+k; else if(k<0) key=key+(-k); else key=key+0; for(int j=2;j>=0;j--) { key=key+c.charAt((c.length()-1)-j); } } else if(g.equals("F")||g.equals("f")) { for (int i=2;i>=0;i--) { key=key+n.charAt((n.length()-1)-i); } String m=Integer.toString(a); int i=0; int k=Character.getNumericValue(m.charAt(i))+Character.getNumericValue(m.charAt(i+1)); key=key+k;
  • 10. JYOTIRADIRYA BHATT 2102818 BCA A 38 10 for(int j=0;j<3;j++){ key=key+c.charAt(j); } } return key; } } * OUTPUT 5.WAP to delete only those text file which are non empty in these folder.[E://MCA/BCA/DCA] * import java.util.*; import java.io.*; public class termwork5 { public static void main(String[] args)throws Exception { File f = new File("E:/MCA/BCA/DCA"); File[] files=f.listFiles(); for(File f1:files) { FileReader fr = new FileReader(f1); int m;
  • 11. JYOTIRADIRYA BHATT 2102818 BCA A 38 11 if ((m = fr.read())!= -1) { fr.close(); boolean s = f1.delete(); if (s) System.out.println("file deleted"); else System.out.println("file not deleted"); } else System.out.println("file is empty"); } } } * OUTPUT 6.WAP to create a method check which returns two values.If first string having a character twice as well second string also then return both the string by removing that character? * import java.util.Scanner; public class termwork6 { public static void main(String[] ar) { Scanner sc=new Scanner(System.in); String s1="";
  • 12. JYOTIRADIRYA BHATT 2102818 BCA A 38 12 String s2=""; System.out.println("enter the value of first string "); s1=sc.next(); System.out.println("enter the value of second string "); s2=sc.next(); System.out.println("enter the character that is needed to be checked"); String m=sc.next(); String s3[]=check(s1,s2,m); System.out.println(s3[0]+" "+s3[1]); } static String [] check(String s1,String s2,String m) { String s []=new String[2]; int q=0,n=0; for(int i=0;i<s1.length();i++) { if(m.charAt(0)==(s1.charAt(i))) { ++q; if(q==2) { s[0]=""; for(int j=0;j<s1.length();j++) { if(m.charAt(0)!=(s1.charAt(j))) { s[0]=s[0]+s1.charAt(j); }
  • 13. JYOTIRADIRYA BHATT 2102818 BCA A 38 13 } } } else if(q!=2) s[0]=s1; } for(int i=0;i<s2.length();i++) { if(m.charAt(0)==(s2.charAt(i))) { ++n; if(n==2) {s[1]=""; for(int j=0;j<s2.length();j++) { if(m.charAt(0)!=(s2.charAt(j))) { s[1]=s[1]+s2.charAt(j); } } } } else if(n!=2) s[1]=s2; } return s; }
  • 14. JYOTIRADIRYA BHATT 2102818 BCA A 38 14 } * OUTPUT 7.WAP to print number of days gape in your age as enter DD-MM-YYYY with current system date? * import java.text.*; import java.util.*; public class termwork7 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Get user input for birthdate System.out.print("Enter your birthdate (DD-MM-YYYY): "); String birthdateInput = scanner.next(); try { // Calculate and print the age difference in days int ageDifference = calculateAgeDifference(birthdateInput); System.out.println("Number of days gap in your age: " + ageDifference + " days"); } catch (ParseException e) {
  • 15. JYOTIRADIRYA BHATT 2102818 BCA A 38 15 System.out.println("Invalid date format. Please enter the date in DD-MM-YYYY format."); } } private static int calculateAgeDifference(String birthdate) throws ParseException { // Get the current system date Date currentDate = new Date(); // Define the date format for parsing DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); // Convert the input birthdate string to a Date object Date birthdateObj = dateFormat.parse(birthdate); // Calculate the age difference in days long ageDifferenceInMillis = currentDate.getTime() - birthdateObj.getTime(); int ageDifferenceInDays = (int) (ageDifferenceInMillis / (24 * 60 * 60 * 1000)); return ageDifferenceInDays; } } * OUTPUT
  • 16. JYOTIRADIRYA BHATT 2102818 BCA A 38 16 9.WAP in an Applet Make two Button one is Circle within Square and second is Square within Circle. When user click on Circle within Square then draw Circle within Square. And when user click on Square within Circle then draw Square within Circle on the panel window? * import java.awt.*; import java.awt.event.*; import java.applet.*; public class termwork9 extends Applet implements ActionListener { int i=0; Button b1; Button b2; public void init() { b1=new Button("squareWithinCircle"); b1.addActionListener(this); add(b1); b2=new Button("cirleWithinSquare"); b2.addActionListener(this); add(b2); } public void paint(Graphics g) { if (i==1){ g.drawRect(129,129,141,141); g.drawOval(100,100,200,200); } else if(i==2)
  • 17. JYOTIRADIRYA BHATT 2102818 BCA A 38 17 { g.drawOval(100,100,200,200); g.drawRect(100,100,200,200); } } public void actionPerformed(ActionEvent e) { if(e.getSource()==b1) i=1; else if(e.getSource()==b2) i=2; repaint(); } } //<applet code="termwork9.java" width=1000 height=1000></applet> * OUTPUT
  • 18. JYOTIRADIRYA BHATT 2102818 BCA A 38 18 10.WAP to put one Exception class in a package and to use this Exception class object in an another package class method any how? * package Package1; public class CustomException extends Exception
  • 19. JYOTIRADIRYA BHATT 2102818 BCA A 38 19 { public CustomException(String message) { super(message); } * * package Package2; import Package1.CustomException; public class ExampleClass { public void someMethod() throws CustomException { throw new CustomException("This is a custom exception."); } * * import Package2.ExampleClass; import Package1.CustomException; public class termwork10{ public static void main(String[] args) { ExampleClass example = new ExampleClass(); try { example.someMethod(); } catch (CustomException e) { System.out.println("Caught custom exception: " + e.getMessage()); } * OUTPUT
  • 20. JYOTIRADIRYA BHATT 2102818 BCA A 38 20 11.WAP to initialize 2D string array at runtime and to print reverse value of diagonal position only? * import java.util.Scanner; public class termwork11 { public static void main(String[] args) { String [][]a=new String[2][2]; Scanner sc=new Scanner(System.in); for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { a[i][j]=sc.next(); } } reverseValue(a); } static void reverseValue(String[][]x) { for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { if(i==j)
  • 21. JYOTIRADIRYA BHATT 2102818 BCA A 38 21 { String s=x[i][j].toString(); StringBuffer st=new StringBuffer(s); st.reverse(); System.out.println("The diagonal reverse value of "+x[i][j]+" is:"+st); } } } } } * OUTPUT 12.WAP to show the Working of multithreading . Make separate program by using Runnable interface And by using Thread Class .also use their methods . * class MyRunnable implements Runnable { public void run() { for (int i = 1; i <= 5; i++) { System.out.println("Thread using Runnable: " + i); try {
  • 22. JYOTIRADIRYA BHATT 2102818 BCA A 38 22 Thread.sleep(500); // Simulate some work being done } catch (InterruptedException e) { e.printStackTrace(); } } } } public class termwork12{ public static void main(String[] args) { MyRunnable myRunnable = new MyRunnable(); // Creating two threads using the same instance of MyRunnable Thread thread1 = new Thread(myRunnable); Thread thread2 = new Thread(myRunnable); // Starting the thread thread1.start(); thread2.start(); } } * OUTPUT
  • 23. JYOTIRADIRYA BHATT 2102818 BCA A 38 23 USING Thread CLASS * class MyThread extends Thread { public void run() { for (int i = 1; i <= 5; i++) { System.out.println("Thread using Thread class: " + i); try { sleep(500); // Simulate some work being done } catch (InterruptedException e) { e.printStackTrace(); } } } } public class termwork12 {
  • 24. JYOTIRADIRYA BHATT 2102818 BCA A 38 24 public static void main(String[] args) { MyThread thread1 = new MyThread(); MyThread thread2 = new MyThread(); // Starting the threads thread1.start(); thread2.start(); } } * OUTPUT 13.WAP using Applet to draw circle, line ,rectangle and fill them with a color given by the user. * import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class termwork13 extends Applet { TextField colorTextField;
  • 25. JYOTIRADIRYA BHATT 2102818 BCA A 38 25 public void init() { colorTextField = new TextField("#FF0000", 20); Button fillButton = new Button("Fill Rectangle"); fillButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { repaint(); } }); add(new Label("Enter color code (e.g., #FF0000):")); add(colorTextField); add(fillButton); } public void paint(Graphics g) { g.drawLine(2, 3, 100, 100); g.drawRect(200, 300, 400, 300); g.drawOval(600,200,400,500); String inputColor = colorTextField.getText(); try { Color fillColor = Color.decode(inputColor); g.setColor(fillColor); g.fillRect(200, 300, 400, 300); g.fillOval(600,200,400,500); } catch (NumberFormatException ex) { System.out.println("Invalid number format: " + inputColor); System.out.println("Please enter a valid color code in the format #RRGGBB"); } catch (IllegalArgumentException ex) { System.out.println("Invalid color code: " + inputColor);
  • 26. JYOTIRADIRYA BHATT 2102818 BCA A 38 26 System.out.println("Please enter a valid color code in the format #RRGGBB"); } } } //<applet code="termwork13.java" width=1000 height=1000></applet> * OUTPUT 15.WAP to show the use of KeyListener/MouseListener interface in order to generate and process those events. *import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Pt15 extends JFrame implements KeyListener, MouseListener { public Pt15() { setTitle("Event Demo"); setSize(300, 200);
  • 27. JYOTIRADIRYA BHATT 2102818 BCA A 38 27 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); addKeyListener(this); addMouseListener(this); setFocusable(true); } public void keyPressed(KeyEvent e) { System.out.println("Key Pressed: " + e.getKeyChar()); } public void keyReleased(KeyEvent e) { System.out.println("Key Released: " + e.getKeyChar()); } public void keyTyped(KeyEvent e) { // Not used in this example } public void mouseClicked(MouseEvent e) { System.out.println("Mouse Clicked at (" + e.getX() + ", " + e.getY() + ")"); } public void mousePressed(MouseEvent e) { // Not used in this example } public void mouseReleased(MouseEvent e) { // Not used in this example } public void mouseEntered(MouseEvent e) { // Not used in this example }
  • 28. JYOTIRADIRYA BHATT 2102818 BCA A 38 28 public void mouseExited(MouseEvent e) { // Not used in this example } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new Pt15().setVisible(true); } }); } } * OUTPUT 16.WAP to create TCP/IP Socket on both client and server side . and after socket creation perform the Operation as done in chat server . * Client: import java.io.*; import java.net.*;
  • 29. JYOTIRADIRYA BHATT 2102818 BCA A 38 29 public class ChatClient { public static void main(String[] args) { try { // Connect to the server on the specified port Socket socket = new Socket("localhost", 12345); // Set up input and output streams BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); // Create a BufferedReader to read messages from the console BufferedReader consoleInput = new BufferedReader(new InputStreamReader(System.in)); // Start a thread to continuously read messages from the server new Thread(() -> { String serverMessage; try { while ((serverMessage = in.readLine()) != null) { System.out.println("Server: " + serverMessage); } } catch (IOException e) { e.printStackTrace(); } }).start(); // Start a loop to read and send messages String clientMessage; while (true) {
  • 30. JYOTIRADIRYA BHATT 2102818 BCA A 38 30 // Read a message from the console and send it to the server System.out.print("Enter your message: "); clientMessage = consoleInput.readLine(); out.println(clientMessage); } } catch (IOException e) { e.printStackTrace(); } } } Server: import java.io.*; import java.net.*; public class ChatServer { public static void main(String[] args) { try { // Create a server socket and bind it to a specific port ServerSocket serverSocket = new ServerSocket(12345); System.out.println("Server is waiting for client connection..."); // Wait for a client to connect Socket clientSocket = serverSocket.accept(); System.out.println("Client connected!"); // Set up input and output streams BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  • 31. JYOTIRADIRYA BHATT 2102818 BCA A 38 31 PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); // Create a BufferedReader to read messages from the console BufferedReader consoleInput = new BufferedReader(new InputStreamReader(System.in)); // Start a loop to read and send messages new Thread(() -> { String clientMessage; try { while ((clientMessage = in.readLine()) != null) { System.out.println("Client: " + clientMessage); } } catch (IOException e) { e.printStackTrace(); } }).start(); String serverMessage; while (true) { // Read a message from the console and send it to the client System.out.print("Enter your message: "); serverMessage = consoleInput.readLine(); out.println(serverMessage); } } catch (IOException e) { e.printStackTrace(); }
  • 32. JYOTIRADIRYA BHATT 2102818 BCA A 38 32 } } OUTPUT SERVER CLIENT
  • 33. JYOTIRADIRYA BHATT 2102818 BCA A 38 33 17.WAP to print all numeric digits sum of all database column values. * import com.sun.jdi.connect.Connector; import java.sql.*; public class termwork17 { public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/testDB","root","Tsdk123#"); PreparedStatement pt=con.prepareStatement("Select *from Student "); ResultSet rs=pt.executeQuery(); String s=""; while(rs.next()) { s=rs.getString(1); s=s+rs.getString(3); } System.out.println(s); int sum=0; for(int i=0;i<s.length();i++) { char c=s.charAt(i); sum=sum+Character.getNumericValue(c); }
  • 34. JYOTIRADIRYA BHATT 2102818 BCA A 38 34 System.out.println("Sum of column is :"+sum); } } * OUTPUT 18.WAP to update a table column value using stored procedures. * import java.sql.*; public class termwork18 { public static void main(String[] args) { try { // Load the JDBC driver Class.forName("com.mysql.cj.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/termwork", "root", "Tdsk123#"); CallableStatement callableStatement = connection.prepareCall("{call update_employees(?, ?)}");
  • 35. JYOTIRADIRYA BHATT 2102818 BCA A 38 35 callableStatement.setInt(1, 1); // Assuming 1 is the empid ofthe row you want to update callableStatement.setString(2, "yogesh"); // New employee name // Execute the stored procedure callableStatement.execute(); System.out.println("Update successful!"); // Close resources callableStatement.close(); connection.close(); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); }} } OUTPUT
  • 36. JYOTIRADIRYA BHATT 2102818 BCA A 38 36 19.WAP to show the use of transient keyword. * import java.io.*; class MyClass implements Serializable { private int normalVariable; transient private String transientVariable; public MyClass(int normalVariable, String transientVariable) { this.normalVariable = normalVariable; this.transientVariable = transientVariable; } public void printValues() { System.out.println("Normal Variable: " + normalVariable); System.out.println("Transient Variable: " + transientVariable); } } public class termwork19 { public static void main(String[] args) { MyClass obj = new MyClass(42, "This is a transient variable."); try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("serialized_object.ser"))) { out.writeObject(obj); System.out.println("Object has been serialized."); } catch (IOException e) {
  • 37. JYOTIRADIRYA BHATT 2102818 BCA A 38 37 e.printStackTrace(); } try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("serialized_object.ser"))) { MyClass deserializedObj = (MyClass) in.readObject(); System.out.println("Object has been deserialized."); deserializedObj.printValues(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } }* OUTPUT
  • 38. JYOTIRADIRYA BHATT 2102818 BCA A 38 38 22.WAP for following OUTPUT String s="san12may4tya7yyy678rtb62tp" Output 2 will 4*7=28 Output 1 will sanmytrbp Output 3 will 12+678+62=690+62 =.... * public class termwork22 { public static void main(String[] args) { String p = "san12may4tya7yyy678rtb62tp"; int count = 0, mul = 1, sum = 0; String f = ""; for (int i = 0; i < p.length(); i++) { char c = p.charAt(i); if (Character.isDigit(c)) { char d = p.charAt(i + 1); char e = p.charAt(i - 1); if (!Character.isDigit(d) && !Character.isDigit(e)) { mul = mul * Character.getNumericValue(c); } else if (Character.isDigit(d)) { sum = sum + Character.getNumericValue(c) + Character.getNumericValue(d); char g = p.charAt(i + 2); if (Character.isDigit(g)) { sum = sum + Character.getNumericValue(g); } } } else if (Character.isLetter(c)) { if (!f.contains("" + p.charAt(i))) {
  • 39. JYOTIRADIRYA BHATT 2102818 BCA A 38 39 f = f + p.charAt(i); } } } System.out.println("Output 1 for:4*7" + mul); System.out.println("Output 2 for:" + f); System.out.println("Output 3" + sum); } } * OUTPUT 23. Enter a no and than create array of its digits.ie.If no is 123456 thanStore it's digit in int array of size 6 as If array is a than a[0] should 1 and a[1] should 2......Also after storing in array a then reverse it's all value without using another array i.e After reversing a[0] should 6 and a[1] should 5 ......print common elemnts of both array. * import java.util.Scanner; public class termwork23 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number:"); String s = sc.next(); int c = s.length(); int[] a = new int[c]; // Convert the input string to an array of integers
  • 40. JYOTIRADIRYA BHATT 2102818 BCA A 38 40 for (int i = 0; i < c; i++) { a[i] = Character.getNumericValue(s.charAt(i)); } // Print the original array System.out.print("Original Array: "); for (int i = 0; i < c; i++) { System.out.print(a[i] + " "); } // Reverse the array int temp; for (int i = 0; i < c / 2; i++) { temp = a[i]; a[i] = a[c - 1 - i]; a[c - 1 - i] = temp; } // Print the reversed array System.out.print("nReversed Array: "); for (int i = 0; i < c; i++) { System.out.print(a[i] + " "); } System.out.print("nCommon Elements: "); for (int i = 0; i < c; i++) { if (a[i] == a[c - 1 - i]) { System.out.print(a[i] + " "); } } }
  • 41. JYOTIRADIRYA BHATT 2102818 BCA A 38 41 } * OUTPUT 24.Wap to initlize two integer array of user enterd size .Then perform following operations1.add elements in that array which is longest .I.e. if first array is of 3 size having elements 1,2,3Second array is of size five and elements are 4,5,6,7,8 Than result array will second and now it's elements will 5,7,9,7,8 * import java.util.*; public class termwork24 { public static void main(String ss[]) { int n=0,m=0; Scanner sc=new Scanner(System.in); System.out.println("enter the size of array 1"); n=sc.nextInt(); System.out.println("enter the value of array 2"); m=sc.nextInt(); int a[]=new int[n]; int b[]=new int[m]; System.out.println("enter the elements of array 1"); for(int i=0;i<n;i++) {
  • 42. JYOTIRADIRYA BHATT 2102818 BCA A 38 42 a[i]=sc.nextInt(); } System.out.println("enter the elements of array 2"); for(int i=0;i<m;i++) { b[i]=sc.nextInt(); } if(a.length>=b.length) { for(int i=0;i<b.length;i++) a[i]=a[i]+b[i]; for(int j=0;j<a.length;j++) System.out.print(a[j]+" ");} else if(a.length<b.length) {for(int i=0;i<a.length;i++) b[i]=b[i]+a[i]; for(int j=0;j<b.length;j++) System.out.print(b[j]+" ");} } } * OUTPUT
  • 43. JYOTIRADIRYA BHATT 2102818 BCA A 38 43 25. Initlize integer array of any size containg values 0 and 1 only. Now check which is longest series of either 0 or 1 ,which is existing.I.e. if array values are{1,1,1,0,0,1,1,0,0,0,0,0,1,1,1}Ans is 0 and length is 5 If there is same series of 0 and 1 then ans will which is existing firstI.e. if array values are {1,1,1,0,0,1,1,0,0,0,1,1,1} Ans is 1 and length is 3 As 1,1,1 comming first before 0,0 0 * import java.util.*; public class termwork25 { public static void main(String[]sd) { int n=0; Scanner sc=new Scanner(System.in); System.out.println("enter the size of array"); n=sc.nextInt(); int[] a=new int[n]; System.out.println("enter the elements of array a"); for(int i=0;i<n;i++) a[i]=sc.nextInt(); int[] b =new int[n]; for (int i=0;i<n;i++) { b[i]=1; } for(int i=0;i<a.length-1;i++) { for (int j=i+1;j<a.length;j++) { if (a[j] != -1||a[i]!=-1) {
  • 44. JYOTIRADIRYA BHATT 2102818 BCA A 38 44 if (a[i] == a[j]) { b[j]=-1; ++b[i]; } else break; } } } int max=0,m=0; for(int i=0;i<b.length;i++) if(b[i]!=-1) { if(b[i]>max) { max=b[i]; m=i; } } System.out.println("ans is "+ a[m] +" which repeats :: "+max+" times"); } } * OUTPUT
  翻译: