![]() |
|
[Java] Simple Java Calculator - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Java] Simple Java Calculator (/showthread.php?tid=4722498) Pages: Pages
1
2
|
Simple Java Calculator - CyberPunkw - Mar 8, 2015 This is a simple Java calculator I wrote. It's my first program in Java so let me know how i did. I'm aware I could have used functions, but I haven't learned that in java yet. Code import java.util.Scanner;* - S. Goodman - Mar 8, 2015 For a beginner, I will say that I see some good habits and logic, although it's not perfect. A little tired ATM so if no one gets back to you in detail, I will when I wake up. * - Trisomy 21 - Mar 9, 2015 Don't call System.out.println() ten times in a row if you want to print out options on a console, just format the lines with "\n" in one method call. And as for the rest of it, well it works lol. Next try a calculator where you can enter a single expression like "4+5" or "12/3" and it will automatically detect what operations to do on the numbers. * - CyberPunkw - Mar 9, 2015 (Mar 8, 2015 10:59 PM)S. Goodman Wrote: For a beginner, I will say that I see some good habits and logic, although it's not perfect. A little tired ATM so if no one gets back to you in detail, I will when I wake up. Thanks, this isn't my first language, I know python fairly well. (Mar 9, 2015 01:37 PM)Trisomy 21 Wrote: Don't call System.out.println() ten times in a row if you want to print out options on a console, just format the lines with "\n" in one method call. And as for the rest of it, well it works lol. Next try a calculator where you can enter a single expression like "4+5" or "12/3" and it will automatically detect what operations to do on the numbers. Would only one call make it faster or save memory? And making a calculator like that would be hard, but I’m sure I will be able to do so eventually. * - csrk - Mar 10, 2015 (Mar 9, 2015 04:19 PM)CyberPunkw Wrote: Thanks, this isn't my first language, I know python fairly well. Actually it's not that hard, but for a beginner it's the idead challange! It looks difficult but once you did 'one' of the methods the calculator should have the rest will be easier. The hardest part would be the parsing, reading a string (your calculation) as numbers and operators. I really encourage you to create such a calculator, you will learn loads. :) * - Trisomy 21 - Mar 10, 2015 (Mar 9, 2015 04:19 PM)CyberPunkw Wrote: Would only one call make it faster or save memory? . Every time you call a method, it takes time. For something like println it is literally fractions of a millisecond, but there is no reason not to just use one method call. * - Han. - Mar 10, 2015 (Mar 9, 2015 01:37 PM)Trisomy 21 Wrote: Don't call System.out.println() ten times in a row if you want to print out options on a console, just format the lines with "\n" in one method call. And as for the rest of it, well it works lol. Next try a calculator where you can enter a single expression like "4+5" or "12/3" and it will automatically detect what operations to do on the numbers. Why not? You're encouraging a beginner to reduce the readability of their program. Code is made for people to read, it's a means of communication with yourself at a later date and with other programmers. What he learns now will become deep-rooted habits. * - Entityy - Mar 10, 2015 Thats honestly pretty well done for being a beginner. We're currently working on stuff similar to this in my computer science class. * - CyberPunkw - Mar 10, 2015 (Mar 10, 2015 04:40 PM)Instagram Fanatic Wrote: Thats honestly pretty well done for being a beginner. We're currently working on stuff similar to this in my computer science class. Cool. That makes me feel like more than just a highschooler who likes to code. (Mar 10, 2015 04:40 PM)Instagram Fanatic Wrote: Thats honestly pretty well done for being a beginner. We're currently working on stuff similar to this in my computer science class. So it is a good habit? I'm confused as hell now. * - Trisomy 21 - Mar 11, 2015 (Mar 10, 2015 04:39 PM)Han. Wrote: Why not? You're encouraging a beginner to reduce the readability of their program. Code is made for people to read, it's a means of communication with yourself at a later date and with other programmers. What he learns now will become deep-rooted habits. I explained why above, if you don't understand why calling the same method 10 times at the moment when you have all the information needed to call it once, you should hit the books. And I couldn't agree with you more about what he learns now will become deep-rooted habits, so he should start doing things the right way now. * - Captain Steve Rogers - Mar 12, 2015 Why you wrote this "throws java.io.IOException" ? Btw i'm also going to make this today for the first time without copying from others. * - CyberPunkw - Mar 13, 2015 (Mar 12, 2015 10:45 PM)Lalit Agarwal Wrote: Why you wrote this "throws java.io.IOException" ?At first is was going to use System.in.read(), but I realized that wouldn't work as well as Scanner. I guess i forgot to take it out. * - Captain Steve Rogers - Mar 14, 2015 (Mar 13, 2015 03:42 PM)CyberPunkw Wrote: At first is was going to use System.in.read(), but I realized that wouldn't work as well as Scanner. I guess i forgot to take it out. Frankly specking no idea what is "System.in.read()". Why scanner won't work? you have even used it. Your code looks damn complicated to me. Here is mine : https://hackforums.net/showthread.php?tid=4730078 * - SunnyAkuma - Mar 15, 2015 No need to lengthen the program by taking inputs in every Case.You can take 2 numbers as inputs before entering the switch block. Code import java.util.Scanner;* - Bibl - Mar 15, 2015 (Mar 9, 2015 01:37 PM)Trisomy 21 Wrote: Don't call System.out.println() ten times in a row if you want to print out options on a console, just format the lines with "\n" in one method call. And as for the rest of it, well it works lol. Next try a calculator where you can enter a single expression like "4+5" or "12/3" and it will automatically detect what operations to do on the numbers. Hi there, I would not recommend doing this. If you're code is going to make a million calls to the System.out.println(String) method, then the JIT will come with a solution on it's own. If you really want to do it yourself, use a StringBuilder(docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html) to concat Strings in Java. Bertha. |