--- title: "Jam 01 - Exercise 6 - Circle Calculator & java.lang.Math" tags: - 3 ๐Ÿงช in testing - 4 ๐Ÿฅณ done - jam01 - java - math created: 2025-01-20 updated: 2025-01-20 --- <!-- markdownlint-disable line-length single-h1 no-inline-html --> <!-- markdownlint-configure-file { "ul-indent": { "indent": 4 }, "link-fragments": {"ignore_case": true} } --> # Exercise 6 - Circle Calculator & java.lang.Math {%hackmd dJZ5TulxSDKme-3fSY4Lbw %} ## Overview - Exercise 6 In this final exercise, you'll bring together everything you've learned about Java programming to create a more sophisticated application. By building a circle calculator, you'll practice proper code organization, work with mathematical constants and calculations, handle user input validation, and implement formatted output. This exercise reinforces fundamental programming concepts while introducing you to the powerful Math class in Java's standard library. The program you create will demonstrate your understanding of: - Using constants and the Math class for precise calculations - Organizing code into focused, well-named methods - Validating and processing user input - Formatting output for clarity and professionalism ## Required Steps - Circle Calculations **Note: Read this section** (All the way to 'Save Your Work') **carefully before starting the exercise.** Create a new program called CircleCalc.java in the jam01 package that calculates various properties of a circle based on user input. Your program should: 1. Prompt the user for a radius value 2. Calculate and display: - Circle's circumference - Circle's area - Whether the circle is larger, smaller, or equal to a unit circle (radius = 1) 3. ๐Ÿšง **Important Requirements** 1. Program Structure: - Use constants (final) for ฯ€ and comparison threshold(s) - All calculations must use Math class methods - Main method should be clean and readable 2. Input/Output: - All floating-point output must output exactly 2 decimal places - Include units in your output (e.g., "square units" for area) - Handle invalid input gracefully 3. Code Style: - Use descriptive variable names - Include appropriate comments - Follow Java naming conventions ### Example Output ```text Welcome to the Circle Calculator! Enter the radius of your circle: 2.5 Processing circle with radius 2.50 units... Circle Properties: Circumference: 15.71 units Area: 19.63 square units This circle is larger than a unit circle. ``` ### ๐Ÿ”‘ **Hints** - First, did you notice the title of this exercise? Yes, look at the API for helpful functions in the java.lang.Math class. It has all the functions you need for circle calculations. - Next, if you're overwhelmed, you're thinking too hard and most likely trying to attack the entire problem all at once. **Good software engineers have a plan**, i.e. a design, before they implement their solution! You'll be asked to deal with harder and harder problems throughout the semester. Always lay out a general solution in your notes, and break down the big problem into small, well-defined subproblems. - For example, when I worked out the solution, I had a general idea, then broke my problem into small steps, and TESTED MY CODE AFTER EACH STEP using iterative and incremental development. - I wrote one method to get input from the user - I tested my code - Then I modified the method to perform input validation - I tested my code again - Then I added the calculations - I tested my code again - And that was it! - NEVER, EVER wait to test out your code until you think you have finished an entire program. That will surely guarantee incredible amounts of frustration on your end. - As a student in their third CSCI course, you are expected to understand the importance of following good programming habits. For instance, your main method should simply call other methods to carry out all of the requirements. You never place everything in a single main method unless your program requirements are extremely simple, like writing "Hello, World!". So, you might consider using this class below to get you started. It's a starter CircleCalc class with a complete main program: ```java public class CircleCalc { // Constant used for floating point comparisons private static final double EPSILON = 1.0E-10; // Scanner shared by all methods private static Scanner scnr; public static void main(String[] args) { // Set up a common scanner to be used scnr = new Scanner(System.in); // Get the radius from user // TODO double radius = getRadiusFromUser(); // Calculate and display circle properties // TODO displayCircleProperties(radius); // TODO compareToUnitCircle(radius); // Clean up scnr.close(); System.out.println("Goodbye!"); } } ``` - Notice how this approach makes your main program quite readable! Then, you only need to write the three missing methods: - getRadiusFromUser() - Gets and validates the radius input - displayCircleProperties() - Calculates and shows circumference and area - compareToUnitCircle() - Compares the circle to a unit circle - When working with floating point numbers (like doubles), you should never compare them directly using `==` because of how computers represent decimal numbers. Instead, we check if the difference between two numbers is very small (close enough to zero). - We use a constant called EPSILON (sometimes called DELTA) for these comparisons. The starter code provides this constant: ```java private static final double EPSILON = 1.0E-10; // A very small number ``` - You'll learn more about why this is necessary in zyBooks next week (section 4.3 if you want to read ahead). For now, just remember to use EPSILON when comparing floating point values. A few technical tips: - For input validation, remember Scanner's hasNextDouble() and nextLine() methods - The Math class has constants and methods that will make your calculations easier - Use the Java API! ๐Ÿ”‘ **New Concepts** - The `final` keyword makes a variable constant (unchangeable) - `Math.PI` provides a precise value of ฯ€ - Floating-point comparisons should use a small threshold (EPSILON) ## Save Your Work - Exercise 6 Verify what files are uncommited: ```bash git status ``` Stage your changes (This should be the file shown in `git status` as modified) Feel free to use a different message as long as it's descriptive ```bash git add src/main/java/jam01/CircleCalc.java ``` Commit your work ```bash git commit -m "jam01: Complete circle calculator program" ``` Your working directory should be clean. > ๐Ÿ” **Checkpoint**: Before continuing, verify: > > - Your program handles invalid input appropriately > - All calculations are accurate > - Output is formatted to 2 decimal places > - Code follows style guidelines > - Constants are used appropriately