We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Day 4: Class vs. Instance
Day 4: Class vs. Instance
+ 1 comment what is the problem plz java8
public Person(int initialAge) { // Add some more code to run some checks on initialAge if (initialAge<0){ this.age=0; System.out.println("age is not valid, setting age to 0."); } 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() { age++; }
+ 0 comments class Person: def __init__(self,initialAge): self.initialAge=initialAge if self.initialAge<0: self.initialAge=0 print('Age is not valid, setting age to 0.') else: self.initialAge=initialAge def amIOld(self): if self.initialAge<13: print('You are young.') elif self.initialAge>=13 and self.initialAge<18: print("You are a teenager.") else: print('You are old.') def yearPasses(self): self.initialAge=self.initialAge+1 t = int(input()) for i in range(0, t): age = int(input()) p = Person(age) p.amIOld() for j in range(0, 3): p.yearPasses() p.amIOld() print("")
+ 3 comments Java 15 Solution-- I am getting wrong answer what's wrong with this code Please anybody tell me.
import java.io.; import java.util.;
public class Solution {
public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ class Person { private int age; public Person (int initialAge) { if(initialAge < 0 ) { System.out.println(" Age is not valid, setting age to 0."); } this.age = initialAge; } 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++; } } }
}
+ 0 comments Simple solution in java public class Person { private int age;
public Person(int initialAge) { if(initialAge <=0) { age=0; System.out.println("Age is not valid, setting age to 0."); } this.age=initialAge; // Add some more code to run some checks on 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() { age++;
}
+ 0 comments java solution
import java.io.; import java.util.;
public class Person { private int age;
public Person(int initialAge) { this.age=initialAge; } public void amIOld() { if(this.age<0){ System.out.println("Age is not valid, setting age to 0."); System.out.println("You are young."); }else if(this.age<13) { System.out.println("You are young."); }else if(this.age>=13 &&this.age<18){ System.out.println("You are a teenager."); }else{ System.out.println("You are old."); } } public void yearPasses() { if(this.age<0){ this.age=this.age*-1; }else{ this.age++; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for (int i = 0; i < T; i++) { int age = sc.nextInt(); Person p = new Person(age); p.amIOld(); for (int j = 0; j < 3; j++) { p.yearPasses(); } p.amIOld(); System.out.println(); } sc.close(); }
}
Load more conversations
Sort 1831 Discussions, By:
Please Login in order to post a comment