How to deal with an empty argument in Java? - 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;
}

Related

Beginner question about different data types in constructors

I'm new to coding and I'm currently working with OOP in Java. My instructor had us create a rectangle class instead of just importing the built-in. Anyway, we were to create constructors featuring ints and doubles as parameters. My questions is, with having two constructors if different data types, do I need separate getters / setters for each type? Would this be a time I could use generic datatypes? I have included what I created below. Thanks.
public class Rectangle {
private int height;
private int width;
private double doubleHeight;
private double doubleWidth;
public Rectangle() {
height = 0;
width = 0;
}
public Rectangle(int h, int w) {
height = h;
width = w;
System.out.println("A Rectangle has been created using integer dimensions.");
}
public Rectangle(double h, double w) {
doubleHeight = h;
doubleWidth= w;
System.out.println("A Rectangle has been created using double dimensions.");
}
public int getIntHeight() {
return height;
}
public double getDoubleHeight() {
return doubleHeight;
}
public int setIntHeight(int h) {
height = h;
}
public double setDoubleHeight(double h) {
doubleHeight = h;
}
public int getIntArea() {
return width * height;
}
public double getDoubleArea() {
return doubleWidth * doubleHeight;
}
public int getIntPerimeter() {
return width*2 + height*2;
}
public double getDoublePerimeter() {
return doubleWidth*2 + doubleHeight*2;
}
public String toString() {
if (this.getIntHeight() >= 1) {
return "Height: " + height + ", Width: " + width;
}
else {
return "Height: " + doubleHeight + ", Width: " + doubleWidth;
}
}
}

Two constructors?

I already did my assignment with setters and getters (I did this with OOD) however I still don't understand what's the purpose of the two Rectangle methods and if ever I remove the empty Rectangle an error will prompt:
P.S. This is not the full code.
// private double length = 25.0;
private double width = 15.5;
public Rectangle(){
}
public Rectangle(double length, double width){
this.length = length;
this.width = width;
}
public void setDimension(double length,double width){
this.length = length;
this.width=width;
}
public double getLength(){
return length;
}
public double getWidth(){
return width;
}
public double area(){
return length * width;
}
public double perimeter(){
return 2 * (length + width);
}
public static void print(){
Rectangle rt = new Rectangle();
Box box = new Box();
System.out.println("The rectangle has a length of " + rt.getLength() + " and a width of " + rt.getWidth() );
System.out.println("The rectangle has an area of "+ rt.area());
System.out.println("The rectangle has a perimeter of "+ rt.perimeter());
box.print();
}
That's the default (no-arg) constructor. Since you have another constructor, Java will not implicitly create it if you don't define it explicitly. Since the first line in your print method calls it, you'll get an error if you remove it.

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

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

Categories