Introduction: How to Write Useful Programs in JAVA

Picture of How to Write Useful Programs in JAVA

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.

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!

Picture of 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/
-(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]

check out the link to see what I'm talking about it's step seven

Step 2: Humble Beginnings

Picture of Humble Beginnings

let's do a little of the basics first
ok, so you've opened up the word processor,
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!

Picture of 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.

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

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:

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

very similar to Windows
open up
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
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

Picture of Make It Say Something

ok, so you already have the code

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

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.";


let's break it down!
A string is any line of text
this is a variable: it can vary
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
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

Picture of 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.";

float a;
float b;

Scanner myScanner = new Scanner(;

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);

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;
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(;
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( means it's a NEW object
it's a Scanner object
and it's taking input from (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

Picture of Let's See It in Action

it should go something like this (see picture)
ignore the /n parts
that's my bad


Picture of 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
help, criticism, anything really, is greatly appreciated
feel free to say whatever you want, and I'll change this as soon as possible

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!

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).


admin (author)2009-02-24

Hey, this is a great instructable and is very informative. Just one thing is missing... pictures! It really helps a lot when trying to follow directions so you should consider taking some photographs. Once you do that and leave me a message when you have so that we can publish your work. Thanks! Thanks for the cool instructable and we hope to publish this soon!

EliyahuGeorgios (author)admin2015-11-21

he is corrct!!!!!!!!!

The_Vinninator (author)admin2009-02-24

sorry about that, I should've thought of that ahead of time is this any better? o, should I hit publish again?

nevermind, it apparently worked

Daniel Ariba (author)2012-12-15

hey this was a great tutorial from u- and i think u did a great job i'm no-beginner in java but i love great intros like this- love the analogy of a class as a recipe (priceless). the bottomline is most manuals bore the heck out of someone but simple flows like yours makes it engaging and relatable. Good job!

axeman911 (author)2011-12-17

Best tutorial ever!!!!!!

dowd95 (author)2009-07-23

i gots a problem already my command says 'javac' is not recognized as an internal or external

The_Vinninator (author)dowd952009-07-23

that means you don't actually have java installed
it's a simple mistake that I've made countless times on all the computers I've worked on
follow the instructions from this link and you'll be all set

dowd95 (author)2009-07-23

i am trying to get started in java and u have inspired me with that amaaaazing intro paragraph i actually laughed time to read...

Matruskan (author)2009-07-13


Try the BlueJ Editor for coding:
It shows classes as blocks, to emphasize the "Object Oriented Programming", and it's very easy to use.

And bookmark these URLs:
The first one has a list of all the classes, interfaces, etc that you might need.
The second one has many useful Java tutorials.

tinkerC (author)2009-07-12

I program in C, and am learning Java. The only thing Sun Micro-Systems didn't include was short names for the library functions.

pharoah (author)2009-03-14

One thing, you shouldn't write programs in "pretty much any word processor". Programs should be written in an IDE or text editor. Text editors (like notepad, but NOT like wordpad, word, etc...) produce plain text files. Word processors like m$ word and OpenOffice writer produce files full of formatting commands that no compiler or interpreter (as far as I know) will be able to understand.

The_Vinninator (author)pharoah2009-03-14

you can write them in pretty much any word processor though when you hit "Save As" there's almost ALWAYS a list that allows you to choose your extension this is true for BOTH Wordpad and Word and I've used both to program before

KennetFlcn (author)2009-03-04

OK in the command line i typed in C:\Java\javac and it just tells me that 'javac' isn't an external or internal command. i don't know how to do it i tried it the way you typed on the 'for windows' and it doesn't work

it works like this C:/directory/subdirectory>javac make sure you know EXACTLY where it's saved AND use the little > thing

if it still doesn't work, I should've told you to make sure java is installed

whatever thanks anyways for the additional help your instrucatble is great keep at it

don't give up dude! this has everything you need download java 6 and I'll help you from there

KennetFlcn (author)2009-03-03

o great and what word processer should i use i tried notepad the rectangle didnt work there? thnx sorry i posted twice ihad a prob w/ the comp

I'm guessing you fixed it, looking at your next post

KennetFlcn (author)2009-03-03

uh how do you do the rectangle thingy on line 1?

it's the keys you use to make { and } without the shift

KennetFlcn (author)2009-03-03

can u please tell me how to do the rectangle shape thing class Pythagorean{public static void main(string? args){ im a noob so any additional help would be great

it's not a rectangle thing it's two keys it's the ones you use to make { and } without the shift

comodore (author)2009-02-22

I don't know much about Java, but this Instructables looks great for beginners like me or other people...well explained!!! Maybe change the front picture with a Java symbol and add more images to attract visitors and maybe get featured... All in all a great Instructables! rate:*****

The_Vinninator (author)2009-02-21

FIRST! no, just kidding but seriously, if you have questions or anything... I love helping people it makes you feel warm and fuzzy!

About This Instructable




Bio: 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 ... More »
More by The_Vinninator:How to write useful programs in JAVA
Add instructable to: