java ut1
java ut1
while (number != 0) {
int digit = number % 10;
sum += Math.pow(digit, digits);
number /= 10;
}
if (sum == originalNumber) {
System.out.println(originalNumber + " is an Armstrong number.");
} else {
System.out.println(originalNumber + " is not an Armstrong number.");
}
}
}
Q4) write the use of/ purpose of this keyword, sta c keyword and
final keyword
this keyword:
Purpose: The this keyword is used to refer to the current instance of a class. It is mainly used
in constructors and methods to dis nguish between class a ributes and parameters with the
same name. It can also be used to invoke other constructors in the same class.
sta c keyword:
Purpose: The sta c keyword is used to define class-level variables and methods, which
belong to the class itself rather than to any specific instance of the class. Sta c members are
shared among all instances of the class.
final keyword:
Purpose: The final keyword is used to define constants, prevent method overriding,
and prevent inheritance. It can be applied to variables, methods, and classes.
In single inheritance, a class (child) inherits from only one parent (superclass).
This is the simplest form of inheritance, providing a straightforward way to extend the
functionality of an existing class.
For instance, if Dog is a subclass of Animal, the Dog class can access the methods
and properties of the Animal class.
Multilevel Inheritance:
package packageName;
Example
package com.example.myapp;
Interfaces:
Definition: An interface is a reference type in Java, similar to a class, that can contain only
abstract methods (methods without a body) and static constants. Interfaces are used to
achieve abstraction and multiple inheritance in Java.
Syntax
interface InterfaceName {
// Abstract method
void methodName();
Example
interface Animal {
void eat();
void sleep();
@Override
System.out.println("Dog is eating.");
}
@Override
System.out.println("Dog is sleeping.");
dog.eat();
dog.sleep();
scanner.close();
if (number <= 1) {
isPrime = false;
} else {
if (number % i == 0) {
isPrime = false;
break;
if (isPrime) {
} else {
4 Marks :-
Let's break down the key components of exception handling and also explain the concepts of
multithreading and multitasking in the context of Java.
. try Block:
The try block is used to enclose the code that might throw an exception. The code within this block is
monitored for exceptions, and if an exception occurs, it is passed to the catch block for handling.
catch Block:
The catch block follows the try block and is used to handle exceptions. If an exception is thrown
inside the try block, the flow of control is transferred to the catch block. Multiple catch blocks can be
used to handle different types of exceptions.
throw Keyword:
The throw keyword is used to explicitly throw an exception from a method or a block of code. It is
used when you want to throw a custom exception or an exception manually based on a condition.
throws Keyword:
The throws keyword is used in method declarations to specify which exceptions may be thrown by
that method. It is used when a method might throw an exception, but the method itself doesn't
handle it.
finally Block:
The finally block is optional and contains code that will be executed regardless of whether an
exception occurs or not. It is commonly used for cleanup activities such as closing database
connections, file streams, etc.
Multithreading is a concept where multiple threads (small units of a process) are executed
concurrently. It allows for performing multiple operations simultaneously, improving the
performance and responsiveness of applications.
In multithreading, exception handling becomes more critical because each thread operates
independently. If an exception occurs in one thread, it may affect other threads or the overall
program.
Multitasking refers to the ability of a CPU to execute multiple tasks or processes at the same time. In
Java, multitasking can be achieved using multithreading where multiple threads are used to perform
tasks concurrently.
Java's multitasking capabilities allow for efficient resource utilization, where tasks that are
independent of each other can run simultaneously.
if (!password.equals("msbte")) {
} else {
System.out.println("Password accepted!");
try {
String userPassword = "somepassword"; // You can replace this with input from the user
checkPassword(userPassword);
} catch (NoMatchException e) {
NoMatchException class: This is a custom exception that extends the Exception class. The
constructor takes a message as an argument and passes it to the superclass Exception constructor.
checkPassword method: This method checks if the provided password equals "msbte". If it
doesn't, the NoMatchException is thrown with a specific error message.
main method: In this method, we simulate checking the password by calling checkPassword. If the
password doesn't match "msbte", the NoMatchException is thrown and caught in the catch block,
where the error message is printed.
Q3) Define thread mention two ways to create a thread and give
syntax of notify, slip, run, and start method
n Java, a thread is a lightweight process that allows concurrent execution of code. You can create a
thread in Java in two main ways:
You can create a new class that extends the Thread class and override its run() method
You can create a class that implements the Runnable interface and pass it to a Thread
object.
1. start() Method:
o The start() method is used to begin the execution of a thread. It invokes the run()
method in a separate call stack.
Syntax
thread.start();
2. run() Method:
o The run() method is where the code to be executed by the thread is written. It
contains the logic that will run in the separate thread.
Syntax:
@Override
3. notify() Method:
o The notify() method is used to wake up a single thread that is waiting on the object's
monitor (lock). It should be called from a synchronized block or method.
Syntax:
synchronized (object) {
object.notify();
4. sleep() Method:
o The sleep() method is used to pause the execution of the current thread for a
specified amount of time (in milliseconds).
Syntax:
1. Create a Package:
o A package is created using the package keyword at the beginning of a Java file. The
classes in the package should be placed in a directory corresponding to the package
name.
2. Import the Package:
o After creating the package, you can import the package into another class using the
import statement.
Example
Polymorphism means "many forms". It allows a single entity to represent different types or
forms. In Java, polymorphism is achieved in two primary ways:
Method Overloading (Compile-time polymorphism)
Method Overriding (Runtime polymorphism)
2. Inheritance:
Inheritance is a mechanism where a new class inherits the properties (fields) and
behaviors (methods) of an existing class. The new class is called the subclass or child
class, and the class from which it inherits is called the superclass or parent class.
3. Abstraction:
Abstraction is the concept of hiding the implementation details and showing only the
essential features of an object. It simplifies complex systems by focusing on the high-level
operations. In Java, abstraction is achieved using:
Abstract classes: Classes that cannot be instantiated on their own and may contain
abstract methods (methods without implementation).
Interfaces: A collection of abstract methods that a class can implement.
4. Encapsulation:
Encapsulation is the technique of bundling the data (variables) and the methods that operate
on the data into a single unit, called a class. It also restricts access to some of the object's
components by using access modifiers like private, protected, and public.
Simple Secure
Object-Oriented Distributed
Basic_sal() Display()
Class :- Gross_salary
Total_sal()
Solution:
1. Interface Salary:
The Salary interface will define the method Basic_sal() that needs to be implemented by any class that
implements this interface.
2. Class ExEmployee:
The ExEmployee class will implement the Salary interface. It will include the basic details like name
and age and the method Basic_sal() to return the basic salary. It will also have the display() method to
print the details.
3. Class GrossSalary:
The GrossSalary class will use the ExEmployee class and add allowances (TA, DA, HRA) to
calculate the total salary.
Example:-
interface Salary {
double Basic_sal();
// Instance variables
String name;
int age;
this.name = name;
this.age = age;
}
// Implement Basic_sal() method from Salary interface
@Override
public GrossSalary(String name, int age, double TA, double DA, double HRA) {
this.TA = TA;
this.DA = DA;
this.HRA = HRA;
employee.display();