Just Built My First Calculator in Java! What Do You Think?

0
15
Asked By CodingNinja99 On

I've just finished writing my very first program, a simple calculator, after only five days of learning Java through videos! I typically study for an hour or two each night. It took me almost an hour to write and debug everything from scratch, which I know is a bit slow, but I feel really accomplished! I'm open to all kinds of feedback. Thanks!

4 Answers

Answered By FeedbackFan88 On

Great job on getting your first program up and running! Here are a few tips:
1. Try to keep your `System.out.println` statements on one line unless they get too long. It keeps your code cleaner.
2. Use more descriptive variable names instead of just `a` and `b` to make your code easier to understand.
3. Consider breaking your main method into smaller methods to enhance readability and reuse code.
4. Define your variables only when needed. For example, you can directly initialize them with `keyboard.nextDouble()`.
5. Use comments sparingly. Your code should ideally be self-explanatory, but you can add comments if something isn't clear.
Happy coding!

Answered By CuriousCoder42 On

Awesome work! Diving right into projects is a fantastic way to learn. You’ve built a solid foundation here. To push your skills, consider allowing multiple operands—like entering `20 + 20 + 20` to total 60—or implementing operator precedence with parentheses, such as `4 * (5 + 2)`. Parsing more complex math could be a fun challenge! Good luck with your coding journey!

Answered By JavaGuru2023 On

Nice job checking for division by zero! In the future, you might want to explore using a try/catch block for exceptions like `ArithmeticException`. You could also consider implementing a variable for `result` that stays outside the switch statement to avoid duplicating the print statement. It could simplify your code. Plus, you can print errors to `System.err` instead of `System.out` for better debugging.

CodingNinja99 -

Thanks for the tips! I'll definitely try that out!

Answered By MathWhiz56 On

Does your calculator work with decimals, or just whole numbers?

CodingNinja99 -

I set it up to use double, so it can handle decimals!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.