Java Code Only Answers-1
Java Code Only Answers-1
1. Write a Java program to find out the even numbers from 1 to 100 using
for loop.
class EvenNumbers {
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
System.out.print(i + " ");
}
}
}
}
class BubbleSort {
public static void main(String[] args) {
int[] arr = {5,3,8,4,2};
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; j < arr.length-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
for (int num : arr) {
System.out.print(num + " ");
}
}
}
3. Write a program to check whether the given number is prime or not.
class PrimeCheck {
public static void main(String[] args) {
int num = 7;
boolean flag = false;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
flag = true;
break;
}
}
System.out.println(flag ? "Not Prime" : "Prime");
}
}
class Employee {
int empid;
String name;
double salary;
class ReverseNumber {
public static void main(String[] args) {
int num = 1234, rev = 0;
while (num != 0) {
rev = rev * 10 + num % 10;
num /= 10;
}
System.out.println(rev);
}
}
import java.util.Vector;
class VectorDemo {
public static void main(String[] args) {
Vector<Object> v = new Vector<>();
v.add(10);
v.add(20);
v.add("Hello");
v.add("World");
v.add(3.14f);
v.add(6.28f);
java.util.Scanner sc = new java.util.Scanner(System.in);
Object elem = sc.nextLine();
v.remove(elem);
for (Object o : v) {
System.out.print(o + " ");
}
}
}
7. Write a program to check whether the string provided by the user is a
palindrome or not.
class PalindromeCheck {
public static void main(String[] args) {
String str = "madam";
String rev = new StringBuilder(str).reverse().toString();
System.out.println(str.equals(rev) ? "Palindrome" : "Not Palindrome");
}
}
class ArmstrongNumbers {
public static void main(String[] args) {
for (int i = 0; i <= 999; i++) {
int sum = 0, temp = i, digits = String.valueOf(i).length();
while (temp > 0) {
sum += Math.pow(temp % 10, digits);
temp /= 10;
}
if (sum == i) System.out.print(i + " ");
}
}
}
9. Write a program to copy all elements of one array into another array.
class CopyArray {
public static void main(String[] args) {
int[] src = {1,2,3,4,5};
int[] dest = new int[src.length];
for (int i = 0; i < src.length; i++) {
dest[i] = src[i];
}
for (int num : dest) {
System.out.print(num + " ");
}
}
}
class ASCIIValue {
public static void main(String[] args) {
System.out.println((int)'9');
}
}
import java.util.Arrays;
class ArraySort {
public static void main(String[] args) {
int[] arr = {5,2,9,1,5};
Arrays.sort(arr);
for (int num : arr) {
System.out.print(num + " ");
}
}
}
class Box {
int length, width;
Box(Box b) {
this.length = b.length;
this.width = b.width;
}
Box(int l, int w) {
length = l;
width = w;
}
public static void main(String[] args) {
Box b1 = new Box(10,20);
Box b2 = new Box(b1);
System.out.println(b2.length + " " + b2.width);
}
}
13. Write a program to print the sum, difference, and product of two
complex numbers.
class Complex {
double real, imag;
Complex(double r, double i) {
real = r;
imag = i;
}
static Complex add(Complex c1, Complex c2) {
return new Complex(c1.real + c2.real, c1.imag + c2.imag);
}
static Complex subtract(Complex c1, Complex c2) {
return new Complex(c1.real - c2.real, c1.imag - c2.imag);
}
static Complex multiply(Complex c1, Complex c2) {
return new Complex(c1.real*c2.real - c1.imag*c2.imag, c1.real*c2.imag +
c1.imag*c2.real);
}
public static void main(String[] args) {
Complex c1 = new Complex(2,3);
Complex c2 = new Complex(4,5);
Complex sum = add(c1, c2);
Complex diff = subtract(c1, c2);
Complex prod = multiply(c1, c2);
System.out.printf("Sum: %.1f+%.1fi\n", sum.real, sum.imag);
System.out.printf("Difference: %.1f+%.1fi\n", diff.real, diff.imag);
System.out.printf("Product: %.1f+%.1fi\n", prod.real, prod.imag);
}
}
14. Write a program using sqrt() and pow() to calculate the square root and
power of a given number.
class MathOperations {
public static void main(String[] args) {
double num = 25;
System.out.println("Square root: " + Math.sqrt(num));
System.out.println("Power: " + Math.pow(num, 3));
}
}
15. Write a program to accept four numbers from the user using command
line arguments and print the smallest number.
class SmallestNumber {
public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = Integer.parseInt(args[2]);
int d = Integer.parseInt(args[3]);
int min = Math.min(Math.min(a,b), Math.min(c,d));
System.out.println("Smallest: " + min);
}
}
class ArmstrongCheck {
public static void main(String[] args) {
int num = 153;
int sum = 0, temp = num, digits = String.valueOf(num).length();
while (temp > 0) {
sum += Math.pow(temp % 10, digits);
temp /= 10;
}
System.out.println(sum == num ? "Armstrong" : "Not Armstrong");
}
}
class Book {
String author, title;
double price;
}
class BookInfo extends Book {
String stockposition;
void display() {
System.out.println(author + " " + title + " " + price + " " + stockposition);
}
public static void main(String[] args) {
BookInfo[] books = new BookInfo[3];
java.util.Scanner sc = new java.util.Scanner(System.in);
for (int i = 0; i < 3; i++) {
books[i] = new BookInfo();
books[i].author = sc.nextLine();
books[i].title = sc.nextLine();
books[i].price = sc.nextDouble();
sc.nextLine();
books[i].stockposition = sc.nextLine();
}
for (BookInfo b : books) {
b.display();
}
}
}
interface Allowance {
double DA_RATE = 0.9;
double HRA_RATE = 0.1;
double PF_RATE = 0.0833;
}
class Salary implements Allowance {
String empid, name;
double basicsalary;
void calculateNet() {
double net = basicsalary + (basicsalary * DA_RATE) + (basicsalary *
HRA_RATE) - (basicsalary * PF_RATE);
System.out.println("Net Salary: " + net);
}
public static void main(String[] args) {
Salary s = new Salary();
s.basicsalary = 50000;
s.calculateNet();
}
}
interface Exam {}
class Student {}
class Result extends Student implements Exam {}
interface Interest {
double RATE = 0.25;
double simpleInterest(double p, double t);
double compoundInterest(double p, double t);
}
class InterestCalc implements Interest {
public double simpleInterest(double p, double t) {
return p * RATE * t;
}
public double compoundInterest(double p, double t) {
return p * Math.pow(1 + RATE, t) - p;
}
}
interface Salary {}
class Employee {}
class GrossSalary extends Employee implements Salary {}
1. Write a Java program in which thread A will display the even numbers
between 1 to 50 and thread B will display the odd numbers between 1 to 50.
After 3 iterations, thread A should go to sleep for 500 ms.
class EvenOddThreads {
public static void main(String[] args) {
new Thread(() -> {
for (int i = 2; i <= 50; i += 2) {
System.out.println("A: " + i);
if (i % 6 == 0) {
try { Thread.sleep(500); } catch (InterruptedException e) {}
}
}
}).start();
new Thread(() -> {
for (int i = 1; i <= 49; i += 2) {
System.out.println("B: " + i);
}
}).start();
}
}
4. Write a program to print even and odd numbers using two threads with
a delay of 1000ms after each number.
class DelayedThreads {
public static void main(String[] args) {
new Thread(() -> {
for (int i = 0; i <= 10; i += 2) {
System.out.println("Even: " + i);
try { Thread.sleep(1000); } catch (InterruptedException e) {}
}
}).start();
new Thread(() -> {
for (int i = 1; i <= 9; i += 2) {
System.out.println("Odd: " + i);
try { Thread.sleep(1000); } catch (InterruptedException e) {}
}
}).start();
}
}
7. Write a program to create two threads. One thread will display the odd
numbers from 1 to 50, and the other thread will display the even numbers.
class OddEvenThreads {
public static void main(String[] args) {
new Thread(() -> {
for (int i = 1; i <= 49; i += 2) {
System.out.println("Odd: " + i);
}
}).start();
new Thread(() -> {
for (int i = 2; i <= 50; i += 2) {
System.out.println("Even: " + i);
}
}).start();
}
}
import javax.swing.*;
class ATMSimulation {
static double balance = 0;
public static void main(String[] args) {
while (true) {
String choice = JOptionPane.showInputDialog("1. Withdraw\n2.
Deposit\n3. Check Balance\n4. Exit");
switch (choice) {
case "1":
double amt =
Double.parseDouble(JOptionPane.showInputDialog("Enter amount"));
balance -= amt;
break;
case "2":
amt = Double.parseDouble(JOptionPane.showInputDialog("Enter
amount"));
balance += amt;
break;
case "3":
JOptionPane.showMessageDialog(null, "Balance: " + balance);
break;
case "4":
System.exit(0);
}
}
}
}