This is going to be quite long winded as I'm still relatively very new to Java as a language.
This program has a Class called Point. This creates Points for vertices of shapes.
public class Point {
private int x;
private int y;
public Point(int x, int y){
this.x = x;
this.y = y;
}
In this class, there are methods such as distance (between 2 points), scale(increases by factor), translate(takes an x and y coordinate and adds it to this.x and x.y) and so on.
I then have 3 more classes to create shapes. Triangle, Rectangle and Circle, where the constructors look like this.
public class Triangle {
private int sides;
private Point[] points;
public Triangle(Point[] vertices) {
this.sides = 3;
this.points = vertices;
}
.
public class Rectangle {
private int sides;
private Point topCorner;
private int width;
private int length;
public Rectangle(Point corner, int w, int l){
this.sides = 4;
this.topCorner = corner;
this.width = w;
this.length = l;
}
and
public class Circle {
private Point center;
private int radius;
public Circle(Point center, int radius){
this.center = center;
this.radius = radius;
}
All these classes have the same methods translate, and scale. An example of which is shown here.
//For the Rectangle Class
public void translate(int dx, int dy){
topCorner.translate(dx, dy);
}
My problem is in my main class. What happens is I created 3 points, and used all 3 as an array to create the triangle, and 2 others as points for the rectangle and for the circle. Now when I call the translate method on all 3 objects in one go, it ends up doubling the value, as I believe my mistake is that I have passed a reference to "p1" "p2" and "p3" somewhere, and I am changing their values instead of the objects values. Here is the code, and I will explain the output after
public class GraphicsDemo {
public static void main(String[] args) {
Point p1 = new Point(0,20);
Point p2 = new Point(20,0);
Point p3 = new Point(30,30);
Point[] vertices = {p1,p2,p3};
Triangle t = new Triangle(vertices);
Rectangle r = new Rectangle(p1,10,15);
Circle c = new Circle (p2, 25);
System.out.println("Triangle" + "\n" + t.toString() + "\n" + "area:" + t.area() + "\n" + "perimeter: " + t.perimeter());
System.out.println("Reactangle" + "\n" + r.toString() + "\n" + "area:" + r.area() + "\n" + "perimeter: " + r.perimeter());
System.out.println("Circle" + "\n" + c.toString() + "\n" + "area:" + c.area() + "\n" + "perimeter: " + c.perimeter());
c.translate(10, 15);
t.translate(10, 15);
r.translate(10, 15);
System.out.println("Triangle" + "\n" + t.toString() + "\n" + "area:" + t.area() + "\n" + "perimeter: " + t.perimeter());
System.out.println("Reactangle" + "\n" + r.toString() + "\n" + "area:" + r.area() + "\n" + "perimeter: " + r.perimeter());
System.out.println("Circle" + "\n" + c.toString() + "\n" + "area:" + c.area() + "\n" + "perimeter: " + c.perimeter());;
The output for this is that instead of adding 10 and 15 to a shapes coordinates, 20 and 30 are added if the point is used in more than one shape. I'm sure that this is because I'm actually changing the value of the points instead of the objects, but I have no idea how to change this.
Thank you to anyone that can help in advance. This is the first time in my experience where I have tried a lot of things and am up against a wall.
You missed to show the most important part of your code, however it looks like your analysis is correct. In your translation code you actually need to generate new point instances (and replace them).
This is generally a good idea to not modify objects but keep them immutable. Best way to ensure this is to skip the setters:
class Point {
Point(int x, int y) { this.x = x; this.y = y; }
int getX() { return x; } intGetY() { return y; }
/** Create a new point displaced by given coordinates. */
Point translate(int deltaX, int deltaY)
{
return new Point(this.getX() + deltaX, this.getY() + deltaY);
}
}
They are indeed the same object. I.e Triangle t has p1. If you change Rectangle p1 you are changing triangle p1 as well.
You should do deep clones in Point's for them to be independent.
You assumption is correct, you are passing references. Circle, Triangle, Rectangle are all taking one or more points in common with the other shapes in their constructor. See rectangle which takes the same point (p1) as triangle.
Point[] vertices = {p1,p2,p3};
Triangle t = new Triangle(vertices);
Rectangle r = new Rectangle(p1,10,15);
Circle c = new Circle (p2, 25);
A simple solution is to clone the points in the constructors as I did in the Triangle class below. This means the two shapes will share the same x and y to start with, but changes to one shape will no longer directly affect the other.
A class is an object reference. So you need to create a new Object reference to create separate objects to work on. This is what the .clone() method I added to you point class does, it copies the internals of point and creates an exact copy. The constructors of the other methods then invoke the clone method on the points you feed them, so that they are only working on copies of the points you give them.
class Point{
public int x;
public int y;
Point(int x, int y){
this.x = x;
this.y = y;
}
public Point clone(){
return new Point(x,y);
}
}
class Triangle{
final static int m_sides = 3; //constant
Point[] m_points;
Triangle(Point[] points){
//You need to clone your ponits.
m_points = new Point[]{
points[0].clone(),
points[1].clone(),
points[2].clone()};
}
Related
I am trying to write Ray Tracing in the LWJGL to go from the mouse position to the end of the display. I have written the code to find the mouse positions and to create the Vector3's for the start and end position of the rays, but how do I 'fire' the ray and detect an object?
I have tried using Camera's for the positioning, but my friend is the one who wrote the main Display system and its pretty gargled up and hard to read. Instead I opted for just firing the ray from the mouse position inside of the Display to the back Z of the display.
public class Picker {
static Vector3f startRay;
static Vector3f endRay;
public Picker() {
startRay = new Vector3f();
endRay = new Vector3f();
}
private static Vector2f getMouseCoords() {
float x = (2f* Mouse.getX()) / Display.getWidth() - 1;
float y = (2f * Mouse.getY()) / Display.getHeight() - 1f;
return new Vector2f(x, y);
}
public static String getRayStr() {
float x = (2f* Mouse.getX()) / Display.getWidth() - 1;
float y = (2f * Mouse.getY()) / Display.getHeight() - 1f;
String str = "Mouse X: " + x + " Mouse Y: " + y;
str += "\nStart X: " + startRay.x + " Start Y: " + startRay.y + " Start Z: " + startRay.z;
str += "\nEnd X: " + endRay.x + " End Y: " + endRay.y + " End Z: " + endRay.z;
return str;
}
public static void setStartRay() {
Vector2f mousePos = getMouseCoords();
Vector3f tempStartPos = new Vector3f();
tempStartPos.x = mousePos.x;
tempStartPos.y = mousePos.y;
tempStartPos.z = 0;
startRay = tempStartPos;
}
public static void setEndRay() {
Vector2f mousePos = getMouseCoords();
Vector3f tempEndPos = new Vector3f();
tempEndPos.x = mousePos.x;
tempEndPos.y = mousePos.y;
tempEndPos.z = -1;
endRay = tempEndPos;
}
}
I want to be able to detect an object directly inbetween startRay and endRay. The object I want to detect is a 3d model inside of the Display area.
Returning the startRay and endRay Vector3 positions has already confirmed to work, just needing to know about how to:
1) Cast the ray
2) Detect an object between the two points
Thank you very much for your assistance.
Every time when I try to generate random coordinates of city in this way,
public City () {
super();
this.x = Math.random()*200;
this.y = Math.random()*200;
}
I get the same values for the X and Y, which means that all cities are on same line on map. My question is how to avoid this? How to create different coordinates? Thanks.
I assumed a class like this:
class City {
private double x;
private double y;
public City() {
super();
this.x = Math.random() * 200;
this.y = Math.random() * 200;
}
#Override
public String toString() {
return "City [x=" + x + ", y=" + y + "]";
}
}
When I create new City objects I get (as it should be expected by the Math API) different values:
City [x=10.552289272723247, y=28.548756787475504]
City [x=58.96588997141927, y=146.87205149574288]
City [x=186.69728798772306, y=179.3787764147533]
Don't get me wrong - maybe at the point where you check the values you use by mistake twice the same?
To get different double values use java.util.Random class. Below, there's an example, it prints four different double values:
java.util.Random random = new java.util.Random();
System.out.println(random.nextDouble());
System.out.println(random.nextDouble());
System.out.println(random.nextDouble());
System.out.println(random.nextDouble());
Use java.util.Random.
Example:
Random random = new Random();
int x = random.nextInt(11); // from 0 to 10
double y = random.nextDouble(); // from 0 to 1
You must do wrong something else which is not in question.
public static void main(String[] args) {
double x = Math.random() * 200;
double y = Math.random() * 200;
System.out.println(x + ":"+y);
}
consider above code snap and you will get different x and y values..!
Please make sure that you are using new keyword every time while creating object of city.
The following code is what I'm having issues understanding:
public class Rectangle {
public Rectangle() {
double width = 1;
double height = 1;
}
public Rectangle(double w, double h) {
double width = w;
double height = h;
}
public double getArea(double w, double h) {
return (w*h);
}
public double getPerimeter(double w, double h) {
return ((2*w)+(2*h));
}
public static void main(String[] args) {
Rectangle oldRectangle = new Rectangle(4, 40);
Rectangle newRectangle = new Rectangle(3.5, 35.9);
double height1 = oldRectangle.height;
double height2 = newRectangle.height;
double width1 = oldRectangle.width;
double width2 = newRectangle.width;
System.out.println("Width of Rectangle 1 is: " + 4);
System.out.println("Height of Rectangle 1 is: " + 40);
System.out.println("Area of Rectangle 1 is: " + oldRectangle.getArea(4, 40));
System.out.println("Perimeter of Rectangle 1 is: " + oldRectangle.getPerimeter(4, 40));
System.out.println("Width of Rectangle 1 is: " + 3.5);
System.out.println("Height of Rectangle 1 is: " + 35.9);
System.out.println("Area of Rectangle 1 is: " + newRectangle.getArea(3.5, 35.9));
System.out.println("Perimeter of Rectangle 1 is: " + newRectangle.getPerimeter(3.5, 35.9));
}
}
I was instructed to create two constructors for a Rectangle class—one with no arguments but assigned a default value of 1 for both variables width and height. The second constructor was to contain parameters that would take in two doubles that would get assigned to their appropriate variable.
I was then told to create two 'get()' methods that returned their respective values—in my case, they were perimeter and area of said Rectangle.
I was then instructed to create two Rectangle instances, one with a width of 4 and a height of 40—and another with a width of 4.5 and a height of 35.9. So, I did just that and made two new Rectangle objects, as you can see.
Lastly, I was instructed to print out the Width, Height, Perimeter, and Area of both Rectangle objects. My issue is that I don't know of a way to reference them. I took a beginners tutorial class for JavaScript and if I'm not mistaken, I recall there was a way to reference a property value of an object by assigning it to a variable. Again, I'm saying "If I'm not mistaken", so I could be wrong. It's been a while...
I do realize that Java and Java Script are entirely different things in their own right. Java Script was a scripting language developed and based off of Java.
Anyway, any help will be grand.
Please feel free to help me understand how I can implement what I'm trying to do by giving examples. You don't have use my exact code, but I'd like to be able to get my code to make more sense.. I'm using Eclipse btw.
You are on the right track. What you are looking to do is something like this:
public double getArea(Rectangle r){
return r.width*r.height;
}
public double getPerimeter(Rectangle r){
return (2*r.width + 2*r.height);
}
For the print statements you are hard-coding in values which you dont have to do.
System.out.println("Width of Rectangle 1 is: " + oldRectangle.width);
System.out.println("Height of Rectangle 1 is: " + oldRectangle.height);
System.out.println("Area of Rectangle 1 is: " + getArea(oldRectangle));
System.out.println("Perimeter of Rectangle 1 is: " + getPerimeter(oldRectangle));
My issue is that I don't know of a way to reference them
As Java is a strong-typed language, to store the references to each Rectangle (or any object) attribute, you must create variables that match the field.
First, you need your rectangle have attributes:
public class Rectangle {
public double width;
public dobule height;
// constructors here...
After, methods to calculate area and perimeter:
public double getArea() {
return (width*height);
}
public double getPerimeter() {
return ((2*width)+(2*height));
}
Now, to reference them (i guess you still mean area and perimeter):
double area1 = getArea(newRectangle);
double area2 = getArea(oldRectangle);
and
double perimeter1 = getPerimeter(newRectangle);
double perimeter2 = getPerimeter(oldRectangle);
There are no width and height properties in your class, they are only defined inside the constructor, declare them in the class and not inside the constructors so you can acces them like this:
public class Rectangle {
double width;
double height;
public Rectangle() {
width = 1;
height = 1;
}
public Rectangle(double w, double h) {
width = w;
height = h;
}
public double getArea(double w, double h) {
return (w*h);
}
public double getPerimeter(double w, double h) {
return ((2*w)+(2*h));
}
//....
}
Note: It's better to declare them as private and use getters and setters to acces them, take a look at Adding Setter and Getter Methods for further information.
EDIT:
In your case you don't need to pass parameters to your methods (because the calculation needs this rectangle width and height), just calculate them using your class fields, like this:
public double getArea() {
return (width*height);
}
public double getPerimeter() {
return ((2*width)+(2*height));
}
Here's what you need to do to acces your variables:
public static void main(String[] args) {
Rectangle oldRectangle = new Rectangle(4, 40);
Rectangle newRectangle = new Rectangle(3.5, 35.9);
//In the following you access all the object variables and methods
System.out.println("Width of Rectangle 1 is: " + oldRectangle.width);
System.out.println("Height of Rectangle 1 is: " + oldRectangle.height);
System.out.println("Area of Rectangle 1 is: " + oldRectangle.getArea());
System.out.println("Perimeter of Rectangle 1 is: " + oldRectangle.getPerimeter());
System.out.println("Width of Rectangle 1 is: " + newRectangle.width);
System.out.println("Height of Rectangle 1 is: " + newRectangle.height);
System.out.println("Area of Rectangle 1 is: " + newRectangle.getArea());
System.out.println("Perimeter of Rectangle 1 is: " + newRectangle.getPerimeter());
}
You can test the DEMO here.
And to answer your question about using constructor, calling the parametrized constructor like this:
Rectangle oldRectangle = new Rectangle(4, 40);
Is equivalent to:
// create the object
Rectangle oldRectangle = new Rectangle();
//And then assigning the values 4 and 40 to its width and height
oldRectangle.width=4;
oldRectangle.height=40;
I am currently working on a project called Rectangle project in which I am supposed to do the following on Java:
Make the following methods:
setOrigin
area
move
Also make a method that determines if two rectangles intersect and returns a new intersection Rectangle. Test all your methods in the ObjectDemo program for the following rectangles:
A: Origin 0,0: width 10: height 20
B: Origin 5,5: width 15, height 15
C: Origin 20,12: width 10: height 20
What is the area of each? Test if each of them intersect with the other two and what is the intersection area. Move A by 5,5; B by -5,-5: and C by -20, 0. Now give the intersection area of each.
I need to finish this by Monday but I keep getting a ton of errors like unrecognized variables, etc., and I'm not sure how to fix them. Please let me know!
I have three files: Point, RectangleTest, and Rectangle.
Here are their codes:
Point code:
public class Point
{
//Class variables
private int xCoord; //Private (instead of Public) because we are going to use this class in the other file
//We don't want people changing the values unless we let them
private int yCoord; //Variables are not in a function so will maintain their value
//Constructor
Point()
{
xCoord = 0;
yCoord = 0;
}
//Constructor
Point(int startX, int startY)
{
xCoord = startX;
yCoord = startY;
}
public int getX()
{
return xCoord;
}
public int getY()
{
return yCoord;
}
public void setX(int newX)
{
xCoord = newX;
}
public void setY(int newY)
{
yCoord = newY;
}
public void move(int moveX, int moveY)
{
xCoord+=moveX;
yCoord+=moveY;
}
Point(Point p)
{
xCoord = p.getX();
yCoord = p.getY();
}
}
RectangleTest Code:
public class RectangleTest
{
public static void main(String [] args)
{
Rectangle A = new Rectangle(0,0,10,20);
Rectangle B = new Rectangle(5,5,15,15);
Rectangle C = new Rectangle(20,12,10,20);
//Move rectangles
A.moveby(5,10);
B.moveby(-5,-5);
C.moveby(-20,0);
int areaA = A.getarea;
System.out.println("The area of rectangle A is " +areaA);
int areaB = B.getarea;
System.out.println("The area of rectangle B is " +areaB);
int areaC = C.getarea;
System.out.println("The area of rectangle C is " +areaC);
Rectanlge iAB = A.intersect(B);
Rectangle iAC = A.intersect(C);
Rectangle iBC = B.intersect(C);
if(iab != null)
{
System.out.println("The area of intersection rectangle iab = " +iAB.area());
}
if(iac != null)
{
System.out.println("The area of intersection rectangle iac = " +iAC.area());
}
if (ibc != null)
{
System.out.println("The area of intersection area ibc = " +iBC.area());
}
}
}
Rectangle Code:
public class Rectangle
{
Point origin;
int height;
int width;
//Constructor for rectangle object
Public Rectangle(int startX, int startY, int startW, int startH)
{
origin = new Point (startX, startY);
width = startW;
height = startH;
}
//Set origin point for NEW rectangle origins
//FIX
public void setOrigin(int newX, int newY)
{
origin.setX(newX);
origin.setY(newY);
}
public int moveBy(int moveX, int moveY)
{
origin.move(moveX, moveY);
}
public int getArea()
{
int recArea = height*width;
return recArea;
}
public Rectangle intersect(Rectangle testR)
{
int meTRX = origin.getX() + width;
int meTRY = origin.getY() + height;
int testTRX = testR.origin.getX() + width;
int testTRY = testR.origin.getY() + height;
//Boolean to get iTRX
if(meTRX>testTRX)
{
int iTRX = testTRX;
}
else
{
int iTRX = meTRX;
}
//Boolean to get iTRY
if(meTRY>testTRY)
{
int iTRY = testTRY;
}
else
{
int iTRY = meTRY;
}
//Boolean to get iBLX
if(testBLX>meBLX)
{
int iBLX = testBLX;
}
else
{
int iBLX = meBLX;
}
//Boolean to get iBLY
if(testBLY>meBLY)
{
int iBLY = testBLY;
}
else
{
int iBLY = meBLY;
}
//Testing for whether or not there is an intersection rectangle
if(iTRX-iBLX<0 || iTRY-iBLY<0)
{
return null;
}
int iH = iTRY - iBLY;
int iW = iTRX - iBLX;
int intersectArea = iH * iW;
}
}
Please point out any problems! I'm rather new to programming, so I usually make a lot of simple mistakes. Also, I would appreciate if there are no newly introduced commands or anything because my teacher is pretty strict about doing it this way.
Thanks!
P.S. I would appreciate any extra knowledge or info on code improvement (just in general). Thanks!
Couple of Issues:
Java is case sensitive so Public is not same as public in your rectangle class.
When your method doesnt return anything you should use void as return type. So in your method public int moveBy(int moveX, int moveY), you should change it to public void moveBy(int moveX, int moveY)
You need to define variables before using them. So variables like testBLX, meBLX, testBLY, meBLY, iTRX, iTRY, iBLX, iBLY are undefined. I am not sure from where the values will get populated. But you could avoid the compilation error by defining them as int testBLX = 0; and similarly the others.
In your Rectangle class:
In the constructor your wrote Public Rectangle(int startX, int startY, int startW, int startH), but you actually want public Rectangle(int startX, int startY, int startW, int startH). In Java keywords start always with a lower case.
Your method for changing the origin of a rectangle public int moveBy(int moveX, int moveY) has int as a return type, so the compiler wants you to return an integer value. I suppose you did not want to return anything at all so you can change the return type to void.
In your intersect method public Rectangle intersect(Rectangle testR) you declare your variables (iTRX, iTRY, iBLX, iBLY) such as int iTRX = testTRX; only in the scope of your if/else statements which means that after every if/else statement these variables are not available anymore. To learn more about the different scopes of variables: Variable scopes
In your RectangleTest class:
You forgot a part of your task: What is the area of each? Test if each of them intersect with the other two and what is the intersection area.
Some general leads:
The use of more descriptive variable names improves the readability. For example the variable name meTRX does not have any meaning for me as person who did not work on your code or maybe for you if you review your code two months later.
Before you start coding, you could check if Java has built-in classes which you can use. In your case Java provides a Point class in the package java.awt.Point. You do not have to reinvent the wheel.
I would also recommend to read the Java Code Conventions Code Conventions which can bring you and others who read your code on a common denominator in the future.
I'm new to Java and I received an assignment that asks me to write a class called PointerTester that has two Points as instance variables. I need to initialize one of these at coordinate (0.0,0.0) and one at (10.0,12.0). Then I need to move each point by +2.0 in x and -3.0 in Y, query the coordinates of the points, and print out the values.
So far I have this:
public class PointerTester{
/*instance variables*/
private double pointOneX;
private double pointOneY;
private double pointTwoX;
private double pointTwoY;
private double deltaX;
private double deltaY;
/*constructor to initialize*/
public PointerTester (){
pointOneX = 0.0;
pointOneY = 0.0;
pointTwoX = 10.0;
pointTwoY = 12.0;
deltaX = 2.0;
deltaY = -3.0;
}
/*constructor to initialize to specific value*/
PointerTester(double pointOneX, double pointOneY, double pointTwoX, double pointTwoY){
this.pointOneX = pointOneX;
this.pointOneY = pointOneY;
this.pointTwoX = pointTwoX;
this.pointTwoY = pointTwoY;
}
/*command to change value*/
public void moveBy(double deltaX, double deltaY){
this.pointOneX = this.pointOneX + deltaX;
this.pointOneY = this.pointOneY + deltaY;
this.pointTwoX = this.pointTwoX + deltaX;
this.pointTwoY = this.pointTwoY + deltaY;
}
/*Queries*/
public double getOneX(){
return pointOneX;
}
public double getOneY(){
return pointOneY;
}
public double getTwoX(){
return pointTwoX;
}
public double getTwoY(){
return pointTwoY;
}
/*print values*/
public static void main(String[] args){
PointerTester pointOne = new PointerTester();
PointerTester pointTwo = new PointerTester();
System.out.println("Point One after move (" + pointOne + ")");
System.out.println("Point Two after move (" + pointTwo + ")");
}
}
I cannot figure out how to correctly output the values or if I am completely wrong in working on this problem.
Edit It seems I needed to use this code that I at first thought was supposed to be separate
public class Point{
public double x;
public double y;
public Point(double x, double y){
this.x = x;
this.y = y;
}
public void setX(double x){
this.x = x;
}
public void setY(double y){
this.y = y;
}
public double getX(){
return x;
}
public double getY(){
return y;
}
}
How do I incorporate this into my code?
I think to get started you should have a separate class called "Point" that encapsulates an X and Y value, and includes a "moveBy" method. It could also implement "toString()" such that "System.out.println" will print something nice for it. [Edit: Or just use java.awt.Point.]
Beyond that, I'll leave that for you to do as your homework.
You might want to use some Point objects.
Point p1 = new Point();
Point p2 = new Point(10,12);
you can then use the setLocation or translate methods found in the point class. Maybe the whole thing would look something like this? Hope this helps.
main(){
//make some points
Point p1 = new Point();
Point p2 = new Point(10,12);
//move our points
p1.translate(2,-3);
p2.translate(2,-3);
//print our points
System.out.println(p1);
System.out.println(p2);
}
You can implement the toString method. There are examples here:
http://www.javapractices.com/topic/TopicAction.do?Id=55
You're trying to print the PointerTester object instead of the values contained within. System.out.println(pointOne.getOneX()) should at least stop throwing exceptions and allow you to print correctly.