Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
125 views

Java Questions and Code

This document contains code for a Java assignment submitted by student Shristi Baranwal. The assignment contains multiple coding questions involving validating user input for username and password, creating a Student class with methods to get/display student info and sort an array of Student objects, and calculating employee salaries based on designation while allowing employees to have dependent details. The code provided implements solutions to each question posed in the assignment.

Uploaded by

shristi baranwal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views

Java Questions and Code

This document contains code for a Java assignment submitted by student Shristi Baranwal. The assignment contains multiple coding questions involving validating user input for username and password, creating a Student class with methods to get/display student info and sort an array of Student objects, and calculating employee salaries based on designation while allowing employees to have dependent details. The code provided implements solutions to each question posed in the assignment.

Uploaded by

shristi baranwal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Name: Shristi Baranwal Submitted to: Prof.Satish C. J.

Reg. No.: 18BCB0127


Course Name: Java Programming
Course Code: CSE1007
Slot: L15+L16

Digital Assignment-8

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.

Code:
Package Practice;

importjava.util.Scanner;

public class Username_Password_Check {

public static void main(String[] args) {


try {
Scanner input=new Scanner(System.in);
System.out.println("Enter username: ");
String username=input.nextLine();
System.out.println("Enter password: ");
String password=input.nextLine();
System.out.println("Enter password again: ");
String cpassword=input.nextLine();
try {
if(username.length()<8)
{
Thrownew Username_Exception("Invalid Username
length");
}
Else if(username.contains(" "))
{
Thrownew Username_Exception("Username should not
contain spaces");
}
}
Catch (Username_Exceptionu) {
System.out.println(u.getMessage());
}
try {
if(password.length()<8)
{
Thrownew Password_Exception("Invalid Password
length");
}

Else if(password.contains(" "))


{
Thrownew Password_Exception("Password should not
contain spaces");
}
else {
Boolean flag=true;
for(inti=0;i<username.length()-2;i++)
{
String check=username.substring(i,i+3);
if(password.contains(check))
{
flag=false;
thrownew Password_Exception("Weak
password");
}
}
if(flag)
{
Thrownew Password_Exception("Strong
password");
}
}
}
catch(Password_Exceptionp) {
System.out.println(p.getMessage());
}
try {
if(!password.equals(cpassword))
{
Thrownew Confirm_Password_Exception("Passwords
don't match");
}
}
catch(Confirm_Password_Exceptionc) {
System.out.println(c.getMessage());
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}

}
Class Password_Exception extends Exception{
Public Password_Exception(String input)
{
super(input);
}
}
Class Username_Exception extends Exception{
Public Username_Exception(String input)
{
super(input);
}
}
Class Confirm_Password_Exception extends Exception{
Public Confirm_Password_Exception(String input)
{
super(input);
}
}

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.
• staticsortobj – 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.

Code:
Package Practice;
importjava.util.*;
publicclass Student_Information {

public staticvoid main(String[] args) {


Scanner input=new Scanner(System.in);
Student3 s1[]=new Student3[5];
try {
for(inti=0;i<s1.length;i++)
{
System.out.println("Enter name: ");
String name=input.nextLine();
System.out.println("Enter registration number: ");
String regno=input.nextLine();
System.out.println("Enter phone number: ");
String phone=input.nextLine();
s1[i]=new Student3();
s1[i].getInfo(name, regno, phone);
}
}
catch(InputMismatchExceptioni)
{
System.out.println("Invalid Input");
}
System.out.println("The sorted list of students is given by: ");
Student3.sortobj(s1);
}

}
class Student3
{
String Name;
String Regno;
String Phone;
Public void getInfo(String name, String regno, String phone)
{
Name=name;
Regno=regno;
Phone=phone;
}
Public void displayInfo(Student3 s)
{
System.out.println(s.Name+" "+s.Regno+" "+s.Phone);
}
Publicstaticvoid sort obj(Student3 s[])
{
try {
Student3 temp=newStudent3();
for(inti=0;i<s.length-1;i++)
{
for(intj=0;j<s.length-1-i;j++)
{
if(s[j].Name.compareTo(s[j+1].Name)>0)
{
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}
for(inti=0;i<s.length;i++)
{
temp.displayInfo(s[i]);
}
}
catch(ArrayIndexOutOfBoundsExceptiona)
{
System.out.println(a.getMessage());
}
}
}

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.
Code:
Package Practice;

importjava.time.LocalDateTime;
importjava.time.format.DateTimeFormatter;
importjava.util.*;

publicclassEmployee_Dependent_Exception {

publicstaticvoid main(String[] args) {


try {
Scanner input=newScanner(System.in);
System.out.println("Enter number of employees: ");
intn=input.nextInt();
Employee2 e1[]=new Employee2[n];
for(inti=0;i<n;i++)
{
String t=input.nextLine();
System.out.println("Enter employee id: ");
String id=input.nextLine();
System.out.println("Enter employee designation: ");
String desig=input.nextLine();
e1[i]=new Employee2(id, desig);
}
System.out.println("Enter employee id to view details: ");
String eid=input.nextLine();
booleancheck=false;
for(inti=0;i<n;i++)
{
if(e1[i].empid.contentEquals(eid))
{
check=true;
System.out.println("The employee details are: ");
e1[i].display(e1[i]);
}
}
if(!check)
{
System.out.println("No such employee");
}

}
catch(InputMismatchExceptioni)
{
System.out.println("Invalid input");
}
catch(ArrayIndexOutOfBoundsExceptiona)
{
System.out.println(a.getMessage());
}
catch(NullPointerExceptionn)
{
System.out.println(n.getMessage());
}
}

}
class Employee2{
protected String empid;
private String designation;
privatedoublesalary;
Dependent2 d;
public Employee2(String id, String desig)
{
try {
empid=id;
designation=desig;
if(designation.contentEquals("Professor"))
{
floatx=(float)130/100;
salary=x*150000;
d=newDependent2();
}
elseif(designation.contentEquals("Associate Professor"))
{
floatx=(float)120/100;
salary=x*120000;
d=newDependent2();
}
elseif(designation.contentEquals("Assistant Professor"))
{
floatx=(float)110/100;
salary=x*100000;
d=newDependent2();
}
elseif(designation.contentEquals("Teaching Research
Assistant"))
{
salary=20000;
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
publicvoid display(Employee2 e)
{
System.out.println(empid+" "+designation+" "+salary);
if(!designation.contentEquals("Teaching Research Assistant"))
{
d.display(d);
}
}
}
class Dependent2{
private String dname;
privatelongdpno;
private String ddob;

public Dependent2()
{
try {
Scanner input=newScanner(System.in);
System.out.println("Enter dependent name: ");
dname=input.nextLine();
System.out.println("Enter dependent phone number: ");
dpno=input.nextLong();
String t1=input.nextLine();
System.out.println("Enter dependent date of birth: ");
String date=input.nextLine();
// SimpleDateFormat format = new SimpleDateFormat("dd-mm-yyyy");
// Date iDOB1 = format.parse(date);
LocalDateTimedatetime = LocalDateTime.parse(date,
DateTimeFormatter.ofPattern("yyyy-MM-ddHH:mm:ss.S"));
String iDOB=datetime.format(DateTimeFormatter.ofPattern("yyyy-
MM-dd"));
ddob=iDOB;
}
catch(InputMismatchExceptioni)
{
System.out.println("Invalid input");
}
}
publicvoid display(Dependent2 d)
{
System.out.println(dname+" "+dpno+" "+ddob);
}
}

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. Surface Area of Sphere is 4πr2 where r is the
radius.]. Apply the concepts of Dynamic Polymorphism to achieve the results.

Code:
Package Practice;

importjava.util.*;

publicclass Shapes_Dynamic_Polymorphism_2 {

publicstaticvoid main(String[] args) {


Scanner input=newScanner(System.in);
Shape2 s[]=new Shape2[4];
try {
System.out.println("Enter radius of circle: ");
intradius=input.nextInt();
System.out.println("Enter side of square: ");
intside=input.nextInt();
System.out.println("Enter side of cube: ");
intcside=input.nextInt();
System.out.println("Enter radius of sphere: ");
intsradius=input.nextInt();
s[0]=new Circle2(radius);
s[1]=new Square2(side);
s[2]=new Cube2(cside);
s[3]=new Sphere2(sradius);
}
catch(InputMismatchExceptioni){
System.out.println("Invalid input");
}
try {
for(inti=0;i<4;i++)
{
if(s[i] instanceof TwoDimensionalShape2)
{
TwoDimensionalShape2 twod =
(TwoDimensionalShape2) s[i];
twod.computearea();
twod.display_area();
}
elseif(s[i] instanceof ThreeDimensionalShape2)
{
ThreeDimensionalShape2 threed =
(ThreeDimensionalShape2) s[i];
threed.computearea();
threed.display_area();
}
}
}
catch(ArrayIndexOutOfBoundsExceptiona)
{
System.out.println(a.getMessage());
}
catch(NullPointerExceptionn)
{
System.out.println(n.getMessage());
}
}

}
abstractclass Shape2{
doublearea;
publicabstractvoiddisplay_area();
}
abstractclass TwoDimensionalShape2 extends Shape2{

@Override
publicvoiddisplay_area() {
System.out.println("The area of two dimensional shape is: "+area);
}
publicabstractvoidcomputearea();

}
abstractclass ThreeDimensionalShape2 extendsShape2{

@Override
publicvoiddisplay_area() {
System.out.println("The surface area of three dimensional shape is:
"+area);
}
publicabstractvoidcomputearea();
}
class Circle2 extends TwoDimensionalShape2{
intradius;
public Circle2(intr)
{
radius=r;
}
@Override
publicvoidcomputearea() {
area=3.14*radius*radius;
System.out.println("Circle");
}
}
class Square2 extends TwoDimensionalShape2{
intside;
public Square2(inta)
{
side=a;
}
@Override
publicvoidcomputearea() {
area=side*side;
System.out.println("Square");
}
}
class Cube2 extends ThreeDimensionalShape2{
inta;
public Cube2(intside)
{
a=side;
}
@Override
publicvoidcomputearea() {
area=6*a*a;
System.out.println("Cube");
}
}
class Sphere2 extends ThreeDimensionalShape2{
intr;
public Sphere2(intradius)
{
r=radius;
}
@Override
publicvoidcomputearea() {
area=4*3.14*r*r;
System.out.println("Sphere");
}
}

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.

Code:
Package Practice;
importjava.util.*;
publicclassProfessor_marks {

publicstaticvoid main(String[] args) {


Scanner input=new Scanner(System.in);
Professor2 p[]=new Professor2[3];
try {
for(inti=0;i<3;i++)
{
System.out.println("Enter name: ");
String Name=input.nextLine();
System.out.println("Enter number of subject marks to be
entered: ");
intN=input.nextInt();
intMarks[]=newint[N];
for(intj=0;j<N;j++)
{
System.out.println("Enter marks for subject "+
(j+1)+": ");
Marks[j]=input.nextInt();
if(Marks[j]<0)
{
thrownewMarksException("Cannot enter marks
below 0");
}
elseif(Marks[j]>100)
{
thrownewMarksException("Cannot enter marks
above 100");
}
}
p[i]=new Professor2(Name, N, Marks);
String t=input.nextLine();
}
}
catch(InputMismatchExceptioni) {
System.out.println("Invalid input");
}
catch(ArrayIndexOutOfBoundsExceptiona)
{
System.out.println(a.getMessage());
}
catch(NullPointerExceptionn)
{
System.out.println(n.getMessage());
}
catch(MarksExceptionm)
{
System.out.println(m.getMessage());
}

for(inti=0;i<3;i++)
{
System.out.println();
p[i].display(p[i]);
}
}

}
class Professor2{
private String name;
intn;
intmarks[];

public Professor2(String Name, intN, intMarks[])


{
name=Name;
n=N;
marks=newint[n];
for(inti=0;i<n;i++)
{
marks[i]=Marks[i];
}
}

publicvoid display(Professor2 p)
{
System.out.println(p.name);
for(inti=0;i<p.n;i++)
{
System.out.print(p.marks[i]+" ");
}
}
}

classMarksExceptionextends Exception{
publicMarksException(String input)
{
super(input);
}
}

You might also like