Set all members to private or public access mode in Java - java

In C++, I can use public and private access modifiers like this to set all members private together-
class myClass{
private:
int x;
int y;
void add(int X,int Y){
x += X;
y += Y;
}
public:
myClass(int X,int Y){
x = X; y = Y;
}
}
Is there a similar construct in Java, or do I have to set each function and member to public/private/protected separately?
(I can group similar ones like - private int x,y; private float i,j; etc)

You have to set each function and member to public/private/protected separately.
You can still group them by modifier, if you like, although this would be purely to improve readability:
// Public
public int getTotal();
public final double MAX_X = 100;
// Package
int foo;
// Privates
private int i,j;
private double x, y;

You'll have to mention it for each one.
Similarly, it'll be
Class myClass {
private int x;
private int y;
}

Related

how can i change the value of the x and y in if(ab) at getArrow() locally

public class Arrow {
protected static int x;
protected static int y;
public void setA(boolean a) {
this.a = a;
}
public void setB(boolean b) {
this.b = b;
}
public void setAb(boolean ab) {
this.ab = ab;
}
public Arrow( int x1, int y1) {
this.x=x1;
this.y=y1;
}
public double getySpeed(){
return (-ySpeed*Time+Time*Time/10);
}
public boolean getX(){
return x +Math.abs(xSpeed * Time)<canvasWidth-90;
}
public boolean getY(){
return y+getySpeed()<canvasHeight-110;
}
public Matrix getArrow(){
Matrix matrix = new Matrix();//1140,540
matrix.postRotate((int)getAngle(), arrowWidth/2, arrowHeight / 2);
if (a&&!ab) {
// here if i do sout(x) it will show 75 which is the value i gave it in the constructor
return a(x);
}
if(b&&!ab){
// here if i do sout(y) it will show 125 which is the value i gave it in the constructor
return (b(y));
}
if(ab){
x =x+(int) Math.abs(xSpeed * Time);
y = y+(int)getySpeed();
matrix.postTranslate(x,y );
}
return matrix;
}
public Matrix b(int yy ){
Matrix matrix = new Matrix();
matrix.postRotate((int)getAngle(), arrowWidth/2, arrowHeight / 2);
matrix.postTranslate(canvasWidth-90,yy );
return matrix;
}
public Matrix a(int xx ){
Matrix matrix = new Matrix();
matrix.postRotate((int)getAngle(), arrowWidth/2, arrowHeight / 2);
matrix.postTranslate(xx,canvasHeight-100 );
return matrix;
}
I am trying to make a Bitmap arrow stop from leaving the screen so I figured out the maximum x and y coordinates and the arrow moves until it reaches these coordinates.
In getArrow(), at the if(ab) block, what I think I am doing is changing x and y, but in reality, they're not changing.
- They stay the same value I gave them in the constructor.
How can I change the x and y in the class to the value I gave them at
if(ab) in getArrow()
Thank you :*)
The reason for the issue you are facing is because you have declared x and y as static making them class variables which means their values will be the same for all objects.
protected static int x;
protected static int y;
Just remove the term, static from their declaration.
Feel free to comment in case of any doubt/issue.

Java - Implement Cloneable or add a constructor?

Hey I'm actually working with a custom Vector class on Java,
public class Vector {
private double X;
private double Y;
public Vector(double x, double y) {
this.X = x;
this.Y = y;
}
public void setX(double x) {
this.X = x;
}
public double getX(double x) {
return this.X;
}
public void setY(double y) {
this.Y = y;
}
public double getY(double y) {
return this.Y;
}
}
I wanted to add the multiply() method that would return this vector * by the specified factor like that,
public void multiply(double factor) {
this.X *= factor;
this.Y *= factor;
}
The thing is, when I use a function requiring a vector, I'd like to use it like
doSomething(ancientVector.multiply(-1D));
but the jvm isn't satisfied because the arg I send to the function is a void...
How could I also do to make it clean, should I implement Cloneable or create another constructor working the way multiply does?
doSomething(ancientVector.multiply(-1D));
OR add
public Vector(Vector previous, double mFactor) {
this.X *= previous.getX() * mFactor;
this.Y *= previous.getY() * mFactor;
}
I would keep the class immutable and return a new Vector:
public Vector multiply(double factor) {
return new Vector(X * factor, Y * factor);
}
You could do as #Basti said or you could also return a new instance of your Vector:
public Vector multiply(double factor) {
return new Vector (this.X * factor, this.Y * factor);
}
This way when any change is made to the result of your multiply function it does not affect the initial vector object.
Your Vector will have various operations (you've started with multiply) and its usage looks similar to Java API classes such as BigDecimal. I would therefore recommend following its lead, and make the class immutable. That means all its fields should be final:
public class Vector {
private final double x, y; // Note: final. And use lowercase.
public Vector(double x, double y) {
this.x = x;
this.y = y;
}
// Note: no setters!
public double getX() { // Note: no argument.
return x;
}
public double getY() {
return y;
}
public Vector multiply(double factor) {
return new Vector(x*factor, y*factor);
}
}
One of the advantages of immutable classes is that they are purely value-based so you don't have to worry about copy constuctors or cloning. (By the way, Cloneable is hardly ever used nowadays – copy constructors are preferred – except perhaps for arrays.) Instead of copying, just use assignment: Vector secondVector = firstVector;.

Trouble with interfaces and parameters of objects

This is a difficult problem to describe so I will do my best.
Basically my program has two concrete classes: MovablePoint and MovableCircle. Both implement the interface Movable which has four methods: moveUp Down left right.
Now where I am stuck is that when I am constructing my MovableCircle I need to use an instance variable of MovablePoint to represent the center of the circle. However I also need the constructor of MovableCircle to take in 5 params: int x, int y, int xSpeed, int ySpeed, int radius. The first 4 params are the "center" of the circle and they should be instantiated by the MovablePoint.
Here is what I have:
public class MovableCircle implements Movable {
private int radius;
private int y;
private int x;
private int xSpeed;
private int ySpeed;
public MovablePoint circleCenter;
public MovableCircle (int x, int y, int xSpeed, int ySpeed, int radius) {
this.radius = radius;
this.x = circleCenter.getX();
this.y = circleCenter.getY();
this.xSpeed = circleCenter.getxSpeed();
this.ySpeed = circleCenter.getySpeed();
}
Here also is the MovablePoint code if that helps:
public class MovablePoint implements Movable{
private int x;
private int y;
private int xSpeed;
private int ySpeed;
public MovablePoint(int x, int y, int xSpeed,
int ySpeed) {
this.x = x;
this.y = y;
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
}
Obviously there is more code beyond what I have posted but I am fairly positive I have coded the class methods properly.. I believe my problem lies in the fact that I need to instantiate the MovableCircle's center with a MovablePoint but I'm not sure how to remedy it. Thanks for your help and I apologize if this problem is easily solved and I am just oblivious..
How about something like:
public class MovableCircle implements Movable {
private int radius;
private MovablePoint circleCenter;
public MovableCircle (int x, int y, int xSpeed, int ySpeed, int radius) {
this.radius = radius;
this.circleCenter = new MovablePoint(x, y, xSpeed, ySpeed);
}

Coordinate (ArrayList)

I'm having trouble formatting when using coordinates.
public class Coordinate {
public int x;
public int y;
public Coordinate( int x, int y) {
this.x = x;
this.y = y;
}
}
So, later, when I'm trying to find the location of my rabbit, I use:
Coordinate (x, y) = rabbit.get(i);
And that doesn't work, but this does:
Coordinate z = rabbit.get(i);
I want to find the x and y values so I'm confused as to how to do that and why the Coordinate (x, y) doesn't work. Thanks for your help!
As your attributes x,y of Coordinate are public:
Coordinate z = rabbit.get(i);
int xCor = z.x; //this is your x coordinate
int yCor = z.y; //this is your y coordinate
Normaly these attriubtes are private and you access them with a getter/setter-Method:
public class Coordinate {
private int x;
private int y;
public Coordinate( int x, int y) {
this.x = x;
this.y = y;
}
public int getX(){
return this.x;
}
public void setX(int newX){
this.x = newX;
}
//same for Y
}
//in the main program.
Coordinate z = rabbit.get(i);
int yourX = z.getX() //this is your x coordinate
int yourY = z.getY() //this is your y coordinate
I assume you use Java, so I added the Tag, this enables highlighting. This works with other languages in the same way.

Java initializing classes without repeating myself

Is it possible to rewrite the following a bit more concise that I don't have to repeat myself with writing this.x = x; two times?
public class cls{
public int x = 0;
public int y = 0;
public int z = 0;
public cls(int x, int y){
this.x = x;
this.y = y;
}
public cls(int x, int y, int z){
this.x = x;
this.y = y;
this.z = z;
}
}
BoltClock's answer is the normal way. But some people (myself) prefer the reverse "constructor chaining" way: concentrate the code in the most specific constructor (the same applies to normal methods) and make the other call that one, with default argument values:
public class Cls {
private int x;
private int y;
private int z;
public Cls(int x, int y){
this(x,y,0);
}
public Cls(int x, int y, int z){
this.x = x;
this.y = y;
this.z = z;
}
}
Call the other constructor within this overloaded constructor using the this keyword:
public cls(int x, int y, int z){
this(x, y);
this.z = z;
}
Read about constructor overloading
http://www.javabeginner.com/learn-java/java-constructors
You can use initilization block for this purpose.
Very simple: Just write an initialize function like this:
public class cls{
public int x = 0;
public int y = 0;
public int z = 0;
public cls(int x, int y){
init(x,y,0);
}
public cls(int x, int y, int z){
init(x,y,z);
}
public void init(int x, int y, int z ) {
this.x = x;
this.y = y;
this.z = z;
}
}

Categories

Resources