Unit 1 Programs JAVA
Unit 1 Programs JAVA
Unit 1 Programs JAVA
PART-B
1. Implement a program in Java that interchanges the odd and even
components in an array
public class AraayInterchange {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6};
System.out.println("Original Array:");
for (int num : array) {
System.out.print(num + " ");
}
OUTPUT:
Original Array:
123456
Array after interchanging odd and even components:
214365
2. Write a Java program to sort set of names stored in an array in
alphabetical order.
public class SortNamesWithoutMethod {
public static void main(String[] args) {
String[] names = {"John", "Alice", "Bob", "Eve", "Charlie"};
System.out.println("Original Names:");
for (String name : names) {
System.out.println(name);
}
System.out.println("Fibonacci Series:");
scanner.close();
}
}
OUTPUT:
Enter the number of terms in the Fibonacci series: 6
Fibonacci Series:
011235
5. Create a simple Java program to implement basic Calculator operations
import java.util.Scanner;
System.out.println("Basic Calculator");
System.out.print("Enter first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter operator (+, -, *, /): ");
char operator = scanner.next().charAt(0);
double result = 0;
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
System.out.println("Error: Division by zero");
System.exit(1);
}
break;
default:
System.out.println("Error: Invalid operator");
System.exit(1);
}
scanner.close();
}
}
OUTPUT:
Basic Calculator
Enter first number: 10
Enter operator (+, -, *, /): *
Enter second number: 5
Result: 50.0
PART-C
1. How would you find a given two one dimensional arrays A and B which
are sorted in ascending order. Write a Java program to merge them into a
single sorted array, see that is contains every item from array A and B, in
ascending order.
public class MergeSortedArrays {
public static void main(String[] args) {
int[] A = {1, 3, 5, 7, 9};
int[] B = {2, 4, 6, 8, 10};
int i = 0, j = 0, k = 0;
while (j < n) {
mergedArray[k++] = B[j++];
}
return mergedArray;
}
}
OUTPUT:
Merged and Sorted Array:
1 2 3 4 5 6 7 8 9 10
2. i. Can you make the keyword public to private in main() method? Justify
(3)
ii. Write a java program using array to perform
Sum of all values in an array (6)
Find the greatest number in an array(6)
i.
In Java, the main() method is typically declared as public
static void main(String[] args), and it must be public because
it serves as the entry point for the Java application. When the
Java Virtual Machine (JVM) starts an application, it looks
for the public static void main(String[] args) method to begin
execution. Making it public allows the JVM to access and
execute this method.
Changing the access modifier from public to private in the
main() method would result in a compilation error, and the
program would not execute. It's necessary for the main()
method to be accessible from outside the class to start the
program.
ii. Calculate Sum of Values in an Array
public class SumOfArrayValues {
public static void main(String[] args) {
int[] numbers = {10, 7, 15, 23, 4, 17, 9};
int sum = 0;
System.out.println("Array: " +
java.util.Arrays.toString(numbers));
System.out.println("Sum of all values: " + sum);
}
}
OUTPUT:
Array: [10, 7, 15, 23, 4, 17, 9]
Sum of all values: 85
System.out.println("Array: " +
java.util.Arrays.toString(numbers));
System.out.println("Greatest number: " + max);
}
}
OUTPUT:
Array: [10, 7, 15, 23, 4, 17, 9]
Greatest number: 23
3. Develop a Java Program to find
i) Largest of three numbers using Ternary Operator (8)
ii) Smallest of three numbers using Ternary Operator (7)
Largest of Three Numbers using Ternary Operator:
public class LargestOfThreeNumbers {
public static void main(String[] args) {
int num1 = 25;
int num2 = 10;
int num3 = 35;
int largest = (num1 > num2) ? ((num1 > num3) ? num1 : num3) :
((num2 > num3) ? num2 : num3);
int smallest = (num1 < num2) ? ((num1 < num3) ? num1 : num3) :
((num2 < num3) ? num2 : num3);
OUTPUT:
*
**
***
****
*****