41

This kind of programming exercice, despite its apparent simplicity, is a good opportunity to learn various things. Style Python has a Code Style guide called PEP 8. If you begin with Python, I highly recommend reading it every now and then and trying to apply it. In your case, a few things could be improved regarding style: blank lines between the ...


37

Some of the numbers might seem plucked out of thin air but they aren't Still better have them explained. I can understand 3.6 and even 19.6 (it is \$2g\$, isn't it?), but I have no idea what 98/490 stands for. Declare them as symbolic constants with meaningful names. I don't see a need for abs in reaction_time = round(abs(diff.total_seconds()), 2) ...


31

Welcome! Here is some feedback, in no particular order. Avoid using namespace std This advice is often repeated for good reason. See also: code guidelines SF.6. It's useful when you're learning the C++ standard library to know what belongs in the standard library and what doesn't. cout << "Enter your 2 numbers: "<< endl; Becomes std::cout &...


24

Input validation You do absolutely no input validation when the user enters an integer. If the user enters a non-integer value, like foo, or bar, your code will crash with a ValueError. The correct way to obtain user input is to use a try/except block, like this: try: var1 = int(input( ... )) var2 = int(input( ... )) ... except ValueError: ...


22

I'll blame the extra-weird broken indentation on copy/paste difficulties, and ignore that. There are as many ways to write a calculator as there are programmers willing to write one. So I'm not going to suggest other ways, instead I'm going to hint at how you could improve the flexibility of what you've got. The first concern you have, is getting user ...


21

I suppose you don't indent the printf debug lines to make them easier to spot. That's a sensible idea (although not one I've seen before). However, I treat Code Review answers as a "just before checkin" thing, so I'd suggest removing these by this point. You pre-declare your variables. Don't do this; write them as close to point-of-use as reasonable. Your ...


20

The reason it seems messy is because you have one function doing multiple things. So what does your function do? It queries the DOM for elements containing the input. It calculates the BMI based on that input It has logic that determines what kind of output to produce It produces output to the DOM. There are a few problems with that: What if you want to ...


18

This is very nicely written, pleasure to read. Most of my comments will concern usability and readability. The NONE operator typedef enum { AND = '&', OR = '|', XOR = '^', NONE = 0 } operator; NONE kinda sticks out there: it's not an operator. It made me wonder how it's used. It turns out, if the operator is NONE, then the calculator ...


18

Welcome to C++. A few notes: When working outside of a toy project, avoid using namespace std. For why, see here. In addition, when working outside of a toy project, I encourage you to define your classes within the scope of a namespace. Only you can fight namespace pollution. Your a header does not make use of the iostream, string, or stringstream headers....


18

I suggest breaking the code into functions, so you can easily import your code into a Python shell and test that your computation is exact (and not just thinking it is). To expand on other answers, you may: retry asking the speed if it is not an integer (detected when the int function raises a ValueError); in the same vein, since you don't really need to ...


17

power should probably be using Math.pow(). I'm picturing how slow your code would be at calculating 1.00000015000000. Or how inaccurate it'll be when someone wants to raise to a non-integer power. If those are not intended to be allowed, then passing doubles all over the place seems odd. But it'd be better to check beforehand and throw an error than ...


15

Don't flush where you don't need to. Flushing is expensive, so don't use std::endl unless you need it. Nearly always, the shorter '\n' suffices, which can be merged with any bordering string-literal. Still, that advice seems to be for your teacher, not you ;-) return 0; is implicit in main if control reaches the closing brace }. The same is true for C ...


15

This looks pretty nice! I have only a few minor nitpicks and practical tips for you. Returning boolean values directly I'm a bit surprised by this: if type(self) != type(other): return False return True I'm wondering if you have a particular reason for not writing simply: return type(self) == type(other) The same goes for all the ...


15

I'm going to go over a few things in this answer, hopefully it makes them simple enough. Then, at the end, I'm going to go way overboard and significantly over-engineer this programme while still making it shorter. First, let's talk about your input handling (or lack thereof). As programmers, we should strive to be able to gracefully handle any/all input, ...


15

Unless you'll be maintaining an accumulated number for multiple operations, this may not be the best use of classes. For your code, you'd get the same effect by just performing these calculations within the switch statement (something similar to this). Right now, you're just using a class to contain similar functions, including a trivial output function. ...


14

Write a helper method, e.g. static Button createButton(Frame f, String label, int x0, int y0, int width, int height) { Button b = new Button(label); b.setBounds(x0, y0, width, height); f.add(b); return b; } Then your code would simplify to lines like: createButton(f, "+", 70, 100, 50, 40); which is a reduction of roughly 2/3. Of course ...


14

Looks good for a beginner. Couple tweaks if you want to make it look nicer. The name of the class should be somewhat describing the purpose so in your case Calculator would fit better. If you are familiar with enum you could parse your +, -, / into an enum and do switch on that. Then next tweak could be to do the System.printout at the end and in the ...


14

I must say, I respectively disagree with @ishyfishy's example of the addition function. Although, I do agree with the point about splitting the arithmetic functions away from the main function. I believe that the arithmetic functions should only handle performing the arithmetic; all the I/O should be handled by main function (I have been taught that this ...


13

For a beginner it's a good approach. You need to know about Java Naming Convention. We use mixed case for naming any variable or instance of a class. So in your code Maths Maths = new Maths(); should be Maths maths = new Maths();. Or even better make all the methods in Maths class public static as the instance of the class doesn't play any part in ...


13

There are several things that can be improved in your code, I will point out some of them here. Polymorphism Your Addition/Subtraction/Multiplication/Division classes has a lot in common. You should use polymorphism and inheritance to use them better. You can make an abstract class for them. Also, you should make your unchangeable Fraction fields final. ...


13

Nice job! I hope you find the comments below useful as you progress. Variable names: Try to have variable names which describe what the variable actually represents or holds. For example, I have no idea why you named your Scanner "kb". Method names: Method names should always be verb phrases (actions) which describe what the method does. So for example, ...


13

Style I'm sorry for making it personal here, camelCase, but the preferred styling for function names is that they're, well, camelCased. (Apologies for the bad pun). So not GetBinarySum, but getBinarySum. Removing the Operations class I think your approach for implementation of Operations is flawed. You're taking two Strings, converting them to numbers, ...


13

I don't know how your assignment looked exactly, so some of the below may be misguided criticisms. I've just tried to poke as many holes in the code as possible; fair or otherwise. Something will probably overlap with your interviewer's thoughts. But of course, I don't know what that might be, so there's no real order to these points - I just wrote them as I ...


13

For a beginner this is pretty good. But, there are three glaring problems with it. You should use a 'main' function. You should use a dictionary. operators Starting with (2), a dictionary holds a key value pair. If you've come across lists/arrays, it's like them, apart from that you can use a lot of different types, not just integers. Defining one is ...


13

It crashes if user input for speed_ms is not integer If user presses any key before t = input("GO!! ") his reaction time is saved as 0. This is a major flaw in a code, and should be fixed.


13

Some rules of thumb: If you have your entire program in a single method (Main(), in this case), there are some design considerations you should probably revisit. Especially from a DRY ("Don't Repeat Yourself") perspective. If you find yourself using goto in a language invented after 1985, you're likely not availing yourself of the rich and varied control ...


12

Fraction representation @Simon has a good suggestion, which I'll elaborate on: Fraction should be immutable, for several reasons: Reduce defensive copying: In a more complex application, you'll be passing Fractions around. If fractions are not immutable, then each function that you pass it to will want to make a defensive copy to make sure that it stays ...


12

A few notes: You see those global variables that you have at the top? char input[20]; char num[20]; int i; int minus = 1; float answer, num1, num2; char operations; Those shouldn't be there. Those should be declared within functions. If another function required the value of one of those variables, pass it as a parameter. If you need to store a value ...


12

A CLI-based calculator is often a good way to prepare one for bigger programming tasks, as there are just so many ways of doing it, catering for beginner to advanced implementations. I happen to have one myself about two months ago, for reference. :) Indentation There's no other way of putting this, but your indentation is extremely poor. Nested code ...


Only top voted, non community-wiki answers of a minimum length are eligible