There is something wrong with the if-statement in our setter - java

We are making a setter with the if-statement. We are trying to limit the approved values, and are using a print-method to print out the value that is set, or "wrong" if the value is not accepted.
This is the method:
Public class Sykkel extends Kjøretøy
{
private int height;
public Sykkel(String colour)
{
// initialise instance variables
super(1, colour);
this.height = height;
}
public void setHeight (int height) {
if(height > 35 && height < 70){
System.out.println("Sykkelen er " + height + " cm høy.");
} else {
System.out.println("Wrong");
}
}
public int getHeight() {
return height;
}
No matter what we set as the height it prints out "Wrong". Does anyone see the error in the code?
we've tried both this.height and just height, but the outcome is still the same.
Thanks in advance.

My observations.
Your constructor should be:
public Sykkel(String colour, int height)
{
// initialise instance variables
super(1, colour);
this.height = height;
}
In your setHeight method, you need to assign the height to the instance variable.
public void setHeight (int height) {
if(height > 35 && height < 70){
System.out.println("Sykkelen er " + height + " cm høy.");
} else {
System.out.println("Wrong");
}
// add the below line:
this.height = height;
}
The other method seems okay.
When I call setHeight(40);, I am getting
Sykkelen er 40 cm høy.
Hope this helps!

There is no assignment in setter method, just append
this.height = height;
public void setHeight (int height) {
if(height > 35 && height < 70){
System.out.println("Sykkelen er " + height + " cm høy.");
} else {
System.out.println("Wrong");
}
this.height = height;
}

Related

error "The operator * is undefined for the argument type(s) int, Box"

So I have no idea why vol and boxes[i] isnt multiplying, Im trying to get the dimensions of my boxes which I have already done and the volume of each individual box using a loop, that needs to be in the box class for some reason according to my professor
public class Main {
public static void main(String[] args) {
Box[] boxes = new Box[5];
boxes[0] = new Box(2.5, 1.2, 2);
boxes[1] = new Box(1.5, 1.2, 2);
boxes[2] = new Box(2.5, 1.2, 0.5);
boxes[3] = new Box(3.5, 2.1, 0.3);
for (int i = 0; i < boxes.length - 1; i++) {
System.out.println("The dimensions of box " + (i + 1) +
" is..\n" + boxes[i]);
}
Box.volume(boxes);
}
}
public class Box {
private double length;
private double width;
private double height;
Box(double length, double width, double height) {
this.length = length;
this.width = width;
this.height = height;
}
public String toString() {
return "l: " + length + "\n" + "w: "+ width + "\n" + "h: " + height;
}
//SETTERS AND GETTERS
public void setLength(double length) {
this.length = length;
}
public double getLength() {
return length;
}
public void setWidth(double width) {
this.width = width;
}
public double getWidth() {
return width;
}
public void setHeight(double height) {
this.height = height;
}
public double getHeight() {
return height;
}
public static void volume(Box[] boxes) {
int vol = 1;
for (int i = 0; i < boxes.length - 1; i++) {
//vol = vol * boxes[i];
}
System.out.println(vol);
}
}
The operator * is undefined for the argument type(s) int, Box
An instance method boxVolume() should be implemented to calculate the volume of specific box, and then this method needs to be called inside static method volume:
// class Box
public double boxVolume() {
return this.length * this.width * this.height;
}
public static double volume(Box[] boxes) {
double total = 0.0;
// use for each loop to iterate boxes array
for (Box box : boxes) {
if (null != box) {
total += box.boxVolume();
}
}
return total;
}

Don't want to use the initialize the given height/weight on the draw() method, Instead I want to use the main driver

So, I been trying to figured out how can I able draw the shape of a rectangle by given the height and the width on the main driver, but I was having trouble because somehow I couldn't able to draw the rectangle, instead I was able to put the values inside of the draw() methods.
For the draw() method was supposed to do this description: The draw
method must “draw” the rectangle using System.out.println(). Simply
print width number of asterisks in height number of lines.
The interface Shape represents a closed geometrical shape. It has
three methods.
1. draw(): This has no parameters and returns void. The method draws the shape on
the screen.
2. getArea(): The method has no parameters and returns the area of the shape. The
return type is double.
3. getPerimeter(): The method has no parameters and returns the perimeter of the
shape. The return type is double.
public class Rectangle implements Shape {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
#Override
public void draw() {
/**
* Don't want to use the initialize given height/weight
* Instead use the main driver
*
*/
double height = 3,width = 3;
for(int i=0; i<height;i++) {
for(int w =0; w<width; w++) {
System.out.print("*");
}
System.out.println(" ");
}
}
#Override
public double getArea() {
return width*height;
}
#Override
public double getPerimeter() {
return 2*(height+width);
}
public boolean equals(Object obj)
{
// check that the type of the parameter is Rectangle
if( !(obj instanceof Rectangle) )
return false;
// we already know that obj is of type Rectangle, so it's safe to cast
Rectangle rectangleObject = (Rectangle) obj;
// return true or false depending on whether length and width have the same value
return height == rectangleObject.height && width == rectangleObject.width;
}
public String toString() {
String info = "Area: " + getArea() + "\n" + "Perimeter:" + getPerimeter();
return info;
}
public static void main(String[] args) {
Shape rectangle1 = new Rectangle(5,5);
rectangle1.draw();
}
}
The output should look like:
Shape rectangle1 = new Rectangle(4, 5);
rectangle1.draw();
****
****
****
****
****
Use the height and width member variables that are part of the Rectangle object instance. In your example, the width would be 4 and the height would be 5 based one the object instantiation via:
Shape rectangle1 = new Rectangle(4, 5);
Your draw method would look like:
#Override
public void draw() {
for(int i=0; i<this.height;i++) {
for(int w =0; w<this.width; w++) {
System.out.print("*");
}
System.out.println(" ");
}
}

setheight() not changing the variable

I'm going over Java tutorials and I got stuck.
when I execute wall.setHeight(-1.5)
its returning me 4 instead of 0 for the height.
can anyone tell me what is wrong with my syntax?
public class Wall {
private double width;
private double height;
public Wall(){
System.out.println("Empty");
}
public Wall(double width, double height) {
this.width = width;
this.height = height;
System.out.println("width=" + this.width + "height=" + this.height);
}
public void setWidth(double width) {
if((this.width)<0){
this.width = 0.00;
}
}
public void setHeight(double height) {
System.out.println("got here ");
if((this.height)<0){
this.height = 0.00;
}
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
public double getArea(){
return (this.width) * (this.height);
}
}
public class main {
public static void main(String[] args) {
Wall wall = new Wall(5,4);
System.out.println("area= " + wall.getArea());
wall.setHeight(-1.5);
System.out.println(wall.getHeight());
System.out.println("width= " + wall.getWidth());
System.out.println("height= " + wall.getHeight());
System.out.println("area= " + wall.getArea());**
}
}
To fix your setHeight() method with input of "-1.5", do this:
public void setHeight(double height) {
System.out.println("got here ");
if (height < 0) { // use "height" instead of "this.height"
this.height = 0.00;
}
}
To fix it so that it also saves other, zero-or-greater values, do this;
public void setHeight(double height) {
System.out.println("got here ");
if (height < 0) {
this.height = 0.00;
} else {
this.height = height; // set a new value for "this.height" if you didn't zero it out above
}
}
The confusion you ran into is when to use height as opposed to this.height. One of them represents the parameter value being passed into the method (height), and the other represents the value already stored on the object (this.height).
Also, if you use an IDE (such as IntelliJ), you would see the "height" variable name grayed out along with hover text "Parameter 'height' is never used" which would give you a clue that you have a programming error in your code.
This is why I always suggest passing in a variable with a different name than the variable you are setting. You are checking if this.height < 0... naturally it will be whatever you set it to in the constructor.
this.height will refer to the variable height of your Wall object, not the height that is passed in to the setter.
Try this:
public void setHeight(double h) {
if(h < 0) {
this.height = 0.00;
}
}
public void setHeight(double height) {
System.out.println("got here ");
if((this.height)<0){
this.height = 0.00;
}
else{
this.height = height;
}
}

What is missing from my code in order for the volume to be read?

Everything compiles and works. The program is not reading the Volume value and comes out as 0.0
===========================================================================
Write a superclass encapsulating a rectangle. A rectangle has two attributes representing the width and the height of the rectangle. It has methods returning the perimeter and the area of the rectangle. This class has a subclass, encapsulating a parallelepiped, or box. A parallelepiped has a rectangle as its base, and another attribute, its length; it has two methods that calculate and return its area and volume. You also need to include a client class (with the main method) to test these two classes.
public class Rectangle
{
protected double width;
protected double height;
public Rectangle(double width, double height)
{
this.width = width;
this.height = height;
}
public double getWidth()
{
return width;
}
public void setWidth(double width)
{
this.width = width;
}
public double getHeight()
{
return height;
}
public void setHeight(double height)
{
this.height = height;
}
public double getArea()
{
return width * height;
}
public double getPerimeter()
{
return 2 * (width + height);
}
}
public class Box extends Rectangle
{
protected double length;
public Box(double length)
{
super(length, length);
}
public double getLength()
{
return length;
}
public void setLength(double length)
{
this.length = length;
}
public double getVolume()
{
return width * height * length;
}
}
public class TestRectangle
{
public static void main(String[] args)
{
Rectangle rectangle = new Rectangle(2,4);
Box box = new Box(5);
System.out.println("Rectangle:" );
System.out.println("The area is " + rectangle.getArea());
System.out.println("The perimeter is " +rectangle.getPerimeter());
System.out.println("The volume is " + box.getVolume());
}
}
No errors. Missing value for volume.
Your Box simply needs to set the length field.
public Box(double length)
{
super(length, length);
this.length = length;
}
Although, this assumes your Box is always going to be a cube, and not some arbitrary box shape.
The problem here is you are never assigning any value to the length variable of your box class. The current output of your code in your example would be the following for getVolume(): 5 * 5 * 0 which always returns 0.
Assuming your width, length, height are the same, you still need to assign the length to the variable:
public Box(double length)
{
super(length, length);
this.length = length;
}

How to deal with an empty argument in Java?

I have a simple program which read the argument parameters and output them as a result.
for example, if i use:
Rectangle r1 = new Rectangle(1.0, 2.0, "RED", false);
System.out.println(r1);
should return:
1.0 x 2.0, color: RED
But what if I entered the following argument:
Rectangle r8 = new Rectangle();
System.out.println(r8);
How can my program output the following default result when the above argument is used ?
1.0 x 1.0, filled with RED
Here's a part of my code:
public class Rectangle extends Shape {
protected double width;
protected double length;
public Rectangle() {
super();
}
public Rectangle(double width, double length) {
super();
this.width = width;
this.length = length;
}
public Rectangle(double width, double length, String color, boolean filled) {
super(color, filled);
this.width = width;
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getArea() {
return (width * length);
}
public double getPerimeter() {
return (2 * (width + length));
}
public String toString() {
if (super.isFilled()) {
return width + " x " + length + ", " + "filled with RED";
} else {
return width + " x " + length + ", " + "color: RED";
}
}
}
I tried to use if (width = 0){} for width but since it is a primitive type so it doesn't work...can anyone tell me how can I print out the default value if a empty argument is used ? help will be appreciated...Thank you!
You can set the default values in the parameterless constructor :
public Rectangle() {
super("RED", true);
this.width = 1.0;
this.length = 1.0;
}
Or you can assign default values in the declaration of the members :
protected double width = 1.0;
protected double length = 1.0;
and have a similar declaration with default value for the Shape members.
Or you can call a different constructor from the parameterless constructor, as suggested by Seelenvirtuose :
public Rectangle() {
this(1.0, 1.0, "RED", true);
}
You should always set a value in your constructor for the object properties. In your case you can define your default constructor to recall the one with parameters and passing it the default values:
public Rectangle() {
this(1.0, 1.0, "RED", true);
}
You may define defaults in your constructor
public class Rectangle extends Shape {
private final double DEFAULT_WIDTH=1.0;
private final double DEFAULT_LENGTH=1.0;
private final String DEFAULT_COLOUR="RED";
private final boolean DEFAULT_FILLED=true;
protected double width;
protected double length;
public Rectangle() {
this(DEFAULT_WIDTH, DEFAULT_LENGTH, DEFAULT_COLOUR, DEFAULT_FILLED);
}
public Rectangle(double width, double length) {
super();
this.width = width;
this.length = length;
}
public Rectangle(double width, double length, String color, boolean filled) {
super(color, filled);
this.width = width;
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getArea() {
return (width * length);
}
public double getPerimeter() {
return (2 * (width + length));
}
public String toString() {
if (super.isFilled()) {
return width + " x " + length + ", " + "filled with RED";
} else {
return width + " x " + length + ", " + "color: RED";
}
}
}
I don't fully understand the problem , but this might help , if you want something else just say
public Rectangle() {
this(1.0,1.0,"RED",true);
}
public Rectangle(double width, double length) {
this.width = width;
this.length = length;
}

Categories