QuestionQuestion

Transcribed TextTranscribed Text

Activity: Comparable Interface Page 1 of 4 Page 1 of 4 Goals: By the end of this activity you should be able to do the following: Ø Implement interfaces Ø Overload methods 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 Activity: Comparable Interface Page 2 of 4 Page 2 of 4 • 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. Activity: Comparable Interface Page 3 of 4 Page 3 of 4 • 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. Activity: Comparable Interface Page 4 of 4 Page 4 of 4 • 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.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import org.junit.Assert;
//import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;

/**
* CustomerTest class, it tests the setLocation,
* changeBalance, toString and compareTo methods
* of the Customer class.
*/
public class CustomerTest {


   /** Fixture initialization (common initialization
    * for all tests). **/
   @Before public void setUp() {
   }

   /**
    * This tests the setLocation() method.
   */
   @Test
   public void setLocationTest1() {
      Customer cstmr = new Customer("Lane, Jane");
      cstmr.setLocation("Boston, MA");
      Assert.assertEquals("Boston, MA", cstmr.getLocation());
   }
   
   
   /**
    * This tests the setLocation(city, state) method.
   */
   @Test public void setLocationTest2() {
      Customer cstmr = new Customer("Lane, Jane");
      cstmr.setLocation("Atlanta", "GA");
      Assert.assertEquals("Atlanta, GA", cstmr.getLocation());
   }
   
   
   /**
    * This tests the changeBalance() method.
   */
   @Test public void changeBalanceTest() {
      Customer cstmr = new Customer("Lane, Jane");
      cstmr.changeBalance...

By purchasing this solution you'll be able to access the following files:
Solution.java and Solution1.java.

50% discount

Hours
Minutes
Seconds
$50.00 $25.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats