Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-26-2012, 02:21 AM   PM User | #1
dannyboi
New Coder

 
Join Date: Jul 2012
Location: NYC
Posts: 23
Thanks: 1
Thanked 0 Times in 0 Posts
dannyboi is an unknown quantity at this point
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..
dannyboi is offline   Reply With Quote
Old 07-26-2012, 12:03 PM   PM User | #2
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
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;
        }
    }
}
Quote:
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();
Quote:
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
alykins is offline   Reply With Quote
Reply

Bookmarks

Tags
java, java programming

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:02 PM.


Advertisement
Log in to turn off these ads.