Sort by

recency

|

1903 Discussions

|

  • + 0 comments

    im getting wrong anwser, please help me

    class Person: def init(self,initialAge): # Add some more code to run some checks on initialAge self.age = 0 self.Age = initialAge

        print("Age is not valid, Seeting age to 0..")
    def amIOld(self):
        # Do some computations in here and print out the correct statement to the console
        if age < 13:
            print("You are young")
        elif age>=13 and age <18:
            print("You are a teenager")
        else:
            ("You are old")
    def yearPasses(self):
        # Increment the age of the person in here
        self. Age +=1
    

    t = int(input())

  • + 0 comments

    The stub was not available for Java 15 so you can do the challange in Java 8 instead.

  • + 0 comments

    public class Person { private int age;

    public Person(int initialAge) {
        // Add some more code to run some checks on initialAge
        if(initialAge<0){
              System.out.println("Age is not valid, setting age to 0.");
              this.age=0;   
        }
        else
        this.age=initialAge;
    }
    
    public void amIOld() {
        // Write code determining if this person's age is old and print the correct statement:
        if(age<13)
        System.out.println("You are young.");
        else if(age>=13 && age<18)
        System.out.println("You are a teenager.");
        else
        System.out.println("You are old.");
    
    }
    
    public void yearPasses() {
        // Increment this person's age.
        age++;
    }
    
    public static void main(String[] args) {
    
  • + 0 comments

    Person::Person(int initialAge) { // Add some more code to run some checks on initialAge if(initialAge<0) { initialAge=0; cout<<"Age is not valid, setting age to 0.\n"; } else age=initialAge; }

    void Person::amIOld(){
        // Do some computations in here and print out the correct statement to the console 
    

    if(age<13) cout<<"You are young.\n"; else if(age>=13&&age<18) cout<<"You are a teenager.\n"; else cout<<"You are old.\n"; }

    void Person::yearPasses(){
        // Increment the age of the person in here
    

    age=age+1; } }

    return 0;
    

    }

  • + 0 comments

    import java.util.Scanner;

    public class Solution { private int age;

    public Solution(int initialAge) {
        if (initialAge > 0) {
            this.age = initialAge;
        } else {
            System.out.println("Age is not valid, setting age to 0.");
            this.age = 0;
        }
    }
    
    public void amIOld() {
        if (age < 13) {
            System.out.println("You are young.");
        } else if (age >= 13 && age < 18) {
            System.out.println("You are a teenager.");
        } else {
            System.out.println("You are old.");
        }
    }
    
    public void yearPasses() {
        this.age++;
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int T = scan.nextInt(); 
        int[] ages = new int[T]; 
        for (int i = 0; i < T; i++) {
            ages[i] = scan.nextInt();
        }
        for (int age : ages) { 
            Solution p = new Solution(age);
            p.amIOld();
            for (int j = 0; j < 3; j++) {
                p.yearPasses();
            }
            p.amIOld();
            System.out.println();
        }
    
        scan.close();
    }
    

    }