Introduction: Intro to Java Programming
So if you have wandered onto this instructable, you are probably looking to learn how to program in java, or you just want to learn how to better understand how your computer/smartphone/tablet works. Java is a programming language that is compatible with almost every computing device in the world. Java was first produced at the very beginning of the widespread use of the internet, as it was designed to be used on the internet. This means that most dynamic websites that your use will usually contain both HTML and Java. So that means that Java code can be run as an applet, and integrated with a website. By learning Java you will be able to make web apps and draw graphics while learning how computers and the internet work.
Step 1: Downloads
Before you even think about programming with Java you will need to download a few pieces of software:
Java JDK - This is what makes your computer able to create compile and run your Java code
Eclipse IDE - What I've found to be the best development environment for Java
After you have installed these pieces of software, you will be able to start coding in Java
Java JDK - This is what makes your computer able to create compile and run your Java code
Eclipse IDE - What I've found to be the best development environment for Java
After you have installed these pieces of software, you will be able to start coding in Java
Step 2: Methods
In Java code is written in methods, so if you need to repeat code you can just call the method again. An example of this follows:
public void runner(){
integer();
}
public void integer(){
int x = 3
int b = 3 * x
int y = x/b
}
Methods are useful because they allow you to make your code shorter and more efficient, methods are always started and finished with curly braces { }. When you add a new method you should always make sure that you have indented the ending curly brace at the same indent as the beginning of the method
public void runner(){
integer();
}
public void integer(){
int x = 3
int b = 3 * x
int y = x/b
}
Methods are useful because they allow you to make your code shorter and more efficient, methods are always started and finished with curly braces { }. When you add a new method you should always make sure that you have indented the ending curly brace at the same indent as the beginning of the method
Step 3: Hello, World!
So now we get to start writing some Java code. The first thing that we are going to is learn how to print something in the console, I will explain everything in my comments, while in Java you can comment by putting // for a one line comment, or if you want to do a multi-line comment you start it with /* and finish it with */ . When I type System.out.println(" "); it will print what ever is in the parentheses and skip to the next line.
public class runner {//most beginners start by calling the first class runner
public static void main(String[] args) {// this is put in automatically by eclipse
System.out.println("Hello, World!");//this line will print out whatever is in between the quotes
}// you must always put code in between curly braces
}// note the different indents for each curly brace and each line of code
The output will look like this:
Hello, World!
So the first method is by default public static void main(String[] args){ this is where you will list the methods you create later in your code. After the first closing brace is where you can put new methods.
public class runner {//most beginners start by calling the first class runner
public static void main(String[] args) {// this is put in automatically by eclipse
System.out.println("Hello, World!");//this line will print out whatever is in between the quotes
}// you must always put code in between curly braces
}// note the different indents for each curly brace and each line of code
The output will look like this:
Hello, World!
So the first method is by default public static void main(String[] args){ this is where you will list the methods you create later in your code. After the first closing brace is where you can put new methods.
Step 4: Ints, Doubles, and Strings
So before we go any further, I must teach you about ints, doubles, and strings. A string is a group of alphanumeric characters that can be printed on the console or placed in a graphics window. An int is a variable that can have a value from 0 to 255 but cannot have a decimal, this can be used to change the colour of something in an animation or graphics, or can be used to compare two things. A double is a number that has a decimal point, and has a maximum value of 1.7976931348623157 * 10^308, doubles can for comparing things like dollar amounts that more than likely will have at least one decimal value.
Step 5: I/O
It's easy to write stuff to the screen in Java. As you learned in the previous step, all you have to do is:
System.out.println("This is the stuff I want to print to the screen!");
A Scanner is a useful piece of code that allows a computer to accept input from a user, a scanner can be used for strings, ints, and doubles. A scanner that is designed to look for a string, an int and a double will look like this:
Scanner reader = new Scanner(System.in);// this allows your code to accept input from the user
String name;// a string is an input of letters
int age;// an int is a number that is from 0 to 255 and does not contain a decimal
double hourlyWage;// a double is a number that contains a decimal value
To use the above scanner in a useful fashion it makes sense to print a question that a the user can answer with the input that the scanner is expecting, so it would look something like this:
System.out.print("What is your name? ");
So after you write the question you must tell the scanner that it must be ready to accept input from the user:
name = reader.nextLine();
After that you can just repeat the code for the other variables:
System.out.print("How old are you? ");
age = reader.nextInt();
System.out.print("How much do you make per hour? ");
hourlyWage = reader.nextDouble();
Next you can print the answers to the questions in paragraph form. To do this you must use println, like in the previous steps, except
when you print a variable you must use the closing quotes and place a + before each variable, and if you want to add text after the variable you must place another +. This is what it should look like:
System.out.println();
System.out.println("Your name is " + name);// when you type a + it will print the result of your input from earlier
System.out.println("You are " + age + " years old");// note the + after age because you want to something to print after the int
System.out.println("You make $" + hourlyWage + " per hour");
System.out.println("Have a nice day!");
So this is what the full code will look like when you are finished:
import java.util.*;
public class runner {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);// this allows your code to accept input from the user
String name;// a string is an input of letters
int age;// an int is a number that is from 0 to 255 and does not contain a decimal
double hourlyWage;// a double is a number that contains a decimal value
System.out.print("What is your name? ");//note that there is no need to type println because the scanner does it for you
name = reader.nextLine();
System.out.print("How old are you? ");
age = reader.nextInt();
System.out.print("How much do you make per hour? ");
hourlyWage = reader.nextDouble();
System.out.println();
System.out.println("Your name is " + name);// when you type a + it will print the result of your input from earlier
System.out.println("You are " + age + " years old");// note the + after age because you want to something to print after the int
System.out.println("You make $" + hourlyWage + " per hour");
System.out.println("Have a nice day!");
}
}
System.out.println("This is the stuff I want to print to the screen!");
A Scanner is a useful piece of code that allows a computer to accept input from a user, a scanner can be used for strings, ints, and doubles. A scanner that is designed to look for a string, an int and a double will look like this:
Scanner reader = new Scanner(System.in);// this allows your code to accept input from the user
String name;// a string is an input of letters
int age;// an int is a number that is from 0 to 255 and does not contain a decimal
double hourlyWage;// a double is a number that contains a decimal value
To use the above scanner in a useful fashion it makes sense to print a question that a the user can answer with the input that the scanner is expecting, so it would look something like this:
System.out.print("What is your name? ");
So after you write the question you must tell the scanner that it must be ready to accept input from the user:
name = reader.nextLine();
After that you can just repeat the code for the other variables:
System.out.print("How old are you? ");
age = reader.nextInt();
System.out.print("How much do you make per hour? ");
hourlyWage = reader.nextDouble();
Next you can print the answers to the questions in paragraph form. To do this you must use println, like in the previous steps, except
when you print a variable you must use the closing quotes and place a + before each variable, and if you want to add text after the variable you must place another +. This is what it should look like:
System.out.println();
System.out.println("Your name is " + name);// when you type a + it will print the result of your input from earlier
System.out.println("You are " + age + " years old");// note the + after age because you want to something to print after the int
System.out.println("You make $" + hourlyWage + " per hour");
System.out.println("Have a nice day!");
So this is what the full code will look like when you are finished:
import java.util.*;
public class runner {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);// this allows your code to accept input from the user
String name;// a string is an input of letters
int age;// an int is a number that is from 0 to 255 and does not contain a decimal
double hourlyWage;// a double is a number that contains a decimal value
System.out.print("What is your name? ");//note that there is no need to type println because the scanner does it for you
name = reader.nextLine();
System.out.print("How old are you? ");
age = reader.nextInt();
System.out.print("How much do you make per hour? ");
hourlyWage = reader.nextDouble();
System.out.println();
System.out.println("Your name is " + name);// when you type a + it will print the result of your input from earlier
System.out.println("You are " + age + " years old");// note the + after age because you want to something to print after the int
System.out.println("You make $" + hourlyWage + " per hour");
System.out.println("Have a nice day!");
}
}
Step 6: Loops
So now we are going to learn some loops, the main ones are while, if, for, an do loops. A while loop will run an infinite loop while a certain condition is true. An if statement will run code once if a certain condition is true, but will only run once. A for loop runs exactly like a while loop, but is shorter to write. A do loop is a loop that runs once, then it checks if it meets a necessary condition, and if it does it will run again, but if it doesn't it will run the next line of code that you have. Here are some examples:
While loop:
int i = 20;
while (i < 25) { // we want to print 5 numbers
System.out.print(i + " ");
i++;
}
System.out.println();
If statement:
if( a <= b){
System.out.println("a is smaller or equal to b");
}else{
System.out.println("a is bigger than b");
}
For loop:
for (int i = 20; i < 25; i++) {
System.out.print(i + " ");
}
System.out.println();
Do loop:
do
{
System.out.println("i is : " + i);
i++;
}while(i < 5);
While loop:
int i = 20;
while (i < 25) { // we want to print 5 numbers
System.out.print(i + " ");
i++;
}
System.out.println();
If statement:
if( a <= b){
System.out.println("a is smaller or equal to b");
}else{
System.out.println("a is bigger than b");
}
For loop:
for (int i = 20; i < 25; i++) {
System.out.print(i + " ");
}
System.out.println();
Do loop:
do
{
System.out.println("i is : " + i);
i++;
}while(i < 5);
Step 7: Graphics
So now that you have a basic understanding of loops we can start having fun with graphics. To start with you need to use a library(those funny looking things at the top of your code) to create a window, then you need to start to draw different shapes with , different commands, note that when you create a graphics window the origin of the coordinate plane will start at the top left corner of the screen.
When you draw in Java, you must use the command g.draw or g.fill followed by either Line, Rect, Oval or Poly to draw a line rectangle oval or a custom polygon respectively, which would look something like this:
g.drawLine(20, 30, 40, 50);//draws a line, note that where you draw the is based on the coordinate that you input
g.fillRect(20, 30, 40, 50);// draw a rectangle that is filled
g.drawRect(20, 30, 40, 50);// when you type draw instead of fill it will just draw an outline of the shape
g.fillOval(20, 30, 40, 50);//draws an oval
If you want to make your own polygon then you must write this code:
Polygon poly = new Polygon();
poly.addPoint(50, 50);//each of these lines are a new point on your polygon
poly.addPoint(75, 75);
poly.addPoint(75, 100);
poly.addPoint(25, 100);
poly.addPoint(25, 75);
g.fillPolygon(poly);
Now if you want to add text you can simply draw it like everything else:
g.drawString("Hi there!", 40, 50);
Now if you type g.fill it will default to colour it black so to change the colour you must type the following:
g.setColor(Color.red);
or if you want to make your own colour you can change the int value of red green and blue like this:
int red=100, green=0, blue=255;
Color color = new Color(red, green, blue);
you can find a colour chart here
This code will draw a line, some shapes, a string, and colour the background, try to guess what it will look like before you run it on your computer:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
public class MyFrame extends JFrame {
/*
* Constructor
*
* sets up the window when it is created
*/
public MyFrame() {
super("Graphics Window");
Container container = getContentPane();
// if you want a bigger window, change the numbers
setSize(300, 200);
setVisible(true);
}
/*
* paint
*
* performs the drawing of the window
*/
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.red);
g.fillRect(50, 50, 200, 100);
g.setColor(Color.black);
g.drawLine(50, 50, 250, 150);
g.setColor(Color.blue);
g.fillOval(60, 90, 30, 30);
g.setColor(Color.yellow);
Polygon poly = new Polygon();
poly.addPoint(220, 70);
poly.addPoint(240, 90);
poly.addPoint(200, 90);
g.fillPolygon(poly);
g.setColor(Color.darkGray);
g.drawString("Smile!", 130, 170);
}
/**
* main
*
* creates the window
*/
public static void main(String[] args) {
MyFrame frame = new MyFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Now we will w
When you draw in Java, you must use the command g.draw or g.fill followed by either Line, Rect, Oval or Poly to draw a line rectangle oval or a custom polygon respectively, which would look something like this:
g.drawLine(20, 30, 40, 50);//draws a line, note that where you draw the is based on the coordinate that you input
g.fillRect(20, 30, 40, 50);// draw a rectangle that is filled
g.drawRect(20, 30, 40, 50);// when you type draw instead of fill it will just draw an outline of the shape
g.fillOval(20, 30, 40, 50);//draws an oval
If you want to make your own polygon then you must write this code:
Polygon poly = new Polygon();
poly.addPoint(50, 50);//each of these lines are a new point on your polygon
poly.addPoint(75, 75);
poly.addPoint(75, 100);
poly.addPoint(25, 100);
poly.addPoint(25, 75);
g.fillPolygon(poly);
Now if you want to add text you can simply draw it like everything else:
g.drawString("Hi there!", 40, 50);
Now if you type g.fill it will default to colour it black so to change the colour you must type the following:
g.setColor(Color.red);
or if you want to make your own colour you can change the int value of red green and blue like this:
int red=100, green=0, blue=255;
Color color = new Color(red, green, blue);
you can find a colour chart here
This code will draw a line, some shapes, a string, and colour the background, try to guess what it will look like before you run it on your computer:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
public class MyFrame extends JFrame {
/*
* Constructor
*
* sets up the window when it is created
*/
public MyFrame() {
super("Graphics Window");
Container container = getContentPane();
// if you want a bigger window, change the numbers
setSize(300, 200);
setVisible(true);
}
/*
* paint
*
* performs the drawing of the window
*/
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.red);
g.fillRect(50, 50, 200, 100);
g.setColor(Color.black);
g.drawLine(50, 50, 250, 150);
g.setColor(Color.blue);
g.fillOval(60, 90, 30, 30);
g.setColor(Color.yellow);
Polygon poly = new Polygon();
poly.addPoint(220, 70);
poly.addPoint(240, 90);
poly.addPoint(200, 90);
g.fillPolygon(poly);
g.setColor(Color.darkGray);
g.drawString("Smile!", 130, 170);
}
/**
* main
*
* creates the window
*/
public static void main(String[] args) {
MyFrame frame = new MyFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Now we will w
Step 8: Closing Statements
So that's some of the basics of writing code in Java, you can apply these lessons towards using Java in more advanced programs and even different programming languages by just changing the syntax a little. If you have any questions comment below or send me a private message, if I get enough positive feed back I will make a more advanced instructable, make sure to vote for me in the teacher contest and the mad science fair contest, we could really use those prizes at my school. Thanks!