19BCE2416 Java Lab Exercise 3 PDF
19BCE2416 Java Lab Exercise 3 PDF
LAB EXERCISE-3
NAME : V.JAYASURYA
REG NUM : 19BCE2416
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:
78
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;
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
}
}
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);
}
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.
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:
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);
}
cal.dividetwonumber();
cal.validateInput();
}
}
OUTPUT:
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;
Output
file name is
javalabexercise
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);
}
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:
With Exception:
import java.util.*;
import java.util.*;
import java.io.*;
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";
}
}
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();
import java.util.*;
import java.io.*;
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.*;
}
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;
}
}
list.add("customer1");
list.add("customer2");
list.add("customer3");
list.add("customer4");
list.add("customer5");