Results 1 to 7 of 7
  1. #1
    phixion is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default Receive data from an array

    I've only been programming in Java for three weeks, so take it easy on me.

    I have created an array inside of a class. I want to receive the values of that array from within another class. The programs compile, but the answers are wrong.

    Java Code:
    public class Circle
    {
        private double radius; //instance variable
    
        /**
         * Constructor for objects of class Circle
         */
        public Circle()
        {
            radius = 0; //initialise instance variables
        }
        
        public Circle(double r)
        {
            radius = r;
        }
    
        public void SetRadius(double r) 
        //modifying method allowing you to assign a new value to the variable
        {
            radius = r;
        }
        
        public double[] GetProperties()
        {
            double[] Properties = new double[3];
            
            Properties[0] = 2 * radius; //Diameter
            Properties[1] = 2 * Math.PI * radius; //Circumference
            Properties[2] = Math.PI * Math.pow(radius, 2); //Area
            
            return Properties;
        }
    }
    Java Code:
    import java.util.Scanner;
    
    public class CircleTester
    {
        public static void main (String[]args)
        {
            Scanner keyboard = new Scanner(System.in);
            
            while (true)
            {
                System.out.print("Please enter the radius: ");
                
                Circle circle = new Circle(keyboard.nextDouble());
    
    
                //Below, shouldn't I be declaring which position in the array I want
               // to access from?
    
                System.out.println("Diameter: " + circle.GetProperties());
                System.out.println("Circumference: " + circle.GetProperties());
                System.out.println("Area: " + circle.GetProperties());
            }       
        }
    }
    When I input 1 for my radius, here is my output:

    Diameter: [D@fefe3f
    Circumference: [D@e61a35
    Area: [D@c2b2f6

    What am I doing wrong?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    //Below, shouldn't I be declaring which position in the array I want
    // to access from?

    Yes, you should.

    Try

    Java Code:
    double[] props = circle.GetProperties();
    System.out.println("Diameter: " + props[0]);
    // etc

    Those [D@hhhhhh things are Java's way of naming double arrays.

    You should get into the habit of starting methods and variables with a lowercase letter: properties, getProperties() etc.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    When you attempt to print an array, all the values do not get automagically displayed for you. You have to use a loop to iterate over the array and print each element one at a time.

    For an explanation of what you are seeing read the toString method of Object in the Java API. What your code is doing is "print array". Since array does not have a toString method it uses the toString method it inherits from Object.

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Thanks for the proper use of CODE tags.

    As you suggest in your comment, you need to indicate which element of the array you want. You can either do this:
    Java Code:
                System.out.println("Diameter: " + circle.GetProperties()[0]);
                System.out.println("Circumference: " + circle.GetProperties()[1]);
                System.out.println("Area: " + circle.GetProperties()[2]);
    or this:
    Java Code:
                double[] properties = circle.GetProperties();
                System.out.println("Diameter: " + properties[0]);
                System.out.println("Circumference: " + properties[1]);
                System.out.println("Area: " + properties[2]);
    But why in the world would you want to do either? Why not simply have separate getDiameter(), getCircumference() and getArea() methods? What do you gain from the array?

    -Gary-

  5. #5
    phixion is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    Thank you all for your replies.

    Quote Originally Posted by pbrockway2 View Post

    You should get into the habit of starting methods and variables with a lowercase letter: properties, getProperties() etc.

    Duly noted.


    Quote Originally Posted by gcalvin View Post
    But why in the world would you want to do either? Why not simply have separate getDiameter(), getCircumference() and getArea() methods? What do you gain from the array?

    -Gary-

    This is a modification of a previous program. In that program, we did have separate methods (getDiameter, etc). For this assignment, we are simply introducing arrays by modifying that existing program and storing the information in an array.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    When will teachers learn NOT to use bad designs to teach concepts?

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by phixion View Post
    This is a modification of a previous program. In that program, we did have separate methods (getDiameter, etc). For this assignment, we are simply introducing arrays by modifying that existing program and storing the information in an array.
    OK. I'm not a fan of instructors that require their students to do silly things, but I understand that there are plenty of silly instructors out there. A much more useful assignment would be to do something with an array of Circle objects.

    -Gary-

Similar Threads

  1. how to receive all data with UDP client
    By simplo in forum Networking
    Replies: 16
    Last Post: 08-19-2010, 02:14 PM
  2. How to receive byte array from Server?
    By k80sg in forum New To Java
    Replies: 0
    Last Post: 03-18-2010, 08:37 AM
  3. Replies: 0
    Last Post: 12-02-2008, 10:55 AM
  4. Replies: 1
    Last Post: 01-28-2008, 10:49 AM
  5. PC receive data via Bluetooth using JAVA
    By toncoolx in forum New To Java
    Replies: 0
    Last Post: 11-27-2007, 04:50 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
  •