In: Computer Science
The Hole Class Description:
For the Hole class, there are three instance variables: the par for the hole, the length of the hole in yards, and a Boolean valued indicator of whether the hole is one in which the players’ full drives will be measured for distance. John clarifies: The length of the hole in yards determines par for the hole. The current standard is:
No more than 250 yards |
Par 3 |
251 to 470 yards |
Par 4 |
471 to 690 yards |
Par 5 |
Greater than 690 yards |
Par 6 |
The distance of drives will only be counted as “full” on holes at least 400 yards long. Both the Hole constructor and a setLength method use the length of the hole to calculate the par for the hole and determine whether full drives on the hole will be measured. There are “getters” for the length and the par of the hole and an isFullDriveHole method that returns true if and only if the hole is one where drives are measured.
The Hole Class:
public class Hole
{
// instance variables
private int par;
private int lengthInYds;
private boolean fullDriveHole;
/**
* Constructor for objects of class Hole.
*
* @param length of hole in yards
*
**/
public Hole(int length)
{
this.setLength(length);
}
/**
* Set the length of the hole and compute its par.
*
* @param length the length in of the hole in yards.
*/
public void setLength(int length)
{
// constants for determining par
final int par3Limit = 250;
// GUIDE: add constants for limits
for par 4 and par 5
// GUIDE: set the length of the hole, initialize
the instance variable
// GUIDE: Write an IF-ELSE for a
sequence of comparisons to
// GUIDE: assign par
based on the length of the hole
// GUIDE: Be sure to use the
constants you defined above
// determine if this a driving hole
// GUIDE: Write code to properly
assign a value to this.fullDriveHole
}
/**
* get the length of the hole.
*
* @return int the length of the hole in yards.
*/
public int getLength()
{
return this.lengthInYds;
}
/**
* get the par of the hole.
*
* @return int par for the hole.
*/
public int getPar()
{
return this.par;
}
/**
* determine if drives are measured on this hole.
*
* @return boolean true iff drives in the fairway are
measured.
*/
public boolean isFullDriveHole()
{
return this.fullDriveHole;
}
}
The Hole Class TestCase:
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class HoleTest
{
/**
* Default constructor for test class HoleTest.
*/
public HoleTest()
{
// not used in this Lab
}
/**
* Test the constructor.
*/
@Test
public void testConstructor()
{
// Create a Hole instance
// GUIDE: Replace this and next line with your code
assertTrue(false);
// Test to see if the instance was created
// GUIDE: Replace this and next line with your code
assertTrue(false);
// Test the instance variables have been initialized
correctly.
// Note - these tests assumes the get methods are correct.
// GUIDE: Replace this and next line with your code
assertTrue(false);
}
/**
* Test the setLength method.
*/
@Test
public void testSetLength()
{
// Create a Hole instance
Hole hole1 = new Hole(250);
// Change the length of the hole
//hole1.setLength(249);
// Test the impact of changing the length to one of a par 3
hole
assertEquals(249, hole1.getLength());
assertEquals(3, hole1.getPar());
assertFalse(hole1.isFullDriveHole());
// Add a test for a par 4 that will be measured
// GUIDE: Replace this and next line with your code
hole1.setLength(460);
assertEquals(460, hole1.getLength());
assertEquals(3, hole1.getPar());
assertFalse(hole1.isFullDriveHole());
// Add a test for par 5
// GUIDE: Replace this and next line with your code
hole1.setLength(475);
assertEquals(475, hole1.getLength());
assertEquals(5, hole1.getPar());
assertFalse(hole1.isFullDriveHole());
// Add a test for par 6
// GUIDE: Replace this and next line with your code
hole1.setLength(695);
assertEquals(695, hole1.getLength());
assertEquals(6, hole1.getPar());
assertFalse(hole1.isFullDriveHole());
}
}
Here is the completed code for both classes. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
//Hole.java (updated)
public class Hole {
// instance variables
private int par;
private int lengthInYds;
private boolean fullDriveHole;
/**
* Constructor for objects of class Hole.
*
* @param length
* of hole in yards
*
**/
public Hole(int length) {
this.setLength(length);
}
/**
* Set the length of the hole and compute its par.
*
* @param length
* the length in of the hole in yards.
*/
public void setLength(int length) {
// constants for determining par;also for minimum distance to be counted
// as full
final int par3Limit = 250;
final int par4Limit = 470;
final int par5Limit = 690;
final int fullDistance = 400;
// setting the length of the hole, initialize the instance variable
lengthInYds = length;
// finding par based on length
if (length <= par3Limit) {
par = 3;
} else if (length <= par4Limit) {
par = 4;
} else if (length <= par5Limit) {
par = 5;
} else {
par = 6;
}
// determine if this a driving hole (length must be at least
// fullDistance)
this.fullDriveHole = length >= fullDistance;
}
/**
* get the length of the hole.
*
* @return int the length of the hole in yards.
*/
public int getLength() {
return this.lengthInYds;
}
/**
* get the par of the hole.
*
* @return int par for the hole.
*/
public int getPar() {
return this.par;
}
/**
* determine if drives are measured on this hole.
*
* @return boolean true iff drives in the fairway are measured.
*/
public boolean isFullDriveHole() {
return this.fullDriveHole;
}
}
//HoleTest.java (corrected)
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class HoleTest {
/**
* Default constructor for test class HoleTest.
*/
public HoleTest() {
// not used in this Lab
}
/**
* Test the constructor.
*/
@Test
public void testConstructor() {
// Create a Hole instance
Hole hole = new Hole(123);
// Test to see if the instance was created
assertTrue(hole != null);
// Test the instance variables have been initialized correctly.
assertEquals(123, hole.getLength());
assertEquals(3, hole.getPar());
assertFalse(hole.isFullDriveHole());
}
/**
* Test the setLength method.
*/
@Test
public void testSetLength() {
// Create a Hole instance
Hole hole1 = new Hole(250);
// Change the length of the hole
hole1.setLength(249);
// Test the impact of changing the length to one of a par 3 hole
assertEquals(249, hole1.getLength());
assertEquals(3, hole1.getPar());
assertFalse(hole1.isFullDriveHole());
// Add a test for a par 4 that will be measured
hole1.setLength(460);
assertEquals(460, hole1.getLength());
assertEquals(4, hole1.getPar());
assertTrue(hole1.isFullDriveHole());
// Add a test for par 5
hole1.setLength(475);
assertEquals(475, hole1.getLength());
assertEquals(5, hole1.getPar());
assertTrue(hole1.isFullDriveHole());
// Add a test for par 6
hole1.setLength(695);
assertEquals(695, hole1.getLength());
assertEquals(6, hole1.getPar());
assertTrue(hole1.isFullDriveHole());
}
}
/*OUTPUT (of JUnit test)*/