Introduction: How to Write Useful Programs in JAVA

About: I'm bored, I got time on my hands, and I love to make things. My motto has always been, "If you want something done right, buy the parts at radio-shack, grab the soldiering iron, and get to it." It's a pretty …

Are you lazy?
Are you INSANELY lazy?
Well why do one more thing, when you can make the computer do it for you???
This instructable is supposed to open the doors to your awesome computer-programming experience while YOU (yes YOU) make an application that is totally useful if you happen to be in Mr. Y's geometry class.
IT'S THE PYTHAGOREAN THEOREM APPLICATION!

disclaimer: I know a lot of you out there are a lot better at this than me. Speak to me if you think something in this instructable should be modified/deleted/changed/whatever. Any help will be greatly appreciated.
---if you are already interested, feel free to skip to the first step...
otherwise: Java is an elementary-level computer programming language. This language is preferred by many programmers for three great reasons:
1. it's object-oriented (explanation coming later)
2. it's powerful, versatile, and uses many concepts similar to C and C++
3. it's called Java! I mean, come on, who wouldn't want to program applications with something named for coffee?

And coffee you will need, as you will find this language to be extremely addictive and fun.
So fire up the word processor, turn on the Star-Trek", and get ready for the nerdiest experience in your life.

Step 1: Get Da Supplies!

You will need:
-pretty much any word processor
-a computer
-(for Mac) an application called "Terminal" located in Macintosh HD/Applications/Utilities/Terminal.app
-(for Windows) just the command line

(optional) an SDK (software development kit) like Eclipse (free, I think) [it is, however, recommended that you learn how to program both with and without an SDK: in this tutorial, we will not be using one]

EDIT: IT IS FREE!
check out the link to see what I'm talking about https://www.instructables.com/id/A-Guide-to-The-best-and-the-safest-Freeware-out-th/ it's step seven

Step 2: Humble Beginnings

let's do a little of the basics first
ok, so you've opened up the word processor,
YOU DO THE WORK IN THE WORD PROCESSOR, NOT THE COMMAND LINE ITSELF
this is what you type into the first line:

class Pythagorean {
public static void main(String[] args){
}
}

explanation: Class opens a new class. Let me explain this in some detail. A class in java is like one separate set of instructions, or a recipe. Some recipes will be self reliant (like this one). Some recipes will need other recipes to help. Let me give you an example. Say you wanted to make a guacamole recipe. You would write down, step by step, the way to make guacamole. This is one recipe or class. Then, say you wanted to make a burrito recipe that called for guacamole. You'd have two options. You could write the burrito recipe with the necessary ingredients and step to make the guacamole, OR, you could say "please reference guacamole recipe on pg. whatever". This is what java can do. With java, you could have one class (burrito) reference another class (guacamole) in it's recipe, or not... depending on your preference. That's why java is called object-oriented.

Pythagorean is the name of the class. ALL JAVA MUST BE SAVED AS [name of class].java!
the first little brackety thingy opens up the class
public static void main(String[] args){ calls the MAIN method of the class. This is a slightly advanced concept. All you need to know for now is that that's where most of the stuff goes on. Memorize that line! You'll use it often.
It's safe to say that an opening bracket opens a subdivision of the program and a closing bracket closes it. Therefore: one way to check your applications is to make sure that there are the same number of opening and closing brackets.

Ok! So now you have an application that doesn't do anything. WHOOPIE! Let's save it.
All applications must be saved in plain text.
Screenshot time.

Step 3: Let's Run It!

Why would you run an application that does absolutely nothing?
It makes for some good practice.
This is how you test your applications.

WINDOWS:
ok, open up the command line
make sure you know exactly where you saved your application
now make the command line say something like this
C:/java/samples>javac Pythagorean.java

let's break it down
the /java/samples is the directory you're working in
with computers, a /name means a folder
find out where you saved the application
for example; if you saved it in the desktop it'd be:
/desktop>javac Pythagorean.java

javac means java compile
compiling is a process that turns the application from words that you can read into a language the computer can read
this step is also where a lot of errors show up
errors can be corrected inside the original application

if NOTHING happens, then it worked!
check the folder you saved it in
there should be an additional file now that says Pythagorean.class
if there is, GOOD JOB
if there isn't, check both the application's text and the command line prompt to make sure you did everything right

FOR MACS:
very similar to Windows
open up Terminal.app
Terminal is Linux based, so if you know basic Linux, this will be a breeze
again, remember where you saved your work
a picture says a thousand words, right?
ok, look at the pretty picture

pwd stands for print working directory
the computer says I'm in /Users/vinny/Desktop/Java_Apps which is where my file is saved already, however, I change the directory to show you how to do it yourself
change directory by usind cd and then typing the directory of your choice
REMEMBER: THE FILE MUST BE SAVED IN THIS DIRECTORY
otherwise you'll get a nasty error that used to plague me in my youth
(even though I'm still young)
javac means java compile
now I jumped the gun with this one
but after the next step, if you hit java Pythagorean, text should show up
let's get on with it

Step 4: Make It Say Something

ok, so you already have the code

class Pythagorean {
public static void main(String[] args){
}
}

right?
good
now make it say
class Pythagorean {
public static void main(String[] args){

String greeting = "Hello. Welcome to the Pythagorean Theorem Application. Today we will be computing the Pythagorean Theorem for you.";

String prompt = "Please enter a number for the first leg of a right triangle, followed by pressing ENTER or RETURN, followed by the remaining leg. Then hit ENTER or RETURN one more time.";

System.out.println(greeting);
System.out.println(prompt);
}
}

let's break it down!
A string is any line of text
this is a variable: it can vary
duh
greeting and prompt are the names of the strings
so in the line
String greeting;
you are creating a string called greeting
note the semicolon
all statements in java should end in a semicolon or else ERROR
you use = to assign the value to the variable
this can be done like this:

String greeting = "Hello. Welcome to the Pythagorean Theorem Application. Today we will be computing the Pythagorean Theorem for you.";

or like this:
String greeting;
greeting = "Hello. Welcome to the Pythagorean Theorem Application. Today we will be computing the Pythagorean Theorem for you.";

in this case you are declaring and assigning the value of the variable in two steps
in the former case you are doing in one
both work equally well

all the text that you want in the variable must be put it "quotes"

the line System.out.println call for the SYSTEM OUTPUT to PRINT the LINE of the variable
if you called System.out.println("HI");
the terminal would print
HI
but note the quotes
the quotes say that what's inside is text, not the name of a variable
if you called System.out.println(HI);
the terminal would respond with an error because it thinks you wanted to print the value of the variable HI
memorize this command: you'll use it all the time

Step 5: Make It Do Something

ok, so you have that much code so far

now this might get a little complicated but pay attention:
make it say THIS:
import java.util.Scanner;
class Pythagorean {
public static void main(String[] args){

String greeting = "Hello. Welcome to the Pythagorean Theorem Application. Today we will be computing the Pythagorean Theorem for you.";

String prompt = "Please enter a number for the first leg of a right triangle, followed by pressing ENTER or RETURN, followed by the remaining leg. Then hit ENTER or RETURN one more time.";

System.out.println(greeting);
System.out.println(prompt);
float a;
float b;

Scanner myScanner = new Scanner(System.in);

a = myScanner.nextFloat();
b = myScanner.nextFloat();
float a2 = a * a;
float b2 = b * b;
double c2 = a2 + b2;
double c = Math.sqrt(c2);

System.out.println("The square of a is: " + a2 + " The square of b is: " + b2 + " The square of c is: " + c2 + " C is: " + c);
}
}

DON'T PANIC
let's break it down
so the very first line is different
it should be very obvious though
we're telling the program to IMPORT a JAVA UTILITY called SCANNER
now a scanner scans input that you type into the command line
simple enough, right?

float a;
and
float b;
are declaring variables
float calls for a floating point number
that means a number with a decimal place
like 8.987
again, fairly simple
int doesn't have a decimal point
float does
got it?

here's where things get slightly more tricky (ONLY SLIGHTLY)
let's break down the line
Scanner myScanner = new Scanner(System.in);
it works kinda like declaring a variable
"Scanner myScanner" is like saying "float a"
we're creating a SCANNER (which we imported) and naming it MYSCANNER
then we're assigning it's value with the =
new Scanner(System.in) means it's a NEW object
it's a Scanner object
and it's taking input from System.in (which is the command line)
you can make it scan docs, html, etc... but that's a little advanced

so you basically get the idea...
we're making it read what you put in

now the line
a = myScanner.nextFloat();
says that the variable a is to be assigned the NEXT FLOATING POINT NUMBER that is read by the SCANNER

that's when you put the number into the command line and hit ENTER

b = myScanner.nextFloat();
does roughly the same thing

now for the mathy bit
float a2 = a * a;
this line declares a variable called a2 and assigns it the value of the variable a times the value of the variable a
float b2 = b * b;
same drift
double c2 = a2 + b2;
this declares a DOUBLE ACCURACY NUMBER (really long decimal) and assigns it the value of a2 added to the value of b2
double c = Math.sqrt(c2);
this takes the root of c2 and puts it in a variable c

still with me?

The last line prints everything out.
Everything in quotes is printed as text
the + is used to combine output
everything not in quotes prints the value of the variable
this will make more sense on the next page

Step 6: Let's See It in Action

1. SAVE THE CODE AGAIN!
2. COMPILE THE CODE AGAIN!
3. RUN THE CODE AGAIN!
it should go something like this (see picture)
ignore the /n parts
that's my bad

Step 7: WE CAN DO MORE

Java is one of the most versatile languages ever.
However, I only have so much time on my hands.
I plan to update this instructable CONSTANTLY!
In the future, it should ask if you want more help, etc...
I'm working on this
it should be done really really shortly
anyways
help, criticism, anything really, is greatly appreciated
feel free to say whatever you want, and I'll change this as soon as possible
thanks!

Step 8: NVFAQ's

Q: How much of my computer can I control from this?
A: A lot. A whole lot. You can use JAVA to read, write, scan, upload, download, copy, paste... I could go on forever.

Q: Can I control real world appliances with this type of thing?
A: Actually, yes, but it's a difficult concept and a long and tedious one.

Q: Can I make a virus with this?
A: Don't expect any help from me.

Q: Should I learn other languages with JAVA?
A: In short, the more languages you learn, the better. If you're going to pursue a career, definitely learn JavaScript (which is different from Java), CSS, HTML, C++ (maybe) and... well, that's enough for starters... haha. If you just want to be a super-awesome haxor that can control his computer like no-one else can then definitely learn Linux. Python is also a really useful language and you can expect a tutorial on that soon.

Q: Can I make something that can open from double-clicking?
A: They're called executables and yes, though that's not really for beginners. That happens when you learn swing.

Q: Where can I go further with this?
A: My personal favorite is JAVA ALL-IN-ONE DESK REFERENCE FOR DUMMIES,but Sun Micro Systems has some pretty good tutorials free. Check em' out! http://java.sun.com

Q: Do you really plan on taking this further?
A: This application? Yes! I really enjoy going overboard with making my applications really helpful, and I'll keep you guys posted to everything that I add (which may or may not be frequent).