Answer to Question #237570 in Java | JSP | JSF for hajjk

Question #237570

Define Exception Handling. Write a java program to create a user defined

exception for the following

a) Create a student class and get the name of the student and 3 subject marks

b) Find the length of the name. If the length > 7 throw LengthException

c) Find the average of marks, if avg < 50 throw FailedException

d) If avg < 75 && avg > 50 throw NotFirstClassException

e) If avg > 75 throw FirstClassException


1
Expert's answer
2021-09-15T07:54:09-0400
package com.company;

import java.util.Scanner;

// Defining the exception classes
class My_Exeption_Class extends Exception
{
    public My_Exeption_Class(String s)
    {
        // Call constructor of parent Exception
        super(s);
    }
}

 class Student
{
    void student_details_check( String student_name, double average_mark) throws My_Exeption_Class{
        // determine the length of the name
        int length = student_name.length();
        if(length > 7){
            System.out.println("\nLengthException throwed");
            throw new My_Exeption_Class("The length of the name is greater than 7");
        }

        if(average_mark < 50){

            System.out.println("\nFailedException throwed");
            throw new My_Exeption_Class("You have failed, your average is below 50");
        }
        else if(average_mark >= 50 &&  average_mark < 70){
            System.out.println("\nNotFirstClassException throwed");
            throw new My_Exeption_Class("You have not reached the first class mark");
        }
        else {
            System.out.println("\nFirstClassException throwed");
            throw new My_Exeption_Class("Congratualtions, you have got first class honours");
        }
    }

    public static void main(String args[])
    {

        Scanner sc= new Scanner(System.in); //System.in is a standard input stream
        //Read the name of the student from keyboard
        System.out.print("Enter Student name: ");
        String student_name = sc.nextLine();
        //Read the marks for the subjects
        System.out.print("Enter Marks for subject 1: ");
        double subject_1 = sc.nextDouble();
        System.out.print("Enter Marks for subject 2: ");
        double subject_2 = sc.nextDouble();
        System.out.print("Enter Marks for subject 3: ");
        double subject_3 = sc.nextDouble();
        double average_mark = (subject_1+subject_2+subject_3)/3;
        Student obj = new Student();
        try
        {
            obj.student_details_check(student_name, average_mark);
        }
        catch (My_Exeption_Class ex)
        {
            System.out.println(ex.getMessage());
        }

    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS