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...
I need to make this into a Java Code: Write a program that prompts the user...
I need to make this into a Java Code: Write a program that prompts the user for a double value representing a radius. You will use the radius to calculate: circleCircumference = 2πr circleArea = πr2 sphereArea = 4πr2 sphereVolume = 43πr3 You must use π as defined in the java.lang.Math class. Your prompt to the user to enter the number of days must be: Enter radius: Your output must be of the format: Circle Circumference = circleCircumference Circle Area...
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...
I need to make an application that calculates the occupancy rate for a hotel with eight...
I need to make an application that calculates the occupancy rate for a hotel with eight floors. The user will be asked how many rooms are occupied on each floor. The program will iterate for each floor. Users must enter numbers and the rooms occupied on each floor cannot exceed 30. The occupancy rate is the number of occupied rooms/ unnoccupied.  The application also needs to display the occupancy for each individual floor aswell. Each floor has a maximum of 30...
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){...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program read an "input.txt" file (like below) and store the information in an respective arrays. This input.txt file will look like below and will be stored in the same directory as the java file. The top line of the text represents the number of people in the group for example. the lines below it each represent the respective persons preferences in regards to the other...
**I need to make this program to make sure that the numbers are unique. In another...
**I need to make this program to make sure that the numbers are unique. In another word, if the number has been generated and appear early, it should continue to find the next number until the number is unique. Hint: use the array to check whether the number has been generated. #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { srand(time(NULL)); int n, low, high; cout << "Enter how many random numbers you would like to see:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT