first commit
This commit is contained in:
17
Semester 1/Programming 1/Java/examples/projects/chapter09/bricks/Brick.ctxt
Executable file
17
Semester 1/Programming 1/Java/examples/projects/chapter09/bricks/Brick.ctxt
Executable file
@@ -0,0 +1,17 @@
|
||||
#BlueJ class context
|
||||
comment0.params=height\ width\ depth
|
||||
comment0.target=Brick(int,\ int,\ int)
|
||||
comment0.text=\n\ Create\ a\ Brick\ given\ edge\ lengths\ in\ centimeters.\n\ @param\ height\ The\ brick's\ height.\n\ @param\ width\ The\ brick's\ width.\n\ @param\ depth\ The\ brick's\ depth.\n
|
||||
comment1.params=
|
||||
comment1.target=double\ getSurfaceArea()
|
||||
comment1.text=\n\ @return\ The\ surface\ area\ of\ this\ brick\ in\ square\ centimeters.\n
|
||||
comment2.params=
|
||||
comment2.target=double\ getWeight()
|
||||
comment2.text=\n\ @return\ The\ weight\ of\ this\ brick\ in\ kg.\n
|
||||
comment3.params=
|
||||
comment3.target=int\ getVolume()
|
||||
comment3.text=\n\ @return\ The\ volume\ of\ this\ brick\ in\ cubic\ centimeters.\n
|
||||
comment4.params=
|
||||
comment4.target=double\ getHeight()
|
||||
comment4.text=\n\ @return\ The\ height\ of\ this\ brick\ in\ centimeters.\n
|
||||
numComments=5
|
65
Semester 1/Programming 1/Java/examples/projects/chapter09/bricks/Brick.java
Executable file
65
Semester 1/Programming 1/Java/examples/projects/chapter09/bricks/Brick.java
Executable file
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Brick models a simple brick.
|
||||
*
|
||||
* @author: Michael Kölling and David J. Barnes
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Brick
|
||||
{
|
||||
// Constant.
|
||||
private static final int WEIGHT_PER_CM3 = 2; // weight per cubic cm in grams
|
||||
|
||||
private int height;
|
||||
private int width;
|
||||
private int depth;
|
||||
|
||||
/**
|
||||
* Create a Brick given edge lengths in centimeters.
|
||||
* @param height The brick's height.
|
||||
* @param width The brick's width.
|
||||
* @param depth The brick's depth.
|
||||
*/
|
||||
public Brick(int height, int width, int depth)
|
||||
{
|
||||
this.height = height;
|
||||
this.width = width;
|
||||
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;
|
||||
}
|
||||
}
|
11
Semester 1/Programming 1/Java/examples/projects/chapter09/bricks/Pallet.ctxt
Executable file
11
Semester 1/Programming 1/Java/examples/projects/chapter09/bricks/Pallet.ctxt
Executable file
@@ -0,0 +1,11 @@
|
||||
#BlueJ class context
|
||||
comment0.params=bricksInPlane\ height
|
||||
comment0.target=Pallet(int,\ int)
|
||||
comment0.text=\n\ Create\ a\ pallet\ with\ a\ given\ number\ of\ bricks.\n\ @param\ bricksInPlane\ The\ number\ of\ bricks\ in\ each\ level\ on\ the\ base.\n\ @param\ height\ The\ number\ of\ bricks\ stacked\ on\ top\ of\ each\ other.\n
|
||||
comment1.params=
|
||||
comment1.target=double\ getWeight()
|
||||
comment1.text=\n\ @return\ The\ weight\ of\ the\ pallet\ (in\ kg)\n
|
||||
comment2.params=
|
||||
comment2.target=double\ getHeight()
|
||||
comment2.text=\n\ @return\ The\ height\ of\ this\ stack\ (in\ cm)\n
|
||||
numComments=3
|
45
Semester 1/Programming 1/Java/examples/projects/chapter09/bricks/Pallet.java
Executable file
45
Semester 1/Programming 1/Java/examples/projects/chapter09/bricks/Pallet.java
Executable file
@@ -0,0 +1,45 @@
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
19
Semester 1/Programming 1/Java/examples/projects/chapter09/bricks/README.TXT
Executable file
19
Semester 1/Programming 1/Java/examples/projects/chapter09/bricks/README.TXT
Executable file
@@ -0,0 +1,19 @@
|
||||
Project: bricks
|
||||
Authors: Michael Kölling and David J. Barnes
|
||||
|
||||
This project is part of the material for the book
|
||||
|
||||
Objects First with Java - A Practical Introduction using BlueJ
|
||||
Sixth edition
|
||||
David J. Barnes and Michael Kölling
|
||||
Pearson Education, 2016
|
||||
|
||||
Here is a part of an application for a company producing bricks.
|
||||
Bricks are delivered in pallets (stacks of bricks).
|
||||
|
||||
The Pallet class is supposed to provide methods telling its height
|
||||
and weight.
|
||||
|
||||
There are (at least) four errors in this project. Find them. Fix them.
|
||||
Then discuss how you went about finding the errors. What strategies
|
||||
did you employ?
|
@@ -0,0 +1,45 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=Pallet
|
||||
dependency1.to=Brick
|
||||
dependency1.type=UsesDependency
|
||||
objectbench.height=76
|
||||
objectbench.width=760
|
||||
package.editor.height=435
|
||||
package.editor.width=652
|
||||
package.editor.x=70
|
||||
package.editor.y=80
|
||||
package.numDependencies=1
|
||||
package.numTargets=2
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
readme.editor.height=570
|
||||
readme.editor.width=846
|
||||
readme.editor.x=34
|
||||
readme.editor.y=23
|
||||
target1.editor.height=708
|
||||
target1.editor.width=893
|
||||
target1.editor.x=50
|
||||
target1.editor.y=60
|
||||
target1.height=60
|
||||
target1.name=Brick
|
||||
target1.naviview.expanded=true
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.typeParameters=
|
||||
target1.width=120
|
||||
target1.x=290
|
||||
target1.y=200
|
||||
target2.editor.height=705
|
||||
target2.editor.width=835
|
||||
target2.editor.x=50
|
||||
target2.editor.y=60
|
||||
target2.height=60
|
||||
target2.name=Pallet
|
||||
target2.naviview.expanded=true
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.typeParameters=
|
||||
target2.width=120
|
||||
target2.x=140
|
||||
target2.y=100
|
Reference in New Issue
Block a user