INDEX
[Link]. Program Name Remakes
1. Write a Java program for sum of two integer number
2. Write a Java program for taking input string and display it
3. Write a Java program for calculating factorial
4 Write a Java program for LinkkList and perform remove(),
RemoveFirst(),and removeLast(),methods
5. Write a Java program for ArrayList to add 6 different inputs
and display it
6. Write a Java program for threading using Thread class and
start() method
7. Write a Java program for ordering strings in alphabetical order
using Arrays and sort() method
8. Write a Java program for Stack and perform push() and pop()
methods
9. Write a Java program for fetching Ips of
[Link],[Link], and [Link]
10. Write a Java program for performing boxing and unboxing on
integer value
11. Write a Java program for ArrayList to store integer values and
manipulte integer list
12. Write a Java program for High School Marksheet to evaluate
percentage and final result
Program-1
# Write a java program for sum up two integer
numbers
import [Link];
public class AddTwoIntegers {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first integer: ");
int num1 = [Link]();
[Link]("Enter the second integer: ");
int num2 = [Link]();
int sum = num1 + num2;
[Link]("The sum of " + num1 + " and " +
num2 + " is " + sum);
[Link]();
}
}
OUTPUT:-
Program-2
#Write a java program for taking input string and
display it
import [Link];
public class GetName {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter your full name: ");
String fullName = [Link](); // Read the
entire line as a string
[Link]("Hello, " + fullName + "!");
[Link]();
}
}
OUTPUT:-
Program-3
# write a java program for calculating
factorial
import [Link];
public class FactorialCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a non-negative integer to calculate
its factorial: ");
int number = [Link]();
if (number < 0) {
[Link]("Factorial is undefined for negative
numbers.");
} else {
long factorial = calculateFactorial(number);
[Link](number + "! = " + factorial);
}
[Link]();
}
public static long calculateFactorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
long result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}}}
OUTPUT:-
Program-4
# write a java program for LinkdList and perform
reomve(), removeFirst(), and removeLast() methods
import [Link].*;
public class LL {
public static void main(String args[])
{
LinkedList<String> ll = new LinkedList<String>();
[Link]("A");
[Link]("B");
[Link]("C");
[Link]("D");
[Link](2, "E");
[Link](ll);
[Link]("B");
[Link](3);
[Link]();
[Link]();
[Link](ll);
}
}
OUTPUT:-
Program-5
# Write a java program for ArrayList to add 6
different inputs and display it
import [Link];
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<String> myList = new ArrayList<>();
[Link]("MOTOROLA G32");
[Link]("MOTOROLA G52");
[Link]("MOTOROLA G62");
[Link]("MOTOROLA G72");
[Link]("MOTOROLA G82");
[Link]("Contents of the
ArrayList:");
for (String item : myList) {
[Link](item);
}
}
}
OUTPUT:-
Program- 6
# Write a java program for threading using Thread
class and start() method
class A extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
[Link]("\tFrom Thread A: i = " + i);
}
[Link]("Exit from class A");
}
}
class B extends Thread {
public void run() {
for (int j = 1; j <= 5; j++) {
[Link]("\tFrom Thread B: j = " + j);
}
[Link]("Exit from class B");
}
}
class C extends Thread {
public void run() {
for (int k = 1; k <= 5; k++) {
[Link]("\tFrom Thread C: k = " + k);
}
[Link]("Exit from class C");
}
}
class MyThreadTesting {
public static void main(String[] args) {
A threadA = new A();
B threadB = new B();
C threadC = new C();
[Link]();
[Link]();
[Link]();
}
}
Program-7
#Write a Java program for ordering strings in
alphabetical order using Arrays and sort() method
import [Link];
public class AlphabeticalOrder {
public static void main(String[] args) {
String[] cities = {"JAUNPUR", "VARANASI", "SULTANPUR",
"AGRA", "NOIDA", "MATHURA", "LUCKNOW"};
[Link](cities);
[Link]("Cities in alphabetical order:");
for (String city : cities) {
[Link](city);
}
}
}
OUTPUT:-
Program-8
# Write a Java program for Stack and perform push()
and pop() methods
import [Link];
public class StackExample {
public static void main(String[] args) {
Stack<String> stack = new Stack<>();
[Link]("Element 1");
[Link]("Element 2");
[Link]("Element 3");
[Link]("Element 4");
[Link]("Element 5");
[Link]("Element 6");
[Link]("Element 7");
[Link]("Element 8");
[Link]("Element 9");
[Link]("Element 10");
[Link]("Stack elements:");
for (String element : stack) {
[Link](element);
}
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]("Stack elements after popping:");
for (String element : stack) {
[Link](element);
}
[Link]("Is stack empty? " + [Link]());
[Link]("Size of the stack: " + [Link]());
}
}
OUTPUT:-
Program-9
# Write a Java program for fetching IPs of [Link],
[Link], and [Link]
import [Link];
import [Link];
public class IPAddressFetcher {
public static void main(String[] args) {
fetchIPAddress("[Link]");
fetchIPAddress("[Link]");
fetchIPAddress("[Link]");
}
private static void fetchIPAddress(String hostName) {
try {
InetAddress[] addresses =
[Link](hostName);
[Link]("IP addresses for " + hostName + ":");
for (InetAddress address : addresses) {
[Link]([Link]());
}
[Link]();
} catch (UnknownHostException e) {
[Link]("Unable to find IP address for " +
hostName);
[Link]();
}
}
}
OUTPUT:-
Program-10
# Write a Java program for performing boxing and unboxing on
integer value
import [Link];
public class BoxingUnboxingExample {
public static void main(String[] args) {
Stack<Integer> integerStack = new Stack<>();
// Boxing (converting int to Integer) and pushing onto the stack
int primitiveValue = 42;
Integer boxedValue = primitiveValue; // Autoboxing
[Link](boxedValue);
// Unboxing (converting Integer to int) and popping from the stack
Integer poppedValue = [Link]();
int unboxedValue = poppedValue; // Autounboxing
[Link]("Original Primitive Value: " + primitiveValue);
[Link]("Boxed Value on Stack: " + boxedValue);
[Link]("Popped Value from Stack: " + poppedValue);
[Link]("Unboxed Value: " + unboxedValue);
}
}
OUTPUT:-
Program-11
# Write a Java program for ArrayList to store integer values
and manipulate integer list
import [Link];
import [Link];
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
Scanner scanner = new Scanner([Link]);
while (true) {
[Link]("ArrayList Menu:");
[Link]("1. Add a number");
[Link]("2. Remove a number");
[Link]("3. Display numbers");
[Link]("4. Exit");
[Link]("Enter your choice: ");
int choice = [Link]();
switch (choice) {
case 1:
[Link]("Enter a number to add: ");
int numToAdd = [Link]();
[Link](numToAdd);
[Link]("Number added to the list.");
break;
case 2:
[Link]("Enter a number to remove: ");
int numToRemove = [Link]();
if ([Link]([Link](numToRemove))) {
[Link]("Number removed from the
list.");
} else {
[Link]("Number not found in the
list.");
}
break;
case 3:
[Link]("Numbers in the list: " +
numbers);
break;
case 4:
[Link]("Goodbye!");
[Link]();
[Link](0);
default:
[Link]("Invalid choice. Please select a
valid option.");
}
}
}
}
OUTPUT:-
Program-12
# Write a Java program for High School Marksheet to evaluate
percentage and final result
import [Link];
public class StudentResult {
public static void main(String[] args) {
int hindi, english, maths, science, computer, s_science, total,
rollNum;
float percentage;
String name;
Scanner scanner = new Scanner([Link]);
[Link]("-------------------------");
[Link]("Please enter name: ");
name = [Link]();
[Link]("Please enter roll number: ");
rollNum = [Link]();
[Link]("\nPlease enter Six subjects marks: ");
[Link]("-------------------------");
[Link]("Enter Hindi marks: ");
hindi = [Link]();
[Link]("Enter English marks: ");
english = [Link]();
[Link]("Enter Maths marks: ");
maths = [Link]();
[Link]("Enter Science marks: ");
science = [Link]();
[Link]("Enter Computer marks: ");
computer = [Link]();
[Link]("Enter Social Science marks: ");
s_science = [Link]();
total = hindi + english + maths + science + computer +
s_science;
if (total > 600) {
[Link]("It seems like you have given a number
more than 100 in one or multiple subjects.");
[Link]("Please enter within 100 marks");
} else {
percentage = (float) total / 600 * 100;
[Link]("-------------------------");
[Link]("ROLL NUMBER: " + rollNum);
[Link]("HINDI " + hindi + "/100");
[Link]("ENGLISH " + english + "/100");
[Link]("MATHEMATICS " + maths + "/100");
[Link]("SCIENCE " + science + "/100");
[Link]("COMPUTER " + computer + "/100");
[Link]("SOCIAL SCIENCE " + s_science + "/100");
[Link]("TOTAL " + total + "/600");
[Link]("Percentage is: %.2f%%\n", percentage);
[Link]("-------------------------");
if (percentage >= 60) {
[Link]("Congratulations %s you have passed
the examination with %.2f%%\n", name, percentage);
[Link]("You have got First Division");
} else if (percentage >= 50) {
[Link]("You have got Second Division");
} else if (percentage >= 40) {
[Link]("You have got Third Division");
} else {
[Link]("You have Failed.");
[Link]("Better luck for next time.");
}
}
[Link]();
}
}
OUTPUT:-