Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a method in the Store class called averageCustomerAge() which should retur

ID: 3880820 • Letter: W

Question

Write a method in the Store class called averageCustomerAge() which should return an int representing the average age of all Customer objects in the store. Test your code by appending the following line to the end of the StoreTestProgram:. You should get a result of 26.

System.out.println(" Average age of customers: " + walmart.averageCustomerAge());

Write a method in the Store class called mostPopularGender() which should return a char representing the most popular gender of all Customer objects in the store (i.e., 'M' or 'F'). Test your code by appending the following lines to the end of the StoreTestProgram. The answer should be M.

System.out.println(" Most popular gender is: " + walmart.mostPopularGender());

Write a method in the Store class called richestCustomer() which should return a Customer object representing the Customer in the store who has the most money. Make sure to return the whole Customer object, not just the name. Test your code by appending the following lines to the end of the StoreTestProgram. The result should be Lore who has $1000:

System.out.println(" Richest customer is: " + walmart.richestCustomer());

Write the following two methods in the Store class.

Then test your code by appending the following lines to the end of the StoreTestProgram:

Clearly, Zack is having a bad day. Clearly, in the real world, it would not be acceptable to allow a walmart store to change a person's gender and rob them ! We have written our code in such a way so as to have all of our Customer attributes visible and modifiable. That is, we are allowing any class object (e.g., a Store, a Lawyer's office, a Movie Theatre, etc..) to mess around with the internal data stored in our customers. This is not safe, nor advisable and represents poor coding style. To prevent this, we need to use encapsulation. That is, we need to hide information that others do not need to know and also prevent others from modifying some of internal attributes (i.e., the private parts) of our objects. Fortunately, this is easily done in JAVA by using the private access modifer. Go to your Customer class and write the word private in front of all attributes:

Now you will notice that the Store and StoreTestProgram will no longer compile. That is because they are directly accessing the internal attributes of the Customer object. You will see this being done everywhere that IntelliJ highlights the attribute in red: We will need to go back into that code and fix it. But first, we need to decide which attributes should be visible to the world ... that is ... which attributes are ok to be seen and read in by other objects. Well, for the sake of our tutorial, let's assume that the name, age and gender are all publicly visible (i.e., we are willing to share that information with everybody) but that nobody should know how much money we have. Currently, we are already preventing everyone outside the Customer class from seeing how much money a Customer has. So we just need to allow them to see the other attributes. To do this, we make what are called get methods ... which are public methods that will get the values for us. Write the following in the Customer class:

Now ... write two more similar methods called getAge() and getGender() that return the values of the age and gender attributes. Go into the Store class and StoreTestProgram class ... and everywhere that you notice that a Customer's age or gender attribute is being accessed directly (i.e., look for the red), you should adjust the code to use your new get methods. For example, customers[i].age will become customers[i].getAge(). Note that you will still have issues where the money attribute is accessed, because we did not make a get method for the money attribute. Also, in the changeGender() method, you will have some problems placing a method call on the left side of an = sign. In fact, we don't really want to change anyone's gender. So we go ahead and remove the changeGender() method completely. Also remove those two lines from the test program. While we are at it, lets prevent any store from robbing its customers. Delete the rob() method as well. Now, all that remains is to fix the richestCustomer() method, which still accesses the money attribute. Since we did not make a get method for the money, we cannot even access it from the Store class. We only have access to the money from within the Customer class. Depending on how you wrote your code, notice what the code in the if statement is doing. It compares the money attributes of two Customer objects. We can actually write a method that does this within the Customer class. Create a method in the Customer class called hasMoreMoneyThan(Customer c) which returns true if the customer has more money than the customer c, otherwise it should return false.

Now adjust your code in the Store class's richestCustomer() method to make use of this newly created hasMoreMoneyThan() method. Now ... the code no longer accesses the money attribute of either Customer object and actually has no idea how much money each Customer has. We have successfully encapsulated our data and protected it from being altered by anyone outside of the class.

Explanation / Answer

Hello There,

PFB code for requested functionality with comments stating the functionality of methods added and the output of test run.

Customer.java

----------------------------

package test;

public class Customer {

private String name;

private int age;

private char gender;

private float money;

// A simple constructor

public Customer(String n, int a, char g, float m) {

name = n;

age = a;

gender = g;

money = m;

}

// Return a String representation of the object

public String toString() {

String result;

result = "Customer " + name + ": a " + age + " year old ";

if (gender == 'F')

result += "fe";

return result + "male with $" + money;

}

//getter methods to access name, age and gender

public String getName() {

return name;

}

public int getAge() {

return age;

}

public char getGender() {

return gender;

}

// Method to check this customer has more money than the customer c

public boolean hasMoreMoneyThan(Customer c) {

if(this.money > c.money) {

return true;

}else {

return false;

}

}

}

-------------------------------

Store.java

-------------------------------

package test;

public class Store {

public static final int MAX_CUSTOMERS = 500;

String name;

Customer[] customers;

int customerCount;

public Store(String n) {

name = n;

customers = new Customer[MAX_CUSTOMERS];

customerCount = 0;

}

public void addCustomer(Customer c) {

if (customerCount < MAX_CUSTOMERS)

customers[customerCount++] = c;

}

public void listCustomers() {

for (int i=0; i<customerCount; i++)

System.out.println(customers[i]);

}

//Method to calculate average age of all the customers

public int averageCustomerAge() {

int sumOfAges = 0;

for (int i=0; i<customerCount; i++) {

sumOfAges += customers[i].getAge();

}

return sumOfAges/customerCount;

}

  

//Method to find most popular gender among all the customers

public char mostPopularGender() {

int numberOfMales = 0;

int numberOfFemales = 0;

for (int i=0; i<customerCount; i++) {

if(customers[i].getGender() == 'M') {

numberOfMales++;

}else if(customers[i].getGender() == 'F') {

numberOfFemales++;

}

}

if(numberOfMales > numberOfFemales) {

return 'M';

}else {

return 'F';

}

}

//Method to find the richest customer among all the customers

public Customer richestCustomer() {

Customer richestCustomer = customers[0];

for (int i=1; i<customerCount; i++) {

if(customers[i].hasMoreMoneyThan(richestCustomer)) {

richestCustomer = customers[i];

}

}

return richestCustomer;

}

  

/*public void changeGender(Customer c) {

if (c.gender == 'M')

c.gender = 'F';

else

c.gender = 'M';

}

public void rob(Customer c) {

c.money = 0;

}*/

}

--------------------------------------

StoreTestProgram.java

---------------------------------------

package test;

public class StoreTestProgram {
public static void main(String args[]) {
Customer[] result;
Store walmart;
walmart = new Store("Walmart off Innes");
walmart.addCustomer(new Customer("Amie", 14, 'F', 100));
walmart.addCustomer(new Customer("Brad", 15, 'M', 0));
walmart.addCustomer(new Customer("Cory", 10, 'M', 100));
walmart.addCustomer(new Customer("Dave", 5, 'M', 48));
walmart.addCustomer(new Customer("Earl", 21, 'M', 500));
walmart.addCustomer(new Customer("Flem", 18, 'M', 1));
walmart.addCustomer(new Customer("Gary", 8, 'M', 20));
walmart.addCustomer(new Customer("Hugh", 65, 'M', 30));
walmart.addCustomer(new Customer("Iggy", 43, 'M', 74));
walmart.addCustomer(new Customer("Joan", 55, 'F', 32));
walmart.addCustomer(new Customer("Kyle", 16, 'M', 88));
walmart.addCustomer(new Customer("Lore", 12, 'F', 1000));
walmart.addCustomer(new Customer("Mary", 17, 'F', 6));
walmart.addCustomer(new Customer("Nick", 13, 'M', 2));
walmart.addCustomer(new Customer("Omar", 18, 'M', 24));
walmart.addCustomer(new Customer("Patt", 24, 'F', 45));
walmart.addCustomer(new Customer("Quin", 42, 'M', 355));
walmart.addCustomer(new Customer("Ruth", 45, 'F', 119));
walmart.addCustomer(new Customer("Snow", 74, 'F', 20));
walmart.addCustomer(new Customer("Tamy", 88, 'F', 25));
walmart.addCustomer(new Customer("Ulsa", 2, 'F', 75));
walmart.addCustomer(new Customer("Vern", 9, 'M', 90));
walmart.addCustomer(new Customer("Will", 11, 'M', 220));
walmart.addCustomer(new Customer("Xeon", 17, 'F', 453));
walmart.addCustomer(new Customer("Ying", 19, 'F', 76));
walmart.addCustomer(new Customer("Zack", 22, 'M', 35));
System.out.println("Here are the customers: ");
walmart.listCustomers();
  
System.out.println(" Average age of customers: " + walmart.averageCustomerAge());
  
System.out.println(" Most popular gender is: " + walmart.mostPopularGender());
  
System.out.println(" Richest customer is: " + walmart.richestCustomer());
}
}

---------------------------------

Output of test run:

--------------------------------

Here are the customers:

Customer Amie: a 14 year old female with $100.0
Customer Brad: a 15 year old male with $0.0
Customer Cory: a 10 year old male with $100.0
Customer Dave: a 5 year old male with $48.0
Customer Earl: a 21 year old male with $500.0
Customer Flem: a 18 year old male with $1.0
Customer Gary: a 8 year old male with $20.0
Customer Hugh: a 65 year old male with $30.0
Customer Iggy: a 43 year old male with $74.0
Customer Joan: a 55 year old female with $32.0
Customer Kyle: a 16 year old male with $88.0
Customer Lore: a 12 year old female with $1000.0
Customer Mary: a 17 year old female with $6.0
Customer Nick: a 13 year old male with $2.0
Customer Omar: a 18 year old male with $24.0
Customer Patt: a 24 year old female with $45.0
Customer Quin: a 42 year old male with $355.0
Customer Ruth: a 45 year old female with $119.0
Customer Snow: a 74 year old female with $20.0
Customer Tamy: a 88 year old female with $25.0
Customer Ulsa: a 2 year old female with $75.0
Customer Vern: a 9 year old male with $90.0
Customer Will: a 11 year old male with $220.0
Customer Xeon: a 17 year old female with $453.0
Customer Ying: a 19 year old female with $76.0
Customer Zack: a 22 year old male with $35.0

Average age of customers: 26

Most popular gender is: M

Richest customer is: Customer Lore: a 12 year old female with $1000.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote