Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Java Lab

Uploaded by

Niyathi Kelegeri
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Java Lab

Uploaded by

Niyathi Kelegeri
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

grade

import java.util.InputMismatchException;

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 {

java.util.Scanner scanner = new java.util.Scanner(System.in);

// Input marks for 10 students

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

System.out.print("Enter marks for student " + (i + 1) + ": ");

int mark = scanner.nextInt();

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

System.out.println("Average marks: " + average);

// Calculate and display grades

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

int mark = marks[i];

String grade = calculateGrade(mark);

System.out.println("Student " + (i + 1) + ": " + grade);

} catch (InputMismatchException e) {

System.out.println("InputMismatchException: " + e.getMessage());

} catch (ArrayIndexOutOfBoundsException e) {

System.out.println("ArrayIndexOutOfBoundsException: " + e.getMessage());

} catch (NotValidEvaluationException e) {

System.out.println("NotValidEvaluationException: " + e.getMessage());

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 java.util.*;

class Tourist {

private String name;

private String state;

private String famousSpot;

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

this.name = name;

this.state = state;

this.famousSpot = 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

touristList.add(new Tourist("Goa Beach", "Goa", "Calangute Beach"));

touristList.add(new Tourist("Mysore Palace", "Karnataka", "Mysore Palace"));

touristList.add(new Tourist("Marina Beach", "Tamil Nadu", "Marina Beach"));

touristList.add(new Tourist("Kovalam Beach", "Kerala", "Kovalam Beach"));

touristList.add(new Tourist("Charminar", "Telangana", "Charminar"));

// Display the list sorted by state

Collections.sort(touristList, Comparator.comparing(Tourist::getState));

System.out.println("Tourist Spots in South India:");

for (Tourist tourist : touristList) {

System.out.println(tourist);

// Search for a tourist spot

String searchSpot = "Charminar";

boolean found = false;

for (Tourist tourist : touristList) {

if (tourist.getFamousSpot().equals(searchSpot)) {

System.out.println("\nDetails of " + searchSpot + ":");

System.out.println(tourist);

found = true;

break;

}
}

// If spot not found, raise an exception

if (!found) {

System.out.println("\nTourist spot " + searchSpot + " not found.");

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

You might also like