Created
February 28, 2019 04:55
-
-
Save hribeirosantana/72c60bbacd3c4f8e185bb014c91ef705 to your computer and use it in GitHub Desktop.
HackerRank > 30 Days of Code > Day 4: Class vs. Instance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
(define person% | |
(class object% | |
(super-new) | |
(init-field age) | |
(when (< age 0) (displayln "Age is not valid, setting age to 0.") (set-age)) | |
(define/private (set-age) (set! age 0)) | |
(define/public (get-age) age) | |
(define/public (year-passes) (set! age (add1 age))) | |
(define/public (am-i-old?) (cond [(< age 13) (displayln "You are young.")] | |
[(< age 18) (displayln "You are a teenager.")] | |
[else (displayln "You are old.")])))) | |
(define test-cases (read)) | |
(define (loop i max) | |
(when (< i max) (let ([person (new person% [age (read)])]) | |
(send person am-i-old?) | |
(send person year-passes) | |
(send person year-passes) | |
(send person year-passes) | |
(send person am-i-old?) | |
(loop (add1 i) max)))) | |
(loop 0 test-cases) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment