Download as pdf or txt
Download as pdf or txt
You are on page 1of 39

JAVA PROGRAMMING

LAB EXERCISE-3
NAME : V.JAYASURYA
REG NUM : 19BCE2416

1. Read the following details from the user


*Username
*Password
*Confirm Password.
Write a Java Program and perform the following checks on the input data using String
methods.
a) If the username or password is less than 8 characters in length then display Invalid
username length or Invalid Password length to the user.
b) If the username or password contains a space then display Username or Password
should not contain spaces.
c) If the password does not match confirm password then display Passwords don’t
match
to the user.
d) If any three adjacent characters of the username in the same order is part of the
password then display password cannot contain username message to the user.

SOLUTION

import java.util.*;
class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String username, password, confirmpass;
System.out.println("Enter User Name: ");
username=sc.nextLine();
System.out.println("Enter Password: ");
password=sc.nextLine();
System.out.println("Enter Confirm Password: ");
confirmpass=sc.nextLine();
int l1= username.length();
int l2=password.length();

if((l1 <=8)||(l2<=8))
{
System.out.println("Invalid Username Length or Invalid Password Length!
User Name & Password Length Should be Greater than or equal to 8");

}
if((username.contains(" ")) || (password.contains(" ")))
{
System.out.println("Username or Password Should not contain spaces!");
}
if(!password.equals(confirmpass))
{
System.out.println("Password Doesn't Match Confirm Password!");
}
int i=0;
if(password.indexOf(username.substring(i,i+3))!=-1)
{
System.out.println("Password Should Not Contain Username!");
}
}
}
OUTPUT:

Enter User Name:


Johny
Enter Password:
Hi John
Enter Confirm Password:
HiJohn
Invalid Username Length or Invalid Password Length! User Name & Password Length
Should be Greater than or equal to 8
Username or Password Should not contain spaces!
Password Doesn't Match Confirm Password!
Password Should Not Contain Username!

78

2. Consider a class by name Student containing the following


Class Instance Variables
• Name – type String
• Regno – type String
• Phone - type String
Methods
• getInfo – Method receives the name, regno, phone details for a student using its
input parameters and assigns it to the class instance variables .
• displayinfo - Displays all the data from the class instance variables to the user.
• static sortobj – This method receives an array of student objects. It sorts the array of
objects in the ascending order using the name field and displays all details of each
student object in the sorted order.
Write a Java program that creates the student class and instantiates an array of
student
objects. The details of the student objects in the sorted order should be displayed by
using
the sortobj method of the student class.

package labexercise;
import java.util.*;
public class democlass
{
public static void main(String[] args)
{
student s[]=new student[3];
for(int i=0;i<s.length;i++)
{
s[i]=new student();
}
for(int i=0;i<s.length;i++)
{
s[i].set_details();
}

sort_student(s);
}
public static void sort_students(student k[])
{
student temp=new student();
for(int i=0;i<k.length-1;i++)
{
for(int j=0; j<k.length-1-i;j++)
{
if(k[j].name.compareTo(k[j+1].name)>0)
{
temp=k[j];
k[j]=k[j+1];
k[j+1]=temp;
}
}
}
for(student m:k)
{
System.out.println(m.get_details());
}

}
}

class student
{
public String name;
public String regno;
public String phonenumber;

public void set_details()


{
Scanner input = new Scanner(System.in);
System.out.println("Enter the name");
name=input.nextLine();
System.out.println("Enter the registration number");
regno=input.nextLine();
System.out.println("Enter the phone number");
phonenumber=input.nextInt();
}

public String get_details()


{
return name+regno+phonenumber;
}
}

Output
Enter the name
james
Enter the registration number
11bce
Enter the phonenumber
7339219765
Enter the name
ruby
Enter the registration number
13bce
Enter the phonenumber
9339219343
Enter the name
anbu
Enter the registration number
14bce
Enter the phonenumber
9790263997

anbu14bce9790263997
james11bce7339219765
ruby13bce9339219343
3. A university has faculty members with different designations as mentioned below
• Professors
• Associate Professors
• Assistant Professors.
• Teaching Research Assistants
The salary computation for each designation is decided as follows
• Professors- Salary is basic Pay(150000) + 30% of Basic pay as DA
• Associate Professors – Salary is basic pay(120000) + 20% of Basic Pay as DA
• Assistant Professors – Salary is basic pay( 100000) + 10% of Basic Pay as DA
• Teaching Research Assistants (TRA) are appointed on a contract basis and are paid a
fixed monthly salary of 20000.
Every faculty member (Except TRA’s) has a dependent member (dependent class has a
composition relationship with faculty) added to the system. The dependent details like
dependent name, dependent phone number, dependent date of birth is registered while
adding an employee to the system. Design a class diagram and implement a Java
application that will display the employee salary and dependent details for an employee
upon receiving the employee id

public class Exercise {


public ststic void main(String[] args){
employee james =new employee("james","1","7339219765","31-08-2001","20000");
james.display

}
}
class dependant
{
String dependentname;
String dependentphonenumber;
String dependentDateOfBirth;
public dependent(String dependentname,String dependentphonenumber,String
dependentDateOfBirth){
this.dependentname==dependentname;
this.dependentphonenumber = dependentphonenumber;
this.dependentDateOfBirth= dependentDateOfBirth;
}
public void displaydepend(){
System.out.println(dependentname+dependentphonenumber+dependentDateOfBirth);
}
}
class employee
{
String name;
String empid;
dependent salary;
public employee (String name,String empid,String dname,String dphonenumber,String
dDateOfBirth){
this.name=name;
this.empid=empid;
Salary= new dependent(dname , dphonenumber,dDateOfBirth);
}

public void display(){

System.out.println(name+empid+salary.dependentphonenumber+salary.dependentDateOfBir
th)

}
Question No 4.
Shape, TwoDimensionalShape and ThreeDimensionalShape are abstract classes with
abstract methods. Create a Java program that uses an array of Shape references to
objects of each concrete class in the hierarchy. Also, in the loop that processes all the
shapes in the array, determine whether each shape is a two dimensional shape or a
three dimensional shape. If the shape is a two dimensional shape then display its area.
If the shape is a three dimensional shape then display its surface area. [Surface Area of
Cube is 6a2 where a is a side of the cube. sSurface Area of Sphere is 4πr2 where r is the
radius.]. Apply the concepts of Dynamic Polymorphism to achieve the results.

public abstract class Shape


{
private int x;
private int y;
public Shape( int x, int y )
{
this.x = x;
this.y = y;
}
public void setX( int x )
{
this.x = x;
}
public void setY( int y )
{
this.y = y;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public String toString()
{
return String.format( "(%d, %d)", getX(), getY() );
}
public abstract String getName();
}
public abstract class TwoDimensionalShape extends Shape
{
private int dimension1;
private int dimension2;
public TwoDimensionalShape( int x, int y, int d1, int d2 )
{
super( x, y );
dimension1 = d1;
dimension2 = d2;
}
public class Circle extends TwoDimensionalShape
{
public Circle( int x, int y, int radius )
{
super( x, y, radius, radius );
}
public class Square extends TwoDimensionalShape
{

public Square( int x, int y, int side )


{
super( x, y, side, side );
}
public abstract class ThreeDimensionalShape extends Shape
{
private int dimension1;
private int dimension2;
private int dimension3;
public ThreeDimensionalShape(int x, int y, int d1, int d2, int d3 )
{
super( x, y );
dimension1 = d1;
dimension2 = d2;
dimension3 = d3;
}
public class Sphere extends ThreeDimensionalShape
{
public Sphere( int x, int y, int radius )
{
super( x, y, radius, radius, radius );
}
public class Cube extends ThreeDimensionalShape
{
public Cube( int x, int y, int side )
{
super( x, y, side, side, side );
}
public class ShapeTest
{
public static void main( String args[] )
{
Shape shapes[] = new Shape[ 4 ];
shapes[ 0 ] = new Circle( 22, 88, 4 );
shapes[ 1 ] = new Square( 71, 96, 10 );
shapes[ 2 ] = new Sphere( 8, 89, 2 );
shapes[ 3 ] = new Cube( 79, 61, 8 );
for ( Shape currentShape : shapes )
{
System.out.printf( "%s: %s",currentShape.getName(), currentShape );
if ( currentShape instanceof TwoDimensionalShape )
{
TwoDimensionalShape twoDimensionalShape =
( TwoDimensionalShape ) currentShape;
System.out.printf( "%s's area is %s\n",
currentShape.getName(), twoDimensionalShape.getArea() );
}
if ( currentShape instanceof ThreeDimensionalShape )
{
ThreeDimensionalShape threeDimensionalShape =
( ThreeDimensionalShape) currentShape;
System.out.printf( "%s's area is %s\n",
currentShape.getName(), threeDimensionalShape.getArea() );
System.out.printf( "%s's volume is %s\n",
currentShape.getName(),
threeDimensionalShape.getVolume() );
}
System.out.println();
}
}
}

OUTPUT:
Area of a Circle: 28.26
Area of a Square: 25
Surface Area of a Cube : 216
Surface Area of Sphere : 144
Question No 5.
Professors are allowed to enter marks for students. Professors can enter only marks
between
0 and 100 . Anything entered below 0 or above 100 is considered to be an exception.
Write a program that receives an array of marks from Professor. If the marks fail to
satisfy
the criteria then handle them as exceptions.
Apply Exception handling where ever necessary in this program

SOLUTION

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter number: ");
n = s.nextInt();
int marks[] = new int[n];
System.out.println("Enter marks: ");
for(int i = 0; i < n; i++)
{
marks[i] = s.nextInt();

}
for(int i=0;i<marks.length;i++)
{
if((marks[i]<0)||(marks[i]>100))
System.out.println("Array Index out of Bound Exception!: "+marks[i]+" Mark
Should be in the range of 0 to 100!");
}

}
}

OUTPUT:

Enter number: 5
Enter marks:
0
100
-9
108
98
Array Index out of Bound Exception!: -9 Mark Should be in the range of 0 to 100!
Array Index out of Bound Exception!: 108 Mark Should be in the range of 0 to 100!

Question No 6

Write a simple Java class in which all the employee details like emp_id, name, age,
desig, exp_yrs, dept, sal, mgr_id need to be validated for their input values. Develop
new user defined exception classes for prompting the user for their wrong input. Test
them with Java program.

Solution:

import java.util.*;
class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int emp_id, age, exp_yrs, sal, mgr_id;
String name, desig, dept;
try
{
System.out.println("Enter emp_id: ");
emp_id=sc.nextInt();
System.out.println("Enter name: ");
name=sc.next();
System.out.println("Enter age: ");
age=sc.nextInt();
System.out.println("Enter desig: ");
desig=sc.next();
System.out.println("Enter exp_yrs: ");
exp_yrs=sc.nextInt();
System.out.println("Enter dept: ");
dept=sc.next();
System.out.println("Enter sal: ");
sal=sc.nextInt();
System.out.println("Enter mgr_id: ");
mgr_id=sc.nextInt();
System.out.println("Success! Your Details are valid!");
}
catch(Exception e)
{
System.out.println("InputMismatchException!Please Enter Valid Input!");
sc.reset();

}
}
}

Output:

Without Exception:

Enter emp_id:
35
Enter name:
Sai
Enter age:
40
Enter desig:
Employee
Enter exp_yrs:
5
Enter dept:
Sotware
Enter sal:
6000000
Enter mgr_id:
45
Success! Your Details are valid!

With Exception:

Enter emp_id:
67
Enter name:
Surya
Enter age:
30
Enter desig:
Developer
Enter exp_yrs:
5
Enter dept:
Engineer
Enter sal:
Sixty
InputMismatchException!Please Enter Valid Input!

Question No 7

Write a Java program that gets the registration number, name and list of five subjects
the student has registered for. Subject details should be subject code and subject name.
Your program should get such details for a minimum of three students.
If the student enters a subject code or subject name that’s not present in the given list
of subjects then throw a subjectexception object (user defined) and print the detailed
message in getmessage() method of Throwable class as “Subject Not allowed contact
Timetable Team”
The list of valid subjects are
CSE1007 Java Programming
CSE4003 Cyber Security
CSE2004 Database Management Systems
CSE6006 NoSQL Databases
CSE1001 Python Programming
In addition to this handle InputMismatch and ArrayoutofBounds exception in your
code.
Solution

//Using Scanner
import java.util.*;
class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String reg_no, name, subject_code, subject_name;
int n;
System.out.println("Enter the number of students: ");
n=sc.nextInt();
String scode= "CSE1007, CSE4003, CSE2004, CSE6006, CSE1001";
String ssubject="Java Programming, Cyber Security, Database Management Systems,
NoSQL Databases, Python Programming";
System.out.println("Enter Reg.No: ");
reg_no=sc.next();
System.out.println("Enter Name: ");
name=sc.next();
System.out.println("Enter Subject Code: ");
subject_code=sc.next();
System.out.println("Enter Subject Name: ");
subject_name=sc.next();

try
{
if((!subject_code.contains(scode)) || (!subject_name.contains(ssubject)))
{
System.out.println("Subject Exception! ");
System.out.println("Subject Not Allowed Contact Timetable Team!");
}
}
catch(Exception e)
{
if((subject_code.contains(scode)) || (subject_name.contains(ssubject)))
{
for(int i=0;i<n;i++)
{
System.out.println("Student Reg No : "+reg_no);
System.out.println("Student Name : "+name);
System.out.println("Student Subject Code: "+subject_code);
System.out.println("Student Subject Name: "+subject_name);
}
}
}
}
}

//Using Arrays

import java.util.*;
class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String reg_no, name, subject_code, subject_name;
int n;
System.out.println("Enter the number of students: );
n=sc.nextInt();
System.out.println("Enter Reg.No: ");
reg_no[]=new reg_no[n];
for(int i = 0; i < n; i++)
{
reg_no[i] = sc.next();
}
System.out.println("Enter Name: ");
name=new name[n];
for(int i = 0; i < n; i++)
{
name[i]= sc.next();
}
System.out.println("Enter Subject Code: ");
subject_code[]=new subject_code[n];
for(int i=0;i<n;i++)
{
subject_code[i]=sc.next();
}
System.out.println("Enter Subject Name: ");
subject_name[]=new subject_name[n];
for(int i=0;i<n;i++)
{
subject_name[i]=sc.next();
}

}
}
OUTPUT:

Enter the number of students:


1
Enter Reg.No:
19BCE2416
Enter Name:
VJAYASURYA
Enter Subject Code:
SWE1008
Enter Subject Name:
Software Requirements
Subject Exception!
Subject Not Allowed Contact Timetable Team!

Question Number 8

A class by name calculator contains four methods and two data members Data
members are int a and int b . They are initialized using the parameterized constructor.
Handle inputmismatch exception during object creation as an outer try block
The four methods are given below
Method 1 – validateInput() . If number a and number b is greater than 500 then the
method should throw an userdefined exception called numberoverflow exception
Method 2 – dividetwonumber . This method should throw an arithmetic exception.
Handle the method exception as a nested try block within the outer try block. Method
signatures should use Throws clause

Solution:

import java.util.*;
class Calculator
{
Calculator(int a, int b);
{
System.out.println("The entered two numbers are: " +a +b);
}
try
{
void validateInput()
{
if((a >500) && (b>500))
{
System.out.println("Number Overflow Exception!");
}
}

void dividetwonumber()
{
System.out.println("Arithmetic Exception!");
}
}
catch (Exception e)
{
System.out.println("Exception Handled : "+e);
}

public static void main(String args[])


{
Calculator cal=new Calculator();

Calculator obj=new Calculator(400, 300);


Calculator obj2=new Calculator(500, 500);

Calculator obj3=new Calculator(50, 54);

Calculator obj4=new Calculator(5000, 504);

cal.dividetwonumber();
cal.validateInput();
}
}

OUTPUT:

The entered two numbers are: 400 300


Arithmetic Exception!
The entered two numbers are: 50 54
Number Overflow Exception!

Question No 9
Create a File object for a file named javallabexercise. And perform the following on the
file
Display the file name
Display the path for the file
Display the directory of the file
Display the permissions on the file (Can you read and write to the file). If you can
read then display the message “File reading is allowed”. If you can write then display
File Writing is allowed”
Display the absolute path for the file
Create another file by name lablabexercise2 in the same directory and display the
message the file is created
import java.util.*;
import java.io.*;
import java.sql.SQLException;

public class labclass{


public static void main(String[] args) throws Throwable{
String fname=args[0];
File f = new File("F:\\Fall Semester 2020-2021\\Java
Programming\\workspace\\sampleproject\\javalabexercise.txt");
//Display the file name
System.out.println("File name is:"+f.getName());
//Display the path for the file
System.out.println("Path for the file: "+f.getPath());
//Display the absolute path for the file
System.out.println("Absolute path for the file:" +f.getAbsolutePath());
//Display the parent of the file
System.out.println("Parent:"+f.getParent());
System.out.println("Exists :"+f.exists());
if(f.exists())
{
//Diplay the permission on the file
System.out.println("File writing is allowed:"+f.canWrite());
System.out.println("File reading is allowed"+f.canRead());
//Display the directory of the file
System.out.println("Is a directory:"+f.isDirectory());
System.out.println("File Size in bytes "+f.length());
}
}
//Create a file name javalabexercise2 having same directory
File obj=new File("F:\\Fall Semester 2020-2021\\Java
Programming\\workspace\\sampleproject\\javalabexercise","javalabexercise2.txt");
if(obj.createNewFile()
{
System.out.println("New File is created");
}
}

Output
file name is
javalabexercise

path for the file is


F:\\Fall Semester 2020-2021\\Java
Programming\\workspace\\sampleproject\\javalabexercise.txt

permission on the file

file reading is allowed


file writing is allowed

Absolute path for the file

F:\\Fall Semester 2020-2021\\Java Programming\\workspace\\sampleproject

New file is created


Question Number 10

Create a class rectangle that reads two integer values length and breadth in
parameterized constructors. If an inputmismatch exception occurs catch the exception
and print the detailedmessage from Throwable class and print the message “Support
team number is 77667777”. If there is no exception then print only the message
“Support team number is 77667777”. Use Finally block to achieve the results.

Solution

import java.util.*;
class Rectangle
{
Rectangle(int length, int breadth);
{
System.out.println("The entered two numbers are: " +length +breadth);
}

public static void main(String args[])


{
Rectangle r=new Rectangle(11,22);

boolean bError = true;


int length = 0, breadth= 0, nQuotient = 0;
do {
try {
System.out.println("Enter first num: ");
length = Integer.parseInt(input.next());

System.out.println("Enter second num: ");


breadth = Integer.parseInt(input.next());

System.out.println("Support team number is 77667777");

nQuotient = length/breadth;;
System.out.println("The Division Result Is: "+nQuotient);

bError = false;
}
catch (Exception e) {
System.out.println("InputMismatchException!Please Enter Valid Input!");
input.reset();
}
} while (bError);
finally
{
System.out.println("Support team number is 77667777");
}
}
}

OUTPUT:

Without Exception:

Support team number is 77667777

With Exception:

InputMismatchException!Please Enter Valid Input!

Support team number is 77667777


11. How will you handle exceptions for the code given below? Demonstrate the
handling of exceptions by rewriting the code. Create an object for calculator and
invoke all the three methods for the object.

import java.util.*;

public class calculator


{
public static void main (String[] args)
{
Scanner input =new Scanner(System.in);
String name = input.nextline();
int num1 =input.nextint();
int num 2=input.nextint();
}
class calculator(String name,int num1,int num2){
this.name=name;
this.num1=num1;
this.num2=num2;
}

class Exception1extends Exception


{
try{
int num1=30, num2=0;
int output=num1/num2;
System.out.println ("Result: "+output);
}
catch(ArithmeticException e){
System.out.println ("You Shouldn't divide a number by zero");
}
}
}

class Exception2 extends Exception


{
try{
int num1=30, num2=0;
int output=num1+num2;
System.out.println ("Result: "+output);
}
catch(ArithmeticException e){
System.out.println ("You Shouldn't add a number zero");
}
}
}

class Exception3 extends Exception


{
try{
String str="beginnersbook";
System.out.println(name.length());;
char c = name.charAt(0);
c = name.charAt(40);
System.out.println(c);
}catch(StringIndexOutOfBoundsException e){
System.out.println("StringIndexOutOfBoundsException!!");
}
}
}
12. a) Four files contain numbers in them. Each file has some 50 numbers each number
separated by a space. Sample data in each file is 101 302 405 5298…etc
Write a java program that reads the four files using separate Threads and performs
the following
I. Find the sum (sum1) of all numbers that contains 7 or 9 from File 1
II. Find the sum (sum2) of square of all numbers that are divisible by 9 or 11 in File
2
III. Find the sum (sum3) of only four digit numbers that end with 8 from File 3
IV. Displays the sum of all numbers from all the files.
b) Create a linked list containing four car objects (car id, car name,car_brand).
Traverse the linked list and display only details of the cars that belong to the brand
“Ford”

import java.util.*;
import java.io.*;

public class democlass{

public Static void main(String args[]) throws IoException{


file obj=new File("file1.txt");
FileOutputStream fout=new FileOutputStream(obj);
for(int i=1;i<=50;1++)
{
fout.write(i);
}
fout.close();
file obj1=new File("file2.txt");
FileOutputStream fout=new FileOutputStream(obj1);
for(int i=51;i<=100;1++)
{
fout1.write(i);
}
fout.close();
file obj2=new File("file3.txt");
FileOutputStream fout=new FileOutputStream(obj2);
for(int i=101;i<=150;1++)
{
fout2.write(i);
}
fout.close();
file obj3=new File("file4.txt");
FileOutputStream fout=new FileOutputStream(obj3);
for(int i=151;i<=200;1++)
{
fout3.write(i);
}
fout.close();

System.out.println(f1.getName());
int ch=fileReader.read();
while(ch==-1)
System.out.println(ch+"file created ");
try(
int ch=FileReader f1.read()
while(sum1=7||9)
}
catch(File not found Exception e){
Systen.out.println("The FileReader f1 contain 7 and 9);
}
try(
int ch=FileReader f2.read()
while(sum2=(sum)^2 /7||9)
}
catch(File not found Exception e){
Systen.out.println("The FileReader f2contains the required information );
}
try(
int ch=FileReader f3.read()
while(sum3=8,18,28...)
}
catch(File not found Exception e){
Systen.out.println("The FileRead contains the digits end with 8);
}
System.out.println("The sum of all the numbers"+f1+f2+f3+f4);
}
public class linked list
{
node head;
static class node
{
int data;
Node next;
Node(int d)
{
data=d;
Next=null;
}
public main
{
Linkedlist list=new Linkedlist();
list=insert(list,carid)
list=insert(list,carname)
list=insert(list,car_brand)
print List(list);
car_id="Ford1,vols2,merceder1";
car_name="Ford,rolroyle,Merceder";
printlist(where) if car_brand="Ford";
}
}

13. a) Implement a Java program for the scenario given below


A thread by name thr1 reads in an array of 5 faculty objects (faculty object data
members – faculty id, faculty designation, faculty gender) and writes all the objects to
a file by name faculty.txt.
Another thread by name thr2 reads all the faculty objects written by thr1 and displays
the following to the user
Displays only data about faculty members whose designation is Assistant Professor
Displays the name of all faculty members in sorted order of their name.
If thr2 doesn’t have any data to read from the file then it should be in the wait state.
thr1 after writing data should notify thr2 to start reading data.

import java.util.*;
import java.io.*;
public class democlass{
public static void main(String[] args) throws IoException
{multi thr1=new multi();
thr1.start();
thr1.join();
public class process extends thread
{
static int numberof values=17;
static object lock1=new object();
}
public class Array of object(2)
{
static void;
static Thread threadsArray[];
private static void initiale Array()
{
threadsArray=new Thread[number of values);
for(int i==0;i<threadsArray.length;i++)
{
threadsArray[i]=new Thread(new Runnable())
{
void faculty()
int facultyid;
string facultydesignation;
string facultygender;

facultyid[0].setdata(112);
facultydesignation[1].setdata("profesor");
faculty gender[2].setdata("male);
}
object creation:
objectArray obj1=new objectArray();
objectArray obj2=new objectArray();
objectArray obj3=new objectArray();
objectArray obj4=new objectArray();
objectArray obj5=new objectArray();
obj1();
obj2();
obj3();
obj4();
obj5();
faculty();

public static void main(String args[])


{
read data();
write data();
try
{
File write fw=new Filewriter("faculty.txt);
fw.write("Array of object");
fw.class();
}
catch(Exception e)
{
system.out.println(e);
}
}

14. Implement a Java program for the scenario given below


A thread thr_read1 reads two integers at a time from a file input.txt and adds these
numbers and displays the result to the user. Another thread thr_read2 reads the two
integers from the same file input.txt and performs multiplication and displays the
result to the user. Another thread thr_write writes two integers to the file input.txt. If
thr_read1 or thr_read2 doesn’t have numbers to read from the file then it should enter
the wait state. thr_write after writing the numbers should notify any thread that’s
waiting to perform the write operation on the file.

import java.util.*;
import java.io.*;

public class democlass{

public static void main(String[]args) throws IoException{

Thread thr_read1=new Thread(new Runnable(){


public void run(){
FileReader f1=new FileReader("input.txt");
FileReader f=new FileReader("input.txt");
int ch=new Reader.read()
a=f1+f2

System.out.println("The added numbers in the file is"+a);


}
});
thr1.start();
thr1.notify;
Thread thr_read2=new Thread(new Runnable(){
public void run(){
FileReader f1=new FileReader("input.txt");
FileReader f=new FileReader("input.txt");
int ch=new Reader.read()
b=f1*f2
System.out.println("The Multiplied number is"+b);
}
});
thr2.start();
thr2.notify;

Thread thr_write=new Thread(new Runnable(){


public void run(){
Filewriter thr_read1=new FileReader("input.txt");
Filewriter thr_read2=new FileReader("input.txt");
int ch=new writer.write()

System.out.println("The number not in the thread1 and thread 2 is"+);


}
});
thr3.start();
thr3.notify;

15)Read the data for 5 customer objects (customer id and customer name) from the
user and add them to a linkedlist. Display the customer objects in the linked list data
structure in the reverse order.

import java.util.*;

public class calculator


{
public static void main (String[] args)
{
Scanner input =new Scanner(System.in);
String customer1 = input.nextline();
String customer2 = input.nextline();
String customer3 = input.nextline();
String customer4 = input.nextline();
String customer5 = input.nextline();

}
class customer1 {
String name;
String customer_id;
}
public Student(String initName, String initId, ) {
name = initName;
customer_id = initId;
}
}
class customer2 {
String name;
String customer_id;
}
public Student(String initName, String initId, ) {
name = initName;
customer_id = initId;
}
}
class customer3 {
String name;
String customer_id;
}
public Student(String initName, String initId, ) {
name = initName;
customer_id = initId;
}
}
class customer4{
String name;
String customer_id;
}
public Student(String initName, String initId, ) {
name = initName;
customer_id = initId;
}
}
class customer5 {
String name;
String customer_id;
}
public Student(String initName, String initId, ) {
name = initName;
customer_id = initId;
}
}

public static main(String args[])


{
LinkedList<String> list = new LinkedList<>();

list.add("customer1");
list.add("customer2");
list.add("customer3");
list.add("customer4");
list.add("customer5");

System.out.println("Initial LinkedList " + ll);


}
}

You might also like