Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Output is Wrong .How to fix it?

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    My Mood
    Sleepy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Output is Wrong .How to fix it?

    class A
    {
    int i;
    int j;
    void funA()
    {
    int z=i+j;
    System.out.println("z value is:"+z);
    }
    }
    class B extends A
    {
    int i,k;
    void funB()
    {
    int x=i-k;
    System.out.println("x value is:"+x);
    }
    public static void main(String[] args)
    {
    B b1= new B();
    b1.i=20;
    b1.j=10;
    b1.k=5;
    b1.funA();
    b1.funB();

    }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Output is Wrong .How to fix it?

    a) Please wrap your code in the code tags to preserve formatting b) you forgot to tell us what output you expect.

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Location
    Nigeria, Lagos
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Output is Wrong .How to fix it?

    Atleast specify what type of output you're expecting

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Output is Wrong .How to fix it?

    Crystal ball says both classes have a variable called i.
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Jul 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Output is Wrong .How to fix it?

    Quote Originally Posted by cmen535 View Post
    class A
    {
    	int i;
    	int j;
    	void funA()
    	{
    		int z=i+j;
    		System.out.println("z value is:"+z);
    	}
    }
    class B extends A
    {
    	int i,k;
    	void funB()
    	{
    		int x=i-k;
    		System.out.println("x value is:"+x);
    	}
    	public static void main(String[] args)
    	{
    		B b1= new B();
    		b1.i=20;
    		b1.j=10;
    		b1.k=5;
    		b1.funA();
    		b1.funB();
     
    	}
    }
    Since we don't know what you're expecting, and you don't provide any feedback, the only bad thing I see here is that B extends A and it has the same variable.

Similar Threads

  1. My output is in the wrong order! (if-else) PLEASE HELP
    By Carecinos in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 26th, 2013, 09:38 AM
  2. My loop compiles and seems correct, but gives wrong output.
    By new2.java in forum What's Wrong With My Code?
    Replies: 8
    Last Post: February 15th, 2013, 08:35 PM
  3. My loop compiles and seems correct, but gives wrong output.
    By new2.java in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 14th, 2013, 10:31 PM
  4. [SOLVED] Extra spaces in output - why & how to fix?
    By KL1209 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 7th, 2012, 11:44 PM
  5. Sudoku: wrong output?
    By lisa92 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: April 15th, 2012, 12:10 PM