1
\$\begingroup\$

This is a calculator for F = MA. (Force = Mass * Acceleration)

I would like a review of my code. I believe that I have gotten better since my last time.

package ufvnik;
import java.util.Scanner;
/*
* @author: Sean
*/

public class tfci {
private static Scanner keyboard;
public static void main(String args[]){
    //calls the equals class method. (this is the organizational part I'm so proud of haha)
    //this statement passes the arguments 0,0 into the method.
    //but they are zero until the user inputs a value for each; Acceleration, and Mass.
    equals(0,0);

}
//this is the method header, contains the parameter variables for equals.
public static void equals(float Mass, float Accel){
    //float force holds the calculations, until 'force' is called. 
    float Theforce;
    //lets user know what this program actually is and does.
    System.out.println("this is a F = M * A calculator.\n");
    keyboard = new Scanner(System.in);
    //prompt the user to input data. 
    System.out.println("how much does it weigh in Kg?: ");
    //holds the Input
    Mass = keyboard.nextFloat();
    //prompt the user again to input data.
    System.out.println("what is the acceleration in m/s?:  ");
    Accel = keyboard.nextFloat();
    //calculate the 'force'
    Theforce = Mass * Accel;
    //end of program- 
    System.out.println("the Force is: " +  Theforce);
    }

}
\$\endgroup\$
3
  • 3
    \$\begingroup\$ Read more on How to ask a good question. \$\endgroup\$
    – Joe C
    Nov 5, 2016 at 16:30
  • \$\begingroup\$ You might want to try doing something more substantial. Learning in small steps is good, but there is only so much you can do wrong in a multiplication and a few input/output operations. \$\endgroup\$ Nov 5, 2016 at 21:56
  • \$\begingroup\$ i was going to do somthing soon, but i want to make the basics near perfect in my understanding. i cant build a great understanding without a strong base. \$\endgroup\$
    – xyz
    Nov 7, 2016 at 23:16

2 Answers 2

4
\$\begingroup\$

I have not compiled or executed it, but it is very simple and there are no glaring errors that would have it not work.

That said as a programmer there is no end of bad in that code.

Naming conventions

  • Class names start with uppercase
  • Variables start with lowercase
  • Names should brief and have contextual meaning

To experienced programmers breaking these simple style rules is frustrating and the very first thing they do when given such badly styled code is fix it, because it hurts to read it.

The function "equals" ?? surly a more descriptive name? calculateForce would have been much better and at least have some relationship to what it is doing.

TheForce LOL lowercase theForce no still wrong, the the is superfluous, just force will do fine.

Comments

When you write comments you are not writing a how to program manual for non programmers. Comments should be minimal, unobtrusive, and only be added if the code needs extra explanation. Comments should improve the readability of code not the reverse. Your commenting has made the whole thing very unreadable.

For example you have:

//this is the method header, contains the parameter variables for equals.
public static void equals(float Mass, float Accel){
    //float force holds the calculations, until 'force' is called. 
    float Theforce;

Not only pointless but incorrect. It's not a method header it is a method declaration, and parameters variables for equals is just stating what is obvious. Yet I am left hanging... The only comments I was looking for was why pass Mass, Accel to the method in the first place?

User interface.

Even if it is a simple program impressions count. Bad presentation can break a product, confusing or obscure front ends are a guaranteed death for the product.

    //lets user know what this program actually is and does.
    System.out.println("this is a F = M * A calculator.\n");

lets user know... Does it? Maybe in context but I am left feeling that some text is missing due to the lower case "t" leading into the vague description. I will wait a see what happens before making conclusions as to what it does. BTW Mass is lower case "m" and same for acceleration "a". Even "F=ma calculator." would have made more sense.

General

  • The two arguments being passed to the input function makes no sense at all, nor is there any comment as to why you did this.

  • Unless you have good reason use private methods rather than public.

  • Use blank lines to separate connected code and make it easier to read.

  • Code should never be more complex than required. Evaluating an expression and storing the result that is then only used once is unnecessary. In your example force (AKA TheForce) is not needed.

  • Class names need to have meaning to all that are expected to read it.

  • This code does not require any comment. Java is expressive enough for all that is needed to know can be put into the code.

A rewrite

package ufvnik;
import java.util.Scanner;

public class CodeReview {

    public static void main(String args[]){
        Scanner inScan = new Scanner(System.in);
        float acceleration;
        float mass;

        System.out.println("Welcome to the force calculator.\n");
        System.out.println("Enter a mass in kg? : ");
        mass = inScan.nextFloat();

        System.out.println("Enter acceleration in meters per second squared? :  ");
        acceleration = inScan.nextFloat();

        System.out.println("Force is equal to : " + (mass * acceleration) + "Newtons\n");
        inScan.close();
    }
}
\$\endgroup\$
6
  • \$\begingroup\$ to answer your questions, its rather simple, im learning from Java: control to data structures. and its the only place im learning from. im not taking a class and im learning from my mistakes. that being said, thank you for the time taken to provide me with a very understandable read, normally i would do it the way you did, but for organization purposes, i broke it down into methods. as my book suggest. \$\endgroup\$
    – xyz
    Nov 7, 2016 at 1:06
  • \$\begingroup\$ @SeanJ I started coding at 13 there were no references in the local library, or school libraries, I knew nobody that had a clue about computers. Without external referance it is very hard to learn. Just getting a program to run is a step forward, which you have achieved. My review is from the eyes of a professional coder with the expectations of a minimum standard. For a beginner runnable code is all that matters. May I suggest that you consider ECMAScript (AKA javascript) as a starting language, it is far better as a learning language than Java (Java is slowly dieing). \$\endgroup\$
    – Blindman67
    Nov 7, 2016 at 9:04
  • \$\begingroup\$ well my brother offered me a ~50k a year job if i learn java(runs airport servers).. so im learning java. I also love it because, compared to python it feels so organized , but it feels very intuitive like C++. i want to finish this book 1k more pages( i'm on pg300, ATM ) before i pick up C++, C, or python...again(python i got pretty good at) thank you so much for your time, it really goes a long way when you want to be great at something... alsoo i really hate javascript right now... i cant understand it) \$\endgroup\$
    – xyz
    Nov 7, 2016 at 16:27
  • \$\begingroup\$ after looking at it again, its like a sheep in a wolfs fur. it seems a little more simple, but idk maybe ill give it a go at the end of this book (10-20pg a day) OMG i just looked at your rewritten code and this part is so beautiful i could not believe i did not see this learning by myself- System.out.println("Force is equal to : " + (mass * acceleration) + "Newtons\n"); inScan.close(); \$\endgroup\$
    – xyz
    Nov 7, 2016 at 16:39
  • \$\begingroup\$ "This code does not require any comment. Java is expressive enough for all that is needed to know can be put into the code." this is how i always felt, i never actually put comments in the code for personal code, but i got hounded at for not doing it here. so i just added 'em in there for the hell of it hoping it would be one less thing i would get hounded for lol(i do understand the use of comments when necessary) \$\endgroup\$
    – xyz
    Nov 7, 2016 at 16:43
1
\$\begingroup\$

First thing I did was put this through the IDEs reformat, after that it already looks a bit more orderly.

Next, I can't say too much about the comments except that some are very obvious - if it helps you to understand the program I guess that's fine, at some point most of them shouldn't be necessary anymore though. If there's JavaDoc comments on it, perhaps also add a sentence about what the program does.

The names are understandable, but don't match the usual camelcase style. In particular lowercase class names are unsual. I also don't (can't) know what ufvnik or tfci are, consider something more descriptive, at least for the class name.

The comment on the equals call explains somewhat why that was done, but really, it should probably be done without this call, especially since equals usually refers to Object.equals. I'd move it into the main method to be honest.

Lastly, keyboard as a static class variable doesn't make sense, not when all the other variables are local ones, convert it to a local one instead. That and most of the above issues are purely about consistency, technically it won't make too much of a difference in such a simple program.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ also i want to thank you for your time, and your input. i will work on my code more and more, until its damn better and this helps me.. " I also don't (can't) know what ufvnik or tfci are, consider something more descriptive, at least for the class name." i was trying to set up eclipse on linux lol sorry, i forgot to change it. \$\endgroup\$
    – xyz
    Nov 7, 2016 at 1:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.