Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2012
    Location
    NYC
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    What is wrong with this piece of code?

    In Java
    public class Main
    {

    public static void main(String[] args)
    {



    Ball b1 = new Ball ();
    Ball b2 = b1;

    b1.setSpeed(50);
    b2.setSpeed (100);
    int speed = b1.getSpeed();
    }

    }
    }
    Last edited by dannyboi; 07-26-2012 at 06:04 PM.

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,739
    Thanks
    41
    Thanked 191 Times in 190 Posts
    no clue- what error message are you getting? what is 'wrong' with it? what does the 'Ball' class look like... assuming that Ball has those properties, and also assuming that Ball has those methods... (and assuming this is C# and not Java)
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace testing
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("start...");
    
                Ball b1 = new Ball();
                Ball b2 = b1;
    
                b1.setSpeed(50);
                b2.setSpeed(100);
                int speed1 = b1.getSpeed();
                int speed2 = b2.getSpeed();
    
                Console.WriteLine("Speed of first ball (b1) is {0}", speed1);
                Console.WriteLine("Speed of second ball (b2) is {0}", speed2);
    
                Console.WriteLine("fin...");
                Console.ReadLine();
            }
        }
    
        public class Ball
        {
            private int _speed;
    
            public Ball()
            {
                this.setSpeed(0);
            }
    
            private int speed
            {
                get
                {
                    return _speed;
                }
                set
                {
                    _speed = value;
                }
            }
    
            public void setSpeed(int i)
            {
                this.speed = i;
            }
    
            public int getSpeed()
            {
                return this.speed;
            }
        }
    }
    Now because b2 is not declared as new Ball() it will output
    start...
    Speed of first ball (b1) is 100
    Speed of second ball (b2) is 100
    fin...
    but by declaring b2 as a new Ball (and thus new object and new resources)

    Code:
    // change this
    Ball b1 = new Ball();
    Ball b2 = b1;
    
    // to this
    Ball b1 = new Ball();
    Ball b2 = new Ball();
    the output would then be
    start...
    Speed of first ball (b1) is 50
    Speed of second ball (b2) is 100
    fin...
    But, without more information- I am just shooting in the dark

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •