How can I write a double slope method for a segment class?
I have two variable: p1 = x1, y1 and p2 = x2, y2.
I did this code but this is wrong:
public double slope() {
return (double)(p2.y - p1.y)/(p1.x-p2.x);
}
Can someone tell me why is it wrong?
What is the right way to write it?
Thank you!
Depending on the type of p1, it could be a Point which takes both an x and a y coordinate.
public class Point {
private final int x;
private final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
You'd have to use getX() and getY() to get the X and Y coordinates. You'd also have to be sure you created the point with new Point(1, 2) as well.
Also, be sure that you're getting the right cast behavior by adding parens around it and your numerator:
return ((double)(p2.getY() - p1.getY()))/(p1.getX() - p2.getX());
(Although that above seems to scream for a deltaY and deltaX method alone)
Related
I have a question. For the first time, I am working with multiple classes in JAVA. I have some trouble doing this. I created a class which I will call from another class. I want to make a type Coordinate, which, as the name suggests, holds coordinates. then, I want to shift those coordinates. So far the code looks as follows:
public class Coordinate {
double x;
double y;
Coordinate(){
x=0;
y=0;
}
public Coordinate(int x, int y){
this.x = x;
this.y = y;
System.out.print(x);//TO TEST WHETHER IT DOES SOMETHING
}
Coordinate shiftCoordinate(int z, int w){
this.x = x + z;
this.y = y+ w;
return new Coordinate(x,y);//ERROR: The constructor Coordinate(double, double) is undefined
}
}
It throws an error where stated. I do not understand this error. In my 'main' class, I did the following:
void start() {
Coordinate coordinate = new Coordinate();
coordinate.x=3;
coordinate.y=4;
}
I would expect this to print 3, but it does not. Where am I wrong?
First you don't work with mutiples class, only one : Coordinate but you want multiple constructors.
As your attributs are double make a constructor that needs that type, it'll be used when you write new Coordinate(5,6)
public Coordinate(double x, double y) {
this.x = x;
this.y = y;
}
If you want a default constructor (no args) it'll be used when you call new Coordinate()
You want a way to shift from a Coordinate instance you have 2 ways : modify the current instance or create a new one, but don't do both together (like your code) it's not usefull to get at the end 2 objects whith the same values
// modify current instance
void shiftCoordinate(double z, double w) {
this.x = x + z;
this.y = y + w;
}
// return a new object
Coordinate shiftCoordinate(double z, double w) {
return new Coordinate(this.x + z, this.y + w);
}
Also you last code uses the default constructor with no args so that's normal you don't see any printing use new Coordinate(3,4) to see it
A classic constructor is also, the constructor to clone an instance, it takes an instance and create a new one with the same values :
public Coordinate(Coordinate clone) {
this.x = clone.x;
this.y = clone.y;
}
public Coordinate(int x, int y){
this.x = x;
this.y = y;
System.out.print(x);
}
This constructor has arguments that are integers, and you're trying to pass in doubles. Change the constructor to this:
public Coordinate(double x, double y){
this.x = x;
this.y = y;
System.out.print(x);
}
I have an assignment explaining like this:
Write a definition of a class named Point that might be used to store and manipulate the location of a point on the plane. The point is stored as two coordinates: x and y. You will need to declare and implement the following methods:
Two constructors:
a. no-argument constructor that sets the point coordinates to (0,0), and
b. a constructor that takes x and y coordinate of the point and sets member
variables.
Method set that sets the private data after an object of this class is created.
A method to move the point by an amount along the vertical and horizontal directions specified by the first and second arguments: move(double dx, double dy)
The method to rotate the point by 90 degrees clockwise around the origin. Hint: when point is getting rotated 90 clockwise around the origin the following changes happen to its coordinates: xrotated = y; yrotated = -x .
two accessor methods to retrieve the coordinates of the point.
It should be 2 different call. these items on second one (not main class)
I will call this on main class. (I do that).
This is my code but I dont understand what I should do next.
private double x;
private double y;
public Point(double initialX, double initialY) {
x = initialX;
y = initialY;
}
public Point() {
x = 0;
y = 0;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public void move(double dx, double dy) {
x += dx;
y += dy;
}
You have almost everything, good job. For the rotate by 90 function, you're given a pretty good clue about what to do. Imagine you have a point (1,2), and you rotate it. you'll end up with (2,-1). if you rotate it again, you'll get (-1,-2). once more gives you (-2,1), and a fourth 90 degree rotation gives (1,2), which is what you started with. come up with a function that does this. It shouldn't be longer than 3 lines.
The setter functions (functions used to set, or change, the values) are simply functions you can use to set the values of the point. so, you'd have a function "setX(...) { ... }" and a function "setY(...) { ... }". These should be very straightforward.
Feel free to ask further questions if you're still confused.
private double x;
private double y;
public Point(double dx, double dy) {
x = dx;
y = dy;
}
public Point() {
x = 0;
y = 0;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public void SetX(double dx)
{
x = dx;
}
public void SetY(double dy)
{
y = dy;
}
public void move(double dx, double dy)
{
x = x + dx;
y = y + dy;
}
public double rotateX()
{
double temp = x;
x=y;
y=temp;
return x;
}
public double rotateY()
{
y=-y;
return y;
}
main page
public static void main(String[] args) {
// TODO Auto-generated method stub
Point p = new Point();
p.SetX(50);
p.SetY(17);
System.out.println("X and Y coordinates are : \n("+p.getX()+","+p.getY()+")");
System.out.println("after 90 degree clockwise rotate: ");
System.out.println(p.rotateX()+","+p.rotateY());
i'm learning to create custom classes and can't figure out where I've gone wrong
From main class...
MyPoint p1 = new MyPoint(317, 10);
the error says:
constructor MyPoint in class MyPoint cannot be applied to given types;
required: no arguments
found: int, int
reason: actual and formal argument lists differ in length
this is from my MyPoint class:
private int x, y;
public void MyPoint(int x, int y)
{
this.x = x;
this.y = y;
}
Why isn't MyPoint(317, 10) being fed into the relevant class along with the x and y values?
Any help would be appreciated, thank you.
Constructors don't have return type. This is just a normal method you just made.
Solution: Remove the void from the method. It should look like
public MyPoint(int x, int y)
{
this.x = x;
this.y = y;
}
remove return type from
public void MyPoint(int x, int y)
constructor cannot have return type not even void
make it
public MyPoint(int x, int y)
{
this.x = x;
this.y = y;
}
You need to declare parameterized constructor in MyPoint class.
public MyPoint(int x, int y)
{
this.x = x;
this.y = y;
}
Constructors must not have return type. So change your code as
public MyPoint(int x, int y)
{
this.x = x;
this.y = y;
}
How come you can get the x and y values from a java.awt.Point class by using a method and referencing the value?
Point p = new Point(10,20);
int x0 = p.getX();
int y0 = p.getY();
int x1 = p.x;
int y1 = p.y;
System.out.println(x0+"=="+x1+"and"+y0+"=="+y1);
Did the people who made this class forget to make x and y private?
Looking at the javadoc, these seem to return different types. p.x returns an int while p.getX() returns a double.
The source code of Point shows this:
public int x;
//...
public double getX() {
return x;
}
So it looks like that's its only purpose. getX() is a more convenient way to get the coordinates as a double.
Change to
double x0 = p.getX();
// getX returns the X coordinate of this Point2D in double precision
I need to implement a Triangle class and im stuck on comparing the lengths of the sides to determine if the triangle is indeed an isosceles. Here is what I have so far:
public class TriangleIsosceles {
private Point cornerA;
private Point cornerB;
private Point cornerC;
private int x1;
private int y1;
private int x2;
private int y2;
private int x3;
private int y3;
public TriangleIsosceles(){
cornerA = new Point(0,0);
cornerB = new Point(10,0);
cornerC = new Point(5,5);
}
public TriangleIsosceles(int x1,int y1,int x2,int y2,int x3,int y3){
cornerA = new Point(x1,y1);
cornerB = new Point(x2,y2);
cornerC = new Point(x3,y3);
}
public String isIsosceles(String isIsosceles){
return isIsosceles;
}
}
The Point object im using is this:
public class Point {
private int x;
private int y;
public Point(){
this(0,0);
}
public Point(int x, int y){
this.x = x;
this.y = y;
}
public void setX(int x){
this.x=x;
}
public void setY(int y){
this.y=y;
}
public void printPoint(){
System.out.println(x + y);
}
public String toString(){
return "x = "+x+" y = "+y;
}
}
In another class (LineSegment) I created a method length() that determines the distance of two points. Which looks like:
public double length() {
double length = Math.sqrt(Math.pow(x1-x2,2) + Math.pow(y1-y2,2));
return length;
}
How can I use this method to help me find the lengths of the triangle in my TriangleIsosceles class?
I know I need to see if (lenghtAB == lengthBC || lengthBC == lenghtCA || lengthAB == lengthCA).
A quick, perfectly valid, solution would be to make your length method a static utility method, i.e.
public static double length(x1, y1, x2, y2)
{
return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
}
or
public static double length(Point p1, Point p2)
{
return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
}
You could also add the method to Point itself, i.e. in the Point class add:
public double calcDistance(Point otherPoint)
{
return Math.sqrt(Math.pow(this.x - otherPoint.x, 2) + Math.pow(this.y - otherPoint.y, 2));
}
Assuming your LineSegment class has a constructor that takes two Point objects, you should create three LineSegment objects (which you can cache in the Triangle class). Then using LineSegment#getLength() you can determine if any two sides are the same length.
Since this looks like homework I won't give you the full solution.