Gaurav Dosad B.
Sc (CS) 4th sem 12
Problem Statement: 1
WAP in Java to print the sum of following series: xn = x1 + x2 + x3 + x4 + …… + xn.
Objective: To find the sum of series
Source code:
import [Link].*;
import [Link].*;
class A {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the base value (x): ");
int x = [Link]();
[Link]("Enter the number of terms (n): ");
int n = [Link]();
double sum = 1.0;
double multi = x;
for (int i = 1; i <= n; i++) {
sum = sum + multi / i;
multi = multi * x;
}
[Link]("The sum of the series is: %.2f%n", sum);
}
}
1
Gaurav Dosad [Link] (CS) 4th sem 12
Output:
2
Gaurav Dosad [Link] (CS) 4th sem 12
Problem Statement: 2
WAP in Java to print the sum of following series: x1/y2 + x3/y4 + x5/y6 + …. Upto n terms.
Objective: To find the sum of series
Source code:
import [Link];
public class B {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the value of x: ");
int x = [Link]();
[Link]("Enter the value of y: ");
int y = [Link]();
[Link]("Enter the number of terms (n): ");
int n = [Link]();
double sum = 0.0;
for (int i = 1; i <= n; i++) {
int powerX = 2 * i - 1;
int powerY = 2 * i;
double term = [Link](x, powerX) / [Link](y, powerY);
sum += term;
}
[Link]("The sum of the series is: %.2f%n", sum);
}
}
3
Gaurav Dosad [Link] (CS) 4th sem 12
Output:
Problem Statement: 3
4
Gaurav Dosad [Link] (CS) 4th sem 12
WAP in Java to print number of objects created of a class.
Objective: To find the sum of series
Source code:
class A {
public static void main(String[] args) {
B obj1 = new B();
B obj2 = new B();
B obj3 = new B();
B obj4 = new B();
B obj5 = new B();
[Link]("Number of objects created: " + [Link]());
}
}
class B {
private static int count = 0;
public B() {
count++;
}
public static int getNumberOfObjects() {
return count;
}
}
5
Gaurav Dosad [Link] (CS) 4th sem 12
Output:
Problem Statement: 4
6
Gaurav Dosad [Link] (CS) 4th sem 12
WAP in Java to check whether an entered string is valid email address or not.
Objective: To find whether an entered string is valid email address or not.
Source code:
import [Link];
class C {
public static void main(String[] args){
[Link]("Type Email Address.");
String email="";
String uName="", domain="", com="";
String comAry[]={".com", ".in", ".org"};
String domainAry[]={"@yahoo", "@gmail", "@outlook"};
int comIndex=0, domainIndex=0;
D d=new D();
Scanner sc=new Scanner([Link]);
email= [Link]();
for(int i=([Link]()-1); i>=0; i--){
if([Link](i)=='.'){
com+=[Link](i);
comIndex=i;
break;
}
else{
com+=[Link](i);
}
}
com=[Link](com);
if([Link](comAry, com)){
7
Gaurav Dosad [Link] (CS) 4th sem 12
[Link]("Valid Com");
}
else{
[Link]("Invalid Com");
[Link](1);
}
for(int i=(comIndex-1); i>=0; i--){
if([Link](i)=='@'){
domain+=[Link](i);
domainIndex=i;
break;
}
else{
domain+=[Link](i);
}
}
domain=[Link](domain);
if([Link](domainAry, domain)){
[Link]("Valid domain");
}
else{
[Link]("Invalid domain");
[Link](0);
}
for(int i=(domainIndex-1); i>=0; i--){
if([Link]([Link](i))){}
8
Gaurav Dosad [Link] (CS) 4th sem 12
else{
[Link]("UserName is Invalid.");
}
}
[Link]("Email is valid : " + email);
}
}
class D {
String revStr(String s){
String str="";
for(int i=([Link]()-1); i>=0; i--){
str+=[Link](i);
}
return str;
}
boolean strInAry(String ary[], String str){
for(int i=0; i<[Link]; i++){
if(ary[i].equals(str)){
return true;
}
}
return false;
}
}
Output:
9
Gaurav Dosad [Link] (CS) 4th sem 12
10
Gaurav Dosad [Link] (CS) 4th sem 12
Problem Statement: 6
WAP to show the use of synchronized keyword in producer consumer problem.
Objective: To use synchronized keyword
Source code:
public class B {
public static void main(String[] args) {
Buffer buffer = new Buffer();
Producer producer = new Producer(buffer);
Consumer consumer = new Consumer(buffer);
[Link]();
[Link]();
}
}
class Buffer {
private int data = -1;
public synchronized void put(int value) {
while (data != -1) {
try {
wait();
} catch (InterruptedException e) {
[Link]().interrupt();
}
}
data = value;
notifyAll();
}
public synchronized int get() {
11
Gaurav Dosad [Link] (CS) 4th sem 12
while (data == -1) {
try {
wait();
} catch (InterruptedException e) {
[Link]().interrupt();
}
}
int value = data;
data = -1;
notifyAll();
return value;
}
}
class Producer extends Thread {
private Buffer buffer;
public Producer(Buffer buffer) {
[Link] = buffer;
}
public void run() {
for (int i = 1; i <= 5; i++) {
[Link](i);
[Link]("Produced: " + i);
try {
[Link](1000);
} catch (InterruptedException e) {
[Link]().interrupt();
}
12
Gaurav Dosad [Link] (CS) 4th sem 12
}
}
}
class Consumer extends Thread {
private Buffer buffer;
public Consumer(Buffer buffer) {
[Link] = buffer;
}
public void run() {
for (int i = 1; i <= 5; i++) {
int data = [Link]();
[Link]("Consumed: " + data);
try {
[Link](1000);
} catch (InterruptedException e) {
[Link]().interrupt();
} } } }
Output:
13
Gaurav Dosad [Link] (CS) 4th sem 12
Problem Statement: 11
WAP in Java to initialize two string and find common characters and different Characters of
these string.
Objective:
Source code:
import [Link];
import [Link];
class C {
public static void main(String[] args) {
String str1 = "hello";
String str2 = "world";
Set<Character> commonChars = findCommonChars(str1, str2);
[Link]("Common characters: " + commonChars);
Set<Character> diffChars = findDiffChars(str1, str2);
[Link]("Different characters: " + diffChars);
}
public static Set<Character> findCommonChars(String str1, String str2) {
Set<Character> commonChars = new HashSet<>();
for (char c : [Link]()) {
if ([Link](c)!= -1) {
[Link](c);
}
}
return commonChars;
}
public static Set<Character> findDiffChars(String str1, String str2) {
Set<Character> diffChars = new HashSet<>();
14
Gaurav Dosad [Link] (CS) 4th sem 12
for (char c : [Link]()) {
if ([Link](c) == -1) {
[Link](c);
}
}
for (char c : [Link]()) {
if ([Link](c) == -1) {
[Link](c);
}
}
return diffChars;
}
}
Output:
15
Gaurav Dosad [Link] (CS) 4th sem 12
Problem Statement: 12
WAP in Java to initialize two string and find common characters and different Characters of
these string.
Objective: To show the use of synchronized keyword
Source code:
import [Link];
import [Link];
class C {
public static void main(String[] args) {
// Initialize string
String str = "hello world";
// Find characters and their frequencies
Map<Character, Integer> charFreq = new HashMap<>();
for (char c : [Link]()) {
if ([Link](c)) {
[Link](c, [Link](c) + 1);
} else {
[Link](c, 1);
}
}
// Find second most frequent character
int maxFreq = 0;
int secondMaxFreq = 0;
char secondMostFreqChar = '\0';
for ([Link]<Character, Integer> entry : [Link]()) {
int freq = [Link]();
16
Gaurav Dosad [Link] (CS) 4th sem 12
if (freq > maxFreq) {
secondMaxFreq = maxFreq;
maxFreq = freq;
secondMostFreqChar = [Link]();
} else if (freq > secondMaxFreq && freq < maxFreq) {
secondMaxFreq = freq;
}
}
// Print second most frequent character
[Link]("Second most frequent character: " + secondMostFreqChar + " with
frequency: " + secondMaxFreq);
}
}
Output:
17
Gaurav Dosad [Link] (CS) 4th sem 12
Problem Statement: 13
WAP to initialize 2D string array at runtime and to print reverse value of diagonal position only?
Objective:
Source code:
import [Link];
public class C {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Get the number of rows and columns from the user
[Link]("Enter the number of rows: ");
int rows = [Link]();
[Link]("Enter the number of columns: ");
int cols = [Link]();
// Initialize the 2D string array
String[][] arr = new String[rows][cols];
// Get the values for the 2D array from the user
[Link]("Enter the values for the 2D array:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
arr[i][j] = [Link]();
// Print the original 2D array
[Link]("Original 2D array:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
[Link](arr[i][j] + " ");
18
Gaurav Dosad [Link] (CS) 4th sem 12
[Link]();
// Print the reverse value of the diagonal positions only
[Link]("Reverse value of diagonal positions:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (i == j) {
String diagonalValue = arr[i][j];
String reversedValue = new StringBuilder(diagonalValue).reverse().toString();
[Link](reversedValue + " ");
} else {
[Link](arr[i][j] + " ");
[Link]();
} }
Output:
19
Gaurav Dosad [Link] (CS) 4th sem 12
Problem Statement: 5
WAP to show the use of transient keyword.
import [Link].*;
class Q5{
public static void main(String args[]){
A a= new A("Max", "Hello1234");
String fileName="C:\\Users\\pulki\\OneDrive\\Desktop\\JavaTermWork\\
[Link]";
try{
// Serialize the A object
FileOutputStream fos = new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
[Link](a);
[Link]();
[Link]();
catch(Exception e){
// Deserialize the a object
try{
FileInputStream fis = new FileInputStream(fileName);
ObjectInputStream ois = new ObjectInputStream(fis);
A b=(A)[Link]();
[Link]();
[Link]();
[Link]("UserName is :" + [Link] + "\n" + "Password is :"
+ [Link]);
}
20
Gaurav Dosad [Link] (CS) 4th sem 12
catch(Exception e){
class A implements Serializable{
String username;
transient String password;
public A(String username, String password) {
[Link] = username;
[Link] = password;
21
Gaurav Dosad [Link] (CS) 4th sem 12
Problem Statement: 10
WAP to show the use of transient keyword.
import [Link].*;
class Q10{
public static void main(String args[]){
A a= new A("Max", "Hello1234");
String fileName="C:\\Users\\pulki\\OneDrive\\Desktop\\JavaTermWork\\
[Link]";
try{
// Serialize the A object
FileOutputStream fos = new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
[Link](a);
[Link]();
[Link]();
catch(Exception e){
// Deserialize the a object
try{
FileInputStream fis = new FileInputStream(fileName);
ObjectInputStream ois = new ObjectInputStream(fis);
A b=(A)[Link]();
[Link]();
22
Gaurav Dosad [Link] (CS) 4th sem 12
[Link]();
[Link]("UserName is :" + [Link] + "\n" + "Password is :"
+ [Link]);
catch(Exception e){
class A implements Serializable{
String username;
transient String password;
public A(String username, String password) {
[Link] = username;
[Link] = password;
23
Gaurav Dosad [Link] (CS) 4th sem 12
Problem Statement: 16
Find the sum of even number of atleast one using command line argument.
class Q16{
public static void main(String args[]){
int sum=0;
for(int i=0; i<[Link]; i++){
if(([Link](args[i]))%2==0){
sum+=([Link](args[i]));
}
[Link](sum);
24
Gaurav Dosad [Link] (CS) 4th sem 12
Problem Statement: 20
WAP to show the use of object serialization?
import [Link].*;
class Q20{
public static void main(String args[]){
A a= new A("Max", "Hello1234");
String fileName="C:\\Users\\pulki\\OneDrive\\Desktop\\JavaTermWork\\
[Link]";
try{
// Serialize the A object
FileOutputStream fos = new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
[Link](a);
[Link]();
[Link]();
catch(Exception e){
// Deserialize the a object
try{
FileInputStream fis = new FileInputStream(fileName);
ObjectInputStream ois = new ObjectInputStream(fis);
A b=(A)[Link]();
[Link]();
25
Gaurav Dosad [Link] (CS) 4th sem 12
[Link]();
[Link]("UserName is :" + [Link] + "\n" + "Password is :"
+ [Link]);
catch(Exception e){
class A implements Serializable{
String username;
transient String password;
public A(String username, String password) {
[Link] = username;
[Link] = password;
26
Gaurav Dosad [Link] (CS) 4th sem 12
Problem Statement: 22
WAP to copy one file contain into another by creating second file?
import [Link];
public class q22{
public static void main(String args[]){
String filePath="D:\\CLG STD\\sem 4\\java lang\\assignment\\
JavaTermWork\\[Link]";
File fs= new File(filePath);
try{
[Link]();
}
catch(Exception e){
[Link]("unable to create file");
[Link]();
}
}
}
27
Gaurav Dosad [Link] (CS) 4th sem 12
Problem Statement: 23
WAP to store different object in an ArrayList and to retrieve using iterator().
import [Link];
import [Link];
class q23{
public static void main(String args[]){
ArrayList<Integer> ar=new ArrayList<>(10);
[Link](1);
[Link](11);
[Link](111);
[Link](1111);
[Link](11111);
[Link](0,2);
Iterator<Integer>iterator =[Link]();
while([Link]()){
int element=[Link]();
[Link](element);
28
Gaurav Dosad [Link] (CS) 4th sem 12
Problem Statement: 24
WAP to make the following series?
x1/y2+x3/y4+......xn/yn
import [Link];
class q24{
public static void main(String[] args){
Scanner sc= new Scanner([Link]);
int x, y, c;
[Link]("Enter X value.");
x=[Link]();
[Link]("Enter Y value.");
y=[Link]();
[Link]("Enter terms.");
c=[Link]();
A a= new A();
[Link]([Link](x, y, c));
class A{
int Series(int a, int b, int c){
int n=1,sum=0, i=1;
while(n<=c){
29
Gaurav Dosad [Link] (CS) 4th sem 12
sum+=(a*i)/(b*(i+1));
i+=2;
n++;
return sum;
30
Gaurav Dosad [Link] (CS) 4th sem 12
Problem Statement: 25
WAP to find the average of second minimum and second highest number?[3,5,7,9,11]
import [Link];
class q25{
public static void main(String args[]){
int[] a = {3,5,7,9,11};
[Link](a);
int avg= (a[1] + a[[Link]-2])/2;
[Link]("average of the second highest and the second lowest number
is"+ avg);
31
Gaurav Dosad [Link] (CS) 4th sem 12
Problem statement 7
WAP to print all numeric digits sum of all database column values
import [Link].*;
import [Link].*;
public class DigitSumCalculator {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/your_database_name";
String username = "your_username";
String password = "your_password";
String columnName = "your_column_name";
try {
Connection connection = [Link](url, username, password);
Statement statement = [Link]();
ResultSet resultSet = [Link]("SELECT " + columnName + " FROM
your_table_name");
int totalDigitSum = 0;
while ([Link]()) {
String columnValue = [Link](columnName);
totalDigitSum += calculateDigitSum(columnValue);
[Link]("Sum of numeric digits in column " + columnName + ": " +
totalDigitSum);
[Link]();
[Link]();
[Link]();
32
Gaurav Dosad [Link] (CS) 4th sem 12
} catch (SQLException e) {
[Link]();
private static int calculateDigitSum(String value) {
int sum = 0;
Pattern pattern = [Link]("\\d");
Matcher matcher = [Link](value);
while ([Link]()) {
sum += [Link]([Link]());
return sum;
33
Gaurav Dosad [Link] (CS) 4th sem 12
Problem statement 8:
WAP using Applet to draw circle, line ,rectangle and fill them with a color given by the user.
import [Link];
import [Link].*;
import [Link].*;
public class DrawingApplet extends Applet implements ActionListener {
private Color chosenColor = [Link];
private int shapeChoice = 0; // 0 for circle, 1 for line, 2 for rectangle
private int startX, startY, endX, endY;
public void init() {
Button circleButton = new Button("Circle");
Button lineButton = new Button("Line");
Button rectButton = new Button("Rectangle");
Button colorButton = new Button("Choose Color");
[Link](this);
[Link](this);
[Link](this);
[Link](this);
add(circleButton);
add(lineButton);
34
Gaurav Dosad [Link] (CS) 4th sem 12
add(rectButton);
add(colorButton);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
startX = [Link]();
startY = [Link]();
public void mouseReleased(MouseEvent e) {
endX = [Link]();
endY = [Link]();
repaint();
});
public void actionPerformed(ActionEvent e) {
String command = [Link]();
switch (command) {
case "Circle":
shapeChoice = 0;
break;
case "Line":
shapeChoice = 1;
break;
case "Rectangle":
35
Gaurav Dosad [Link] (CS) 4th sem 12
shapeChoice = 2;
break;
case "Choose Color":
chosenColor = [Link](this, "Choose a color", chosenColor);
break;
public void paint(Graphics g) {
[Link](chosenColor);
switch (shapeChoice) {
case 0:
[Link](startX, startY, endX - startX, endY - startY);
break;
case 1:
[Link](startX, startY, endX, endY);
break;
case 2:
[Link](startX, startY, endX - startX, endY - startY);
break;
36