Question

Implement a program (in both C# and Python) which includes a user-defined exception giving a message...

Implement a program (in both C# and Python) which includes a user-defined exception giving a message “This score is too low. You failed the exam.” if a score is less than 50. Otherwise, it should give a message “You passed the exam. Congratulations.”.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C# CODE

Code to copy:

// C# CODE:

using System;

// user-defined exception class

class LowResult : Exception

{

// base is used to access members of the base class

// All exceptions inherit from System.Exception class

public LowResult() : base("This score is too low. You failed the exam.")

{}

}

// main class

class MainClass

{

public static void Main (string[] args)

{

try

{

// prompt user to enter the score

Console.WriteLine("Enter the Score: ");

// read the user input as int

// Convert.ToInt32() converts string to int

int score = Convert.ToInt32(Console.ReadLine());

// Check if score is less than 50

if(score < 50)

{

// throw the exception that arises

throw new LowResult();

}

else

{

// display the student passed the exam

Console.WriteLine("You passed the exam. Congratulations.");

}

}

// catch the error thrown within the try-block

catch(LowResult e)

{

// display the error message

Console.WriteLine(e.Message);

}

}

}

Screenshot of the code:

Sample Output:

Test case 1

Test case 2

PYTHON CODE

Code to copy:

# PYTHON CODE:

# User-defined class for low score

class LowResult(Exception):

pass

# main program

try:

# prompt to enter score

score = int(input("Enter the Score: "))

# check if the score is less than 50

if score < 50:

# raise the defined error

raise LowResult()

else:

# display the student passed

print("You passed the exam. Congratulations.")

except:

# display the error message

print("This score is too low. You failed the exam.")

Screenshot of the code:

Sample Output:

Test case 1

Test case 2

Add a comment
Know the answer?
Add Answer to:
Implement a program (in both C# and Python) which includes a user-defined exception giving a message...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello! we are using Python to write this program. we are supposed to use loops in...

    Hello! we are using Python to write this program. we are supposed to use loops in this assignment. I would greatly appreciate the help! Thank you! Write a program that allows the user to play a guessing game. The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: we would have the program select a random number as the "secret number". However, for the purpose of testing...

  • Write a program that prompts the user for an integer that the player (maybe the user,...

    Write a program that prompts the user for an integer that the player (maybe the user, maybe someone else) will try to guess. If the player's guess is higher than the target number, the program should display "too high" If the user's guess is lower than the target number, the program should display "too low" The program should use a loop that repeats until the user correctly guesses the number. Then the program should print how many guesses it took....

  • In Python. Complete the following programs. Begin each program with a comment that includes: • Your...

    In Python. Complete the following programs. Begin each program with a comment that includes: • Your name • The project/program • Brief description of the problem Lab3Pro1 A supervisor in a manufacturing company wishes to display the bonus amount for an employee based on this year's production. Input Output Processing Distribute bonuses as follows: Display the results as follows: Prompt for user input "Enter number of Units produced" Store the amount entered Bonus This year's production bonus will be $nnnnn...

  • In c# create a program that generates a random number from 1 to 1000. Then, ask...

    In c# create a program that generates a random number from 1 to 1000. Then, ask the user to guess the random number. If the user's guess is too high, then the program should print "Too High, Try again". If the user's guess is too low, then the program should print "Too low, Try again". Use a loop to allow the user to keep entering guesses until they guess the random number correctly. Once they figure it, congratulate the user....

  • Matlab

    Part A: Quiz ApplicationIn this part, you are required to implement Quiz Application. Add 10 questions in your program of any domain with their answers. You many use strcmpi function to compare user answer with the saved answer. If the answer is correct/matched with the saved answer user score 1 point.  Functionalities that need to be implemented in Quiz application are: -1.     The user will be asked to enter the name and then asked questions one by one. 2.     The...

  • Write python code on computer, No handwritten code You will implement a program which asks the...

    Write python code on computer, No handwritten code You will implement a program which asks the user to enter scores of different courses in different semesters. The program should read the input and output some simple statistics 1. An example input string by the user is given below. Fal12016-coursel:82, course2:80,course3:85;Spring2018- course4:82;Fall2018-course5:85, course6:50, course7:78 Info from different semesters are separated by a ";", courses in each semester are separated by a,and score and corresponding course is separated by a, information of...

  • Write a Python program that asks the user to enter the number of calories and fat...

    Write a Python program that asks the user to enter the number of calories and fat grams in a food item. The program should display the percentage of the calories that come from fat. One gram of fat has 9 calories, therefore: Calories from fat = fat grams * 9 The percentage of calories from fat can be calculated as follows: Calories from fat / total calories If the calories from fat are less than 30 percent of the total...

  • C++ Programming Question

    The Maybe High School offers a course that prepares students for tertiary-level education. Last year, several students who completed the course matriculated to a tertiary level institution of their choice. Naturally, the college wants to know how well its students did on the exam. You have been asked to write a program to summarize the results. Your program should: Prompt the user to enter the final grades of an indefinite number of students. Note: A final grade greater than or equal to 75 is considered a...

  • Write a C++ program to implement this task. Students are registered in 3 courses in a...

    Write a C++ program to implement this task. Students are registered in 3 courses in a semester. To pass each course, students have to make a grade greater than or equal to 40. You have to write a program to determine if the student will pass or fail the semester, based on the following criteria: A student passes if all three courses are passed. Additionally, a student may pass if only one subject is failed, and the overall average grade...

  • Write a C++ program that dynamically allocates an array large enough to hold a user-defined number...

    Write a C++ program that dynamically allocates an array large enough to hold a user-defined number of test scores. Once all the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate headings. Use pointer notation rather than array notation whenever possible. Input Validation: Do not accept negative numbers for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT