image
Project - Ice Cream Class
Start course
Difficulty
Intermediate
Duration
1h 29m
Students
176
Ratings
5/5
Description

This course looks at Object-Oriented programming in Java and shows how classes are designed and constructed, and how objects are created from them. Then, we'll complete three projects: creating a bank account class, an ice cream class, and a circle class, as well as tests to make sure that they work in order to reinforce what you've learned.

Intended Audience

  • Beginner coders or anyone new to Java
  • Experienced Java programmers who want to maintain their Java knowledge
  • Developers looking to upskill for a project or career change
  • College students and anyone else studying Java

Prerequisites

This is a beginner-level course and can be taken by anyone with an interest in learning about Java.

Transcript

In the last project, we created a bank account class, which was basically a simplified version of what the real thing might look like in a bank teller's software, or maybe a simulation, or maybe as part of an ATM machine's embedded system. Now, in this lecture, we'll create a class to represent Ice Cream. This might be used as part of the software for an ice cream company, or a video game, or maybe a college dorm food consumption tracking system. Who knows?

The sky is the limit. And the more you learn about how to code, the cooler things you can do with your skills. For our Ice Cream class, we are using something familiar, but new in this context.

That is, the ArrayList generic class. An ArrayList can hold a list of the toppings that are supposed to be on an order of ice cream. There is a single constructor, and it takes the name, cost, and number of scoops. For the cost data, we will again assume it's in dollars, an integer. Obviously, wherever you live, you can adjust the numbers to Euros, Pound Sterling, Rupees, Rubles, or whatever you want. The constructor should initialize the name, cost, and number of toppings as mentioned, and it should also instantiate the ArrayList to hold the toppings. Having the field just creates a reference capable of holding the address of an ArrayList object, but of course, it doesn't create the object itself. So, you have to do that, manually.

The addTopping method takes a single String and adds a single topping at a time. The getName, getCost, and getNumScoops methods are, as you probably guessed, just simple getter methods. The printToppings method should use standard output to the console to print all of the toppings on the ice cream. Make sure to code both IceCream.java and IceCreamDemo.java to test out your Ice Cream class. Let me run the IceCreamDemo so you can see what the output should look like. Right click this, then go to 'Run IceCreamDemo'  and your format might be different than mine but I printed out the ice cream flavor, the cost,  and then what it was topped with and then what it was topped with, and notice that this could be done with say a print line in main, but these right here are not; these right there are actually printed from within the printToppings method.

So, I hope that helps. So, pause the video and give this one a go. Come back when you're done or if you need some help. So, how was your attempt at this ice cream project? This one was a bit trickier because it introduced some syntax in context that you have not used before. So, if you didn't get it all, don't worry. You'll get better and you'll consider projects like this to be pretty simple when you have more experience. Regardless of how you did, let's work on this together. So, we need the Ice Cream class. Right-click 'New Java' class IceCream and then we need IceCreamDemo. Here we go, 'public static void main()' in the demo file. Here we go. And over here to the ice cream class, name, numScoops, ArrayList<string> toppings

Now, we need to import ArrayList which is java.util.ArrayList, we can add all enter or to import the class or we can actually just type it up here. It doesn't really matter. But we have to have import java.util.ArrayList for this to work. Now, the constructor, String name, int_cost and int_numScoops,  end IceCream ctor. Cost is cost, numScoops is numScoops. And the one that's a little more hidden. But you have to control internally is toppings. That's our array list. You have to instantiate it, so you create an instance. Here we go. Pretty good. I need a semicolon there and now we have add topping. All right. And all we're doing here is taking the toppings list, ArrayList and adding whatever the topping is.

We could put if statements around it and do some tricks too. Make sure that we're getting something maybe from a standard set of items or whatever you'd like to do. But we're just going to do it simple right here, very simple... just return the name. Get cost, returns the cost. No shock there. And of course getting them scoops, returns the number of scoops. And the slightly more complicated method is public void printToppings for string. I'm going raise this a little bit here, put some extra spaces so you can just see it better. (String_topping : toppings) used the enhanced range based for here. printToppings. Okay. And then into the class.

So in here, all I need to do is system.out.println and I'll say, I think for this, what we're going to do is we're going to put a tab and then I'm going to put each topping after the tab. So, it'll indent all the topics. Now, to ice cream demo. If we go over here in the main method, I will create ice cream here. MyIC for my ice cream, is new IceCream and this one is going to have chocolate and three and two ice cream, strawberry, two and one myIC.addTopping. We'll say we'll add nuts to that. Topping we'll add cherries to that as well. And then your ice cream, let's say we just want to add the topping of sprinkles. Pretty good. Now, I need to do some printing now you probably didn't do this or maybe you did. But I'm going to make this little helper method here for just printing ice cream.

So method your friend, they're not terrifying or anything you need to be afraid of and only use when you have to, they're very simple and they're very easy and useful. So, here I'll just put ice cream.get name. So, that will print the name and then I'll do a print line on the cost to put a dollar sign in there too with no space after it, getCost' then we have topped with, it'll just say, "Topped with:" And then on the next line since we already have a nice handy little method that's going to do this for us. We put print toppings  and of course maybe some extra space here. Just a little sprint like a print line for extra space. So, before we run it we need to call printIC on myIC  and then printIC on yourIC to take advantage of what we just had right there.

Let's right-click it, go to 'Run' and there we go. So chocolate, $3 topped with nuts and cherries. That's mine. And Strawberry, $2 topped with springles. That's funny sprinkles or really run it again just to make it little more clean there. Okay, topped with sprinkles. There we go. Could be Pringles I suppose, potato chips if you like that on your ice cream. But I'm not going to judge, salt and sugar, Anyway, your knowledge of object-oriented programming has increased substantially. And using that OOP programming knowledge,

you have now been able to create a little program to hold data and exhibit behaviors related to ice cream as you can probably see, object oriented programming allows us to represent virtually anything we can think of, whether those are actual tangible objects like ice cream, or more conceptual or intangible objects, like bank accounts. In the next lecture, we'll work on another project to round off the section, a Circle class. Let's get going.

 

About the Author
Students
1769
Courses
20
Learning Paths
4

John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.

Covered Topics