Advertisement
minnera

#30daysofcode #day4

Sep 30th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.88 KB | None | 0 0
  1. //https://www.hackerrank.com/challenges/30-class-vs-instance/
  2.  
  3. class Person {
  4.     public int age;    
  5.     public Person(int initialAge) {
  6.         // Add some more code to run some checks on initialAge
  7.         if(initialAge < 0){
  8.             age = 0;
  9.             Console.WriteLine("Age is not valid, setting age to 0.");
  10.         }
  11.         else{
  12.             age = initialAge;
  13.         }
  14.      }
  15.      public void amIOld() {
  16.         // Do some computations in here and print out the correct statement to the console
  17.         if(age < 13){
  18.             Console.WriteLine("You are young.");
  19.         }
  20.         else if(age < 18){
  21.             Console.WriteLine("You are a teenager.");
  22.         }
  23.         else{
  24.             Console.WriteLine("You are old.");
  25.         }
  26.      }
  27.  
  28.      public void yearPasses() {
  29.         // Increment the age of the person in here
  30.         this.age++;
  31.      }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement