Hypotenuse and name generator

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












I am a self-taught programmer and I just started learning Java and wrote a simple program. If anyone more experienced than myself can go over my code and give me some pointers I would really appreciate it.



The program starts by asking the user to enter either 0 for the name generator or 1 for the hypotenuse generator (uses the Pythagorean Theorem). Then it asks for follow up questions based on the starting choice. (I'm using Terminal to run this program.)



Simple.java



import java.util.Scanner;
public class Simple
static int a = 13;
static int b = 27;
public static void main(String args)
Scanner reader1 = new Scanner(System.in);
System.out.println("Name Gen = 0, Hypotenuse Calc = 1");
int choice = reader1.nextInt();

if (choice == 1)
System.out.println("enter num for a");
a = reader1.nextInt();
System.out.println("enter num for b");
b = reader1.nextInt();
SimpleTwo.firstMethod(a, b);
reader1.close();
String arg = new String[0];
else
SimpleTwo obj = new SimpleTwo();
System.out.println("age?");
int n = reader1.nextInt();
obj.age = n;
System.out.println("first name?");
String fname = reader1.next();
obj.fName = fname;
System.out.println("last name?");
String lname = reader1.next();
obj.lName = lname;
reader1.close();
SimpleTwo.myMethod();






SimpleTwo.java



public class SimpleTwo 
static int age = 0;
static String fName = "";
static String lName = "";

public static void myMethod()
System.out.println(age+", "+fName+", " + lName);


public static void firstMethod(int a, int b)
int ab = (a * a) + (b * b);
double c = Math.sqrt(ab);
System.out.println("The length of the Hypotenuse is " + c + ", when a=" + a + " & b=" + b + ".");








share|improve this question





















  • @Coal_ The title is fixed now, but the general description of the code was already there. The questions asked when running the code make it just about self-documenting I'd say.
    – Mast
    Jun 10 at 23:10
















up vote
2
down vote

favorite












I am a self-taught programmer and I just started learning Java and wrote a simple program. If anyone more experienced than myself can go over my code and give me some pointers I would really appreciate it.



The program starts by asking the user to enter either 0 for the name generator or 1 for the hypotenuse generator (uses the Pythagorean Theorem). Then it asks for follow up questions based on the starting choice. (I'm using Terminal to run this program.)



Simple.java



import java.util.Scanner;
public class Simple
static int a = 13;
static int b = 27;
public static void main(String args)
Scanner reader1 = new Scanner(System.in);
System.out.println("Name Gen = 0, Hypotenuse Calc = 1");
int choice = reader1.nextInt();

if (choice == 1)
System.out.println("enter num for a");
a = reader1.nextInt();
System.out.println("enter num for b");
b = reader1.nextInt();
SimpleTwo.firstMethod(a, b);
reader1.close();
String arg = new String[0];
else
SimpleTwo obj = new SimpleTwo();
System.out.println("age?");
int n = reader1.nextInt();
obj.age = n;
System.out.println("first name?");
String fname = reader1.next();
obj.fName = fname;
System.out.println("last name?");
String lname = reader1.next();
obj.lName = lname;
reader1.close();
SimpleTwo.myMethod();






SimpleTwo.java



public class SimpleTwo 
static int age = 0;
static String fName = "";
static String lName = "";

public static void myMethod()
System.out.println(age+", "+fName+", " + lName);


public static void firstMethod(int a, int b)
int ab = (a * a) + (b * b);
double c = Math.sqrt(ab);
System.out.println("The length of the Hypotenuse is " + c + ", when a=" + a + " & b=" + b + ".");








share|improve this question





















  • @Coal_ The title is fixed now, but the general description of the code was already there. The questions asked when running the code make it just about self-documenting I'd say.
    – Mast
    Jun 10 at 23:10












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I am a self-taught programmer and I just started learning Java and wrote a simple program. If anyone more experienced than myself can go over my code and give me some pointers I would really appreciate it.



The program starts by asking the user to enter either 0 for the name generator or 1 for the hypotenuse generator (uses the Pythagorean Theorem). Then it asks for follow up questions based on the starting choice. (I'm using Terminal to run this program.)



Simple.java



import java.util.Scanner;
public class Simple
static int a = 13;
static int b = 27;
public static void main(String args)
Scanner reader1 = new Scanner(System.in);
System.out.println("Name Gen = 0, Hypotenuse Calc = 1");
int choice = reader1.nextInt();

if (choice == 1)
System.out.println("enter num for a");
a = reader1.nextInt();
System.out.println("enter num for b");
b = reader1.nextInt();
SimpleTwo.firstMethod(a, b);
reader1.close();
String arg = new String[0];
else
SimpleTwo obj = new SimpleTwo();
System.out.println("age?");
int n = reader1.nextInt();
obj.age = n;
System.out.println("first name?");
String fname = reader1.next();
obj.fName = fname;
System.out.println("last name?");
String lname = reader1.next();
obj.lName = lname;
reader1.close();
SimpleTwo.myMethod();






SimpleTwo.java



public class SimpleTwo 
static int age = 0;
static String fName = "";
static String lName = "";

public static void myMethod()
System.out.println(age+", "+fName+", " + lName);


public static void firstMethod(int a, int b)
int ab = (a * a) + (b * b);
double c = Math.sqrt(ab);
System.out.println("The length of the Hypotenuse is " + c + ", when a=" + a + " & b=" + b + ".");








share|improve this question













I am a self-taught programmer and I just started learning Java and wrote a simple program. If anyone more experienced than myself can go over my code and give me some pointers I would really appreciate it.



The program starts by asking the user to enter either 0 for the name generator or 1 for the hypotenuse generator (uses the Pythagorean Theorem). Then it asks for follow up questions based on the starting choice. (I'm using Terminal to run this program.)



Simple.java



import java.util.Scanner;
public class Simple
static int a = 13;
static int b = 27;
public static void main(String args)
Scanner reader1 = new Scanner(System.in);
System.out.println("Name Gen = 0, Hypotenuse Calc = 1");
int choice = reader1.nextInt();

if (choice == 1)
System.out.println("enter num for a");
a = reader1.nextInt();
System.out.println("enter num for b");
b = reader1.nextInt();
SimpleTwo.firstMethod(a, b);
reader1.close();
String arg = new String[0];
else
SimpleTwo obj = new SimpleTwo();
System.out.println("age?");
int n = reader1.nextInt();
obj.age = n;
System.out.println("first name?");
String fname = reader1.next();
obj.fName = fname;
System.out.println("last name?");
String lname = reader1.next();
obj.lName = lname;
reader1.close();
SimpleTwo.myMethod();






SimpleTwo.java



public class SimpleTwo 
static int age = 0;
static String fName = "";
static String lName = "";

public static void myMethod()
System.out.println(age+", "+fName+", " + lName);


public static void firstMethod(int a, int b)
int ab = (a * a) + (b * b);
double c = Math.sqrt(ab);
System.out.println("The length of the Hypotenuse is " + c + ", when a=" + a + " & b=" + b + ".");










share|improve this question












share|improve this question




share|improve this question








edited Jun 10 at 23:15









Daniel

4,1132836




4,1132836









asked Jun 10 at 19:12









Jesse Brior

112




112











  • @Coal_ The title is fixed now, but the general description of the code was already there. The questions asked when running the code make it just about self-documenting I'd say.
    – Mast
    Jun 10 at 23:10
















  • @Coal_ The title is fixed now, but the general description of the code was already there. The questions asked when running the code make it just about self-documenting I'd say.
    – Mast
    Jun 10 at 23:10















@Coal_ The title is fixed now, but the general description of the code was already there. The questions asked when running the code make it just about self-documenting I'd say.
– Mast
Jun 10 at 23:10




@Coal_ The title is fixed now, but the general description of the code was already there. The questions asked when running the code make it just about self-documenting I'd say.
– Mast
Jun 10 at 23:10










2 Answers
2






active

oldest

votes

















up vote
2
down vote













SimpleTwo obj = new SimpleTwo();


is unnecessary because all your properties and methods of SimpleTwo are static, therefore creating an instance of SimpleTwo does nothing unless you have something in it that's not static. I would also guess that lines with code like obj.age give you a compiler warning because you should be using SimpleTwo.age since age is a static member.



I would also change the names of the classes and the methods to something more meaningful. firstMethod doesn't describe what it is for and technically it is the second method in your class






share|improve this answer




























    up vote
    1
    down vote













    You call reader1.close() on both branches of your if statement. It would be better to do it once after the if statement.



    if (...) 
    ...
    else
    ...

    reader1.close();


    Even better would be to use “try-with-resource” to open the scanner, and let it close the resource for you, even if exceptions are thrown.



    try ( Scanner reader1 = new Scanner(System.in) ) 
    ...

    // reader1 is automatically closed here


    —-



    You aren’t using this code:



    String arg = new String[0];


    It should be removed.






    share|improve this answer





















      Your Answer




      StackExchange.ifUsing("editor", function ()
      return StackExchange.using("mathjaxEditing", function ()
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
      );
      );
      , "mathjax-editing");

      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "196"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f196240%2fhypotenuse-and-name-generator%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote













      SimpleTwo obj = new SimpleTwo();


      is unnecessary because all your properties and methods of SimpleTwo are static, therefore creating an instance of SimpleTwo does nothing unless you have something in it that's not static. I would also guess that lines with code like obj.age give you a compiler warning because you should be using SimpleTwo.age since age is a static member.



      I would also change the names of the classes and the methods to something more meaningful. firstMethod doesn't describe what it is for and technically it is the second method in your class






      share|improve this answer

























        up vote
        2
        down vote













        SimpleTwo obj = new SimpleTwo();


        is unnecessary because all your properties and methods of SimpleTwo are static, therefore creating an instance of SimpleTwo does nothing unless you have something in it that's not static. I would also guess that lines with code like obj.age give you a compiler warning because you should be using SimpleTwo.age since age is a static member.



        I would also change the names of the classes and the methods to something more meaningful. firstMethod doesn't describe what it is for and technically it is the second method in your class






        share|improve this answer























          up vote
          2
          down vote










          up vote
          2
          down vote









          SimpleTwo obj = new SimpleTwo();


          is unnecessary because all your properties and methods of SimpleTwo are static, therefore creating an instance of SimpleTwo does nothing unless you have something in it that's not static. I would also guess that lines with code like obj.age give you a compiler warning because you should be using SimpleTwo.age since age is a static member.



          I would also change the names of the classes and the methods to something more meaningful. firstMethod doesn't describe what it is for and technically it is the second method in your class






          share|improve this answer













          SimpleTwo obj = new SimpleTwo();


          is unnecessary because all your properties and methods of SimpleTwo are static, therefore creating an instance of SimpleTwo does nothing unless you have something in it that's not static. I would also guess that lines with code like obj.age give you a compiler warning because you should be using SimpleTwo.age since age is a static member.



          I would also change the names of the classes and the methods to something more meaningful. firstMethod doesn't describe what it is for and technically it is the second method in your class







          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Jun 13 at 1:54









          Zachary Rudzik

          1246




          1246






















              up vote
              1
              down vote













              You call reader1.close() on both branches of your if statement. It would be better to do it once after the if statement.



              if (...) 
              ...
              else
              ...

              reader1.close();


              Even better would be to use “try-with-resource” to open the scanner, and let it close the resource for you, even if exceptions are thrown.



              try ( Scanner reader1 = new Scanner(System.in) ) 
              ...

              // reader1 is automatically closed here


              —-



              You aren’t using this code:



              String arg = new String[0];


              It should be removed.






              share|improve this answer

























                up vote
                1
                down vote













                You call reader1.close() on both branches of your if statement. It would be better to do it once after the if statement.



                if (...) 
                ...
                else
                ...

                reader1.close();


                Even better would be to use “try-with-resource” to open the scanner, and let it close the resource for you, even if exceptions are thrown.



                try ( Scanner reader1 = new Scanner(System.in) ) 
                ...

                // reader1 is automatically closed here


                —-



                You aren’t using this code:



                String arg = new String[0];


                It should be removed.






                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  You call reader1.close() on both branches of your if statement. It would be better to do it once after the if statement.



                  if (...) 
                  ...
                  else
                  ...

                  reader1.close();


                  Even better would be to use “try-with-resource” to open the scanner, and let it close the resource for you, even if exceptions are thrown.



                  try ( Scanner reader1 = new Scanner(System.in) ) 
                  ...

                  // reader1 is automatically closed here


                  —-



                  You aren’t using this code:



                  String arg = new String[0];


                  It should be removed.






                  share|improve this answer













                  You call reader1.close() on both branches of your if statement. It would be better to do it once after the if statement.



                  if (...) 
                  ...
                  else
                  ...

                  reader1.close();


                  Even better would be to use “try-with-resource” to open the scanner, and let it close the resource for you, even if exceptions are thrown.



                  try ( Scanner reader1 = new Scanner(System.in) ) 
                  ...

                  // reader1 is automatically closed here


                  —-



                  You aren’t using this code:



                  String arg = new String[0];


                  It should be removed.







                  share|improve this answer













                  share|improve this answer



                  share|improve this answer











                  answered Jun 13 at 5:28









                  AJNeufeld

                  1,378312




                  1,378312






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f196240%2fhypotenuse-and-name-generator%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      Python Lists

                      Aion

                      JavaScript Array Iteration Methods