Results 1 to 3 of 3

Thread: nid help JAVA

  1. #1

    Default nid help JAVA


    guys medyo naa pa jud ko probz regarding sa java

    any1 can help me understand sa program

    Give the output of each number based on the class Compute

    class Compute {
    private static double x;
    public static double y;
    private double sum;

    public Compute(){
    sum = 0;
    }

    public static void setX(double a){
    x=a;
    }

    public double computeSum(){

    sum= x+y;
    return sum;
    }

    public String toString(){
    return "x = " +x + "y="+y
    + "sum=" + sum;
    }

    public static void displayxy(){
    System.out.println("x="+x+"y="+y);
    }

    public static void Incrementx(){
    x++;
    }
    }

    unsay answer ani if...

    1.
    Compute.y++;
    Compute.setx(9);
    Compute.IcrementX();
    Compute.displayxy();

    2.
    Compute c = new Compute();
    c.setX(4);
    c.y++;
    c.computeSum();
    System.out.println(c);

    3.
    Compute c = new Compute();
    c.setX(4);
    Compute.Incrementx();
    c.computesum();
    System.out.println(c);

    4.
    Compute c = new Compute();
    c.setX(4);
    Compute.IncrementX();
    Compute.y = 7;
    c.computeSum();
    System.ou.println(c);

    5.
    Compute c = new Compute();
    Compute.setX(1);
    Compute.IncrementX();
    Compute.y = 2;
    c.computeSum();
    System.out.println(c);

    6.
    Compute c = new Compute();
    Compute.setX(3);
    Compute.IncrementX();
    Compute.y + c.computeSum() + 2;
    c.displayXY();

    7.
    Compute c = new Compute();
    Compute.setX(3);
    Compute.IncrementX();
    Compute.y = c.computeSum() + 2;
    Compute.setX(c.computeSum());
    System.out.println(c);

    unsay mga output ani?? libug njud ko wa jud ko kasabot pa
    den pwed sad ninyo e explain f g.unsa ninyo pag kuha?? enx agen guys!

  2. #2
    i'll try to explain my answers to Nos. 1 & 2 then you're on your own.





    1.
    Code:
    Compute.y++;
    // y field gets incremented from 0 to 1
    Code:
    Compute.setx(9);
    // sets the value of x field to 9
    Code:
    Compute.IcrementX();
    // x gets incremented from 9 to 10
    Code:
    Compute.displayxy();
    // the display output would be "x=10.0y=1.0"

    accessing a static fields and methods this way should be okay.
    btw, static field changes reflect across objects. meaning that if you have two objects of Compute, the change you made in field y changes both fields y of the two objects. not necessarily two copies of y of each objects maybe. but you don't have to worry about that now kay isa rmn ka object ang gina-create so far.


    2.
    Code:
    Compute c = new Compute();
    // this line of code creates a reference to a new object and stores it into a variable named c. an object reference is like a remote control. you can do something like for example changing the channel of the tv set using the variable c. hihi

    just like this visually:


    Code:
                 Compute
               ___________
               | x = 0   |
    c[.].....> | y = 0   |
               | sum = 0 |
               -----------

    Code:
    c.setX(4);
    // this line uses the remote control c to do setX method of the Compute object. this method assigns the x field of compute to the value you put inside () (called parameter) when you call it.

    the result would look like this:

    Code:
                 Compute
               _____________
               | x = 4.0   |
    c[.].....> | y = 0     |
               | sum = 0   |
               -------------
    Code:
    c.y++;
    // this line is obvious, it just increments the value of y. the result would look like this:

    Code:
                 Compute
               _____________
               | x = 4.0   |
    c[.].....> | y = 1.0   |
               | sum = 0   |
               -------------


    Code:
    c.computeSum();
    // this line executes computeSum method body. it's odd that this method is declared to return a value and used like this.

    after that method gets executed, result would be:

    Code:
                 Compute
               _____________
               | x = 4.0   |
    c[.].....> | y = 1.0   |
               | sum = 5.0 |
               -------------
    Code:
    System.out.println(c);
    // you really have to learn method overriding and overloading lessons to completely understand what this line of code does. basically, this calls toString() method. your tostring method just prints and so the result of print would look like this:

    x = 4.0y=1.0sum=5.0

  3. #3
    ok thankz!

    ako e try na mka kuha njud ko ani ^_^

  4.    Advertisement

Similar Threads

 
  1. nid help..java programming
    By ronninalpha in forum Programming
    Replies: 3
    Last Post: 09-13-2006, 07:55 PM
  2. nid help for assembly language prog..
    By tanilong in forum Programming
    Replies: 1
    Last Post: 02-23-2006, 02:54 PM
  3. nid help for my FF7 pc version
    By tanilong in forum Software & Games (Old)
    Replies: 9
    Last Post: 11-14-2005, 12:49 PM
  4. canon powershot a520 or ixus30?... nid help..
    By for_real in forum Gizmos & Gadgets (Old)
    Replies: 5
    Last Post: 10-08-2005, 10:26 AM
  5. Replies: 2
    Last Post: 09-14-2005, 08:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top