Cannot find symbol Error p1.getX() [duplicate] - java

This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 4 years ago.
And the code I typed for it is given below:
class MyPoint {
public int x;
public int y;
public MyPoint(){
x = 0;
y = 0;
}
public MyPoint(int x, int y) {
this.x = x;
this.y = y;
}
public void setXY(int newx, int newy) {
x = newx;
y = newy;
}
public int[] getXY() {
int [] getXYarray = new int[2];
getXYarray[0] = x;
getXYarray[1] = y;
return getXYarray;
}
public String toString() {
return "(" + x + "," + y + ")";
}
But i cannot see what is actually wrong here.
Please tell me where i am going wrong with this as i am very lost.

The third bullet point from the bottom says you need "Getter and setter for the instance variables x and y". Implement those and I suspect your Tester will pass.
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;
}
Your instructions also call for an overloaded constructor that takes a MyPoint (a copy constructor)
public MyPoint(MyPoint that) {
this.x = that.x;
this.y = that.y;
}

The test code tries to use the methods getX() and getY().
You do not define those methods, you only define getXY().
(Please provide textual information as text in the future, it would have made answering this easier for me.)

Related

Incompatible types error in Command Prompt

I have a work about calculating the distances between two points. It consist of Point Class, Line Class as well as Main Class. The following is my Point class. After working on the private double distance(Point p) method, I am unable to return p at the public double getDistance(Point p) method. I run code on Command Prompt and it shows error: incompatible types: Point cannot be converted to double. Please advice.
class Point
{
private int x;
private int y;
//Constructor
public Point()
{
//nothing
}
//Second constructor
public Point (int x, int y)
{
this.x = x;
this.y = y;
}
//Copy constructor
public Point (Point p)
{
this (p.x, p.y);
}
private double distance(Point p)
{
int dX = this.x - p.x;
int dY = this.y - p.y;
double result = Math.sqrt(dX * dX + dY * dY);
return result;
}
public double getDistance(Point p)
{
return p;
}
//getter
public int getX()
{
return x;
}
public int getY()
{
return y;
}
//setter
public void set(int x, int y)
{
this.x = x;
this.y = y;
}
public String toString ()
{
return String.format ("Given Point (%d, %d)", x, y);
}
}
You have the object Point p as your parameter and return it as a double.
In your block of code you're stating a return of the Object Point p and not a double data type.
public double getDistance(Point p) {
return p;
}
If you're just trying to calculate the distance of the object, use your distance() method. This method already returns the distance calculated as a double.
private double distance(Point p) {
int dX = this.x - p.x;
int dY = this.y - p.y;
double result = Math.sqrt(dX * dX + dY * dY);
return result;
}

Java Algorithm for finding shortest distance between 2d point and Matrix

I'm stuck on this question for a couple of days now and would really like to get some help.
I am given a 2 dimensional point in the range of (0-1 not including 1), such as (0.5,0.2), and N other points (also in the range of 0-1).
The first part of the question is to implement the "dumb" algorithm, which when given a certain point will find the point with the shortest distance from it, which has a complexity of O(N).
The part I'm stuck at, requires to build a Matrix K on K, where each "cell" will contain the points that belong to that cell. Once done, when given the original point I will need to search for the point with the shortest distance to it only in some of the cells and not the entire Matrix, which should result better complexity.
My original thought is to devide the points so that each block will have an arraylist of points that belong to him, and then to somehow go through the main block(the one that the original point belongs to) and continue by going through it's neighbors, however implementing it hasn't been very successful.
I would highly appreciate any help/ advice.
Below is what I currently have:
public class Point {
private double x;
private double y;
private Block b;
public Point(double x, double y)
{
this.x=x;
this.y=y;
}
public Point(double x, double y, Block b) //consrtuctor for complex case
{
this.x=x;
this.y=y;
b.points.add(this);
}
public double getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public double distance(Point p)
{
double res=0;
res = Math.sqrt(Math.pow(this.x-p.getX(),2)+Math.pow(this.y-p.getY(),2));
return res;
}
}
import java.util.ArrayList;
public class Block {
private int x;
private int y;
public ArrayList<Point> points;
public Block(int x, int y) {
points = new ArrayList<Point>();
this.x = x;
this.y = y;
}
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;
}
}
import java.util.Random;
public class ComplexCase {
private Block[][] blockMat;
public ComplexCase(int k, int n)
{
Random generator = new Random();
Point p1;
Block b1;
double x,y;
int bx1,by1;
int t;
t = 1/k;
blockMat = new Block[k][k];
for (int i =0;i<n;i++)
{
x = generator.nextDouble();
y = generator.nextDouble();
bx1 = (int) (x/t);
by1 = (int) (y/t);
b1 = new Block(bx1,by1);
p1 = new Point(x,y,b1);
}
}
public Block[][] getBlockMat() {
return blockMat;
}
public void setBlockMat(Block[][] blockMat) {
this.blockMat = blockMat;
}
}

toString() method not printing the correct values of object

I'm following a book and I have a Point class which defines a point and I am trying to display the values. I've been looking around for a while now, whatever I do it always displays [0,0] here's my code.
class Main {
public static void main(String []args) {
Point point = new Point(10, 20);
System.out.println(point.toString());
}
}
class Point {
private int x, y;
public Point(int x, int y) {
x = x;
y = y;
}
public String toString() {
return "[" + x + ", " + y + "]";
}
You're never initializing class members x and y actually. So they're initialized as 0 automatically hence the [0,0] output.
x = x means parameter x = parameter x which does nothing.
use this.x = x; instead so Point class member x will be set.
Look at your constructor:
public Point(int x, int y) {
x = x;
y = y;
}
In your case you do not initialize class fields. To do that, use this keyword and it should look like:
public Point(int x, int y) {
this.x = x;
this.y = y;
}
Otherwise you are dealing with parameter x and y variables only.
Use this in Constructor as below :-
class Point {
private int x, y; // These x and y are member of Point class
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "[" + x + ", " + y + "]";
}
You are not initializing x and y of Point class.
In toString() method you are printing x and y of Point class which are not initialized and hence taking default value of 0 i.e. 0 is default value of integer.
use the this keyword for the separation of local variable and instance variable. in your parameterized constructor jvm is not able to initialize the instance variable because it gets an ambiguity problem between local and instance variable. And instance variable in java gets implicitly initialized if u we are not initializing that's why you are getting [0,0] as the output.
here is the code..
class Main {
public static void main(String []args) {
Point point = new Point(10, 20);
System.out.println(point.toString());
}
}
class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "[" + x + ", " + y + "]";
}
In your Constructor for the Point object:
public Point(int x, int y) {
this.x = x;
this.y = y;
}
You will need to put this.x to indicate you are setting the value of the Point object's x variable, not the x argument passed into the constructor.
The updated, functional code is below:
class Main {
public static void main(String []args) {
Point point = new Point(10, 20);
System.out.println(point.toString());
}
}
class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "[" + x + ", " + y + "]";
}

Using double method into a segment class

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)

method issue in java

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;
}

Categories