In: Computer Science
I need to make JAVA program that calculates the area of a brick of a specific color.
I have class Brick with the following UML:
String color
int number
int length
int width
I made methods: getColor(), getNumber(), getLength(), getWidth() in
the class Brick
So it starts like this:
#####
public Brick(String color, int number, int length, int
width)
{
this.color = color;
this.number = number;
this.length = length;
this.width = width;
}
public String toString()
{
return number + " " + color + " (" + length + " mm " +
"x " + width + " mm)";
}
####
Then I have class LegoBox with StringName and ArrayList<Brick>bricks;
It looks like this:
##########
public class LegoBox
{
private String name;
private ArrayList<Brick>bricks;
public LegoBox(String name){
this.name = name;
bricks = new ArrayList<>();
}
public void add(Brick b){
bricks.add(b);
}
public int area(String color){
int res = 0;
for(Brick b : bricks){
if(b.getColor().equals(color)){
res= b.getLength()*b.getWidth()*b.getNumber();
}
}
return res;
}
Then I have the third class Driver and in the class Driver:
I made 5 objects:
Brick b1 = new Brick("green",25, 30,10);
Brick b2 = new Brick("orange",20, 35,10);
Brick b3 = new Brick("red",25, 30,15);
Brick b4 = new Brick("green",27, 36,20);
Brick b5 = new Brick("black",28, 37,12);
and print toString:
System.out.println("Area of green bricks is " + lb.area("green") + " mm2");
My problem is: I get wrong answers:
Area of green bricks is 19440 mm2
Area of green bricks is 12432 mm2
But it is not right because 12432 is the area of black bricks!
Could you help me to find my mistake in coding?
The method must return the area (measured in square millimeters) that one can cover with Lego bricks of the specified color. Note that the same color may well occur in several different types of pieces.
import java.util.*;
class Brick
{
private String color;
private int number;
private int length;
private int width;
public Brick(String color, int number, int length, int
width)
{
this.color = color;
this.number = number;
this.length = length;
this.width = width;
}
public String getColor()
{
return color;
}
public int getLength()
{
return length;
}
public int getWidth()
{
return width;
}
public int getNumber()
{
return number;
}
public String toString()
{
return number + " " + color + " (" + length + " mm " + "x " + width
+ " mm)";
}
}
class LegoBox
{
private String name;
private ArrayList<Brick> bricks;
public LegoBox(String name){
this.name = name;
bricks = new ArrayList<>();
}
public void add(Brick b){
bricks.add(b);
}
public int area(String color){
int res = 0;
for(Brick b : bricks){
if(b.getColor().equals(color)){
res += b.getLength()*b.getWidth()*b.getNumber();
// += operator calculates the area of all green bricks
}
}
return res;
}
}
class Test
{
public static void main (String[] args)
{
LegoBox lb = new
LegoBox("Legobox1");
ArrayList<Brick>bricks = new
ArrayList<Brick>();
Brick b1 = new Brick("green",25,
30,10);
Brick b2 = new Brick("orange",20,
35,10);
Brick b3 = new Brick("red",25,
30,15);
Brick b4 = new Brick("green",27,
36,20);
Brick b5 = new Brick("black",28,
37,12);
lb.add(b1);
lb.add(b2);
lb.add(b3);
lb.add(b4);
lb.add(b5);
System.out.println("Area of green bricks is " + lb.area("green") + " mm2");
}
}
Output:
Area of green bricks is 26940 mm2
Do ask if any doubt.