Exercise 9.38 in \"Objects First Blue J\" . It asks Open up the \"bricks\" proje
ID: 3774020 • Letter: E
Question
Exercise 9.38 in "Objects First Blue J" . It asks Open up the "bricks" project. Test it. There are at least four errors in this project. See if you can find them and fix them. What techniques did you use to find the errors? Which technique was most useful? The book is Objects first with Java,, authors Barnes and Kolling, 6th edition.
That is the complete question, and this is the link to the list of exercises here on Chegg:
https://www.chegg.com/homework-help/Objects-First-with-Java-6th-edition-chapter-9-problem-38E-solution-9780134477367
This is a link to a pdf copy of the book. ftp://ftp.shahroodut.ac.ir/Schools/Computer/admohammadi/Advanced%20Programming%20in%20Java/Objects%20First%20with%20Java%20&%20BlueJ/Objects%20First%20With%20Java,%20A%20Practical%20Introduction%20Using%20BlueJ.pdf
Just two classes-Pallet and Brick
Pallet:
/**
* A pallet is a stack of bricks on a wooden base.
*
* @author: Michael Kölling and David J. Barnes
* @version 2016.02.29
*/
public class Pallet
{
private static final double BASE_WEIGHT = 6.5; // in kg
private static final double BASE_HEIGHT = 15; // in cm
private Brick aBrick;
private int bricksInPlane;
private int height;
/**
* Create a pallet with a given number of bricks.
* @param bricksInPlane The number of bricks in each level on the base.
* @param height The number of bricks stacked on top of each other.
*/
public Pallet(int bricksInPlane, int height)
{
this.bricksInPlane = bricksInPlane;
this.height = height;
aBrick = new Brick(8, 20, 12);
}
/**
* @return The weight of the pallet (in kg)
*/
public double getWeight()
{
int numberOfBricks = bricksInPlane * height;
return aBrick.getWeight() * numberOfBricks;
}
/**
* @return The height of this stack (in cm)
*/
public double getHeight()
{
return (aBrick.getHeight() % height) + BASE_HEIGHT;
}
}
this.depth = depth;
}
/**
* @return The surface area of this brick in square centimeters.
*/
public double getSurfaceArea()
{
double side1 = width * height;
double side2 = width * depth;
double side3 = depth * height;
double total = (side1 + side1 + side3) * 2;
return total;
}
/**
* @return The weight of this brick in kg.
*/
public double getWeight()
{
return (getVolume() * WEIGHT_PER_CM3) / 1000;
}
/**
* @return The volume of this brick in cubic centimeters.
*/
public int getVolume()
{
return width * height * depth;
}
/**
* @return The height of this brick in centimeters.
*/
public double getHeight()
{
return height;
}
}
Explanation / Answer
ANS:
/**
* A pallet is a stack of bricks on a wooden base.
*
* @author: Michael Kölling and David J. Barnes
* @version 2016.02.29
*/
public class Pallet
{
private static final double BASE_WEIGHT = 6.5; // in kg
private static final double BASE_HEIGHT = 15; // in cm
private Brick aBrick;
private int bricksInPlane;
private int height;
/**
* Create a pallet with a given number of bricks.
* @param bricksInPlane The number of bricks in each level on the base.
* @param height The number of bricks stacked on top of each other.
*/
public Pallet(int bricksInPlane, int height)
{
this.bricksInPlane = bricksInPlane;
this.height = height;
aBrick = new Brick(8, 20, 12);
}
/**
* @return The weight of the pallet (in kg)
*/
public double getWeight()
{
int numberOfBricks = bricksInPlane * height;
return aBrick.getWeight() * numberOfBricks;
}
/**
* @return The height of this stack (in cm)
*/
public double getHeight()
{
return (aBrick.getHeight() % height) + BASE_HEIGHT;
}
//main() To Test Program
public static void main(String args[])
{
Pallet p=new Pallet(10,10);
System.out.println(p.getWeight());
System.out.println(p.getHeight());
}
}
class Brick
{
//variables to store depth,width and height of Brick
private int depth;
private int width;
private int height;
private static final double WEIGHT_PER_CM3 = 2;
//Constructor
Brick(int depth,int width,int height){
this.depth=depth;
this.width=width;
this.height=height;
}
/**
* @return The surface area of this brick in square centimeters.
*/
public double getSurfaceArea()
{
double side1 = width * height;
double side2 = width * depth;
double side3 = depth * height;
double total = (side1 + side1 + side3) * 2;
return total;
}
/**
* @return The weight of this brick in kg.
*/
public double getWeight()
{
return (getVolume() * WEIGHT_PER_CM3) / 1000;
}
/**
* @return The volume of this brick in cubic centimeters.
*/
public int getVolume()
{
return width * height * depth;
}
/**
* @return The height of this brick in centimeters.
*/
public double getHeight()
{
return height;
}
}
KeyPoints:
1) created new class Brick in line number 50.
2) created 3 instance variables in Brick class (line numbers 53,54,55)
3) created a static variable WEIGHT_PER_CM3 in Brick class
4) To test program i have included main() in Pallet class.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.