Repository navigation
Starts from source: Firmgate
Move the gear shift into drive position
A car starts to move
Approach to the destination: AUST
Push on a brake pedal
The car is stopped
Starts from source: Firmgate
Move the gear shift into drive position
A car starts to move
Approach to the destination: AUST
Push on a brake pedal
The car is stopped
Starts from source: Firmgate
Move the gear shift into drive position
A car starts to move
Approach to the destination: AUST
Push on a brake pedal
The car is stopped
import java.util.Scanner;
// Custom exception for invalid names
class InvalidNameException extends Exception {
public InvalidNameException(String message) {
super(message);
}
}
// Custom exception for invalid email
class InvalidEmailException extends Exception {
public InvalidEmailException(String message) {
super(message);
}
}
// Custom exception for invalid mobile number
class InvalidMobileNumberException extends Exception {
public InvalidMobileNumberException(String message) {
super(message);
}
}
// Student class
class Student {
String firstName;
String lastName;
int age;
String email;
String mobile;
// Constructor
public Student(String firstName, String lastName, int age, String email, String mobile)
throws InvalidNameException, InvalidEmailException, InvalidMobileNumberException {
if (firstName.contains(" ") || lastName.contains(" ")) {
throw new InvalidNameException("First name or last name should not contain spaces.");
}
if (!email.endsWith("@aust.edu")) {
throw new InvalidEmailException("Email must end with '@aust.edu'.");
}
if (!mobile.startsWith("+8801")) {
throw new InvalidMobileNumberException("Mobile number must start with '+8801'.");
}
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.email = email;
this.mobile = mobile;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
// Taking user input for student details
System.out.print("Enter First Name: ");
String firstName = scanner.nextLine();
System.out.print("Enter Last Name: ");
String lastName = scanner.nextLine();
System.out.print("Enter Age: ");
int age = scanner.nextInt();
scanner.nextLine(); // consume the newline
System.out.print("Enter Email: ");
String email = scanner.nextLine();
System.out.print("Enter Mobile: ");
String mobile = scanner.nextLine();
// Create a Student object with the input data
Student student = new Student(firstName, lastName, age, email, mobile);
System.out.println("Student information successfully recorded!");
} catch (InvalidNameException | InvalidEmailException | InvalidMobileNumberException e) {
System.out.println("Error: " + e.getMessage());
} finally {
scanner.close();
}
}
}
'
// Base class Fan
abstract class Fan {
String brand;
int speed;
public Fan(String brand) {
this.brand = brand;
}
// Common method for setting speed
public void setSpeed(int speed) {
this.speed = speed;
System.out.println(brand + " fan speed set to " + speed);
}
// Abstract method to be overridden by subclasses
public abstract void displayFanType();
}
// CeilingFan class
class CeilingFan extends Fan {
public CeilingFan(String brand) {
super(brand);
}
@Override
public void displayFanType() {
System.out.println("This is a ceiling fan.");
}
}
// StandFan class
class StandFan extends Fan {
boolean hasOscillation;
public StandFan(String brand, boolean hasOscillation) {
super(brand);
this.hasOscillation = hasOscillation;
}
@Override
public void displayFanType() {
System.out.println("This is a stand fan with oscillation: " + hasOscillation);
}
}
// DeskFan class
class DeskFan extends Fan {
public DeskFan(String brand) {
super(brand);
}
@Override
public void displayFanType() {
System.out.println("This is a desk fan.");
}
}
// TowerFan class
class TowerFan extends Fan {
public TowerFan(String brand) {
super(brand);
}
@Override
public void displayFanType() {
System.out.println("This is a tower fan.");
}
}
// PedestalFan class
class PedestalFan extends StandFan {
public PedestalFan(String brand, boolean hasOscillation) {
super(brand, hasOscillation);
}
@Override
public void displayFanType() {
System.out.println("This is a pedestal fan.");
}
}
// Example usage
public class FanSystem {
public static void main(String[] args) {
Fan ceilingFan = new CeilingFan("XYZ");
ceilingFan.setSpeed(3);
ceilingFan.displayFanType();
Fan standFan = new StandFan("ABC", true);
standFan.setSpeed(2);
standFan.displayFanType();
}
}
Setter methods are not necessary if a class already has a constructor.
No, I disagree with the statement. Setter methods are still necessary even if a class has a constructor. A constructor initializes the object when it is created, but setter methods are useful for modifying object properties after instiation. This provides flexibility for changing values at runtime.
public class Person {
private String name;
private int age;
// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Setter method
public void setAge(int age) {
this.age = age;
}
}
in the above example, the setAge() method can be used to modify the age of a Person object after it has been created, which is not possible with just a constructor.
Describe a scenario where the unreachable code error will happen while handling exceptions, how is it possible to solve?
An unreachable code error occurs when there is code that will never be executed such as code placed after a return, throw or within a try block when there is a finally block with a return.
public int divide(int a, int b) {
try {
return a / b;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
return 0;
}
System.out.println("This code is unreachable"); // Unreachable code
}
in the above code, the statement after the try-catch block is unreachable because tehre is a return statement inside the catch block.
Solution: To solve this, ensure there is no code after a return or a similiar statement, or restructure the code so that it can be executed in all scenerios.
Describe a scenario where the join method is required to be used.
The join method is used when one thread needs to wait for the completion of another thread before proceeding.
Thread thread1 = new Thread(() -> {
System.out.println("Thread 1 is running");
});
Thread thread2 = new Thread(() -> {
try {
thread1.join(); // Ensure thread1 completes before running thread2
System.out.println("Thread 2 is running");
} catch (InterruptedException e) {
e.printStackTrace();
}
});
thread1.start();
thread2.start();
How does encapsulation ensure the security of a class, explain with example?
Encapsulation ensures the security of a class by restricting direct access to its data and providing controlled ways to modify it using access modifiers(private, public etc)
public class BankAccount {
private double balance;
// Constructor
public BankAccount(double initialBalance) {
balance = initialBalance;
}
// Getter method
public double getBalance() {
return balance;
}
// Setter method
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
}
in the BankAccount example, the balance variable is private, ensuring that it cannot be directly accessed or modified by external classes. Instead, the deposit method provides controlled access, allowing only valid deposits.
Consider the following piece of code and assume the student is a class having a proper constructors and Boolean equals(Object o) method that is overridden to compare the equalities of the values of two student objects.
Student s1 = new Student("Abdul Karim", "02-11-2003", "20200204003");
Student s2 = new Student(p1);
p1.equals(p2): This expression will evaluate to true if the equals() method is overridden in the Student class to compare the values of the fields of Student objects. It compares the content/ attributes of p1 and p2. Since s2 was initialized with s1, assuming equals() compares relevant fields correctly, p1.equals(p2) will return true.
p1==p2: This expression will evaluate to false because == compares references, not values. p1 and p2 are two different objects, even if they have the same content there fore their memory addresses are different, resulting in false.
// Base Phone class
abstract class Phone {
String callerName;
public Phone(String callerName) {
this.callerName = callerName;
}
abstract void receiveCall();
}
// Feature Phone class
class FeaturePhone extends Phone {
public FeaturePhone(String callerName) {
super(callerName);
}
@Override
void receiveCall() {
System.out.println("Receiving call from: " + callerName);
}
}
// Smartphone class
class Smartphone extends Phone {
String callerPicture;
public Smartphone(String callerName, String callerPicture) {
super(callerName);
this.callerPicture = callerPicture;
}
@Override
void receiveCall() {
System.out.println("Receiving call from: " + callerName);
System.out.println("Displaying picture: " + callerPicture);
}
}
// Main class to demonstrate functionality
public class PhoneDemo {
public static void main(String[] args) {
FeaturePhone featurePhone = new FeaturePhone("John Doe");
featurePhone.receiveCall();
Smartphone smartphone = new Smartphone("Jane Doe", "JanePicture.jpg");
smartphone.receiveCall();
}
}