009-001-011
Private Modifier: Setter with Validation
Medium
Problem Description
Private Modifier: Setter with Validation
In this problem, you will create a program that implements range validation (0-100) in a Score class setter, reads two integers from standard input, tests both valid and invalid values, and displays the result to standard output.
Learning Objective: Understand how to perform validation in setters
Overview
Create a class with a setter that rejects invalid values.
Specifications
- Create a
Scoreclass - Private int field
value(range 0-100) setValue(int v)checks range (reset to 0 if out of range)getValue()retrieves value- Read two integers from standard input and test with valid and invalid values in Main
Input Format
validScore
invalidScore
- Line 1: Valid score (integer in range 0-100)
- Line 2: Invalid score (integer outside range 0-100)
Output Format
Valid score: {validScore}
Invalid score reset to: 0
Ready to Try Running Code?
Log in to access the code editor and execute your solutions for this problem.
Don't have an account?
Sign Up