Ok, remember the last thread I made about the Circle problem http://www.c-sharpcorner.com/Forums/Thread/164425/circle-class.aspx
Well this is continuation, this time I have do this "Using the Cirlce class for the last assignment, implement the constructors and give the Circle a point"
So, I got the Point class
============Point class==============================
/**
* This class represents a single 2D x,y coordinate on a
* x/y mapping system.
*
* @author
*/
public class Point
{
private int x;
private int y;
/**
* Assigns 0 to a constructor
*/
public Point()
{
this.x = 0;
this.y = 0;
}
/**
* The method Point returning values for x and y
* @param x returns x
* @param y returns y
*/
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
/**
* @return the x
*/
public int getX()
{
return x;
}
/**
* @param x the x to set
*/
public void setX(int x)
{
this.x = x;
}
/**
* @return the y
*/
public int getY()
{
return y;
}
/**
* @param y the y to set
*/
public void setY(int y)
{
this.y = y;
}
/**
*
* @param x
* @param y
*/
public void move(int x, int y)
{
this.x = x;
this.y = y;
}
/**
*
* @param otherLocation
*/
public void move(Point otherLocation)
{
this.x = otherLocation.getX();
this.y = otherLocation.getY();
}
/**
*
* @param deltaX
* @param deltaY
*/
public void shift(int deltaX, int deltaY)
{
this.x = this.x + deltaX;
this.y = this.y + deltaY;
}
===============Circle class==================
/**
* This class represents a single 2D x,y coordinate on a
* x/y mapping system.
*
* @author
*/
public class Point
{
private int x;
private int y;
/**
* Assigns 0 to a constructor
*/
public Point()
{
this.x = 0;
this.y = 0;
}
/**
* The method Point returning values for x and y
* @param x returns x
* @param y returns y
*/
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
/**
* @return the x
*/
public int getX()
{
return x;
}
/**
* @param x the x to set
*/
public void setX(int x)
{
this.x = x;
}
/**
* @return the y
*/
public int getY()
{
return y;
}
/**
* @param y the y to set
*/
public void setY(int y)
{
this.y = y;
}
/**
*
* @param x
* @param y
*/
public void move(int x, int y)
{
this.x = x;
this.y = y;
}
/**
*
* @param otherLocation
*/
public void move(Point otherLocation)
{
this.x = otherLocation.getX();
this.y = otherLocation.getY();
}
/**
*
* @param deltaX
* @param deltaY
*/
public void shift(int deltaX, int deltaY)
{
this.x = this.x + deltaX;
this.y = this.y + deltaY;
}
==============Main Class================
/**
*
* @author Alexander Trowell
*/
public class CircleDemo {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
double radius; // To hold a radius
int pointX;
int pointY;
// Create a Scanner object for keyboard input
Scanner scan = new Scanner(System.in);
// Get the number for the radius
System.out.println("Please enter the circle's radius");
radius = scan.nextDouble();
scan.nextLine();
System.out.println("Please enter the circle's X point");
pointX = scan.nextInt();
scan.nextLine();
System.out.println("Please enter the circle's Y point");
pointY = scan.nextInt();
// Create an instance of the Circle class,
// passing the data that was entered as arguments
// to the constructor.
Circle circle1 = new Circle(radius);
Point point = new Point(pointX,pointY);
// Output
System.out.println("The area of the circle with radius of " + radius + " is " + circle1.getArea()
+ " with diameter of " + circle1.getDiameter() + " and with circumference of " + circle1.getCircumference()+" and circle located in the "+point);
}
The problem is when you run it , this is what you get ("
The area of the circle with radius of 25.0 is 1963.4937499999999 with diameter of 50.0 and with circumference of 157.0795 and circle located in the Point@1b67f74")
Point@1b67f74 supposed to the points the user gave, but for some reason it gives this gibberish.
Loading
VulpesPosted Mar 14, 2012, 8:06 PM
You won't then get the gibberish caused by trying to print the Point object itself.
Prime bPosted Mar 14, 2012, 8:29 PM
Point point = new Point(pointX,pointY);
// Output
System.out.println("The area of the circle with radius of "
+ radius +" is " + circle1.getArea()+ " with diameter of "
+ circle1.getDiameter() + " and with circumference of "
+ circle1.getCircumference() + " and circle located in the "
+ point.getX()+point.getY());
Prime bPosted Mar 14, 2012, 8:27 PM
But see, as I understood he wants just ask the user for points and then call the constructors of point class?
But even then i cant get it to display.
This is what where i request for the input , right.
System.out.println("Please enter the circle's radius");
radius = scan.nextDouble();
scan.nextLine();
System.out.println("Please enter the circle's X point");
pointX = scan.nextInt();
scan.nextLine();
System.out.println("Please enter the circle's Y point");
pointY = scan.nextInt();
then i tried to create new point object and put pointX, pointY in the parameter of the class(call the constructor), like this
Point point = new Point(pointX,pointY);
and then just do
System.out.println("The area of the circle with radius of "
+ radius +" is " + circle1.getArea()+ " with diameter of "
+ circle1.getDiameter() + " and with circumference of "
+ circle1.getCircumference() + " and circle located in the "
+ point);
But when I run the debugger point variable has the gibberish number... So i created a method called points with two parameters and even then it doesn't work.
public void points(int x, int y)
{
this.x = x;
this.y = y;
}
public class Circle {
// Fields
private double radius; // Radius of the circle
final private double PI = 3.14159; // The value of the PI
/**
* Constructor
*
* @param radius The circles radius
*/
public Circle(double radius) {
this.radius = radius;
}
/**
* Constructor No arg constructor that the sets the radius field to 0.0
*/
public Circle() {
radius = 0.0;
}
/**
* The setRadius method sets the Circles radius
*
* @param radius returns radius of the circle
*/
public void setRadius(double radius) {
this.radius = radius;
}
/**
* The getRadius method
*
* @return the radius of the circle
*/
public double getRadius() {
return radius;
}
/**
* The getArea method
*
* @return the area of the circle
*/
public double getArea() {
return PI * radius * radius;
}
/**
* The getDiameter method
*
* @return the diameter of the circle
*/
public double getDiameter() {
return radius * 2;
}
/**
* The getCircumference method
*
* @return the circumference of the circle
*/
public double getCircumference() {
return 2 * PI * radius;
}