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

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

Related

Constructor undefined while using multiple classes

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

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

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.)

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..

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