B.M.S.
COLLEGE OF ENGINEERING
(Autonomous college under VTU)
Bull Temple Rd, Basavanagudi, Bengaluru, Karnataka
560019 2023-2025
Department of Computer Applications
Report is submitted for fulfillment of AAT 5 in the subject
“JAVA PROGRAMMING”
(22MCA2PCJP)
By
Vinod Kumar K M
( USN 1BM23MC116)
Under the Guidance of
R V Raghavendra Rao
(Assistant Professor)
1. Define an exception called "NoMatchException" that is thrown when a
string is not equal to "India". Write a program that uses this exception.
import java.util.Scanner;
class NoMatchException extends Exception {
public NoMatchException(String message) {
super(message);
}
}
class StringChecker {
private String target;
public StringChecker(String target) {
this.target = target;
}
public void checkString(String input) throws NoMatchException {
if (!target.equals(input)) {
throw new NoMatchException("The input string does not match '" + target + "'.");
}
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the target string: ");
String targetString = scanner.nextLine();
StringChecker checker = new StringChecker(targetString);
System.out.print("Enter the number of strings to test: ");
int numStrings = scanner.nextInt();
scanner.nextLine(); // Consume the newline left-over
String[] testStrings = new String[numStrings];
for (int i = 0; i < numStrings; i++) {
System.out.print("Enter string " + (i + 1) + ": ");
testStrings[i] = scanner.nextLine();
}
for (String testString : testStrings) {
try {
checker.checkString(testString);
System.out.println(testString + " matches '" + targetString + "'.");
} catch (NoMatchException e) {
System.out.println(e.getMessage());
}
}
scanner.close();
}
}
Output :
2. Write a program for user defined exception that checks the internal and
external marks; if the internal marks is greater than 40 it raises the
exception "Internal marks is exceed"; if the external marks is greater than
60 it raises the exception and displays the message "The External Marks is
exceed". Create the above exception and use it in your program.
import java.util.Scanner;
class InternalMarksExceededException extends Exception {
public InternalMarksExceededException(String message) {
super(message);
}
}
class ExternalMarksExceededException extends Exception {
public ExternalMarksExceededException(String message) {
super(message);
}
}
class MarksChecker {
private static final int INTERNAL_MAX = 40;
private static final int EXTERNAL_MAX = 60;
public void checkMarks(int internalMarks, int externalMarks) throws
InternalMarksExceededException, ExternalMarksExceededException {
if (internalMarks > INTERNAL_MAX) {
throw new InternalMarksExceededException("Internal marks exceed the maximum
limit of " + INTERNAL_MAX + ".");
}
if (externalMarks > EXTERNAL_MAX) {
throw new ExternalMarksExceededException("External marks exceed the maximum
limit of " + EXTERNAL_MAX + ".");
}
}
}
public class MarksApp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
MarksChecker checker = new MarksChecker();
System.out.print("Enter internal marks: ");
int internalMarks = scanner.nextInt();
System.out.print("Enter external marks: ");
int externalMarks = scanner.nextInt();
try {
checker.checkMarks(internalMarks, externalMarks);
System.out.println("Marks are within the allowed limits.");
} catch (InternalMarksExceededException | ExternalMarksExceededException e) {
System.out.println(e.getMessage());
}
scanner.close();
}
}
Output :