Java Programming Solutions - Remaining
11. WAP to check whether a given number is Armstrong or not.
import java.util.Scanner;
public class ArmstrongNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
int originalNumber = number, result = 0;
while (number != 0) {
int digit = number % 10;
result += digit * digit * digit;
number /= 10;
if (result == originalNumber) {
System.out.println(originalNumber + " is an Armstrong number.");
} else {
System.out.println(originalNumber + " is not an Armstrong number.");
}
12. WAP to create an interface and show its implementation in a class.
interface Drawable {
void draw();
class Circle implements Drawable {
public void draw() {
System.out.println("Drawing Circle");
public class InterfaceImplementation {
public static void main(String[] args) {
Drawable obj = new Circle();
obj.draw();
13. WAP to create a package Calc with methods Sum() & Sub() and show their
implementation in a class.
// In file Calc.java (in Calc package)
package Calc;
public class Calculator {
public int sum(int a, int b) {
return a + b;
public int sub(int a, int b) {
return a - b;
14. WAP to print reverse of a given number.
import java.util.Scanner;
public class ReverseNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
int reverse = 0;
while (number != 0) {
int digit = number % 10;
reverse = reverse * 10 + digit;
number /= 10;
}
System.out.println("Reversed number: " + reverse);
15. WAP to show the working of a single Catch block.
public class SingleCatchExample {
public static void main(String[] args) {
try {
int[] arr = {1, 2, 3};
System.out.println(arr[5]); // Will cause ArrayIndexOutOfBoundsException
} catch (Exception e) {
System.out.println("Exception caught: " + e);
16. WAP to show working of throw and throws for handling exceptions.
public class ThrowThrowsExample {
static void validateAge(int age) throws ArithmeticException {
if (age < 18) {
throw new ArithmeticException("Not eligible for voting.");
} else {
System.out.println("Eligible for voting.");
}
public static void main(String[] args) {
try {
validateAge(16);
} catch (ArithmeticException e) {
System.out.println("Exception caught: " + e);
17. WAP to print table of a given number.
import java.util.Scanner;
public class MultiplicationTable {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
for (int i = 1; i <= 10; i++) {
System.out.println(number + " * " + i + " = " + (number * i));
}
}
18. WAP to Create a Login Page using an applet.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class LoginApplet extends Applet implements ActionListener {
TextField username, password;
Button loginButton;
Label message;
public void init() {
Label userLabel = new Label("Username:");
Label passLabel = new Label("Password:");
username = new TextField(20);
password = new TextField(20);
password.setEchoChar('*');
loginButton = new Button("Login");
message = new Label();
add(userLabel);
add(username);
add(passLabel);
add(password);
add(loginButton);
add(message);
loginButton.addActionListener(this);
public void actionPerformed(ActionEvent e) {
String user = username.getText();
String pass = password.getText();
if (user.equals("admin") && pass.equals("password")) {
message.setText("Login Successful!");
} else {
message.setText("Invalid Username or Password.");
19. WAP to show working of Vector class in java.
import java.util.Vector;
public class VectorExample {
public static void main(String[] args) {
Vector<String> vector = new Vector<>();
vector.add("Apple");
vector.add("Banana");
vector.add("Cherry");
System.out.println("Vector Elements: " + vector);
vector.remove("Banana");
System.out.println("After Removal: " + vector);
System.out.println("Element at index 1: " + vector.get(1));
20. WAP to arrange components using border layout.
import java.awt.*;
import javax.swing.*;
public class BorderLayoutExample extends JFrame {
public BorderLayoutExample() {
setLayout(new BorderLayout());
add(new JButton("North"), BorderLayout.NORTH);
add(new JButton("South"), BorderLayout.SOUTH);
add(new JButton("East"), BorderLayout.EAST);
add(new JButton("West"), BorderLayout.WEST);
add(new JButton("Center"), BorderLayout.CENTER);
setSize(300, 200);
setVisible(true);
}
public static void main(String[] args) {
new BorderLayoutExample();