1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Introduction to Classes and Objects in Java

Project - Circle Class

Contents

keyboard_tab

The course is part of this learning path

Start course
Overview
Difficulty
Intermediate
Duration
1h 29m
Students
92
Ratings
5/5
starstarstarstarstar
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 previous project, we worked on an IceCream class. That project helped us see some of the variety that we can have with classes and different options we have for data and how to manipulate that data inside a class. In this project, you will be creating and testing out a Circle Class. This will be more similar to the Rectangle class we created earlier because it is a simple geometric shape. You should use the formulas, circumference = 2*pi*r and area = pi*r^2. The radius is kept as a private field that is a double. The constructor should be fairly straightforward. The no-arg constructor should set the radius to 1.0, in order to create the unit circle. 

Remember that we can call one constructor from another with the help of the 'this' keyword. So, where can we get the value for pi from? We could make it a field or even declare and initialize it as a local variable in the methods in which it is used. However, java has a very handy public static double variable that is part of the math class that we can use for this purpose. Since the Math classes are part of java.lang package, which is the default package that's automatically included in all of our java applications, we can just use it directly Math.pi, p-i. And for the area, you could just multiply the radius by itself to get the radius squared or you could use the Math.pow method, p-o-w which takes two arguments. The first being the base and the second being the exponent. 

So, we can just pass it the radius, comma and the number two, either way is fine with me. So, I'd like you to create two files: Circle.java and CircleDemo.java. As I'm sure, you've figured out, create the actual circle class in Circle.java and set up your main method and tests in CircleDemo.java. All right. I'd like to run the CircleDemo for you so you can see what the output might look like. Right click 'run' and there you go. Radius, circumference and area. You'll notice yours might have extra decimal places, but that has to do with something we do as a little follow up a little treat using a formatting method, but if you can figure that out, bonus points to you. But regardless, it should look something like this. Hope that helps. All right. Now, I'd like you to pause the video and give this one your best shot. Come back when you're done or if you need some help.

How did that go for you? Were you able to create the Circle class and test it out? Let's work on it together so that you can see how I would do it. So, we have to create the Circle class. Right click 'Java Class'. Circle that will create a Circle.java file and then we have the CircleDemo. All right. For the demo, we will get it ready with public static void main. There we go. And then Circle will represent the actual circle so we need a radius, private double radius. Now, we have the no-arg constructor. Now, let's look at the other constructor first. And we just set this.radius = radius passed in as the argument to overcome name shadowing. And from the initial no argument constructor, right here, we just pass in 1.0 as the radius. So, this one will call that one and then this one sets the Radius to 1. We need to getRadius() and setRadius() which are pretty straightforward, public void setRadius. this.radius = radius.

And now, we have the circumference and the area methods. 2 * Math.PI * r which is radius and now, the area. Let's give ourselves a little bit more room here. Okay. Now, this is pi * r^2, Math.PI times or we could do radius times radius if we wanted to or we can use Math.pow, which I'm going to do here. Radius is the base and two is the exponent so that's radius squared. There we go. Perfect. Now, over to CircleDemo, I'm actually going to prep a method to help us out first. This is going to be called printCircleData. Sure, that looks good. It just takes a circle. 

Okay, printCircleData and we're going to print out system.out.println r = we're going to say circle.getRadius() and then, we have system.out.println circumference or c = circle.circumference() and then, system.out.println A for area. Right there. And a little extra spacing at the bottom. There we go. Just extra spacing. And now up in main, we probably want to actually use these. So, unitCircle is just going to be new Circle() with no arguments that creates the unitCircle. We could also manually pass on one but that's not necessary. And we'll do a five on this. And then, yourCircle is new Circle(12.75) or some other number you come up with.

All right. Now, printCircleData on the unitCircle, printCircleData on myCircle and printCircleData on yourCircle. Excellent. Now, of course, we probably want to give it a little run here. Right click, 'run'. Okay. And we get radius as one and then this circumference and area right here. Same thing here with five, circumference and area. And the extra space and then 12.75, circumference and area. Now, you might have noticed that there's a lot of decimal places here so we can use a handy static method of the string class called format. It uses special syntax to indicate how many decimal places to show and an in-depth discussion is outside the scope of what we're learning right now but let me show you how to use it quickly. We're going to do this for the area and the circumference, for right now the radius doesn't really matter. But in here, inside of printCircle, we're going to, we have String.format which we're going to use for the radius or I'm sorry, the circumference and the area. So, String.format and the circumference is going to be our second parameter, second argument here, rather.

And in here, you put a % sign to indicate that you're going to use a so called format string and that tells us hey, this has a special meaning, what's about to come. And you put .2f. The f indicates that we're dealing with real numbers and we want point two to indicate that we want two decimal places. Okay. And then, the second argument and subsequent arguments indicate, what do you fill in to those special format strings. And in this case, we've got circle.circumference(). So, String.format has this and that. And then, we also need an additional parenthesis to end the print line. We're going to do something very similar to the area. So, String.format and %.2f. If you put .3f, you'll get, of course, three decimal places. All right. There we go, circle.area().

If you need to break this up a little bit to understand what's going on, then I'll put these on the next line and then that extra parenthesis belongs to the print line method. You can see that circle.circumference() is being called as the second argument and then, right here is where string format begins and ends. And then, right here is where print line begins and ends. That looks pretty cool. Let's see what kind of a difference it makes over here. So, I'm going to right click, 'run' again. 

There we go, two decimal places all the way down. Pretty cool. So, that's awesome. What we've been able to do, we have this amazing little program and we've learned a little about how to format some data when printing it out using String.format. Obviously String.format can work with a lot of other types and offers many other options besides the decimal places and things like that. But hopefully, this gives you a taste of its power. In the next lecture, we're going to do the section wrap up. I'll see you there.

 

About the Author
Students
759
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