Box type OR create new type - java

Looking to use one variable to define an x1, x2, x3, x4 for a method. Is there a type that exists that would allow me to do this? I could just do 2 Points, but I'd rather just have one variable. If one doesn't exist, am I able to create a type similar to this that I can make useable in all classes in my package?
Thanks!

Have you considered defining your own tuple?
public class PointsTuple
{
public Point Point1;
public Point Point2;
// replace with appropriate setters and getters if you want.
}
Hopefully you will give your class a more descriptive name than PointsTuple but you get the idea. Here I used public fields but if you are familiar with encapsulation you may opt to use set & get methods.

Yes! The rectangle!
java.awt.rectangle
Rectangle(int x, int y, int width, int height)
Example:
Rectangle r = new Rectangle(x1, y1, x1-x2, y1-y2)

Try making a custom class like this:
class MyPoint{
private int x1;
private int x2;
private int x3;
private int x4;
//Setters and Getters for the points
}

Related

Dealing with Duplicate Method Headers

I'm new(ish) to coding, so let's get that out of the way. Also, I'm currently using Java.
I want to instantiate a Line object in two different ways:
public Line(double x1, double y1, double x2, double y2){}//creates a line connecting two points and
public Line(double x, double y, double dir, double length){}//creates a line extending off of one point
However, they both have the same method header according to the compiler.
I considered adding a useless parameter to the second constructor, but that seems messy and unnecessary. Does anyone have any suggestions on how to deal with a problem like this one both now and in the future?
Note: This topic is only about fixing the header, not about how my code could be improved. Thanks!
You can create two different static factory method for both of those constructors calls.
According to Wikipedia :
The Factory Method design pattern is one of the "Gang of Four"
design patterns that describe how to solve recurring design problems
to design flexible and reusable object-oriented software, that is,
objects that are easier to implement, change, test, and reuse.
The Factory Method design pattern is used instead of the regular class
constructor for keeping within the SOLID principles of programming,
decoupling the construction of objects from the objects themselves
Factory methods are just essentially methods that can have different names (and not having to use the name of the class) and a different number of parameters or even the same number of parameters with a previous one. You can do something like this :
public static Line createJoining(double x1, double y1, double x2, double y2){...}
public static Line createExtending(double x1, double y1, double x2, double y2){...}
Then you could have a single constructor like this
public Line(double x1, double y1, double x2, double y2){}//creates a line connecting two points
Inside createJoining() and createExtending(), you could call this constructor,
and put your different program logic inside these two factory methods.
createJoining() and createExtending(), would have to return a new instance of Line like this:
public static Line createJoining(double x1, double y1, double x2, double y2){
// program logic goes here, where you can do some calculations
return new Line(x1, y1, x2, y2);
}
public static Line createExtending(double x1, double y1, double x2, double y2){
// program logic goes here, where you can do some calculations
return new Line(x1, y1, x2, y2);
}
Usage example,
Line joinedLine = Line.createJoining(0.1, 3.5, 10.5, 1.0);
Line extendedLine = Line.createExtended(0.6, 29.4, 20.0, 2.0);
More info about Factory Methods here
Another way is to avoid the same method header and at the same time use more meaningful parameter types:
import java.awt.geom.Point2D;
…
public Line(Point2D.Double p1, Point2D.Double p2) { … }
public Line(Point2D.Double p, double dir, double length) { … }

Java Composition and Aggregation at same class?

Consider we have two class named Point and Line. And Line class has two constructors. This is code of Point class.
// The Point class definition
public class Point {
// Private member variables
private int x, y; // (x, y) co-ordinates
// Constructors
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point() { // default (no-arg) constructor
x = 0;
y = 0;
}
}
And this is code of Line class.
public class Line {
// Private member variables
Point begin, end; // Declare begin and end as instances of Point
// Constructors
public Line(int x1, int y1, int x2, int y2) {
begin = new Point(x1, y1);
end = new Point(x2, y2);
}`
public Line(Point begin, Point end) {
this.begin = begin;
this.end = end;
}
}
As you see Line class has two constructor. And First constructor is example of Compositon while second constructor example aggregation. Now, what can we say about this case? Can a class have both aggregation and composition? Thank for your answers.
A generally accepted definition of the difference between aggregation and composition is lifetime responsibility and ownership.
Aggregation: An object A holds references to other objects, but those other objects are shared with other classes. When A is disposed, the other objects continue to live and be used in the application
Composition: An object B is "made" of other objects. When A is disposed, so are the other objects.
It's worth quoting Fowler on this:
Few things in the UML cause more consternation than aggregation and composition
...
Aggregation (white diamond) has no semantics beyond that of a regular association. It is, as Jim Rumbaugh puts it, a modeling placebo
...
Composition (black diamond) does carry semantics. The most particular is that an object can only be the part of one composition relationship
So yes, a class can have both composition and aggregation relationships to the objects it holds references to, but possibly not as per the example you have shown.
Since the defining characteristic of composition (versus aggregation) is having exclusive/non-shareable parts (see https://stackoverflow.com/a/27889087/2795909), your example of a Point-Line part-whole relationship is clearly an aggregation (no matter if you pass the point objects to, or create them in, the constructor) since the two points definijng a line can be shared with other lines.

Is it a good practice to have parameter that is a global variable?

I am an AP java student and while working on a project I wondered if it is a good practice to have a parameter that is a global variable. If you're wondering why I would want to do that well is so I wouldn't have to do this:
public class Circle {
private DrawingTool pen;
private SketchPad paper;
private double myX;
private double myY;
private double myWidth;
private double myHeight;
public Circle(double x, double y, double width, double height){
paper = new SketchPad(500,500);
pen = new DrawingTool(paper);
x = myX; //I don't want to have to assign this every time
y = myY; //like here
width = myWidth; // and here
height = myHeight; // and here
}
}
is it allowed to just do the following:
public Circle(double myX, double myY, double myWidth, double myHeight){
paper = new SketchPad(500,500);
pen = new DrawingTool(paper);
}
}
and every time I pass the arguments to the parameter they will automatically be assigned to the global variables?
and every time I pass the arguments to the parameter they will automatically be assigned to the global variables?
No. There's nothing within Java which will make the "parameter to instance variable" (which isn't really "global") assignment automatic. Many IDEs have the ability to generate the code for you, but it does need to be there.
An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages). Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object-oriented programming.
Source: What Is an Object?

How to deal with JavaDoc comment repetition?

I was wondering what the best way of documenting this potential Point class is:
public class Point {
/* the X coordinate of this point */
private int x;
/* the Y coordinate of this point */
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
My concrete concern lies with the repetition between the x and y attributes and their respective getters and setters, as well with the constructor arguments.
It's not that I'm developing a public API or anything of the likes, it's no problem for me to have a general comment regarding some variable and then having the getter and setter have just the same text, for instance. I'd just like to avoid comment repetition in my own internal code. Is there a way to tie getX() and the int x argument of the constructor to the x attribute, for instance?
Thanks
Is there a way to tie getX() and the int x argument of the constructor
to the x attribute, for instance?
No, not that I'm aware of. What I do:
don't comment getters (or setters) at all
if X needs contextual information and if it somehow represents (part of the) state of the class I document it in the class-level Javadoc only
One obvious convention would be not writing JavaDoc comments for trivial getters.

Accessing Private Final Static Double from Another Class

I am trying to access a private final static double from another class.
Here is the class:
public class coolMath{
private final static double alpha = 5.87;
public coolMath(){
}
public static double calDistance(double x1, double y1, double x2, double y2){
double dist = Math.sqrt(Math.pow((x2-x1), 2) + Math.pow((y2-y1),2));
return dist;
}
}
I need to access the variable alpha in another class. Is this possible? Does something need to happen in the constructor to make it available? Any ideas?
Either make alpha a public field or provide a public static double getAlpha() that returns it.
If you make the field public, you access it like so double a = coolMath.alpha.
Otherwise, double a = coolMath.getAlpha();
I strongly suggest you go through java modifiers again.
This could technically be possible through some esoteric reflection methods, but I highly discourage it. It's better to change the visibility of your alpha variable or write some method that returns it (or perhaps sets it, if you need that also (but not if your var is final)).

Categories