Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Site is,question given on site

[^]

below is what i tried

my code is

C#
public class Person {
    private int Age;	
  
    
	public Person(int intialAge) {
        
  		// Add some more code to run some checks on initialAge
        if(intialAge<0){
            System.out.println("Age given is  invalid ,setting age to 0");
                       intialAge=0;
                        Age=intialAge;
                       }
    }    
                              

	public void amIOld() {
        
         if(Age<13 && Age>0)
         {
             System.out.println("You are young.");
         }
       
        if(13 < Age && Age < 18){System.out.println("You are teenager.");
                                            }
  		if(Age>=18){
        System.out.println("You are old");
            
        }} 
    
	public void yearPasses() {
            Age = Age++;
	
    }


What I have tried:

I am stuck on this problem from many days,actually i dont understand when and how yearPasses(),method is being called,please help
Posted
Updated 4-Oct-16 13:33pm
Comments
[no name] 4-Oct-16 6:46am    
A link to some other website is not a "problem". Learn to use the debugger and you will find out for yourself when/how that method is being called.
Anmol_1994 4-Oct-16 19:41pm    
sorry no debugger available at that sire,I am practicing different problems to brush up,any suggestions for sites where I can do so?

Quote:
i dont understand when and how yearPasses(),method is being called,please help
You have to call it (three times) for each input test case (read carefully requirements and the examples).

Quote:
if(Age<13 && Age>0)

This should be instead
C#
if(Age<13 && Age>=0)


[update]
Quote:
Age = Age++;
This is a blunder. It should be either

C#
Age++;

or
C#
++Age;

or
C#
Age = Age + 1;

[/update]
 
Share this answer
 
v2
Comments
Anmol_1994 4-Oct-16 9:16am    
still doesnt pass the test cases,with this code and changes made,yearpasses called 3 times.
Anmol_1994 4-Oct-16 9:17am    
please revisit the site and retry,maybe you would find some error ,that i couldnt see
CPallini 4-Oct-16 9:24am    
You should run your code against the sample input and check why it doesn't work as expected. That is, please debug your code.
1. Your constructor is wrong, since it only sets Age = intialAge; if intialAge is less than 0. Check where your closing brace is positioned.
2. Your yearPasses function requires four calls to increment the Age value correctly. Read the documentation on prefix and postfix operators. A better way of writing it would be:
Java
public void yearPasses() {
        // Increment this person's age.
        Age += 1;
}

3. You test for Age < 13 and Age > 13 but never Age == 13. You know that Age will always be greater or equal to zero, so you only need to test for the other values. You should also use if ... else if ... else rather than a lot of single if clauses.
 
Share this answer
 
v2
Bug report:
Your code fail for Age=13

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900