Calling a constructor from a class in the main method? - java

I've read some of the other questions, but still couldn't seem to figure out how to get mine to work, any help is appreciated. The code I have so far is given below. I want to be able to call a newPointParameters to create a new class.
public class Lab4ex1 {
public static void main(String[] args) {
System.out.println("" + 100);
new newPointParameter(42,24);
}
class Point {
private double x = 1;
private double y = 1;
public double getx() {
return x;
}
public double gety() {
return y;
}
public void changePoint(double newx, double newy) {
x = newx;
y = newy;
}
public void newPointParameters(double x1, double y1) {
this.x = x1;
this.y = y1;
}
public void newPoint() {
this.x = 10;
this.y = 10;
}
public double distanceFrom(double x2, double y2) {
double x3 = x2 - this.x;
double y3 = y2 - this.y;
double sqaureadd = (y3 * y3) + (x3 * x3);
double distance = Math.sqrt(sqaureadd);
return distance;
}
}
}

So, currently, neither newPointParameters nor newPoint are constructors. Rather, they are just methods. To make them into constructors, they need to share the same name as the class the construct
class Point {
private double x = 1;
private double y = 1;
public Point() {
this.x = 10;
this.y = 10;
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
Then, when you want to create a new point, you simply do the following
For a default point
public class Lab4ex1 {
public static void main(String[] args) {
System.out.println("" + 100);
//this will create a new Point object, and call the Point() constructor
Point point = new Point();
}
For the Point with parameters
public class Lab4ex1 {
public static void main(String[] args) {
System.out.println("" + 100);
//this will create a new Point object, and call the
//Point(double x, double y) constructor
Point point = new Point(10.0, 10.0);
}

It should be
public static void main(String[] args) {
System.out.println("" + 100);
Point p = new Point();
p.newPointParameter(42,24);
}

newPointParameters is not a constructor. I think this is what you are looking to do:
public Point(double x1, double y1) {
x = x1;
y = y1;
}
Then you can create a Point object in your main class using this constructor:
Point p = new Point(42, 24);
It looks like you also intended for newPoint() to be a constructor, so it should look like this:
public Point() {
x = 10;
y = 10;
}

Related

Java - calculation distans and distans to origo - issue

I have forgot my old math from school.
I have made a java class that should calculate the distance between x,y,z and origo.
But have made a wrong turn somewhere and cant get out…
The calculation doesnt give the right value.
Can you help see what i have made wrong?
Its the distance calculation thats is wrong.
import java.io.PrintWriter;
import java.util.Scanner;
public class U3point {
public static void main(String[] args) {
System.out.println("Values for x,y,z ");
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out, true);
double x;
double y;
double z;
String[] koordinater = null;
String rad = in.nextLine();
System.out.println("toString: ");
while (!rad.equals("")) {
koordinater = rad.split("\\s");
x = Double.parseDouble(koordinater[0]);
y = Double.parseDouble(koordinater[1]);
z = Double.parseDouble(koordinater[2]);
Point p = new Point(x, y, z);
out.println(p);
//call method for origo
double d = p.getDistanceToOrigo();
System.out.println("Distance to Origo: " + d);
// call method for distance
double d2 = p.getDistanceTo(x, y, z);
System.out.println("Distance: " + d2);
System.out.println("Exit program with enter or new values");
rad = in.nextLine();
}
}
}
class Point {
private double x;
private double y;
private double z;
public Point(double x1, double y1, double z1) {
x = x1;
y = y1;
z = z1;
}
public void setX(double x1) {
x = x1;
}
public void setY(double y1) {
y = y1;
}
public void setZ(double z1) {
z = z1;
}
public double[] getCoordinates() {
return new double[] { x, y, z };
}
public double getDistanceTo(double x2, double y2, double z2) {
return (Math.sqrt(((x - x2) * (x - x2) + (y - y2) * (y - y2) + (z - z2) * (z - z2))));
}
public double getDistanceToOrigo()
{
return (Math.sqrt(((x) * (x) + (y) * (y) + (z) * (z))));
}
public String toString() {
return " x: =" + x + " y:=" + y + " z:=" + z;
}
}
The problem you were calculating the distance to the same point, here are an approach for you:
import java.io.PrintWriter;
import java.util.Scanner;
public class U3point {
public static void main(String[] args) {
PrintWriter out = new PrintWriter(System.out, true);
Point p1 = generatePoint();
Point p2 = generatePoint();
out.println(p1);
out.println(p2);
// call method for origo double
double d = p1.getDistanceToOrigo();
System.out.println("Distance to Origo: " + d);
// call method for distance
double d2 = p1.getDistanceTo(p2.getX(), p2.getY(), p2.getZ());
System.out.println("Distance: " + d2);
System.out.println("The end!!");
}
public static Point generatePoint() {
System.out.println("Values for x,y,z ");
String[] koordinater = null;
Scanner in = new Scanner(System.in);
String rad = in.nextLine();
Point p = null;
koordinater = rad.split("\\s");
double x = Double.parseDouble(koordinater[0]);
double y = Double.parseDouble(koordinater[1]);
double z = Double.parseDouble(koordinater[2]);
p = new Point(x, y, z);
return p;
}
}
class Point {
private double x;
private double y;
private double z;
public Point(double x1, double y1, double z1) {
x = x1;
y = y1;
z = z1;
}
public void setX(double x1) {
x = x1;
}
public void setY(double y1) {
y = y1;
}
public void setZ(double z1) {
z = z1;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getZ() {
return z;
}
public double[] getCoordinates() {
return new double[] { x, y, z };
}
public double getDistanceTo(double x2, double y2, double z2) {
return (Math.sqrt(((x - x2) * (x - x2) + (y - y2) * (y - y2) + (z - z2) * (z - z2))));
}
public double getDistanceToOrigo() {
return (Math.sqrt(((x) * (x) + (y) * (y) + (z) * (z))));
}
public String toString() {
return " x: =" + x + " y:=" + y + " z:=" + z;
}
}
Output:
Values for x,y,z
1 2 3
Values for x,y,z
4 5 6
x: =1.0 y:=2.0 z:=3.0
x: =4.0 y:=5.0 z:=6.0
Distance to Origo: 3.7416573867739413
Distance: 5.196152422706632
The end!!

Calculate distance between two points in java

I'm kind of new to Java, and trying to write a code that calculate the distance of two points 2 and 3, and scale of 10. Somehow, it does not work. Can you give me a hint, so I can fix the code?
import java.lang.Math;
public class Point {
int x, y;
public Point (int x, int y){
this.x = x;
this.y = y;
}
public float scale(int factor) {
new Point(x * factor, y * factor);
return factor;
}
public float distance(){
double distance = Math.sqrt(x * x + y * y);
return distance;
}
public void main(String[] args) {
float p = new Point(2,3).scale(10);
System.out.println(distance);
}
}
In scale you are creating a new point with the scaled values and doing nothing with it. You're leaving x and y of the point in question untouched.
You probably mean to multiply x and y by factor, rather than creating a new point.
Also you're printing a variable named distance, which does not exist (so this probably doesnt even compile), rather than calling the method named distance() and printing its returned value.
public class Point {
int x, y;
public Point (int x, int y){
this.x = x;
this.y = y;
}
public static Point scalePoint(Point p, int factor) { //scale a given point p by a given factor
Point scaledPoint = new Point(p.x * factor, p.y * factor); //by multipling the x and y value with the factor
return scaledPoint; //and return the new scaled point
}
public static double calculateDistance(Point p1, Point p2){ //to calculate the distance between two points
double distance = Math.sqrt(p1.x * p2.x + p1.y * p2.y); //send the two points as parameter to this method
return distance; //and return the distance between this two as a double value
}
public static void main(String[] args) {
Point p = new Point(2,3);
Point scaledPoint = scalePoint(p, 10);
double distance = calculateDistance(p, scaledPoint);
System.out.println(distance);
}
}
At the moment your distance method is calculating the distance of a point from the origin (i.e. point 0,0). It would make more sense if you made that explicit:
class Point {
private static final Point ORIGIN = new Point(0, 0);
private final int x;
private final int y;
public float distanceTo(Point other) {
float xDelta = other.x - this.x;
float yDelta = other.y - this.y;
return Math.sqrt(xDelta * xDelta + yDelta * yDelta);
}
public Point scale(float factor) {
return new Point(x * factor, y * factor);
}
}
Then finding the distance to the origin becomes point.distanceTo(Point.ORIGIN) which makes the intent clearer.

Why does my class's test program return 0.0 for my answers?

package Geometry;
public class TestGeometryPoint {
public static void main(String[] args) {
//Creates the object PointA and Assigns values
Point pA = new Point(); //PointA(0, 0)
pA.setX(2.0); //PointA(2.0, 0)
pA.setY(5.0); //PointA(2.0, 5.0)
//Creates the object PointB and Assigns values
Point pB = new Point(4.0, 6.1); //PointB(4.0, 6.1)
//Calculates the distance between PointA and PointB
double d = pA.distance(4.0, 6.1); //Distance from class to a set of coordinates
double dP = pA.distance(pB); //Distance from class to another point
double dPtP = Point.distance(pA, pB); //Distance from point to point
//Prints the result of the calculations
System.out.println("Distance between Point A & B: " + d);
System.out.println("Distance between Point A & B: " + dP);
System.out.println("Distance between Point A & B: " + dPtP);
}
}
This is my Test Class and my Point class below.
package Geometry;
public class Point {
//Initializes the coordinates for a point on a graph with the values of x and y
private static double x;
private static double y;
//Defualt Constructor
public Point() {
x = 0;
y = 0;
}
//Point Constructor
public Point(double x, double y) {
this.x = x;
this.y = y;
}
//Returns the x value
public double getX() {
return x;
}
//Changes the x value
public void setX(double x) {
Point.x = x;
}
//Returns the y value
public double getY() {
return y;
}
//Changes the y value
public void setY(double y) {
Point.y = y;
}
//Calculates the distance between the class's point coordinates and another set of point coordinates
public double distance(double x0, double y0) {
double distance = (Math.sqrt(((x0 - x) * 2.0) + ((y0 - y) * 2.0)));
return distance;
}
//Calculates the distance between the class's point and another Point class's location
public double distance(Point p) {
double distance = (Math.sqrt(((p.getX() - x) * (p.getX() - x)) + ((p.getY() - y) * (p.getY() - y))));
return distance;
}
//Calculates the distance between a Point class's location and another Point class's location
public static double distance(Point p1, Point p2) {
double distance = (Math.sqrt((Math.pow((p2.getX() - p1.getX()), 2.0) + (Math.pow((p2.getY() - p1.getY()), 2.0)))));
return distance;
}
}
d, dP, dPtP: all return 0.0 when compiled and run, but I have no idea why as I have tried changing code and checking my math when calculating distance. I think I might just need a new set of eyes to take a look at it.
I believe since your x and y variables are static, they belong to the class Point as a whole. When you create a Point(2,5);
You set the static variable of the class Point to be 2 and 5 and then, you create another Point(4,6) You set the same static value of x and y to be 4,6.
Therefore, comparing the distance between the same two Point are 0.
The right code for the class Point would be like so
public class Point {
//Initializes the coordinates for a point on a graph with the values of x and y
private double x;
private double y;
//Defualt Constructor
public Point() {
x = 0;
y = 0;
}
//Point Constructor
public Point(double x, double y) {
this.x = x;
this.y = y;
}
//Returns the x value
public double getX() {
return x;
}
//Changes the x value
public void setX(double x) {
this.x = x;
}
//Returns the y value
public double getY() {
return y;
}
//Changes the y value
public void setY(double y) {
this.y = y;
}
//Calculates the distance between the class's point coordinates and another set of point coordinates
public double distance(double x0, double y0) {
double distance = (Math.sqrt(((x0 - x) * 2.0) + ((y0 - y) * 2.0)));
return distance;
}
//Calculates the distance between the class's point and another Point class's location
public double distance(Point p) {
double distance = (Math.sqrt(((p.getX() - x) * (p.getX() - x)) + ((p.getY() - y) * (p.getY() - y))));
return distance;
}
//Calculates the distance between a Point class's location and another Point class's location
public static double distance(Point p1, Point p2) {
double distance = (Math.sqrt((Math.pow((p2.getX() - p1.getX()), 2.0) + (Math.pow((p2.getY() - p1.getY()), 2.0)))));
return distance;
Your problem is that you are confusing static and non-static fields.
By putting that little static on your field declarations you are saying: all instances of this class should be seeing the exact same variables.
So, when you create two points
p1 = new Point(5, 5);
p2 = new Point(10, 10);
the declaration of p2 "overrides" the 5-5 from p1 ... because, as said: all Points are using the same x and y.
Thus, solution: simply drop that keyword from the definition of x and y. Then each point has is very own x and y.
And, more importantly: understand that each and any character in your source code matters. This means: you better understand each and any concept that your source code is making use of!

Circle exercise Java?

This is not a homework,this is an exercise in a Java book I am learning by myself.
Build a class with the name circle which represents a circle in the coordinate plane.The fields of the class should be radius length,dy coordinates of the center.The methods of the class should be :
getArea() : Finds the area of the circle
getPerimeter() : Finds the perimeter of the circle
moveCircle() : Changes the coordinates of the center of the circle
modifyRadius() : Modifies the radius of the circle
private int x, y;
public Circle() {
x = 0;
y = 0;
radius = 1;
}
public Circle(int x, int y, double radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
public double getArea() {
return radius * radius * Math.PI;
}
public double getPerimeter() {
return 2 * radius * Math.PI;
}
Now how do I continue this? for moveCircle and ModifyRadius?
There should be a variable called radius in your class.
private int radius;
to move circle, you should pass your new location of the center to the object. Then it will set the new position.
public void moveCircle(int newX, int newY) {
this.x = newX;
this.y = newY;
}
To modify the radius you also can use the same method.
public void ModifyRadius (double newRadius) {
this.radius = newRadius;
}
Another way is to create setters for the variables.
public void setX (int x) {
this.x = x;
}
public void setY (int y) {
this.y = y;
}
public void moveCircle (int x, int y) {
setX(x);
setY(y);
}
public void setRadius (double radius) {
this.radius = radius;
}

translate coordinate method java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I can't seem to get my transform method right. Any suggestions on how I could make the method translate the point better? i.e. when this method is invoked, it should be able to give the new point. Also I'm having issues creating the slope method...I know slope is y2-y1/x2-x1 but how would I make that into a method. Is there a Math class I can import for the slope? Thanks much appreciated
import java.util.*;
public class Point {
private int x; // to store variables for x & y
private int y;
private double slope;
//default constructor
public Point () {
double x = 0;
double y = 0;
}
//alternate constructor
public Point (double x1, double y1) {
double x = x1;
double y = y1;
}
//set coordinates
public void setCoord (double x1, double y1){
double x = x1;
double y = y1;
}
//print method
public void print () {
System.out.print(x + "," + y);
}
//toString Method
public String toString() {
return "(" + x + "," + y + ")";
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void equals () {
if (x==y)
System.out.println(" Coordinates are the same ");
else {
System.out.println(" Coordinates are different ");
}
}
public void copy(Point temp) {
x=temp.x;
y=temp.y;
}
public Point getCopy() {
Point temp = new Point();
temp.x = x;
temp.y = y;
return temp;
}
public void distanceFromOrigin(int x1, int y1) {
double dx = x-x1;
double dy = y-y1;
}
//calculate the distance from one point to another
public void distance (double x1, double y1) {
double distance = Math.sqrt((x * x1) + (y * y1));
}
//shift the location of a point by a given amount
public void transform (double dx, double dy) {
double transform = ((x+dx) (y+dy));
}
// returns true if any given point lines up horizontally with a given point.
public boolean isHorizontal () {
return true;
}
// returns true if any given point lines up vertically with a given point.
public boolean isVertical () {
return true;
}
// returns the slope of the line
public double slope() {
return slope;
}
}
public void equals (Object o) {
if(o instanceof Point) {
Point p = (Point)o;
return p.x == x && p.y == y;
} else {
return false;
}
}
public Point getCopy() {
return new Point(x, y);
}
public double slope(Point otherPoint) {
return ((double)(otherPoint.y - y)) / ((double)(otherPoint.x / x));
}
public double distance (Point otherPoint) {
double dx = otherPoint.x - x;
double dy = otherPoint.y - y;
return Math.sqrt((dx * dx) + (dy * dy));
}
public double distanceFromOrigin() {
return distance(new Point(0, 0));
}
public boolean isHorizontal (Point otherPoint) {
return otherPoint.y == y;
}
public boolean isVertical(Point otherPoint) {
return otherPoint.x == x;
}
public void transform (double dx, double dy) {
x += dx;
y += dy;
}

Categories

Resources