Professional Documents
Culture Documents
Coochbehar Government Engineering College: Submitted by
Coochbehar Government Engineering College: Submitted by
Coochbehar Government Engineering College: Submitted by
ASSIGNMENT (WEEK-5)
Submitted by-
import java.util.Scanner;
public class StringArrSort {
public static void main(String Args[]) {
Scanner sc = new
Scanner(System.in);
System.out.println("Enter the number of elements in the Array: ");
int n = sc.nextInt();
String strArr[] = new String[n];
System.out.println("Enter the elements of Array: ");
for (int i = 0; i < n; i++) {
System.out.println("Enter the element " + (i + 1));
String s = sc.next();
strArr[i] = s;
}
sc.close();
System.out.println("Display Before Sorting: ");
display(strArr);
System.out.println("Display after sorting: ");
sortArr(strArr);
display(strArr);
}
// display method
private static void display(String[] strArr) {
for (int i = 0; i < strArr.length; i++) {
System.out.print(strArr[i] + " ");
}
System.out.println();
}
// sorting method
private static void sortArr(String[] strArr) {
for (int i = 0; i < strArr.length; i++) {
for (int j = 1; j < strArr.length; j++) {
if (strArr[j - 1].compareTo(strArr[j]) > 0) {
String temp = strArr[j - 1];
strArr[j - 1] = strArr[j];
strArr[j] = temp;
}
}
} }
}
Output:
2. Write a Program to sort an array of integer in Descending
order. Solution:
IntArrSort.java
import java.util.Scanner;
public class IntArrSort {
public static void main(String Args[]) {
Scanner scn = new
Scanner(System.in);
System.out.println("Enter the no of elements of Array: ");
int n = scn.nextInt();
int intArr[] = new int[n];
System.out.println("Enter the elements of the Array");
for (int i = 0; i < n; i++) {
System.out.println("Enter element " + (i + 1) + ":");
intArr[i] = scn.nextInt();
}
scn.close();
System.out.println("\nDisplay before sorting of array ");
display(intArr);
sortintArr(intArr);
System.out.println("\nDisplay After sorting the array in decending order ");
display(intArr);
}
//sorting method
private static void sortintArr(int[] intArr) {
for (int i = 0; i < intArr.length; i++)
for (int j = 1; j < intArr.length; j++) {
if (intArr[j - 1] < (intArr[j])) {
int temp = intArr[j - 1];
intArr[j - 1] = intArr[j];
intArr[j] = temp;
}
}
}
//display method
private static void display(int[] intArr) {
for (int i : intArr) {
System.out.print(i + "
");
}
}
Output:
3. Write a program to throw your own exception using throws keyword.
Solution:
InvalidAgeException.java
package
invalid_Exception;
package invalid_Exception;
import java.util.Scanner;
public class TestException
{
static Scanner sc = new Scanner(System.in);
public static void main(String Args[]) throws InvalidAgeException {
try {
userInput();
} catch (Exception e) {
System.out.println(e);
} finally {
System.out.println("Program Done");
sc.close();
}
}
//method userInput()
public static void userInput() throws InvalidAgeException {
System.out.println("Enter Your Age: ");
int n = sc.nextInt();
Output:
4. Write a program for calculator and handle all the exception that occur during
calculation. Solution:
Calculator.java
package
calculator;
}
return (a - b);
}
TestCalculator.java
package calculator;
import java.util.Scanner;
switch (opname) {
case "add":
try {
System.out.println("result: " + cal.add(a, b));
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case "sub":
try {
System.out.println("result: " + cal.sub(a, b));
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case "mul":
try {
System.out.println("result: " + cal.mul(a, b));
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case "div":
try {
System.out.println("result: " + cal.div(a, b));
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
}
}
}
}
Output:
5. Write a Java program to find the average marks of 20 students in class for CGEC assignments. Make
necessary provision for exceptions handling in your program.
Solution:
MarksAvg.java
import java.util.Scanner;
}
Output:
Enter Marks of Assignment|| Full Marks:100
Enter Marks for Student 1
72
Enter Marks for Student 2
65
Enter Marks for Student 3
75
Enter Marks for Student 4
90
Enter Marks for Student 5
100
Enter Marks for Student 6
205
Marks should be in range of 0 to 100
Enter Marks for Student 6
300
Marks should be in range of 0 to 100
Enter Marks for Student 6
66
Enter Marks for Student 7
46
Enter Marks for Student 8
73
Enter Marks for Student 9
79
Enter Marks for Student 10
69
Enter Marks for Student 11
57
Enter Marks for Student 12
59
Enter Marks for Student 13
75
Enter Marks for Student 14
96
Enter Marks for Student 15
65
Enter Marks for Student 16
0
Marks should be in range of 0 to 100
Enter Marks for Student 16
64
Enter Marks for Student 17
56
Enter Marks for Student 18
66
Enter Marks for Student 19
64
Enter Marks for Student 20
72
Display Data
Marks of Student 1:72
Marks of Student 2:65
Marks of Student 3:75
Marks of Student 4:90
Marks of Student 5:100
Marks of Student 6:66
Marks of Student 7:46
Marks of Student 8:73
Marks of Student 9:79
Marks of Student 10:69
Marks of Student 11:57
Marks of Student 12:59
Marks of Student 13:75
Marks of Student 14:96
Marks of Student 15:65
Marks of Student 16:64
Marks of Student 17:56
Marks of Student 18:66
Marks of Student 19:64
Marks of Student 20:72