Multi-Level inheritance not working - java

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.

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!!

Did I correctly translate this pseudocode into Java?

I asked this question on Math.se a few days ago, and got the following answer in pseudocode:
Function RandomCircleInside(centerX, centerY, radius):
Let newRadius = radius * Random()
Let radians = 2.0 * 3.14159265358979323846 * Random()
Let deviation = (radius - newRadius) * Sqrt(Random())
Let newX = centerX + deviation * Cos(radians)
Let newY = centerY + deviation * Sin(radians)
Return (newX, newY, newRadius)
End Function
I changed the pseudocode to Java and added my own changes to fit my needs. The new code looks like this:
Circle createNewCircle(int centerX, int centerY, int radius, int newR, Color newColor) {
int newRadius = radius * Random();
double radians = 2.0 * 3.141592653589793 * Random();
double deviation = (radius - newRadius) * Math.sqrt(Random());
System.out.println(radius + " - " + newRadius + " * sqrt(0 or 1) = " + (radius-newRadius) + " * (0 or 1) = " + deviation);
double newX = centerX + deviation * Math.cos(radians);
System.out.println(centerX + " + " + deviation + " * cos(" + radians + ") = " + (centerX + deviation) + " * " + Math.cos(radians));
double newY = centerY + deviation * Math.sin(radians);
int newCirX = (int) newX;
int newCirY = (int) newY;
Circle newCir = new Circle(newCirX, newCirY, newR*2, newR*2, newR, newColor, true);
return newCir;
}
The code itself is supposed to create a new Circle inside of a preexisting one. I created a circle class that looks like this:
import java.awt.Color;
import java.awt.Graphics;
public class Circle {
public int X, Y, Width, Height, radius;
public Color color;
public boolean toFill;
public Circle(int x, int y, int width, int height, int radius, Color color, boolean fill) {
X = x;
Y = y;
Width = width;
Height = height;
this.radius = radius;
this.color = color;
toFill = fill;
}
public void render(Graphics g) {
g.setColor(color);
for(int i=-5; i<5; i++) {
if(toFill) {
g.fillOval(X+i, Y+i, Width-i, Height-i);
} else {
g.drawOval(X+i, Y+i, Width-i, Height-i);
}
}
}
public boolean contains(Circle pBound) {
int pBoundCenterX = pBound.X+pBound.radius;
int cirCenterX = X+radius;
int diffBetweenCentersX = Math.abs(pBoundCenterX-cirCenterX);
int pBoundCenterY = pBound.Y+pBound.radius;
int cirCenterY = Y+radius;
int diffBetweenCentersY = Math.abs(pBoundCenterY-cirCenterY);
if(diffBetweenCentersX<= (pBound.radius+radius) && diffBetweenCentersX>=Math.abs(pBound.radius-radius)) { // X
if(diffBetweenCentersY>=Math.abs(pBound.radius-radius)) { // Y
return true;
}
}
return false;
}
public int getX() {
return X;
}
public int getWidth() {
return Width;
}
public int getRadius() {
return radius;
}
public void setWidth(int width) {
Width = width;
}
public int getHeight() {
return Height;
}
public void setHeight(int height) {
Height = height;
}
public void setX(int x) {
X = x;
}
public int getY() {
return Y;
}
public void setY(int y) {
Y = y;
}
}
My way of creating the new circle is this:
if(secInGame==timesForCircle[X] && !hasChanged) { // circle 2
Circle cir1 = cir;
cir = createNewCircle(cir1.X+(cir1.Width/2), cir1.Y+(cir1.Height/2), cir1.getRadius(), 135, Color.cyan);
hasChanged = true;
circleOn++;
circ++;
}
Where cir1 is the preexisting Circle and cir is the new circle.
Is there anything I didn't code correctly? I've tried a few different variations, but they all give the same result.
Before I implemented the pseudocode, my circles looked like this:
but now it looks like this:
All of my code can be found on github at: link
I think there are several issues in your code.
1. First of all it is not clear why your Circle has radius, Width and Height. For a circle all 3 things should be the same. Also your render in case toFill is true looks strange. Here is a simplified version (note: I didn't compile it so there might be some bugs):
public class Circle {
public int X, Y, radius;
public Color color;
public boolean toFill;
public Circle(int x, int y, int radius, Color color, boolean fill) {
X = x;
Y = y;
this.radius = radius;
this.color = color;
toFill = fill;
}
public void render(Graphics g) {
g.setColor(color);
final int r2 = 2*radius;
if(toFill) {
g.fillOval(X, Y, r2, r2);
}
else {
for(int i=-5; i<5; i++) {
g.drawOval(X+i, Y+i, r2-i, r2-i);
}
}
}
public boolean contains(Circle pBound) {
int pBoundCenterX = pBound.X+pBound.radius;
int cirCenterX = X+radius;
int diffBetweenCentersX = Math.abs(pBoundCenterX-cirCenterX);
int pBoundCenterY = pBound.Y+pBound.radius;
int cirCenterY = Y+radius;
int diffBetweenCentersY = Math.abs(pBoundCenterY-cirCenterY);
if(diffBetweenCentersX<= (pBound.radius+radius) && diffBetweenCentersX>=Math.abs(pBound.radius-radius)) { // X
if(diffBetweenCentersY>=Math.abs(pBound.radius-radius)) { // Y
return true;
}
}
return false;
}
public int getX() {
return X;
}
public int getRadius() {
return radius;
}
public void setX(int x) {
X = x;
}
public int getY() {
return Y;
}
public void setY(int y) {
Y = y;
}
}
I didn't check your code, but I'd consider as a good practice:
renaming x and y into leftX and topY to avoid confusion with centerX/centerY meaning. Or change meaning to more typical center one.
declaring all your fields as private (see encapsulation);
declaring all your fields as final and remove all the setXyz methods (see immutability)
2. I don't understand why your createNewCircle has newR parameter and at the same time you generate a random newRadius in the first line. One of these definitely should be removed. Given that the parameter is always a constant 135 I think it should be removed.
3. Now I believe the main bug in your translation is in the lines
int newCirX = (int) newX;
int newCirY = (int) newY;
It probably should be something like
int newCirX = (int) newX - newRadius;
int newCirY = (int) newY - newRadius;
It looks like you messed with center vs top-left. Actually I think the fact that you made such a bug is an argument that supports renaming x and y I suggested in item #1.

Im wondering why when I get the string output at then end perimeter is not being calculated and put in the string

I'm trying to do my first methods.
I'm having trouble getting the perimeter to display the output as a String. I'm wondering why this is happening. I very well could have other problems inside of my code but the perimeter not outputting is what is holding me back right now.
Following is my code.
public class Polygon {
public Polygon() {
int numSides = 4;
double SideLength = 5.0;
double xCoord = 0.0;
double yCoord = 0.0;
double apothem = 5.0;
double perimeter = 20.0;
}
private int numSides = 2;
private double SideLength = 2;
private double xCoord;
private double yCoord;
private double apothem;
private double perimeter;
private double area;
public Polygon(int numsides, double sideLength, double xcoord, double ycoord, double Apothem, double Perimeter) {
SideLength = sideLength;
numSides = numsides;
xCoord = xcoord;
yCoord = ycoord;
apothem = Apothem;
perimeter = Perimeter;
}
public int getnumsides() {
return numSides;
}
public double getSideLength() {
return SideLength;
}
public double getxcoord() {
return xCoord;
}
public double getycoord() {
return yCoord;
}
public double getApothem() {
return apothem;
}
public double getPerimeter() {
return numSides * SideLength;
}
public void setsideLength(double ssideLength){
SideLength = ssideLength;
}
public void setnumsides(int snumsides){
numSides = snumsides;
}
public void setxcoord(double sxcoord){
xCoord = sxcoord;
}
public void setycoord(int sycoord){
yCoord = sycoord;
}
public void setApothem(int sApothem){
apothem = sApothem;
}
public void setPerimeter(int sPerimeter){
perimeter = sPerimeter;
}
public String toString() {
String str = numSides + " is the number of sides the polygon has and " + SideLength + " is how long the sides are. "+ xCoord + " is how long the x coordinate is and " + yCoord + " is how long the y coordinate is. " + apothem + " is the apothem of the polygon and " + perimeter + " is the perimeter of the polygon.";
return str;
}
public void getArea() {
area = .5 * apothem * perimeter;
}
}
You are again defining same field variables in Polygon() constructor which is not required because you have already defined them as private class members. This is the reason that some values are setting as default while printing toString() method.
Your Polygon() constructor should be look like this:
public Polygon() {
numSides = 4;
SideLength = 5.0;
xCoord = 0.0;
yCoord = 0.0;
apothem = 5.0;
perimeter = 20.0;
}
What do you mean by "perimeter not outputting"?
May be this is what you want to achieve?
public static void main(String[] args){
Polygon p = new Polygon();
double perimeter = p.getPerimeter();
System.out.println("Perimeter is " + perimeter);
}

I'm trying to draw a new dot every 250 milliseconds but it only draws a dot once

//I'm trying to draw a new dot every 250 milliseconds but it only draws the dot a single time. I have tried fixing it many times, but it still will only paint a single dot, rather than one after 250 milliseconds. Is this a problem with the timer or the paint method? Here is the code:
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Window extends JPanel{
private int size;
private static double maxValue;
private double elevation;
private double vertV;
public double horizV;
public double gravity;
public double range;
public double time;
public double t = 0;
public Window(int s, double v, double e, double v2, double g,double h,double r,double t){
size = s;
maxValue = v;
elevation = e;
vertV = v2;
gravity = g;
horizV = h;
range = r;
time = t;
setPreferredSize(new Dimension(size, size));
}
public void paintComponent(Graphics g){
g.drawLine(size/25, 0,size/25, size);
g.drawLine(0, size - (size/25), size, size - (size/25));
double[] lines = getLine();
int x = size/5 + (size/25), y = size - (size/25);
int x2 = x;
for(int i = 0; i < 4; i++){
g.drawLine(x, y+5, x, y-5);
g.drawString(lines[i]+"",x-size/50,y+size/30);
x+=x2;
}
int yx = size/25, yy = size - (size/5 + (size/25));
int y2 = size/5 + (size/25);
for(int i=0;i<4;i++){
g.drawLine(yx-5, yy, yx+5, yy);
g.drawString(lines[i]+"",yx-size/25,yy+size/30);
yy -= y2;
}
drawDots(g);
}
//this is the place where i make the dots but it only makes one.
//used to be a for loop but i altered it to an if statement so i could paint one dot at a time
public void drawDots(Graphics g)
{
double ratio = (size-((size/25)*2))/maxValue;
double fx;
double xvalue;
// This for loop is where dots are drawn, each iteration draws one dot. It starts at zero, and counts up to the time variable t.
if(t<=time)
{
t+=0.025;
t = Math.round(t*1000.0)/1000.0;
fx = function(t);
xvalue = xfunction(t);
if(fx >= 0){
System.out.print("Time: " + t + " " + "Range: " + xvalue + " " + "Height: ");
System.out.println(fx);
g.drawLine((int)(size/25+(ratio*xvalue)), (int)((size-(size/25))-(ratio*fx)),
(int)(size/25+(ratio*xvalue)), (int)((size-(size/25))-(ratio*fx)));
}
}
}
//where i make the timer
//250 mill
public void dostuff()
{
int delay = 250;
ActionListener taskPerformer = new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
repaint();
}
};
new Timer(delay, taskPerformer).start();
}
public double xfunction(double t){
double x = 0.0;
x = Math.round(horizV * t * 1000.0)/1000.0;
return x;
}
public double function(double t){
double fx = 0.0;
fx = Math.round((vertV*t + .5*(-(gravity))*(t*t) + elevation)*1000.0)/1000.0;
return fx;
}
private static double[] getLine(){
double increment = maxValue / 4;
double currentLine = 0;
double[] lines = new double[4];
for(int i = 0; i < 4; i++){
currentLine+=increment;
lines[i] = Math.round(currentLine * 10.0)/10.0;
}
return lines;
}
}
This is the original version of the code that displays the projectile's motion, but it does not wait 250 milliseconds between drawing each point:
import javax.swing.JPanel;
import java.awt.*;
public class Window extends JPanel{
private int size;
private static double maxValue;
private double elevation;
private double vertV;
public double horizV;
public double gravity;
public double range;
public double time;
public Window(int s, double v, double e, double v2, double g,double h,double r,double t){
size = s;
maxValue = v;
elevation = e;
vertV = v2;
gravity = g;
horizV = h;
range = r;
time = t;
setPreferredSize(new Dimension(size, size));
}
public void paintComponent(Graphics g){
g.drawLine(size/25, 0,size/25, size);
g.drawLine(0, size - (size/25), size, size - (size/25));
double[] lines = getLine();
int x = size/5 + (size/25), y = size - (size/25);
int x2 = x;
for(int i = 0; i < 4; i++){
g.drawLine(x, y+5, x, y-5);
g.drawString(lines[i]+"",x-size/50,y+size/30);
x+=x2;
}
int yx = size/25, yy = size - (size/5 + (size/25));
int y2 = size/5 + (size/25);
for(int i=0;i<4;i++){
g.drawLine(yx-5, yy, yx+5, yy);
g.drawString(lines[i]+"",yx-size/25,yy+size/30);
yy -= y2;
}
drawDots(g);
}
public void drawDots(Graphics g){
double ratio = (size-((size/25)*2))/maxValue;
double fx;
double xvalue;
// This for loop is where dots are drawn, each iteration draws one dot. It starts at zero, and counts up to the time variable t.
for(double t=0;t<=time; t+=0.025){
t = Math.round(t*1000.0)/1000.0;
fx = function(t);
xvalue = xfunction(t);
if(fx >= 0){
System.out.print("Time: " + t + " " + "Range: " + xvalue + " " + "Height: ");
System.out.println(fx);
g.drawLine((int)(size/25+(ratio*xvalue)), (int)((size-(size/25))-(ratio*fx)),
(int)(size/25+(ratio*xvalue)), (int)((size-(size/25))-(ratio*fx)));
}
}
}
public double xfunction(double t){
double x = 0.0;
x = Math.round(horizV * t * 1000.0)/1000.0;
return x;
}
public double function(double t){
double fx = 0.0;
fx = Math.round((vertV*t + .5*(-(gravity))*(t*t) + elevation)*1000.0)/1000.0;
return fx;
}
private static double[] getLine(){
double increment = maxValue / 4;
double currentLine = 0;
double[] lines = new double[4];
for(int i = 0; i < 4; i++){
currentLine+=increment;
lines[i] = Math.round(currentLine * 10.0)/10.0;
}
return lines;
}
}

Creating a method to increase height and width of an oblong by amounts entered by the user

Ok so for class I have been working on some oblong questions based off an oblong class. The question I am having an issue with is to create a method to increase the height and width of the oblong by a user defined amount.
This is my main:
import java.util.Scanner;
public class Oblong6
{
public static void main(String [] args)
{
Oblong ob1 = new Oblong();
Scanner keyboardIn = new Scanner(System.in);
Oblong ob = new Oblong();
double h, w, x;
System.out.print ("Enter the height of the oblong:");
h = keyboardIn.nextDouble();
System.out.print ("Enter the width of the oblong:");
w = keyboardIn.nextDouble();
System.out.print (" Enter the amount to increment by ");
x = keyboardIn.nextInt();
ob.setHeight(h);
ob.setWidth(w);
ob.setX(x);
System.out.println (ob.incHeight());
System.out.println (ob.incWidth());
System.out.println("Height " + h);
System.out.println("Width " + w);
}
}
And this is the method I have created in the oblong class to increase them:
public class Oblong
{
// instance variables
private double height;
private double width;
private double x;
// constructor
public Oblong()
{
height = 0.0;
width = 0.0;
x = 0.0;
}
// methods
public double getHeight()
{
return height;
}
public double getWidth()
{
return width;
}
public double setX(double x)
{
return x;
}
public void setWidth(double w)
{
width = w;
}
public void setHeight(double h)
{
height = h;
}
public double calculateArea()
{
return width * height;
}
public double calculatePerimeter()
{
return width + height * 2;
}
public boolean isSquare()
{
if(height == width)
{
return true;
}
else
{
return false;
}
}
public double incHeight()
{
{
return height + x ;
}
}
public double incWidth()
{
{
return width + x ;
}
}
}// end of class
but it only ever prints out the original height and width.
Your code:
public double incHeight()
{
{
return height + x ;
}
}
That just adds two numbers and returns the result.
It doesn't do anything else. The same is true for your other methods.
Whereas the purpose of the method seems to be to alter the state of the underlying object. But as said; the current implementation does not alter that state.
Hope that is good enough to help you to resolve your problem on your own.
Side note: read about Java syntax. Your extra pair of braces ... doesn't do anything either.
Whit this:
public double incWidth()
{
{
return width + x ;
}
}
You are returning width + 1 but you are not modifying the private attribute. Just do this:
public double incWidth(){
this.width = this.width + 1;
return this.width;
}
Also in setters you don't need to return anything. To change an attribute inside a class do something like:
private double value;
private void setValue( double value ) {
this.value = value;
}
With this.value you are refering to the private value inside the class. Without this you are refering to the parameter value of the method setValue.
Further reading:
How do getters and setters work?
Your instance variable x has not been set, hence the height + x will return height + 0.0.
change this:
public double setX(double x)
{
return x;
}
To this:
public double setX(double x)
{
this.x = x;
}
This will return value to be displayed, but it should be set for later use, so you need this :
public void incHeight()
{
setHeight(height + x) ;
}
and then :
System.out.println("Height " + height); // height not h

Categories

Resources