Java Solve
Java Solve
import java.util.*;
public class swap {
public static void main(String[] args) {
Scanner Sc=new Scanner(System.in);
System.out.println("Before Swapping enter two Number:");
int num1=Sc.nextInt();
int num2=Sc.nextInt();
num1=num1+num2;
num2=num1-num2;
num1=num1-num2;
System.out.println("After Swapping enter two Number:");
System.out.println(num1);
System.out.println(num2);
Sc.close();
}
}
Q2=>Write a program to perform arithmetic operations What are the commands used to
compile and run this JAVA Programs.
public class ArithmeticOperations {
public static void main(String[] args) {
int a = 10;
int b = 5;
// Addition
int sum = a + b;
System.out.println("Sum: " + sum);
// Subtraction
int difference = a - b;
System.out.println("Difference: " + difference);
// Multiplication
int product = a * b;
System.out.println("Product: " + product);
// Division
int quotient = a / b;
System.out.println("Quotient: " + quotient)
// Modulus
int remainder = a % b;
System.out.println("Remainder: " + remainder);
}
}
Q3=>
i)Write a Java Program to convert “32” to Primitive as well as Wrapper.
public class main {
public static void main(String[] args) {
// Convert string "32" to primitive int
int primitiveInt = Integer.parseInt("32");
System.out.println("Primitive int: " + primitiveInt);
@Override
public int compareTo(Student other) {
return this.getEmail().compareTo(other.getEmail());
}
@Override
public String toString() {
return "Student{" +
"studentId=" + studentId +
", studentName='" + studentName + '\'' +
", email='" + email + '\'' +
", phone='" + phone + '\'' +
'}';
}
}
System.out.println("Before sorting:");
for (Student student : students) {
System.out.println(student);
}
Collections.sort(students);
Q6=>Write a Java Program to store the following data, in the collection you feel will suite
best.
Name- Ram
Email- [email protected]
Phone:99887545445
import java.util.*;
Q8=> Write a Java program to write the following, class A with method m1( ) and m2( ) and
write a class B with methods m3( ) and m4( ), Override the methods of A in class B.
class A {
public void m1() {
System.out.println("Method m1() in class A");
}
class B extends A {
@Override
public void m1() {
System.out.println("Overridden method m1() in class B");
}
@Override
public void m2() {
System.out.println("Overridden method m2() in class B");
}
Q9=> Write a Java class for following methods display() -- Display number from 1 to 100
using while loop in Java fibonacci() -- Prints Fibonacci series till 100
public class NumberUtils {
public void display() {
int i = 1;
while (i <= 100) {
System.out.print(i + " ");
i++;
}
System.out.println();
}
Q10b=> Write a Java Program to find the number of String starting with „S‟ from following
TreeSet [ Smith, Alex , Tom, Steve, Mark, Sammy]
import java.util.TreeSet;
int count = 0;
for (String str : treeSet) {
if (str.startsWith("S")) {
count++;
}
}
Q12=> Write an abstract class Car with methods start() and stop(). Write a class Santro and
Audi and override the methods.
abstract class Car {
public abstract void start();
public abstract void stop();
}
@Override
public void stop() {
System.out.println("Santro is stopping...");
}
}
@Override
public void stop() {
System.out.println("Audi is stopping...");
}
}
Q13=> Write a program for using the switch statement to execute a particular task depending
on color value.
public class ColorTask {
public static void main(String[] args) {
String color = "red"; // Change this to test different colors
switch(color) {
case "red":
System.out.println("Stop!");
break;
case "yellow":
System.out.println("Slow down!");
break;
case "green":
System.out.println("Go!");
break;
default:
System.out.println("Invalid color");
}
}
}
Q14=> Write a class Automobile with default constructor, write a class Plane which extends
Automobile and has a default as well as parameterized constructor, write a class Airbus with
a default constructor which extends Plane
class Automobile {
public Automobile() {
System.out.println("Automobile constructor");
}
}
System.out.println("Creating an Airbus:");
Airbus airbus = new Airbus();
}
}
Q15=> Write a program to take a 2D array and display its elements in the form of a matrix.
public class MatrixDisplay {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
System.out.println("Matrix:");
displayMatrix(matrix);
}
Q16=> Write a class User with abstract methods pay() and receive(), later make two concrete
class GoldUser and SilverUser, override the abstract method.
abstract class User {
// Abstract methods
public abstract void pay();
public abstract void receive();
}
class GoldUser extends User {
@Override
public void pay() {
System.out.println("Gold user is making a payment.");
}
@Override
public void receive() {
System.out.println("Gold user is receiving a payment.");
}
}
class SilverUser extends User {
@Override
public void pay() {
System.out.println("Silver user is making a payment.");
}
@Override
public void receive() {
System.out.println("Silver user is receiving a payment.");
}
}
public class Main {
public static void main(String[] args) {
User goldUser = new GoldUser();
User silverUser = new SilverUser();
goldUser.pay();
goldUser.receive();
silverUser.pay();
silverUser.receive();
}
}
try {
checker.checkPalindrome(palindrome);
System.out.println(palindrome + " is a palindrome.");
} catch (StringNotPalindromeException e) {
System.out.println(e.getMessage());
}
try {
checker.checkPalindrome(notPalindrome);
System.out.println(notPalindrome + " is a palindrome.");
} catch (StringNotPalindromeException e) {
System.out.println(e.getMessage());
}
}
}
Q18=> Write a Java program for separate hours, minutes and seconds from following string
01:23:45 PM.
// Split the time string using ":" and " " as delimiters
String[] parts = timeString.split("[:\\s]");
// Interface ISO
interface ISO {
void radiation();
void sound();
}
@Override
public void sms() {
System.out.println("Sending SMS from iPhone");
}
@Override
public void radiation() {
System.out.println("Measuring radiation with iPhone");
}
@Override
public void sound() {
System.out.println("Adjusting sound settings with iPhone");
}
}
@Override
public void sms() {
System.out.println("Sending SMS from Galaxy");
}
@Override
public void radiation() {
System.out.println("Measuring radiation with Galaxy");
}
@Override
public void sound() {
System.out.println("Adjusting sound settings with Galaxy");
}
}
Q20=> Write a Java Program to find the minimum value in Vector [18,9,21,3,4].
import java.util.Vector;
vector.add(18);
vector.add(9);
vector.add(21);
vector.add(3);
vector.add(4);
int min = vector.get(0); // Assume the first element is the minimum initially
min = current;
21. Write an abstract class Car with methods start() and stop(). Write a class Santro and Audi and
override the methods.
CODE:
@Override
System.out.println("Santro started.");
}
@Override
System.out.println("Santro stopped.");
@Override
System.out.println("Audi started.");
@Override
System.out.println("Audi stopped.");
santro.start();
santro.stop();
audi.start();
audi.stop();
22. Write a Java Program to determine whether the number is prime or not
CODE:
import java.util.Scanner;
if (num <= 1) {
isPrime = false;
} else {
if (num % i == 0) {
isPrime = false;
break;
if (isPrime) {
} else {
scanner.close();
}
24. Write a Java Program to print following
CODE:
int rows = 5;
System.out.println();
25. Write a class MathDemo with methods square() with one parameter and add() with two
parameters. Call these methods to get the output.
CODE:
import java.util.Scanner;
System.out.println("Sum of " + num1 + " and " + num2 + " is: " + sum);
scanner.close();
26. Create an Exception StringNotPalindromeException. Write a class with method which throws
this Exception when String passed is not palindrome.
CODE:
super(message);
try {
checkPalindrome(palindrome);
checkPalindrome(notPalindrome);
} catch (StringNotPalindromeException e) {
if (!str.equals(reversed.toString())) {
27. Write a Java class to print the Fibonacci sequence till 100.
CODE:
printFibonacci();
}
int a = 0, b = 1, c;
a = b;
b = c;
29. Write a class mobile with methods call() and sms(). Write a class Demo and access it.
CODE:
import.java.util.scanner;
class Mobile
}
}
demobj.call()
demobj.sms()
30. Write a class Automobile with default constructor, write a class Plane which extends Automobile
and has a default as well as parameterized constructor, write a class Airbus with a default
constructor which extends Plane.
CODE:
// Automobile class
class Automobile {
// Default constructor
public Automobile() {
System.out.println("Automobile created.");
// Default constructor
public Plane() {
System.out.println("Plane created.");
}
// Parameterized constructor
// Default constructor
public Airbus() {
System.out.println("Airbus created.");
// Creating objects
31. Write a Java Program to determine whether the number is Armstrong or not
import java.util.Scanner;
public class ArmstrongChecker {
originalNum = num;
while (originalNum != 0) {
originalNum /= 10;
++n;
originalNum = num;
while (originalNum != 0) {
originalNum /= 10;
if (result == num) {
} else {
scanner.close();
}
32. Write a Java Program to make an Exception AgeException. When user passes some age and if
age is less than 18 throw this Exception
CODE:
super(message);
try {
validateAge(age);
System.out.println("Age is valid.");
} catch (AgeException e) {
CODE:
import java.util.Scanner;
scanner.close();
if (isPalindrome(number)) {
} else {
int reversedNumber = 0;
while (number != 0) {
number /= 10;
}
}
CODE: