Java Programming Solutions - Remaining
11. WAP to check whether a given number is Armstrong or not.
import [Link];
public class ArmstrongNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number: ");
int number = [Link]();
int originalNumber = number, result = 0;
while (number != 0) {
int digit = number % 10;
result += digit * digit * digit;
number /= 10;
if (result == originalNumber) {
[Link](originalNumber + " is an Armstrong number.");
} else {
[Link](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() {
[Link]("Drawing Circle");
public class InterfaceImplementation {
public static void main(String[] args) {
Drawable obj = new Circle();
[Link]();
13. WAP to create a package Calc with methods Sum() & Sub() and show their
implementation in a class.
// In file [Link] (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 [Link];
public class ReverseNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number: ");
int number = [Link]();
int reverse = 0;
while (number != 0) {
int digit = number % 10;
reverse = reverse * 10 + digit;
number /= 10;
}
[Link]("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};
[Link](arr[5]); // Will cause ArrayIndexOutOfBoundsException
} catch (Exception e) {
[Link]("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 {
[Link]("Eligible for voting.");
}
public static void main(String[] args) {
try {
validateAge(16);
} catch (ArithmeticException e) {
[Link]("Exception caught: " + e);
17. WAP to print table of a given number.
import [Link];
public class MultiplicationTable {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number: ");
int number = [Link]();
for (int i = 1; i <= 10; i++) {
[Link](number + " * " + i + " = " + (number * i));
}
}
18. WAP to Create a Login Page using an applet.
import [Link].*;
import [Link].*;
import [Link].*;
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);
[Link]('*');
loginButton = new Button("Login");
message = new Label();
add(userLabel);
add(username);
add(passLabel);
add(password);
add(loginButton);
add(message);
[Link](this);
public void actionPerformed(ActionEvent e) {
String user = [Link]();
String pass = [Link]();
if ([Link]("admin") && [Link]("password")) {
[Link]("Login Successful!");
} else {
[Link]("Invalid Username or Password.");
19. WAP to show working of Vector class in java.
import [Link];
public class VectorExample {
public static void main(String[] args) {
Vector<String> vector = new Vector<>();
[Link]("Apple");
[Link]("Banana");
[Link]("Cherry");
[Link]("Vector Elements: " + vector);
[Link]("Banana");
[Link]("After Removal: " + vector);
[Link]("Element at index 1: " + [Link](1));
20. WAP to arrange components using border layout.
import [Link].*;
import [Link].*;
public class BorderLayoutExample extends JFrame {
public BorderLayoutExample() {
setLayout(new BorderLayout());
add(new JButton("North"), [Link]);
add(new JButton("South"), [Link]);
add(new JButton("East"), [Link]);
add(new JButton("West"), [Link]);
add(new JButton("Center"), [Link]);
setSize(300, 200);
setVisible(true);
}
public static void main(String[] args) {
new BorderLayoutExample();