0% found this document useful (0 votes)
5 views5 pages

Grade Calculation and Tourist Spots

Uploaded by

Niyathi Kelegeri
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Grade Calculation and Tourist Spots

Uploaded by

Niyathi Kelegeri
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

grade

import [Link];

class NotValidEvaluationException extends Exception {

public NotValidEvaluationException(String message) {

super(message);

public class GradeCalculator {

public static void main(String[] args) {

int[] marks = new int[10];

int totalMarks = 0;

try {

[Link] scanner = new [Link]([Link]);

// Input marks for 10 students

for (int i = 0; i < 10; i++) {

[Link]("Enter marks for student " + (i + 1) + ": ");

int mark = [Link]();

if (mark < 0 || mark > 100) {

throw new InputMismatchException("Marks should be between 0 and 100");

marks[i] = mark;

totalMarks += mark;

// Calculate average

double average = (double) totalMarks / 10;


// Check if average is less than or equal to 50

if (average <= 50) {

throw new NotValidEvaluationException("Average marks are not valid");

// Display average

[Link]("Average marks: " + average);

// Calculate and display grades

for (int i = 0; i < 10; i++) {

int mark = marks[i];

String grade = calculateGrade(mark);

[Link]("Student " + (i + 1) + ": " + grade);

} catch (InputMismatchException e) {

[Link]("InputMismatchException: " + [Link]());

} catch (ArrayIndexOutOfBoundsException e) {

[Link]("ArrayIndexOutOfBoundsException: " + [Link]());

} catch (NotValidEvaluationException e) {

[Link]("NotValidEvaluationException: " + [Link]());

private static String calculateGrade(int mark) {

if (mark >= 70) {

return "Distinction";

} else if (mark >= 60) {

return "First Class";

} else if (mark >= 50) {

return "Second Class";


} else {

return "Fail";

2.

import [Link].*;

class Tourist {

private String name;

private String state;

private String famousSpot;

public Tourist(String name, String state, String famousSpot) {

[Link] = name;

[Link] = state;

[Link] = famousSpot;

public String getState() {

return state;

public String getFamousSpot() {

return famousSpot;

public String toString() {

return "Tourist Spot: " + name + ", State: " + state + ", Famous Spot: " + famousSpot;
}

public class TouristApp {

public static void main(String[] args) {

List<Tourist> touristList = new ArrayList<>();

// Create tourist spots

[Link](new Tourist("Goa Beach", "Goa", "Calangute Beach"));

[Link](new Tourist("Mysore Palace", "Karnataka", "Mysore Palace"));

[Link](new Tourist("Marina Beach", "Tamil Nadu", "Marina Beach"));

[Link](new Tourist("Kovalam Beach", "Kerala", "Kovalam Beach"));

[Link](new Tourist("Charminar", "Telangana", "Charminar"));

// Display the list sorted by state

[Link](touristList, [Link](Tourist::getState));

[Link]("Tourist Spots in South India:");

for (Tourist tourist : touristList) {

[Link](tourist);

// Search for a tourist spot

String searchSpot = "Charminar";

boolean found = false;

for (Tourist tourist : touristList) {

if ([Link]().equals(searchSpot)) {

[Link]("\nDetails of " + searchSpot + ":");

[Link](tourist);

found = true;

break;

}
}

// If spot not found, raise an exception

if (!found) {

[Link]("\nTourist spot " + searchSpot + " not found.");

throw new NoSuchElementException("Tourist spot " + searchSpot + " not found.");

Common questions

Powered by AI

In the GradeCalculator program, grades are determined using the calculateGrade method based on conditional checks of each student's mark. If a mark is 70 or higher, the student receives a "Distinction". Marks between 60 and 69 receive "First Class", between 50 and 59 receive "Second Class", and below 50 is classified as "Fail". This system is implemented through a series of if-else statements, categorizing students into performance tiers .

The TouristApp program handles scenarios where a tourist spot is not found by iterating through the list of Tourist objects to find a match for the famous spot. If no match is found, the program prints a message indicating that the tourist spot is not found and then throws a NoSuchElementException with a relevant message, ensuring the user is informed of the issue .

The NotValidEvaluationException custom exception in the GradeCalculator program specifically addresses cases where the computed average mark is not satisfactory (less than or equal to 50). Its purpose is to clearly highlight and handle this unique business rule, improving code readability and distinction of specific failure cases, which enhances the program's functionality by enforcing specific academic evaluation constraints .

Using a List to store Tourist objects in the TouristApp program is necessary due to its dynamic size and ability to maintain an ordered collection of elements. Lists support sorting and flexible manipulation of elements, allowing the program to sort and search tourist spots efficiently. This data structure supports vital application functionalities like ordered display and easy retrieval of tourist spot details .

If a student's mark input is outside the range of 0 to 100, the GradeCalculator program throws an InputMismatchException with the message "Marks should be between 0 and 100". This is important because it ensures that only valid marks are considered in calculations, preventing corrupted data from affecting the final average and grades .

In the TouristApp program, the tourist spots are displayed in alphabetical order by state. This ordering is achieved by using Collections.sort with a Comparator that compares the state properties of Tourist objects. This approach helps users easily locate tourist spots based on the state, enhancing the usability of the application .

The Scanner object in the GradeCalculator program reads user input for student marks. It plays a crucial role in detecting InputMismatchExceptions, which occur when a user inputs an invalid type (such as a non-integer) or out-of-range values, enabling the program to handle and alert the user about improper inputs effectively .

The GradeCalculator program employs try-catch blocks to handle specific exceptions, such as InputMismatchException and ArrayIndexOutOfBoundsException, providing informative messages to the user. A strength of this approach is its proactive user feedback and prevention of runtime crashes. However, areas for improvement include encompassing more potential input errors like decimal inputs or enhancing user instructions for input requirements, improving the overall robustness and user experience .

The GradeCalculator program determines that the average marks are valid if they exceed 50. If the average is less than or equal to 50, a NotValidEvaluationException is thrown with the message "Average marks are not valid", ensuring only qualifying averages are processed for further operations .

The TouristApp demonstrates object-oriented principles through the Tourist class by encapsulating the data related to tourist spots within objects. Each Tourist object holds attributes and related behavior, such as state and famous spots, facilitating modular and organized code. Encapsulation ensures data integrity, while using objects allows for easy sorting and manipulation, demonstrating the strengths of OOP in managing complex data .

You might also like