learning Java
 

To learning Java, you have to know the structure of Java programs. Java programs are made up of words and symbols. There are also keywords that mean special things. You can think of them as a command, that you are telling your program to do something.


There is a special line that all of your JUDO Java programs will begin with; it looks like this:


void main() {


 


Your program must also always end with a special line:


}


 


The the word main means that all the lines following are the main part of your program, and the lines must all be inside the the curly braces ( { and } ).


What you do inside of the main is what makes your program interesting. There are lots of structures and keywords and code that you can put in there, and you will be learning about many of them in the following parts of this tutorial.


Below is a preview program so you have an idea what a Java program looks like. It multiplies two numbers together and prints them out to the user.


void main() {


   int product;


   product = 3 * 4;


   printLine("Three times Four is: " + product);


}


 You can use all the common math operations in your programs. The symbols you use to do the operations are:


+          addition


-           subtraction


*          multiplication


/           division


In the next two parts of the tutorial, you will learn about the most important and basic parts of programming: Data Types and Values, Variables, Declarations, and Assignments.



A data type, as I'm sure you could guess from the name, is a type of data or information. When you write JUDO programs, you will be dealing with five different types of data, or data types. They are Integers, Doubles, Strings, Colors, and Booleans.


You use Integers and Doubles when you are dealing with number data. You use Strings when you are dealing with words or letter data. You use Booleans when you are dealing with truth values, and you use Colors when you are dealing with different colors.


In the below list of data types and their explanations, the word to the left of the dash (-) is the name of the data type, and the word to the left of the dash is the keyword you use when referring to that data type in a Java program. For example, in a Java program, you refer to Integers as int.


Integers - int
Used to store integer numbers. That is, positive and negative numbers without a decimal point.


    Some possible values: -1000000   -499   -12   -1   0   1   3   25   300   321431211


Doubles - double
Used to store floating-point numbers. This is, positive and negative numbers with a decimal point.


    Some possible values: -30005.3   -271.234   -3.0   -1.1   -0.321   0.0   3.14159   7202.111


Strings - String
Used to store one or more characters together. Things like words, letters, symbols, anything on a keyboard (including numbers).


    Some possible values: "Tom Dunn"   "Compaq 321"   "x"   "Ref. Id #3221"   "12:30PM"


Colors - Color
Used to store color values. Things like background color, color of paint, color of a circle.


    Some possible values: red   violet   yellow   pink   black   white


Booleans - boolean
Used to store "truth" values. Things like if checkbox is checked or not or answer to a question.


    Only possible values: true   false



Variables, Values, declarations, and assignments are building blocks of computer programming.


Variables: In a computer program, a variable is a word that can hold a value. It is made up of letters and numbers.


Examples of Variables
name
tomsAge
candybarPrice
skyIsBlue
shirtColor


Values: A value is a piece of data or information. You store values in variables.


Examples of values
"Tom Dunn"
22
0.65
true
green


Declarations: In a computer program, you must declare a variable before you can do anything with it. A declaration starts with a data type, then the variable, then a semicolon (;)


Examples of Declarations
String name;
int tomsAge;
double candybarPrice;
boolean skyIsBlue;
Color shirtColor


Assignments: In a computer program, you use an assignment to give a variable a value. An assignment starts with a variable name, then an equals sign (=), then a value, then a semicolon (;)


Examples of Assignments
name = "Tom Dunn";
tomsAge = 22;
candybarPrice = 0.65;
skyIsBlue = true;
shirtColor = green;



raphical programs are Fun!


Writing a program with graphics means that it puts shapes and colors on the screen. You could write a program that draws a clown face, or a pattern of colorful rectangles. Or you could write a program that bounces a ball around the screen.


If you put the command fillCircle(50, 70, 40); in a program, it will draw a circle 50 points (or pixels) away from the left edge of the screen, 70 pixels down from the top of the screen, and the circle will have a radius of 40 pixels.


If you set the color before you call drawCircle using the command setColor(purple); it will draw a purple circle.


As you can see from the example, you specify where a shape should go by specifying how far over from the left edge of the screen and how far down from the top edge of the screen it is.


You can also have your program set what the color of its background should be by using the command setBackgroundColor. If you want to set the background to yellow, put the call setBackgroundColor(yellow); in your program.


The difference between drawCircle and fillCircle, and between any of the draw and fill functions, is that draw just paints an outline of the shape while fill fills the whole shape in.


Below is a sample program using what we have learned. Type it into JUDO and compare what pops up on the screen with what your code says.

void main() {
   setBackgroundColor(yellow);
 
   setColor(purple);
   fillCircle(50, 70, 40);
 
   setColor(red);
   drawRectangle(60, 90, 70, 290);
 
   setColor(pink);
   fillRectangle(70, 250, 250, 100);
}

The above program produces this output:


If you use graphics functions in a program, be sure to set the Program Properties to use either a "Graphics" Window Style, or a "Text and Graphics" Window Style before you run the program.


Something you may want to do is add delays to your program. Say in between drawing a couple of lines or rectangles you want your program to wait for half a second, you can call the delay(0.5). When you call delay you should use a double value representing how many seconds you want the program to wait before going on.



All computer programs have some way to give and/or receive information from the user. When you type a paper in a word processor or enter numbers in a spreadsheet, you are giving the program information.


When you go to a website using a program called a web browser, the data and information is the text and images of the webpage.


Output is when a program gives the user data or information.
Input is when a program receives data or information from the user.


In JUDO, you give the user output by using the following functions:
print()
printLine()


To receive input from the user, use these functions:
readString()
readInt()
readDouble()
readBoolean()
readColor()


Here is an example programs using some of the above functions:

void main() {
   String name;
   int age;
   
   printLine("Type your name and press enter");
   name = readString();
   
   printLine("Hello " + name + ".  Now enter your age");
   age = readInt();
   
   printLine(name + " you are " + age + " years old");
}


readString() is used to assign a String value to a String variable.
readInt() is used to assign an int value to an int variable.


printLine() prints the value or variable you put between the parenthesis and goes onto a new line
print() prints the value or variable you put between the parenthesis and does not go onto a new line.


In the program above, you can see that Strings can be joined with String variables or other types of variables to make a longer string by using a plus sign (+). This is useful when you want to print something out to the user that includes the value of a variable.


Making a graphics program like you have already seen how to do is another example of a program that does output. The output is the shapes and colors that you display to the user.

(c) Shilpa Sayura Foundation 2006-2017