COMP1210 – Goals: Solved

$ 29.99
Category:

Description

By the end of this activity you should be able to do the following:
Ø Implement interfaces
Ø Overload methods

Directions:
Don’t forget to add your Javadoc comments for your classes, constructor, and methods in this activity.
Part 1: Customer.java
• Open a new Java file in jGRASP and create a class called Customer. Create three instance variables:
o String object for the customer’s name o String object for the customer’s location (town) o double to store the customer’s balance

• Create a constructor for the Customer class that takes the Customer’s name as a parameter. Set the location variable to an empty string and the balance to zero. You do not have to include the comments.

public Customer(String nameIn) {
____________ = nameIn; // sets name to parameter input
____________ = “”; // sets location to empty string
____________ = 0; // sets balance to 0
}

• Create methods that set the customer’s location, change the customer balance by an amount specified by a double parameter, get the location, and get the balance. That is, provide method bodies for the method headers below. Be sure to compile the completed methods before using them in interactions.
public void setLocation(String locationIn) // sets location variable public void changeBalance(double amount) // add amount to balance public String getLocation() // returns variable for location public double getBalance() // returns variable for balance

• Try the following example in the interactions pane (you can leave out the comments):
ϼÏÏCustomer cstmr = new Customer(“Lane, Jane”);
ϼÏÏcstmr.changeBalance(30); // add $30 to balance
ϼÏÏcstmr.getBalance()
ÏÏÏÏ30.0
ϼÏÏcstmr.changeBalance(-5); // take $5 off balance
ϼÏÏcstmr.getBalance()
ÏÏÏÏ25.0
ϼÏÏcstmr.setLocation(“Boston, MA”);
ϼÏÏcstmr.getLocation()
ÏÏÏÏBoston, MA

Suppose you want to be able to set the customer location with city and state in a single String or by entering city and state in two separate String parameters. Overload the setLocation method so that it takes two String parameters (do not delete the original setLocation method; rather create a second method).

• Now when the setLocation method is included in your code, the compiler will check to see whether you have one String parameter or two String parameters. It will then bind the method call with the appropriate declaration of the setLocation method. Try entering the following code into the interactions pane (recompile your program first):

ϼÏÏCustomer cstmr = new Customer(“Lane, Jane”);
ϼÏÏcstmr.setLocation(“Boston, MA”)
ϼÏÏcstmr.getLocation()
ÏÏÏÏBoston, MA
ϼÏÏcstmr.setLocation(“Omaha”, “NE”)
ϼÏÏcstmr.getLocation()
ÏÏÏÏOmaha, NEÏÏÏ

• Create a toString method that shows the customer’s name, their location, and their balance (you do not have to format balance, but do include the dollar sign).

ϼÏÏCustomer cstmr = new Customer(“Lane, Jane”);
ϼÏÏcstmr.setLocation(“Boston, MA”)
ϼÏÏcstmr.changeBalance(5)
ϼÏÏcstmr
ÏÏÏÏLane, Jane
ÏÏÏÏBoston, MA
ÏÏÏÏ$5.0ÏÏÏÏÏÏ

• Suppose that you wanted to be able to compare Customer objects based on some attribute. You can implement the Comparable interface in your customer class by indicating this in the class header as shown below.

public class Customer implements Comparable<Customer>

By adding this to the Customer header as shown above, your Customer class will now need to implement a compareTo mehod as described below.

The Comparable interface has a method called compareTo which compares an object of this class to another object to another compatible object indicated by the generic parameter based on some value. Suppose you want to sort Customer objects based on their balance, which is a double, then the compareTo method can be defined as follows:

Part 2: CustomerTest.java – Testing with JUnit the Comparable Interface

• Create a jGRASP project and add the Customer.java file. Now create the test file for the Customer by clicking on the Create/Edit test file button . Be sure to comment out the following statement: import static org.junit.Assert.*;

• Now begin adding test methods for to test each of the Customer methods created in Part 1. After each test method is added to CustomerTest.java, be sure to run the test file.

Now add the following test method to test the compareTo method. Note that three test methods are needed to ensure that the conditions in the compareTo method are covered.

• Finally, submit your Customer.java and CustomerTest.java files to the grading system.

Reviews

There are no reviews yet.

Be the first to review “COMP1210 – Goals: Solved”

Your email address will not be published. Required fields are marked *