Java 21 J22 J23
Java 21 J22 J23
Q1) Q1) Define a class MyDate(Day, Month, year) with methods to accept and display a
MyDateobject. Accept date as dd,mm,yyyy. Throw user defined exception "InvalidDateException" if
the date is invalid.
Ans_
super(message);
class MyDate {
throw new InvalidDateException("Invalid Date: " + day + "/" + month + "/" + year);
this.day = day;
this.month = month;
this.year = year;
return true;
}
// Method to get the number of days in a month, considering leap years
int[] days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
try {
MyDate date = new MyDate(29, 2, 2023); // Invalid date (not a leap year)
date.displayDate();
} catch (InvalidDateException e) {
System.out.println(e.getMessage());
}
Q2)
Ans
class Employee {
private static int count = 0; // Static variable to keep track of object count
// Default constructor
public Employee() {
// Parameterized constructor
this.id = id;
this.name = name;
this.deptName = deptName;
this.salary = salary;
}
// Method to display the contents of the object
System.out.println("ID: " + id + ", Name: " + name + ", Dept: " + deptName + ", Salary: " + salary);
e1.displayEmployee();
e2.displayEmployee();
e3.displayEmployee();
}
Slip -22
Q1) Q1) Write a program to create an abstract class named Shape that contains two integers and an
empty method named printArea(). Provide three classes named Rectangle, Triangle and Circle such
that each one of the classes extends the class Shape. Each one of the classes contain only the
method printArea() that prints the area of the given shape. (use method overriding).
Ans
this.dim1 = length;
this.dim2 = breadth;
@Override
void printArea() {
this.dim1 = base;
this.dim2 = height;
@Override
void printArea() {
System.out.println("Triangle Area: " + (0.5 * dim1 * dim2));
Circle(int radius) {
this.radius = radius;
@Override
void printArea() {
rect.printArea();
tri.printArea();
cir.printArea();
}
Q2) Write a program that handles all mouse events and shows the event name at the center of the
Window, red in color when a mouse event is fired. (Use adapter classes)
Ans
Slip -23
Q1) Q1) Define a class MyNumber having one private int data member. Write a default constructor to
initialize it to 0 and another constructor to initialize it to a value (Use this). Write methods isNegative,
isPositive, isZero, isOdd, isEven. Create an object in main.Use command line arguments to pass a
value to the Object
Ans
class MyNumber {
public MyNumber() {
}
// Method to check if the number is zero
return number == 0;
if (args.length == 0) {
int inputNumber;
try {
} catch (NumberFormatException e) {
Q2) Write a simple currency converter, as shown in the figure. User can enter the amount of
"Singapore Dollars", "US Dollars", or "Euros", in floating-point number. The converted values shall be
displayed to 2 decimal places. Assume that 1 USD = 1.41 SGD, 1 USD = 0.92 Euro, 1 SGD = 0.65 Euro
Ans ->
import java.util.Scanner;
System.out.println("Currency Converter");
switch (choice) {
break;
break;
break;
default:
Out put:-
Currency Converter
2. US Dollars (USD)
3. Euros (EUR)