I am trying to create a function which makes a random asteroid with (x, y, velocity x, velocity y) values of type BasicAsteroid, this is the constructor and function which creates a random asteroid:
private double x, y;
private double vx, vy;
public BasicAsteroid(double x, double y, double vx, double vy) {
this.x = x;
this.x = y;
this.vx = vx;
this.vy = vy;
}
public static BasicAsteroid makeRandomAsteroid() {
Random rand = new Random();
BasicAsteroid x = new BasicAsteroid((rand.nextInt()%FRAME_WIDTH), (rand.nextInt()%FRAME_HEIGHT), (rand.nextInt()%MAX_SPEED), (rand.nextInt()%MAX_SPEED));
System.out.println(x);
return x;
}
However this is the output when I create the asteroid:
game1.BasicAsteroid#6773120a
game1.BasicAsteroid#4261b6b3
game1.BasicAsteroid#2673b915
game1.BasicAsteroid#113eb90b
game1.BasicAsteroid#1abcc522
How can I output the values instead of the class#hashcode?
Thanks.
Overwrite the toString() method
#Override
public String toString(){
return "Asteroid at "+x+" "+y+" velocity "+vx+" "+vy;
}
You need to override the method toString() on your class.
#Override
public String toString(){
return "x: "+this.x+"y: "+this.y+"vx: "+this.vx+"vy: "+this.vy
}
In java, when you print an object, its toString() method is invoked to create the string that will be printed. What you're seeing here is the output of the default toString method. If you want to print the values nicely, add a function like this:
#Override
String toString(){
return "Asteroid with coords: (" + x + ", " + y + "), velocity: (" + vx + ", " + vy + ")";
}
Related
Before you read ahead this question is for my homework so it will be specific.
I am writing some code that uses polymorphism to display the properties of a rectangle (ie, x, y, height...). The code uses inheritance and polymorphism to achieve the results. Everything like the width and topleftX point is being determined using an algorithm that picks a random integer from a specified range. How I tried to fix the problem is to write a getWidth() and getLength() method to the rectangle class. I put in a super(); for the other class that gets the X and Y coordinates.
The problem that I am having is that I am unsure of how to access the classes that will give me the width and height of the rectangle. Inside of the methods that I have written the super() displays an error and it says "(x,y) cannot be applied to ()" which I am unsure of what it means. I assume that the values are private and I cannot access them, but in the class with the implementation, there are no private instances, so I am confused.
This is my code below:
public GeometricShapeTester(){
shapes = new GeometricShape[20];
Random rand = new Random();
int option;
final int COORD = 50;
final int LENGTH1 = 50;
final int LENGTH2 = 100;
for(int i=0; i<shapes.length; i++){
option=rand.nextInt(4);
switch(option){
case 0:
shapes[i]= new Rectangle(
rand.nextInt(COORD),
rand.nextInt(COORD),
rand.nextInt(LENGTH2),
rand.nextInt(LENGTH2));
break;
case 1:
shapes[i]= new Square(
rand.nextInt(COORD),
rand.nextInt(COORD),
rand.nextInt(LENGTH2));
break;
case 2:
shapes[i]= new Oval(
rand.nextInt(COORD),
rand.nextInt(COORD),
rand.nextInt(LENGTH1),
rand.nextInt(LENGTH2));
break;
case 3:
shapes[i]= new Circle(
rand.nextInt(COORD),
rand.nextInt(COORD),
rand.nextInt(LENGTH1));
}
}
}
public GeometricShape[] getShapes(){
return shapes;
}
}
abstract class GeometricShape{
private int x;
private int y;
public GeometricShape(int x,int y){
this.x = x;
this.y = y;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
abstract public double getArea();
abstract public String toString();
}
class Rectangle extends GeometricShape{
public Rectangle(int x, int y, int nextInt, int nextInt1) {
super();
}
public getWidth(){
super();
}
public getLength(){
super();
}
#Override
public double getArea() {
return 0;
}
#Override
public String toString() {
return "Rectangle: [x:" + getX() + ", y:" + getY() + ", width: + " + getWidth() + " height: " + getHeight() + "]";
}
// body
}
How can I keep precision when working with doubles in Java? I have the following test case:
double x = -1.0 / 3.0;
double y = 1.0000000001;
Point2 p = new Point2(x, y);
String expected = "(" + x + ", " + y + ")";
assertEquals(expected, p.toString());
Point2 constructor:
public Point2(double x, double y) {
this.x = x;
this.y = y;
}
toString Override:
#Override
public String toString() {
return String.format("(%f, %f)", this.x, this.y);
}
Which is failing: org.junit.ComparisonFailure: expected:<(-0.333333[3333333333, 1.0000000001])> but was:<(-0.333333[, 1.000000])>
Problem was in my toString() method. Makes sense that they were being formatted as floats now. Ended up updated it to :
#Override
public String toString() {
return "(" + this.x + ", " + this.y + ")";
}
public class Vector {
private int x, y, z;
public Vector(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public void add(Vector v) {
x += v.x;
y += v.y;
z += v.z;
}
public void silly(int x, int y, int z) {
this.x = ++x;
this.y = y + 1;
this.z += z;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getZ() {
return z;
}
#Override
public String toString() {
return "Vector, <x = " + x + ", y = " + y + ", z = " + z + ">";
}
public static void main(String[] args) {
Vector a = new Vector(1, 0, 0);
Vector b = new Vector(0, 1, 0);
Vector c = a;
int x = 1;
int y = 2;
int z = 3;
a.add(b);
b.add(b);
c.add(c);
c.silly(x, y, z);
System.out.println("a: " + a);
System.out.println("b: " + b);
System.out.println("c: " + c);
System.out.println("x: " + x + "\ty: " + y + "\tz: " + z);
}
}
I have obviously been unclear in my question, sorry about that. I got this as practice from my teacher and I am supposed to explain the output of the last 4 lines in the code. I have no idea why the output looks as it does. I'm not very good at alias and so on. Someone might be able to give me an explanation? Thanks.
Vector c = a;
means that you create reference which is linked to reference a and its object. You don't call a constructor there. You don't create any object there. Just new reference
The only question I can see is 'What is the relation between Vectors a and c?' So I'll answer that.
When you use the 'new' keyword you are creating a new object which is stored in the heap. So 'a' and 'b' are two separate objects when they have been instantiated. When you say:
Vector c = a;
You are not creating a new object in the heap, merely making a new reference to the same object. So now both 'a' and 'c' are referencing the same thing. If you change a, c will change, and vice versa.
When:
c.add(c);
Is called then the ints in c are simply being added to themselves.
I'm working on a lab for school and I have it almost completed, but there's one part that I can't get to work. The inheritance works except when I get to Cube. For some reason, it won't calculate the Area or Volume (it just comes up with 0). I'm thinking it's a problem with the way I have the inheritance from Square to Cube. Help would be awesome!
package InheritanceTest;
import javax.swing.JOptionPane;
public class InheritanceTest {
public static void main(String[] args) {
String input = "";
Point point = new Point();
input = getinput("Set variable X");
point.setx(input);
input = getinput("Set variable Y");
point.sety(input);
System.out.println("Point, x = " + point.getx() + " y = " + point.gety());
Square square = new Square();
input = getinput("Set variable Side Length");
square.setSideLength(input);
System.out.println("Square, x = " + point.getx() + " y = " + point.gety()
+ " Area = " + square.getAreaOfSquare() + " Perimeter = "
+ square.getPerimeterOfSquare());
Cube cube = new Cube();
input = getinput("Set variable depth");
cube.setDepth(input);
System.out.println("cube, x = " + point.getx() + " y = " + point.gety()
+ " Depth = " + cube.getDepth() + " Area = " + cube.getAreaOfCube()
+ " Volume = " + cube.getVolumeOfCube());
}
private static String getinput(String string) {
String x = JOptionPane.showInputDialog(string);
return x;
}
}
package InheritanceTest;
public class Cube extends Square {
private int depth;
Cube() {
super();
depth = 0;
}
Cube(int x, int y, int sideLength, int d) {
super(x, y, sideLength);
this.depth = d;
}
public int getAreaOfCube() {
return (6 * sideLength * sideLength);
}
public int getVolumeOfCube() {
return (sideLength * sideLength * sideLength);
}
public String getDepth() {
return Integer.toString(depth);
}
public void setDepth(String i) {
depth = Integer.parseInt(i);
}
}
package InheritanceTest;
public class Point {
private int x;
private int y;
Point() {
x = 0;
y = 0;
}
Point(int x, int y) {
this.x = x;
this.y = y;
}
public String getx() {
return Integer.toString(x);
}
public String gety() {
return Integer.toString(y);
}
public void setx(String input) {
x = Integer.parseInt(input);
}
public void sety(String input) {
y = Integer.parseInt(input);
}
}
package InheritanceTest;
public class Square extends Point {
protected int sideLength;
Square() {
super();
sideLength = 0;
}
Square(int x, int y, int l) {
super(x, y);
this.sideLength = l;
}
public int getAreaOfSquare() {
return sideLength * sideLength;
}
public int getPerimeterOfSquare() {
return sideLength + sideLength;
}
public String getSideLength() {
return Integer.toString(sideLength);
}
public void setSideLength(String input) {
sideLength = Integer.parseInt(input);
}
}
When you create cube (new Cube()) you aren't setting the side length (or x and y) for the Square Object it extends.
Cube(){
// This is the constructor called.
super();
depth = 0;
}
Cube(int x, int y, int sideLength, int d){
super(x, y, sideLength);
this.depth = d;
}
You probably want extract the x,y and length values into variables and use "new Cube(x, y, length, depth)"
Something like the following
String x = getinput("Set variable X");
String y = getinput("Set variable Y");
String sideLength = getinput("Set variable Side Length");
String depth getinput("Set variable depth");
Cube cube = new Cube(x, y, sideLength, depth);
Look at how you are defining getVolumeOfCube(). You are calculating volume with sideLength, but you never set sideLength to any non-zero value. Change sideLength to depth and you will get the value you are looking for.
please my question are two and very simple
misinterpret enum as is
this idea missing some important abstraction in my code
code example, where oprt.calc(x, y) isn't compilable, with warning cannot find symbol
public enum Operation {
PLUS {
public double calc(double x, double y) {
return x + y;
}
},
MINUS {
public double calc(double x, double y) {
return x - y;
}
},
MULTILPLE {
public double calc(double x, double y) {
return x * y;
}
},
DIVIDED_BY {
public double calc(double x, double y) {
return x / y;
}
};
public static void main(String args[]) {
double x = 15.25;
double y = 24.50;
for (Operation oprt : Operation.values()) {
System.out.println(x + " " + oprt + " "
+ y + " = " + oprt.calc(x, y));
}
}
}
What you miss is abstract declaration of calc() method:
enum Operation {
PLUS {
public double calc(double x, double y) {
return x + y;
}
},
MINUS {
public double calc(double x, double y) {
return x - y;
}
},
MULTILPLE {
public double calc(double x, double y) {
return x * y;
}
},
DIVIDED_BY {
public double calc(double x, double y) {
return x / y;
}
};
**public abstract double calc(double x, double y);**
public static void main(String args[]) {
double x = 15.25;
double y = 24.50;
for (Operation oprt : Operation.values()) {
System.out.println(x + " " + oprt + " "
+ y + " = " + oprt.calc(x, y));
}
}
}
You need to declare an abstract method double calc(double x, double y) in the enum directly, and override it in every enum member.
The correct syntax to use enum methods is:
private enum Operation {
PLUS, MINUS, MULTILPLE, DIVIDED_BY;
public double calc(double x, double y) {
switch (this) {
case PLUS:
return x + y;
case MINUS:
return x - y;
case MULTILPLE:
return x * y;
case DIVIDED_BY:
return x / y;
}
return 0;
}
}
public static void main(String args[]) throws IOException {
double x = 15.25;
double y = 24.50;
for (Operation oprt : Operation.values()) {
System.out.println(x + " " + oprt + " " + y + " = "
+ oprt.calc(x, y));
}
}
You are overriding calc(), while you have no original calc() method. Either declare an abstract method:
public abstract double calc(double x, double y);
or declare a concrete method with a default implementation:
public double calc(double x, double y)
{
// ...
}
It doesn't compile because currently, the calc method only exists in each of the possible values of your enum - but it does not exist on the type Operation itself. That's why your compiler (and mine) doesn't accept it.
So, you need to define the method in the type. Maybe something like this:
public abstract double calc(double x, double y);
Your enum values (PLUS, MINUS, MULTIPLE and DIVIDED_BY each implement that method.
public double calc(double x, double y){}
is same as
private double calc(double x,double y){}
unless you add calc() method to the enum Operation.As per JLS:
Instance methods declared in these class bodies are may be invoked outside
the enclosing enum type only if they override accessible methods in
the enclosing enum type.
So basically , the type of oprt is Operation and as Operationdoesn't have any declaration for method called double calc(double x,double y), you cannot invoke the method using oprt. In short the methods defined in class bodies should be overridden methods, for them to be accessible outside.