Java Questions and Code
Java Questions and Code
Digital Assignment-8
Code:
Package Practice;
importjava.util.Scanner;
}
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);
}
}
Code:
Package Practice;
importjava.util.*;
publicclass Student_Information {
}
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());
}
}
}
importjava.time.LocalDateTime;
importjava.time.format.DateTimeFormatter;
importjava.util.*;
publicclassEmployee_Dependent_Exception {
}
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);
}
}
Code:
Package Practice;
importjava.util.*;
publicclass Shapes_Dynamic_Polymorphism_2 {
}
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 {
for(inti=0;i<3;i++)
{
System.out.println();
p[i].display(p[i]);
}
}
}
class Professor2{
private String name;
intn;
intmarks[];
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);
}
}