Wrong output

Discussion in 'Java' started by bsudhir6, Sep 21, 2010.

  1. bsudhir6

    bsudhir6 New Member

    Joined:
    Apr 4, 2010
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    Enc.java

    Code:
    package Obj5_1;
     
    public class Enc
    {
                    //Instance variables declared as private
                    private int x=6;
                    private char c='s';
                    private float f=5f;
                    //The accessor and mutator methods
                    public void setX(int val)
                    {
                                    x=val;
                    }
                    public int getX()
                    {
                                    return x;
                    }
                    public void setC(char val)
                    {
                                    x=val;
                    }
                    public char getC()
                    {
                                    return c;
                    }
                    public void setF (float val)
                    {
                    f=val;
                    }
                    public float getF()
                    {
                                    return f;
                    }
    }
     
                    class EncEx
                    {
                                    public static void main(String[] args)
                                    {
                                                    int i;
                                                    char j;
                                                    float k;
                                                    Enc E=new Enc();
                                                    System.out.println("The  values of i,j,k before operation:");
                                                    i=44;
                                                    j='d';
                                                    k=43f;
                                                     System.out.println("i="+i);
                                                     System.out.println("j="+j);
                                                     System.out.println("k="+k);
                                                    System.out.println("The  values of i,j,k after get operation:");
                                                    i=E.getX();
                                                    j=E.getC();
                                                    k=E.getF();
                                                     System.out.println("i="+i);
                                                     System.out.println("j="+j);
                                                     System.out.println("k="+k);
                                                    System.out.println("The  values of x,c,f after set operation:");
                                                    E.setX(45);
                                                    E.setC('S');
                                                    E.setF(6.45f);
                                                     System.out.println("x="+E.getX());
                                                     System.out.println("c="+E.getC());
                                                     System.out.println("f="+E.getF());
                                    }
                    }
    Output:
    The values of i,j,k before operation:
    i=44
    j=d
    k=43.0
    The values of i,j,k after get operation:
    i=6
    j=s
    k=5.0
    The values of x,c,f after set operation:
    x=83
    c=s
    f=6.45

    X value is supposed to be 45 not 83 please help me with this…
     
    Last edited by a moderator: Sep 21, 2010
  2. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    I think that the reason is quite obvious!
     
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice