Area and Circumference of a Circle
Study the program below, which uses both variables and constants:
// ************************************************************
// Circle.java
//
// Print the area of a circle with two different radii
// ************************************************************
public class Circle
{
public static void main(String[] args)
{
final double PI = 3.14159;
int radius = 10;
double area = PI * radius * radius;
System.out.println(“The area of a circle with radius ” + radius +
” is ” + area);
radius = 2 0;
area = PI * radius * radius;
System.out.println(“The area of a circle with radius ” + radius +
” is ” + area);
}
}
Some things to notice:
The first three lines inside main are declarations for PI, radius, and area. Note that the type for each is given in these
lines: final double for PI, since it is a floating point constant; int for radius, since it is an integer variable, and double for
area, since it will hold the product of the radius and PI, resulting in a floating point value.
These first three lines also hold initializations for PI, radius, and area. These could have been done separately, but it is
often convenient to assign an initial value when a variable is declared.
The next line is simply a print statement that shows the area for a circle of a given radius.
The next line is an assignment statement, giving variable radius the value 20. Note that this is not a declaration, so the int
that was in the previous radius line does not appear here. The same memory location that used to hold the value 10 now
holds the value 20—we are not setting up a new memory location.
Similar for the next line—no double because area was already declared.
The final print statement prints the newly computed area of the circle with the new radius.
Save this program, which is in file Circle.java, into your directory and modify it as follows:
1. The circumference of a circle is two times the product of Pi and the radius. Add statements to this program so that it
computes the circumference in addition to the area for both circles. You will need to do the following:
Declare a new variable to store the circumference.
Store the circumference in that variable each time you compute it.
Add two additional print statements to print your results.
Be sure your results are clearly labeled.
2. When the radius of a circle doubles, what happens to its circumference and area? Do they double as well? You can
determine this by dividing the second area by the first area. Unfortunately, as it is now the program overwrites the first
Chapter 2: Data and Expressions
16
area with the second area (same for the circumference). You need to save the first area and circumference you compute
instead of overwriting them with the second set of computations. So you’ll need two area variables and two
circumference variables, which means they’ll have to have different names (e.g., area1 and area2). Remember that each
variable will have to be declared. Modify the program as follows:
Change the names of the area and circumference variables so that they are different in the first and second
calculations. Be sure that you print out whatever you just computed.
At the end of the program, compute the area change by dividing the second area by the first area. This gives you the
factor by which the area grew. Store this value in an appropriately named variable (which you will have to declare).
Add a println statement to print the change in area that you just computed.
Now repeat the last two steps for the circumference.
Look at the results. Is this what you expected?
3. In the program above, you showed what happened to the circumference and area of a circle when the radius went from
10 to 20. Does the same thing happen whenever the radius doubles, or were those answers just for those particular
values? To figure this out, you can write a program that reads in values for the radius from the user instead of having it
written into the program (“hardcoded”). Modify your program as follows:
At the very top of the file, add the line
import java.util.Scanner;
This tells the compiler that you will be using methods from the Scanner class. In the main method create a Scanner
object called scan to read from System.in.
Instead of initializing the radius in the declaration, just declare it without giving it a value. Now add two statements
to read in the radius from the user:
A prompt, that is, a print statement that tells the user what they are supposed to do (e.g., “Please enter a value
for the radius.”);
A read statement that actually reads in the value. Since we are assuming that the radius is an integer, this will
use the nextInt() method of the Scanner class.
When the radius gets it second value, make it be twice the original value.
Compile and run your program. Does your result from above hold?
Chapter 2: Data and Expressions
17
Try it now!
How it works?
Follow these simple steps to get your paper done
Place your order
Fill in the order form and provide all details of your assignment.
Proceed with the payment
Choose the payment system that suits you most.
Receive the final file
Once your paper is ready, we will email it to you.
Our Services
Custom Writings Help is a Quality-Oriented Company in Online Writing as a result of hiring exceptional professionals to execute clients' tasks.
Essays
At Custom Writings Help,We understand the struggle of research paper writing, and that is why at Custom WritingS Help, we are all out to help you. We pride ourselves on having a team of clinical writers. The stringent and rigorous vetting process ensures that only the 'BEST' Writers are chosen for the job. We have highly qualified Ph.D. and MA writers working with us; we equally offer these experienced writers specific bonuses and incentives to make them deliver highly original, unique, and informative content at reasonably low prices.
Admissions
Thesis Writing Service
Worlwide, Many Masters Students are struggling with Thesis Completion. A thesis is likely to be the longest and most challenging piece of work a student has ever completed. However, unlike essays and other assignments, a student can pick a particular interest topic and work on their initiative. Fortunately, we are there for you. At Custom Writings Help, you are assured of an authentic, imaginative, informative, linguistically great, and advantageous thesis that adheres to all your needs. So, why continue considering different writers when you have discovered the best in the field?
Editing
Custom Papers
Not a single student can avoid writing custom papers. However, a total lack of experience, skills, and time makes it very hard to produce a superb writing piece. Therefore, if you are seeking professional help, turn to us. Our specialized and experienced writers compose a variety of model papers, including custom essays, college term papers, research papers, book reports, MBA essays, executive summaries, dissertations, Ph.D. theses, admission essays, and research proposals for college and university students at any level.
Coursework
Essay Writing
Most of the students disregard the critical principles of essay writing and compose papers below sensible guidelines. Therefore, with Custom Writings Help, one should not worry about his/her essay. Our Writers compose informative and engaging content on all complexities and topics. We write meaningful and smart essays while prioritizing all aspects that bring about a good grade, such as impeccable grammar, proper structure, zero-plagiarism, and conformance to guidelines.
Coursework
Coourse Work Writing
Don't let the seemingly never-ending onslaught of writing assignments get you down. If you are looking where to get course work assistance online, the writers at Custom Writings Help are here to assist you with all of your writing needs. We undertake to unique delivery of papers that meet the professor's requirements. The content is proofread, edited, and checked plagiarism before submission to customers. No matter how big or small your work is, we will deliver on time. Try US Now! !
Coursework
Dissertation Writing Service
High-Quality Dissertation Writing Services are rare. They require Ph.D. academicians – not easily found. However, are an exception. The years, time, and resources we have invested in the dissertation world has given us a competitive advantage over others. Choose to come to Custom Writings Help; You will find perfect Ph.D. consultants who have written hundreds of dissertations theses ready to help you. Let our dissertation-writing services help you craft your dissertation, for you are assured we will give you the results.