Hypotenuse and name generator

Clash 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 + ".");
java beginner
add a comment |Â
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 + ".");
java beginner
@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
add a comment |Â
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 + ".");
java beginner
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 + ".");
java beginner
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
add a comment |Â
@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
add a comment |Â
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
add a comment |Â
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.
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered Jun 13 at 1:54
Zachary Rudzik
1246
1246
add a comment |Â
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
answered Jun 13 at 5:28
AJNeufeld
1,378312
1,378312
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
@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