Question

In: Computer Science

I need to make JAVA program that calculates the area of a brick of a specific...

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.

Solutions

Expert Solution

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.


Related Solutions

In java. Prefer Bluej Create a program in java that calculates area and perimeter of a...
In java. Prefer Bluej Create a program in java that calculates area and perimeter of a square - use a class and test program to calculate the area and perimeter; assume length of square is 7 ft.
Create a program in java that calculates area and perimeter of a square - use a...
Create a program in java that calculates area and perimeter of a square - use a class and test program to calculate the area and perimeter; assume length of square is 7 ft.
Make a java program of Mickey I have the starter program but I need to add...
Make a java program of Mickey I have the starter program but I need to add eyes and a smile to it. import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import javax.swing.JFrame; public class Mickey extends Canvas { public static void main(String[] args) { JFrame frame = new JFrame("Mickey Mouse"); Canvas canvas = new Mickey(); canvas.setSize(400, 400); canvas.setBackground(Color.white); frame.add(canvas); frame.pack(); frame.setVisible(true); } public void paint(Graphics g) { Rectangle bb = new Rectangle(100, 100, 200, 200); mickey(g, bb); } public void...
Using java, I need to make a program that reverses a file. The program will read...
Using java, I need to make a program that reverses a file. The program will read the text file character by character, push the characters into a stack, then pop the characters into a new text file (with each character in revers order) EX: Hello World                       eyB       Bye            becomes    dlroW olleH I have the Stack and the Linked List part of this taken care of, I'm just having trouble connecting the stack and the text file together and...
So I need to make a java program that reverse or replace first or lastchar or...
So I need to make a java program that reverse or replace first or lastchar or remove char from user's string input. length, concat, charAt, substring, and equals (or equalsIgnoreCase) thses are the string method that only I can use for example if user put asdf asdf and chose reverse input should be fdsa fdsa if user put asdf asdf and chose replace first a with b input should be bsdf asdf if user put asdf asdf and chose remove...
JAVA I need to write a code that calculates mean and standard deviation using arrays and...
JAVA I need to write a code that calculates mean and standard deviation using arrays and OOP. This is what I have so far. I'm close but my deviation calculator method is throwing errors. Thanks so much :) import java.util.Scanner; import java.util.Arrays; public class STDMeanArray { private double[] tenUserNums = new double[10];//Initialize an array with ten index' private double mean; private double deviation; Scanner input = new Scanner(System.in);//create new scanner object /*//constructor public STDMeanArray(double tenUserNum [], double mean, double deviation){...
i need a pseudocode for a program in java that will convert dollars into euros and...
i need a pseudocode for a program in java that will convert dollars into euros and japanese yen using the print and prinln methods an if, if -else, statement a switch statement a while statement utilizes the file class uses the random class and random number generator and uses at least three methods other than main
Write a Java program that calculates a random number 1 through 100. The program then asks...
Write a Java program that calculates a random number 1 through 100. The program then asks the user to guess the number.If the user guesses too high or too low then the program should output "too high" or "too low" accordingly.The program must let the user continue to guess until the user correctly guesses the number. ★Modify the program to output how many guesses it took the user to correctly guess the right number
My Java program keeps "running." I know I need to close a "loop" but I can't...
My Java program keeps "running." I know I need to close a "loop" but I can't find it. I'm learning how to code. This is confusing for me. import java.util.Scanner; import java.util.ArrayList; public class SteppingStone4_Loops {    public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String recipeName = ""; ArrayList<String> ingredientList = new ArrayList(); String newIngredient = ""; boolean addMoreIngredients = true; System.out.println("Please enter the recipe name: "); recipeName = scnr.nextLine();    do {    System.out.println("Would you...
IN JAVA Write a program that calculates the occupancy rate for each floor of a hotel....
IN JAVA Write a program that calculates the occupancy rate for each floor of a hotel. (Use a sentinel value and please point out the sentinel in bold.) The program should start by asking for the number of floors in the hotel. A loop should then iterate once for each floor. During each iteration, the loop should ask the user for the number of rooms on the floor and the number of them that are occupied. After all the iterations,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT