Coochbehar Government Engineering College: Submitted by

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

COOCHBEHAR GOVERNMENT ENGINEERING COLLEGE

ASSIGNMENT (WEEK-5)

Submitted by-

Name-Md Washim Aktar


Roll no: 34900121013
Dept: C.S.E
Semester: 5th
Subject: Object Oriented Programming Lab(PCC-CS593)
1. Write a program to sort an Array of String Object.
Solution:
StringArrSort.java

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;

public class InvalidAgeException extends Exception {

public InvalidAgeException(String msg) {


super(msg);
}
}
TestException.java

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();

if (n < 18 || n > 60) {


throw new InvalidAgeException("Invalid age");
}
System.out.println("You are eligible for Online vote");
}
}

Output:
4. Write a program for calculator and handle all the exception that occur during

calculation. Solution:
Calculator.java

package

calculator;

public class Calculator {


int add(int a, int b) {
return (a + b);
}

int sub(int a, int b) {


if (b > a) {
throw new ArithmeticException("Second number is learge then first one !!");

}
return (a - b);
}

int mul(int a, int b) {


int m = countDigits(a);
int n = countDigits(b);
if ((m + n) > 10)
throw new ArithmeticException("both number are too learge ");
return a * b;
}

int div(int a, int b) {


if (b <= 0)
throw new ArithmeticException("Division by either zero or negative");
return a / b;
}
//method countDigits for to find the digits of input numbers
private int countDigits(int a) {
int c = 0;
if (a > 0) {
c = c + 1;
a = a /
10;
}
return 0;
}
}

TestCalculator.java

package calculator;

import java.util.Scanner;

public class TestCalculator {


public static void main(String args[]) {
Calculator cal = new Calculator();
String op = "yes";
Scanner sc = new Scanner(System.in);
while (op.equals("yes")) {
System.out.println("Enter operation name \n option: add, sub, mul, div");
String opname = sc.next();
System.out.println("Enter two operand: ");
int a = sc.nextInt();
int b = sc.nextInt();

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;

public class MarksAvg {


static int marksArr[] = new int[20];
static Scanner scn = new Scanner(System.in);

public static void main(String[] args) {

System.out.println("Enter Marks of Assignment|| Full Marks:100");


getMarks(); // calling method for taking input
System.out.println("Display Data ");
display(marksArr);
System.out.println("Average Marks of all Students is:" + avgMarks(marksArr));
scn.close();
}

//method for marks input


private static void getMarks() {
// TODO Auto-generated method stub
for (int i = 0; i < marksArr.length; i++) {
try {
System.out.println("Enter Marks for Student " + (i + 1));
int marks = scn.nextInt();
if (marks <= 0 || marks > 100) {
throw new IllegalArgumentException("Marks should be in range
of 0 to 100");
}
marksArr[i] = marks;
} catch (java.util.InputMismatchException e) { // invalid input type
exception if input is not integer
System.out.println("Invlid Input--> Marks should be in range of 0 to
100");
scn.next(); // clear input buffer
i--; // retry for same student
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
i--; // retry the same student
}
}
}

// method for calculate average


private static int avgMarks(int[] marksArr) {
int avg = 0;
int marks = 0;
for (int i = 0; i < marksArr.length; i++) {
marks += marksArr[i];
}
avg = marks / marksArr.length;
return avg;
}

//method for display the marks


private static void display(int[] marksArr) {
for (int m = 0; m < marksArr.length; m++) {
System.out.println("Marks of Student " + (m + 1) + ":" + marksArr[m] + "
");
}
System.out.println();
}

}
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

Average Marks of all Students is:70

You might also like