From the course: Oracle Java Foundations

Unlock this course with a free trial

Join today to access over 24,700 courses taught by industry experts.

Exercise 7-1: Produce and catch an exception

Exercise 7-1: Produce and catch an exception - Java Tutorial

From the course: Oracle Java Foundations

Exercise 7-1: Produce and catch an exception

(bright music) - [Joe] Exercise 7-1: Produce and Catch an Exception. Add an algorithm calculating an average price for all clothing items owned by a given customer and print the calculated average value. What this would look like is, let's say for example we create an integer average and a count. And since average is the sum of all the prices divided by the count of all the items, then we should get the correct average. So we're going to start a loop. We're going to call c1.getItems, which returns the array of items that the customer has. C1 is a customer. Item will be our iterator. We'll increment count++, it goes up by one each time. We'll take the item price and we'll add it into the average and store it back into the average. That's the plus equals. So basically it's the same as average is equal to average plus item.getPrice. So we're going to loop through the item array in the customer, retrieve the price for each item, add it to the average, increment the count, then divide the…

Contents