Constructor undefined while using multiple classes - java

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

Related

Adding an overloaded constructor and two methods (Java) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
So I am wanting to implement the MyPoint class, which models a 2D point with x and y coordinates.
It needs to contain the following requirements:
Two public instance variables: x (of type int) and y (of type int).
A toString() method that returns a string description of the instance in the format "(x, y)".
A default (or "no-argument" or "no-arg") constructor that constructs a point at the default location of (0, 0).
An overloaded constructor that constructs a point with the given x and y coordinates.
An overloaded constructor that constructs a point with the given MyPoint object.
Getter and setter for the instance variables x and y.
A method named setXY(newx, newy) to set both x and y.
A method named getXY() that returns the x and y in a 2-element int array.
This is the code I have written up so far:
class MyPoint {
public int x;
public int y;
public String toString(){
}
public MyPoint(){
this(0, 0);
}
public MyPoint(int x, int y){
this.x = x;
this.y = y;
}
public void setX (int x){
this.x = x;
}
public int getX(){
return x;
}
public void setY (int y){
this.y = y;
}
public int getY(){
return y;
}
}
I have done all the others, but, I got stuck on writing an overloaded constructor with the given MyPoint object and implementing the getXY and setXY methods (highlighted), and overall getting the code to work.
Would the overloaded constructor with the given MyPoint object look similar to:
public MyPoint(int x, int y){
this.x = x;
this.y = y;
}
Any help would be much appreciated. Thank you.
For overloading constructor
public MyPoint(MyPoint p){
this.x =p.x;
this.y =p.y;
}
for setXY() and getXY()
public void setXY (int x,int y){
this.x = x;
this.y = y;
}
public int[] getXY ()
{
return new int[]{this.x,this.y};
}
The overloaded constructor with the given MyPoint object will be taking MyPoint object as argument as shown below:
public MyPoint(MyPoint point) {
this.x = point.x;
this.y = point.y;
}
This kind of overloaded constructor is known as Copy Constructor
public MyPoint(MyPoint mp)
{
this.x = mp.x;
this.y = mp.y;
}
Note : You should pass only MyPoint type reference variable

Java super set private fields

I'm learning java inheritance and encapsulation. Here is sample code
class Base {
private int x;
private int y;
Base(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
class Child extends Base {
Child(int x, int y) {
super(x, y);
}
}
Child c = new Child(1, 2);
System.out.println(c.getX());
Why have I access to x and y (private) in Child class? Does super change anything?
You don't have access to x and y in Child class. When you write
Child(int x, int y) {
super(x, y);
}
the x and y are formal parameters of Child's constructor, that happen to be called the same names as private fields x and y of the superclass. They might as well be called something else - say, a and b, and the effect would be exactly the same:
Child(int a, int b) {
super(a, b);
}
Why have I access to x and y (private) in Child class?
you dont, if you would you could do without problem:
System.out.println(c.x);
System.out.println(c.y);
you wrote instead public setters/getters that are giving you access (capsulation) to the fields..

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