#does this code look right

1 messages · Page 1 of 1 (latest)

shrewd sable
#

Write a class called BlanketLadder that describes a blanket ladder by its height, width, number of rungs and colour. Write setters and getters for all instance data. Write a method that returns the space between rungs, assuming there is no rung at the bottom of the blanket ladder, a rung at the top, and that the rungs are equispaced. If the height of the blanket ladder is 21cm and there are 3 rungs, they should be 7cm apart. This method should return a double and you should print only 2 decimal spaces for each returned value (e.g., a 200cm blanket ladder with 3 rungs would have 66.67cm between rungs, not 66.666666666). When a blanket ladder is printed, it should be described as in the sample output below.

Write a class called Assignment4Smajli162513 that creates two blanket ladders. Give them any dimensions, number of rungs and colours you'd like. Print both blanket ladders. Then print the distance between the rungs for both blanket ladders, referencing the colour of each blanket ladder using a getter (they should have different colours). Change one of the blanket ladder's colour to pink and the other's width to 68. Print both blanket ladders.

the image attached is what it should look like

Blanketladder

import java.text.DecimalFormat;

public class BlanketLadder {
private double height;
private double width;
private int numRungs;
private String color;

public BlanketLadder(double height, double width, int numRungs, String color) {
    this.height = height;
    this.width = width;
    this.numRungs = numRungs;
    this.color = color;
}

public double getHeight() {
    return height;
}

public void setHeight(double height) {
    this.height = height;
}

public double getWidth() {
    return width;
}

public void setWidth(double width) {
    this.width = width;
}

public int getNumRungs() {
    return numRungs;
}

public void setNumRungs(int numRungs) {
    this.numRungs = numRungs;
}

public String getColor() {
    return color;
}

public void setColor(String color) {
    this.color = color;
}

public double spaceBetweenRungs() {
    double space = (height - 1) / (numRungs - 1);
    DecimalFormat df = new DecimalFormat("#.##");
    return Double.parseDouble(df.format(space));
}

@Override
public String toString() {
    return "Blanket Ladder: Height = " + height + "cm, Width = " + width + "cm, Rungs = " + numRungs + ", Color = " + color;
}

}

public class Assignment3Smajli162513 {
public static void main(String[] args) {
BlanketLadder blanketLadder1 = new BlanketLadder(21, 60, 3, "Blue");
BlanketLadder blanketLadder2 = new BlanketLadder(100, 50, 4, "Green");

    System.out.println(blanketLadder1);
    System.out.println(blanketLadder2);
    
    System.out.println("The space between rungs for the " + blanketLadder1.getColor() + " blanket ladder is " + blanketLadder1.spaceBetweenRungs() + "cm");
    System.out.println("The space between rungs for the " + blanketLadder2.getColor() + " blanket ladder is " + blanketLadder2.spaceBetweenRungs() + "cm");
    
    blanketLadder1.setColor("Pink");
    blanketLadder2.setWidth(68);
    
    System.out.println(blanketLadder1);
    System.out.println(blanketLadder2);
}

}

scarlet smeltBOT
#

This post has been reserved for your question.

Hey @shrewd sable! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

jovial mason
#
    public double spaceBetweenRungs() {
        double space = (height - 1) / (numRungs - 1);
        DecimalFormat df = new DecimalFormat("#.##");
        return Double.parseDouble(df.format(space));
    }
```this does not. don't use rounding to say "2 digits", have the full precision double on hand and use `printf` or `format` with `%.2f`
#

printf would also be useful to insert the color in the string, and format for the toString as well

#

other than that lgtm

jovial mason
solid anchor
#

use float

scarlet smeltBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

jovial mason
solid anchor
#

The arithmetic is bullshit

jovial mason
#

wtf are you takling about

jovial mason
#

and even if it was bullshit, why would float do any better than double?

solid anchor
#

using double arithmetic

#

what's 0.1 + 0.2

jovial mason
#

they both are subject to floating-point arithmatic

solid anchor
#

i meant to say

jovial mason
#

double is just a double-precision floating-point

solid anchor
#

double-precision floating point arithmetic is bullshit

jovial mason
#

floating-point arithmatic is bullshit in general

#

double is not special

solid anchor
#
Sum of 0.1D + 0.2D = 0.30000000000000004
Sum of 0.1F + 0.2F = 0.3
jovial mason
#

yeah yeah sure whatever

#

you don't actually understand it do you

solid anchor
#

probably not

jovial mason
#

float is subject to all the same inaccuracies double is

solid anchor
#

but one thing i'd like is intuitive arithmetic

solid anchor
jovial mason
#

it just has less examples because double has greater precision, float just does rounding

jovial mason
jovial mason
#

floating-point is not made for intuitive arithmatic

solid anchor
#

wait, what's fixed point

#

i've never heard of that

jovial mason
#

uh, as the name says?

solid anchor
#

let me google it

#

ah

#

seen

jovial mason
#

a * 10^x
floating point, x is variable (floating)
fixed point, x is constant (fixed)

solid anchor
#

kk

jovial mason
#

anyways to OP: the 2 points i made originally still stand