I have task to implement some methods to make math operations and etc. I'm fine with that part. But I cannot get how and where I have to create other part of code to make program working. Do I have to create Point objects by myself to access cordinates to make calculation?
How can I access x and y of each Point in public Triangle?
class Triangle {
public Triangle(Point a, Point b, Point c) {
//TODO
}
public double area() {
//TODO
}
public Point centroid(){
//TODO
}
}
class Point {
private double x;
private double y;
public Point(final double x, final double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
}
public class Main {
public static void main(String[] args) {
{
double area = new Triangle(new Point(0, 0), new Point(3, 0), new Point(0, 4)).area();
System.out.println(area);
}
{
Point centroid = new Triangle(new Point(0, 0), new Point(3, 0), new Point(0, 3)).centroid();
System.out.println(centroid.getX());
System.out.println(centroid.getY());
}
}
}
Related
There is the code
public class Main {
public static void main(String[] args) throws InterruptedException {
// A running ball
class Ball {
private double x, y, radius, xvel, yvel;
private long timeframe;
private boolean moving;
public Ball(double x, double y, double radius, double xvel, double yvel, long timeframe) throws InterruptedException {
super();
this.x = x;
this.y = y;
this.radius = radius;
this.xvel = xvel;
this.yvel = yvel;
this.timeframe = timeframe;
this.moving = true;
while(moving) {
// The next print line is what i want to run outside of this constructor
System.out.println("x:" + getX() + " " + "y:" + getY());
move();
Thread.sleep(timeframe);
}
}
private void move() {
x += xvel;
y += yvel;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public void setXvel(double xvel) {
this.xvel = xvel;
}
public void setYvel(double yvel) {
this.yvel = yvel;
}
}
Ball ball = new Ball(0, 0, 1, .5, .5, 1000);
}
}
Now i want to access ball's x and y field while it
is running. I cannot do ball.getX() here because
there is a loop inside of the constructor that
to finish first.
how can i do this while the loop is running.
have an idea of using threads but i can't see
implementation details..
Good evening Mac JalLoh,
I think what you are searching for is the synchronized Java keyword:
https://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html
along with the Executor framework introduced in Java 7:
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executor.html
In brief, if you want the ball moving, and if an exterior observer has to read the trajectory, you probably need two different threads, the two of them being synchronized when accessing the same object. The fact is in Java you can't modify the position of the ball AND read it at the same time. Why ? Because you can never be sure as of when the position has been updated. This causes uncertainty in position-reading and writing (look up to thread-safety in google). We do not like uncertainty so we put synchronized keyword (you can say that it is first-arrived first-served).
Also, it is not a good practice in my opinion to include the ball mouvement in the Ball constructor. In fact, the constructor answers the question "What is a ball (a 3D object with origin and radius)", whereas the translation or rotation movements answer the physical problem of motion. These two problems needs to be decorrelated (you could introduce an external object that acts on the Ball instance)
I think i finally found the solution to what i needed.There is the code
public class Main {
// a running ball
public class Ball extends Circle implements Runnable{
private double xvel, yvel;
private long timeframe;
private boolean moving = true;
public boolean isMoving() {
return moving;
}
public void setMoving(boolean moving) {
this.moving = moving;
}
public Ball(double x, double y, double radius, double xvel, double yvel, long timeframe) {
super(new Point(x, y), radius);
this.xvel = xvel;
this.yvel = yvel;
this.timeframe = timeframe;
new Thread(this).start();
}
#Override
public void run() {
while(moving) {
move(xvel, yvel);
try {
Thread.sleep(timeframe);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public double getX() {
return getCenter().getX();
}
public double getY() {
return getCenter().getY();
}
public void setXvel(double xvel) {
this.xvel = xvel;
}
public void setYvel(double yvel) {
this.yvel = yvel;
}
}
public static void main(String[] args) throws InterruptedException {
// a Ball fields getter
class Getter implements Runnable{
Ball ball;
public Getter(Ball ball) {
this.ball = ball;
}
public void run() {
while(ball.isMoving()) {
System.out.println(ball.getX() + " " + ball.getY());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Ball ball = new Main().new Ball(0, 0, 1, .5, .5, 1000);
Thread getter = new Thread(new Getter(ball));
getter.start();
}
}
And i like putting the moving mecanism inside of the constructor because it looks more natural with respect to what i am modeling, that is a moving ball that need not an external actor to make it move!
Could someone explain to me why I am getting these errors on my coding when it comes to the main method project5 class. thearray[count++] keeps giving me errors, and I am not entirely sure where I made my error. I've been stuck on it for awhile now. Some guidance would be nice.
Here is my main method:
public class Project5 {
private Shape [] thearray = new Shape[100];
public static void main (String [] args) {
Project5 tpo = new Project5();
tpo.run();
}
public void run () {
int count = 0;
thearray[count++] = new Circle(20, 20, 40);
thearray[count++] = new Triangle(70, 70, 20, 30);
thearray[count++] = new Rectangle(150, 150, 40, 40);
for (int i = 0; i < count; i ++ ) {
thearray[i].display();
}
int offset = 0;
double totalarea = 0.0;
while (thearray[offset] != null) {
totalarea = totalarea + thearray[offset].area();
offset++;
}
System.out.println("The total area for " + offset + " Shape objects is " + totalarea);
}
}
Here is my Circle class:
public class Circle {
private double radius;
public Circle() {
radius = 1.0;
}
public Circle(double newRadius) {
radius = 1.0;
setRadius(newRadius);
}
public void setRadius(double newRadius) {
if(newRadius > 0) {
radius = newRadius;
}
else {
System.out.println("Error: "
+ newRadius + " is a bad radius value.");
}
}
public double getRadius() {
return radius;
}
public double getArea() {
return radius * radius * Math.PI;
}
}
and here is my Shape class:
abstract class Shape{
int x = 1;
int y = 1;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Shape(int x, int y) {
this.x = x;
this.y = y;
}
public void display() {
}
public abstract double area();
}
I would post my triangle and rectangle class, but I figured this would be enough to at least have it explained where I am messing up.
I looked at your Circle class and the constructor currently only takes one argument. However, when you create a Circle you are specifying 3 arguments.
Circle should probably extend from Shape and the first two parameters are then the x and y position of the shape, correct? If this is the case, change your class as follows:
public class Circle extends Shape {
private double radius;
public Circle(int x, int y) {
super(x, y);
radius = 1.0;
}
public Circle(int x, int y, double newRadius) {
super(x, y);
setRadius(newRadius);
}
...etc
I get an error when i try to add a new point into my ArrayList
Here is my coder i don t get it why there is an error.
here is my Point class
public class Point {
public int X;
public int Y;
public Point(int X, int Y) {
this.X = X;
this.Y = Y;
}
public int getX() {
return X;
}
public void setX(int x) {
X = X;
}
public int getY() {
return Y;
}
public void setY(int y) {
Y = y;
}
// Field-Area to be Colored
public Point Color_Field(Point a) {
Point fieldAreaColor= new Point(0, 0);
fieldAreaColor.setX(a.getX() + 5);
fieldAreaColor.setX(a.getY() + 5);
return fieldAreaColor;
}
}
and here is my List Class weher i try to put new points into my
list. which doesn t works.
package Handler;
import java.awt.List;
import java.util.ArrayList;
import Basic_Geom.Point;
public class Liste {
ArrayList<Point> points=new ArrayList<Point>();
points.add(new Point(2,3)); //here is my error i can t add a point to my list.
}
A statement such as points.add(new Point(2,3)); must be inside some method/constructor.
I'm having a hard time figuring out why when I create an Circle instance with no arguments and adding the values after with setters isn't working? When I delete the line Circle c3 = new Circle() my program works fine. I also can't call the getters to see if the setters worked.
public class MyCircle {
private double radius;
private double x;
private double y;
public MyCircle()
{
x = 0;
y = 0;
radius = 0;
}
public MyCircle(double X, double Y, double rad)
{
x = X;
y = Y;
radius = rad;
}
public void setX(double value)
{
x = value;
}
public void setY(double value)
{
y = value;
}
public void setRadius(double value)
{
radius = value;
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
public double getRadius()
{
return radius;
}
public double getArea()
{
return (radius * radius) * Math.PI;
}
public boolean doesOverlap(MyCircle oC)
{
double distance = Math.sqrt((Math.pow(x - oC.x, 2) + Math.pow(y-oC.y, 2)));
if ((radius + oC.radius) > distance)
{
return true;
}
else
{
return false;
}
}
public static void main(String[] args) {
Circle c3 = new Circle();
Circle c1 = new Circle(1.0, 2.0, 3.0);
Circle c4 = new Circle(1.0, 6.0, .5);
c3.setX(1.0);
c3.setY(2.0);
c3.setRadius(5.0);
System.out.println(c3.getArea());
System.out.println(c1.getRadius());
System.out.println(c1.toString());
System.out.println(c4.toString());
if (c1.equals(c4))
{
System.out.println("c1 equals c4");
}
else
{
System.out.println("c1 does not equal c4");
}
if (c1.doesOverlap(c4))
{
System.out.println("c1 Overlaps c4");
}
else
{
System.out.println("c1 Does Not Overlap c4");
}
}
}
Your class is called MyCircle not Circle. You are trying to create Circle objects.
Replace:
Circle c3 = new Circle();
With:
MyCircle c3 = new MyCircle();
your class name is MyCircle instead of Circle
change the name of the class for Circle
Or change where you instantiate for MyCircle
If I have a constructor called Point inside class Point:
public Point(double x, double y) {
this.x = x;
this.y = y;
}
How can I use it another class called Square and initialize the points. For example, I want to initialize 4 points in a square. How can I do that?
I don't know if that makes sense. But, I tried my best... ask me questions so that I can explain better.
Your Square class should have a constructor like this:
public Square(Point p1, Point p2, Point p3, Point p4) {
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
this.p4 = p4;
}
You initialize your Square like this:
Square s = new Square(new Point(1,1), new Point(2,2), new Point(3,3), new Point(4,4));
If you want to have a Square with for points, make them attributes:
class Square {
Point p1, p2, p3, p4;
public Square() {
p1 = new Point(0,0);
p2 = new Point(0,0);
p3 = new Point(0,0);
p4 = new Point(0,0);
}
}
Of course, there are a zillion other ways of defining and using this. It will depend primarily on your class / program design what you should choose.
I do not see the problem. Your Square would simply have four members of type Point which would be initialized using the usual new-syntax:
class Square {
Point topLeft;
public Square() {
topLeft = new Point(0,0);
}
}
You can do
public Shape(Point... points) {
this.points = points;
}
or
public Quadrilateral(Point p1, Point p2, Point p3, Point p4) {
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
this.p4 = p4;
}
or
public Square(double x, double y, double size) {
this.x = x;
this.y = y;
this.size = size;
}
I'm not sure I follow, but if you have a constructor like you say, you can simply call new Point(10.0, 15.0) to create a point at the designated coordiantes.
Maybe it's something like this that you are after?
class Square {
private Point upperLeftCorner;
private Point upperRightCorner;
private Point lowerLeftCorner;
private Point lowerRightCorner;
public Square(double x, double y, double size) {
upperLeftCorner = new Point(x, y);
lowerLeftCorner = new Point(x, y+size);
upperRightCorner = new Point(x+size, y);
lowerRightCorner = new Point(x+size, y+size);
}
}
Is this what you mean?
java GeoTest
[{0.0,0.0}, {0.0,10.0}, {10.0,10.0}, {10.0,0.0}]
Code:
import java.util.Arrays;
public class GeoTest{
public static void main(String[] args){
System.out.println(Arrays.toString(new Square(new Point(0,0), new Point(10,10)).getCourners()));
}
}
class Point{
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public String toString(){
return new String("{"+x+","+y+"}");
}
public double getX(){
return x;
}
public double getY(){
return y;
}
}
class Square {
private Point lowerLeft;
private Point upperRight;
/**
* Assuming x & y axis as follows:
* Lower left corner (x,y):0,0 -> upper right corner (x,y): n,n where n > 0
*/
public Square(Point lowerLeft, Point upperRight){
this.lowerLeft = lowerLeft;
this.upperRight = upperRight;
}
public Point[] getCourners(){
return new Point[]{lowerLeft, new Point(lowerLeft.getX(),upperRight.getY()),upperRight, new Point(upperRight.getX(), lowerLeft.getY())};
}
}