Question

In: Computer Science

The Hole Class Description: For the Hole class, there are three instance variables: the par for...

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());   
}
}

Solutions

Expert Solution

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)*/



Related Solutions

in Java, Create a class called EMPLOYEE that includes three instance variables – a first name...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app names EmployeeTest that demonstrates class EMLOYEE’s capabilities. Create two EMPLOYEE objects and display each object’s yearly...
Create a Java class named Trivia that contains three instance variables, question of type String that...
Create a Java class named Trivia that contains three instance variables, question of type String that stores the question of the trivia, answer of type String that stores the answer to the question, and points of type integer that stores the points’ value between 1 and 3 based on the difficulty of the question. Also create the following methods: getQuestion( ) – it will return the question. getAnswer( ) – it will return the answer. getPoints( ) – it will...
PREVIOUS CODE: InventroryItems class InventoryItem implements Cloneable{ // instance variables protected String description; protected double price;...
PREVIOUS CODE: InventroryItems class InventoryItem implements Cloneable{ // instance variables protected String description; protected double price; protected int howMany; // constructor public InventoryItem(String description, double price, int howMany) { this.description = description; this.price = price; this.howMany = howMany; } // copy constructor public InventoryItem(InventoryItem obj) { this.description = obj.description; this.price = obj.price; howMany = 1; } // clone method @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } // toString method @Override public String toString() { return description +...
with PHP Create a class called Employee that includes three instance variables—a first name (type String),...
with PHP Create a class called Employee that includes three instance variables—a first name (type String), a last name (type String) and a monthly salary int). Provide a constructor that initializes the three instance data member. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its 0. Write a test app named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary....
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class:...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class: • An instance variable called wheelsCount of type int • An instance variable called vType of type String • An instance variable called isTruck of type boolean .  Add getters and setters for the three instance variables  Add the following methods to the Account class: • A void method called initialize that takes a parameter of type int, a String,and one double...
(a) What is a class? What is an object? What is the relationship? (b) What are the instance variables and methods of a class?
 (a) What is a class? What is an object? What is the relationship? (b) What are the instance variables and methods of a class? (c) What is the effect of declaring instance variables and methods public or private? (d) Why do we often declare the instance variables of classes private? (e) Could we declare methods private? Would we want to do so?  (f) What does the identifier this mean? Give an example of its use (g) What is a constructor? (h) What is inheritance? Why is...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
You need to make an AngryBear class.(In java) The AngryBear class must have 2 instance variables....
You need to make an AngryBear class.(In java) The AngryBear class must have 2 instance variables. The first instance variable will store the days the bear has been awake. The second instance variable will store the number of teeth for the bear. The AngryBear class will have 1 constructor that takes in values for days awake and number of teeth. The AngryBear class will have one method isAngry(); An AngryBear is angry if it has been awake for more than...
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # #...
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # # - meat # - to_go # - rice # - beans # - extra_meat (default: False) # - guacamole (default: False) # - cheese (default: False) # - pico (default: False) # - corn (default: False) # #The constructor should let any of these attributes be #changed when the object is instantiated. The attributes #with a default value should be optional. Both positional #and...
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a...
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used to hold the rectangle’s width....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT