Trouble with interfaces and parameters of objects - java

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

Related

How to specify width and height of my object JAVA

So i am trying to add collision detection for a little game i am creating and i am specifying the width and height of my defender class however i am unable to do so. My code below gives me an error saying the global variable width and height do not exist
class Defender
{
int x,y;
Defender(int x, int y)
{
this.x = x;
this.y = y;
}
void render()
{
//draw a defender
fill(255,0,0);
rect(x,y,50,20);
triangle(x+50,y,x+50,y+20,x+60,y+10);
fill(0,0,100);
rect(x,y-10,20,10);
}
boolean collision()
{
color detectedColour;
for (int i=y; i<y+ Defender.height; i++) {
detectedColour = get(x + Defender.width, i);
if (detectedColour == Alien1) {
return true;
}
}
return false;
}
}
The problem is you're trying to access the variables statically when they do not exist in the class. Create class members that represent the width and height of the defender.
class Defender {
private final int x;
private final int y;
private final int width;
private final int height;
public Defender(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
// getters
}
Defender defender = new Defender(0, 0, 1, 1);
int width = defender.width;
int height = defender.height;

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

Set all members to private or public access mode in 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;
}

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