Sinisterly
[Java] My First Program - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Java, JVM, & JRE (https://sinister.ly/Forum-Java-JVM-JRE)
+--- Thread: [Java] My First Program (/Thread-Java-My-First-Program)

Pages: 1 2 3


[Java] My First Program - LightX - 02-21-2013

Hey HackCommunity!
As some of you may know, I'm learning java. I just wanted to post my progress and see what you guys think. I have been getting help from a couple people. Especially one who has helped me come up with these programs to make (you know who you are) Smile Anyways, just wanted to post these SUPER simple programs to use as an example for those in my skill level area, and for others to critique. Anyways, here they are.

This first one is a calculator:
Code:
import java.util.Scanner; public class calculator { public static void main(String args[]) { Scanner lightx = new Scanner(System.in); double fnum, snum, answer = 0, operation; System.out.println("Enter your first number: "); fnum = lightx.nextDouble(); System.out.println("Enter your second number: "); snum = lightx.nextDouble(); System.out.println("What operation would you like to use? (+ = 1, - = 2, * = 3, / = 4)"); operation = lightx.nextDouble(); if (operation == 1){ answer = fnum + snum; } if (operation == 2){ answer = fnum - snum; } if (operation == 3){ answer = fnum * snum; } if (operation == 4){ answer = fnum / snum; } System.out.println(answer); } }

This next one is just a simple program that counts from 0 to the number that you tell it to:
Code:
import java.util.Scanner; public class LightX { public static void main(String args[]){ Scanner number = new Scanner(System.in); int lightx = 0; System.out.println("What would you like the number to end on?"); int num = number.nextInt(); while (lightx <= num){ System.out.println(lightx); lightx++; } } }

I also have one for just evens, which is just the same code but with one more variable++ (lightx++):
Code:
import java.util.Scanner; public class LightX { public static void main(String args[]){ Scanner number = new Scanner(System.in); int lightx = 0; System.out.println("What would you like the number to end on?"); int num = number.nextInt(); while (lightx <= num){ System.out.println(lightx); lightx++; lightx++; } } }



[Java] My First Program - LightX - 02-21-2013

Hey HackCommunity!
As some of you may know, I'm learning java. I just wanted to post my progress and see what you guys think. I have been getting help from a couple people. Especially one who has helped me come up with these programs to make (you know who you are) Smile Anyways, just wanted to post these SUPER simple programs to use as an example for those in my skill level area, and for others to critique. Anyways, here they are.

This first one is a calculator:
Code:
import java.util.Scanner; public class calculator { public static void main(String args[]) { Scanner lightx = new Scanner(System.in); double fnum, snum, answer = 0, operation; System.out.println("Enter your first number: "); fnum = lightx.nextDouble(); System.out.println("Enter your second number: "); snum = lightx.nextDouble(); System.out.println("What operation would you like to use? (+ = 1, - = 2, * = 3, / = 4)"); operation = lightx.nextDouble(); if (operation == 1){ answer = fnum + snum; } if (operation == 2){ answer = fnum - snum; } if (operation == 3){ answer = fnum * snum; } if (operation == 4){ answer = fnum / snum; } System.out.println(answer); } }

This next one is just a simple program that counts from 0 to the number that you tell it to:
Code:
import java.util.Scanner; public class LightX { public static void main(String args[]){ Scanner number = new Scanner(System.in); int lightx = 0; System.out.println("What would you like the number to end on?"); int num = number.nextInt(); while (lightx <= num){ System.out.println(lightx); lightx++; } } }

I also have one for just evens, which is just the same code but with one more variable++ (lightx++):
Code:
import java.util.Scanner; public class LightX { public static void main(String args[]){ Scanner number = new Scanner(System.in); int lightx = 0; System.out.println("What would you like the number to end on?"); int num = number.nextInt(); while (lightx <= num){ System.out.println(lightx); lightx++; lightx++; } } }



RE: [Java] My First Program - Solixious - 02-22-2013

You could put an error message for invalid operation input in your first code.

In your last code,
Code:
lightx++; lightx++;
Replace that with
Code:
lightx=lightx+2;
or
Code:
lightx+=2;

It looks neater.

Make a habit of starting class names with capital letter, and giving class names that could be related to the content inside. Not some random class name.

Keep coding and I hope you become a good coder one day.
Cheers Smile


RE: [Java] My First Program - Solixious - 02-22-2013

You could put an error message for invalid operation input in your first code.

In your last code,
Code:
lightx++; lightx++;
Replace that with
Code:
lightx=lightx+2;
or
Code:
lightx+=2;

It looks neater.

Make a habit of starting class names with capital letter, and giving class names that could be related to the content inside. Not some random class name.

Keep coding and I hope you become a good coder one day.
Cheers Smile


RE: [Java] My First Program - Deque - 02-22-2013

Aw, TheNewBoston!

If you want to do yourself (and everyone who will maybe work with your code) a favour, you shouldn't learn from TheNewBoston videos. You will just learn shit that takes even longer to get rid of. He has no clue about Java, the code is bad and the things he says are often just plain wrong. I can give you lots of examples for every video that I have seen so far. There are so many people using his videos to learn Java, because he is funny and so on, but he does more harm than good.

First off it is very good that you didn't name your class "apples", because this name has nothing to do with the purpose of your program.
Nevertheless your classname needs correction. Every class name starts with an uppercase letter, the rest is camel case (i.e. CamelCase). There are Java code conventions I would like you to read briefly: http://www.oracle.com/technetwork/java/codeconv-138413.html

I can't say the same about the variable name lightx for the Scanner and the classname LightX later on. Why the hell would it make sense to name it like that?

Code:
double fnum, snum, answer = 0, operation;

This doesn't comply with the code conventions.
Put every declaration on a new line and use proper names. The operation shouldn't be a double. Your choices given are integers and the user input is always a String, never a number.

Things like:
Code:
fnum = lightx.nextDouble();
For a system.in scanner will produce exceptions if the user enters something that is not a number and the program breaks. You should get the console input with scanner.nextLine() instead and compare the strings or try to convert the strings to numbers and act with an appropriate output to the user if it isn't convertable (i.e. "Enter a number, you douche!").

And why not input and compare the operator (/,+,-,*), instead of a magic operation number?
Everyone will have to guess what the values 1,2,3,4 mean in that context (or look for the explanation string given to the user, which becomes tedious in bigger programs), but /,+,-,* are immediately clear.

Code:
while (lightx <= num){ System.out.println(lightx); lightx++; }

This cries for a for-loop.

That's it for now, I have to go, but I might add some comments about thenewboston later.

PS: It is great that you do some coding. I hope I didn't discourage you. Don't be, it is TheNewBoston's fault. Wink
You will go your way and get better soon.


RE: [Java] My First Program - Deque - 02-22-2013

Aw, TheNewBoston!

If you want to do yourself (and everyone who will maybe work with your code) a favour, you shouldn't learn from TheNewBoston videos. You will just learn shit that takes even longer to get rid of. He has no clue about Java, the code is bad and the things he says are often just plain wrong. I can give you lots of examples for every video that I have seen so far. There are so many people using his videos to learn Java, because he is funny and so on, but he does more harm than good.

First off it is very good that you didn't name your class "apples", because this name has nothing to do with the purpose of your program.
Nevertheless your classname needs correction. Every class name starts with an uppercase letter, the rest is camel case (i.e. CamelCase). There are Java code conventions I would like you to read briefly: http://www.oracle.com/technetwork/java/codeconv-138413.html

I can't say the same about the variable name lightx for the Scanner and the classname LightX later on. Why the hell would it make sense to name it like that?

Code:
double fnum, snum, answer = 0, operation;

This doesn't comply with the code conventions.
Put every declaration on a new line and use proper names. The operation shouldn't be a double. Your choices given are integers and the user input is always a String, never a number.

Things like:
Code:
fnum = lightx.nextDouble();
For a system.in scanner will produce exceptions if the user enters something that is not a number and the program breaks. You should get the console input with scanner.nextLine() instead and compare the strings or try to convert the strings to numbers and act with an appropriate output to the user if it isn't convertable (i.e. "Enter a number, you douche!").

And why not input and compare the operator (/,+,-,*), instead of a magic operation number?
Everyone will have to guess what the values 1,2,3,4 mean in that context (or look for the explanation string given to the user, which becomes tedious in bigger programs), but /,+,-,* are immediately clear.

Code:
while (lightx <= num){ System.out.println(lightx); lightx++; }

This cries for a for-loop.

That's it for now, I have to go, but I might add some comments about thenewboston later.

PS: It is great that you do some coding. I hope I didn't discourage you. Don't be, it is TheNewBoston's fault. Wink
You will go your way and get better soon.


RE: [Java] My First Program - Anima Templi - 02-22-2013

Nothing more to say, Deque explained it way better than I would have done it.


RE: [Java] My First Program - Anima Templi - 02-22-2013

Nothing more to say, Deque explained it way better than I would have done it.


RE: [Java] My First Program - LightX - 02-22-2013

@Deque - I didn't learn any type of variables except doubles when i coded those. And I used the while loop cuz thats what I was told to use. I didn't know that about thenewboston...I thought he did know what he was talking about, but I didn't just use his code, I coded it myself and used the name of the variables he showed and that sort of stuff. I did his as an example, but I didn't use his code for those if he does even have them.


I really don't feel like starting all over again (this would be my second time starting over) so im just gonna accept im not supposed to be coding I guess. Its just not my thing. :p


RE: [Java] My First Program - LightX - 02-22-2013

@Deque - I didn't learn any type of variables except doubles when i coded those. And I used the while loop cuz thats what I was told to use. I didn't know that about thenewboston...I thought he did know what he was talking about, but I didn't just use his code, I coded it myself and used the name of the variables he showed and that sort of stuff. I did his as an example, but I didn't use his code for those if he does even have them.


I really don't feel like starting all over again (this would be my second time starting over) so im just gonna accept im not supposed to be coding I guess. Its just not my thing. :p