Java Programming for beginners in four hoursLoirak -> Programming -> Java |
||
The Four Hour Java TutorialHave you ever wanted to master a programming language? Well, you have chosen a language which is considered by most to be the future of programming. This tutorial will give you the basics that will enable you to build powerful programs and applets.Java is a high-level programming language, or a 3rd generation language. It operates like this: you type in the source code, then a program called a "compiler" transforms it into Java bytecode. Bytecode makes Java architecture neutral, since the computer that runs your program needs an interpreter or a bytecode compiler. Now to the pre-requisite. "Aller Anfang ist schwer!" Java Developement Kit (JDK) 20+MB - this includes all of the standard objects and classes to make and run your programs. If you like running programs, download this! This file can be downloaded from the Sun Java Page (http://java.sun.com). Now to programming, in only four hours to go! Hour I. Hello Program, Orientation, and InputExample I.class Hello { public static void main (String[] args) { System.out.println("Welcome to the world of Java Programming."); } // method main } // class HelloTo compile and run this program, you need to have installed JDK and added a line to your path statement referring to the directory of where it was install + \bin. (e.g. path %path%;c:\jdk\bin;)
Example II.import java.io.*; //include Java's standard Input and Output routines class Echo { public static void main (String[] args) throws IOException { // Defines the standard input stream BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); String message; // Creates a varible called message for input System.out.print ("Enter the message : "); System.out.flush(); // empties buffer, before you input text message = stdin.readLine(); System.out.print("You "); System.out.println("entered : " + message); } // method main } // end of class EchoYou have just learned the standard way of getting text. First you create a reader, and then you input the text with readLine() , and finally you use the print command to output it. Notice the difference in the print commands : println prints, then goes to the next line, while print does not. The throws IOException in main lets Java know what to do with errors, when it encounters them. Hour II. Data types and LoopsJava has several standard(primitive) data types, this hour will cover the two most common. Since String is actually an object, it will be covered in the fourth hour.Example III.import java.io.*; class Candy { public static void main (String[] args) throws IOException { BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); int num1, num2, sum; // declares three integers double dollars; // declares a number that can have decimals System.out.print ("How many candy bars do you want : "); System.out.flush(); // read a line, and then converts it to an integer num1 = Integer.parseInt( stdin.readLine()); System.out.print ("How many suckers you do want : "); System.out.flush(); num2 = Integer.parseInt( stdin.readLine()); sum = num1 + num2; // Adds the two numbers; dollars = (double) sum * .75; System.out.println("You owe : $" + dollars); } // method main }Using (double) to convert the integer sum to a double, we can multiply by .75 to calculate the amount of money. This method of conversion using parentheses is called casting. Java has several types of loops, the most useful being the for & while loops, which are both demonstrated in this next example. The standard if-then-else, will also be a very handy programming tool. Example IV.import java.io.*; class Loopit { public static void main (String[] args) throws IOException { BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in)); int count, max, num; num = 0; // Assign initial value of count while (num != -1) { System.out.print ("Enter a number to factorialize (-1 to quit): "); System.out.flush(); num = Integer.parseInt (stdin.readLine()); max = 1; // Assign to 1, so factorial isn't zero every time if (num == -1) { System.out.println("Okay, quiting..."); } else { // Since they're not quitting we better factorialize for (count = 1; count<=num; count++) { max = count * max; } System.out.println (num+"! (factorial) is : "+ max); } } } // method main }The first loop above is called a while loop, the syntax being : � � � while (condition) { dosomething; } The program runs what is in the brackets until the condition becomes false. For instance, the above program runs until the user enters negative one. Hence, "!=" mean not equal to. Next is if, then and else. The syntax being : � � � if (condition) { dosomething; } � � � else { dosomethingdifferent; } The above program compares (==) the number entered to negative one. If they are equal it tells the user the program is quitting, otherwise it factorializes the number using the for loop. The syntax for a for loop is : � � � for (initialization; condition; increment) { dosomething; } In example four, the initialization, count = 1, assigns count to one. The increment, count++, adds one to the variable count until the condition, count<=num, becomes false. In other words, count is assigned one, two, three, ... num, with the inside of the loop being processed after each increment. Notice again that count++ increments by one; however, count+=2 would increment count by a factor of two. This is also true for count-- and count-=2. The former decreases by a factor of one, the latter by a factor of two. Hour III. AppletsNow we are going to learn how to create Applets, to include in your web pages and amaze your friends.Example V.import java.applet.Applet; // Includes standard Applet classes import java.awt.*; // Standard Graphics Routines public class Wow extends Applet { // Applets use paint instead of main public void paint (Graphics page) { page.drawString("Wow, this is an Applet!", 5, 10); } // method paint } // class WowThe page.drawString statement is like the print command. Note that it also accepts the coordinates for the text. In the above example, the text is placed five pixels right and ten down from the top left corner of the Applet. To actually use this Applet :
Applets is a topic that could easily be the subject matter of an entire book,
to learn more visit :
|
|
|||||