Results 1 to 7 of 7
Thread: Receive data from an array
- 01-27-2011, 12:12 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 18
- Rep Power
- 0
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; } }When I input 1 for my radius, here is my output: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()); } } }
Diameter: [D@fefe3f
Circumference: [D@e61a35
Area: [D@c2b2f6
What am I doing wrong?
- 01-27-2011, 12:32 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 01-27-2011, 12:33 AM #3
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.
- 01-27-2011, 12:35 AM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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:
or this:Java Code:System.out.println("Diameter: " + circle.GetProperties()[0]); System.out.println("Circumference: " + circle.GetProperties()[1]); System.out.println("Area: " + circle.GetProperties()[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?Java Code:double[] properties = circle.GetProperties(); System.out.println("Diameter: " + properties[0]); System.out.println("Circumference: " + properties[1]); System.out.println("Area: " + properties[2]);
-Gary-
- 01-27-2011, 12:45 AM #5
Member
- Join Date
- Jan 2011
- Posts
- 18
- Rep Power
- 0
Thank you all for your replies.
Duly noted.
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.
- 01-27-2011, 12:49 AM #6
When will teachers learn NOT to use bad designs to teach concepts?
- 01-27-2011, 12:50 AM #7
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Similar Threads
-
how to receive all data with UDP client
By simplo in forum NetworkingReplies: 16Last Post: 08-19-2010, 02:14 PM -
How to receive byte array from Server?
By k80sg in forum New To JavaReplies: 0Last Post: 03-18-2010, 08:37 AM -
Java code that allows me to make and receive calls, send and receive sms
By nareshbabu@live.in in forum NetworkingReplies: 0Last Post: 12-02-2008, 10:55 AM -
how to send and receive data from servlet to applet Continuously ??
By jega_ms in forum Java ServletReplies: 1Last Post: 01-28-2008, 10:49 AM -
PC receive data via Bluetooth using JAVA
By toncoolx in forum New To JavaReplies: 0Last Post: 11-27-2007, 04:50 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks