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
I need it in java. Write a program that will print if n numbers that the...
I need it in java. Write a program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times...
Write a program that calculates the area and circumference of a circle. It should ask the...
Write a program that calculates the area and circumference of a circle. It should ask the user first to enter the number of circles n, then reads the radius of each circle and stores it in an array. The program then finds the area and the circumference, as in the following equations, and prints them in a tabular format as shown in the sample output. Area = πr2 , Circumference = 2πr, π = 3.14159 Sample Output: Enter the number...
I only need to update this JAVA program to be able to : 1 ) Exit...
I only need to update this JAVA program to be able to : 1 ) Exit cleanly after creating the chart 2 ) change the pie chart to a perfect circle rather than an oval 3 ) provide a separate class driver import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.util.Scanner; import javax.swing.JComponent; import javax.swing.JFrame; // class to store the value and color mapping of the // pie segment(slices) class Segment { double value; Color color; // constructor public...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT