Results 1 to 2 of 2

Thread: Java: An Introduction

  1. #1
    Senior Member
    Join Date
    Oct 2005

    Java: An Introduction

    This is designed for those without any programming experience whatsoever. If you do have some programming skill, you may want to skip some of the exercises. This is not intended to be a treatise on java, or a "Tractus Java-cus"; only a simple introduction that will adequately allow you to program.

    This deals with the basic beginnings, then strings, variables, conditionals, and maybe math.

    It is also recommended that you do the exercises. WARNING: not doing the exercises WILL result in the following: you will have lower self-esteem, you will not be able to go to a prestigeous university, you will not marry, and you will be poor and lonely.

    In the Beginning...
    Before programming, if you do not have the Java 2.0 v 1.4 get it here. You will need the JDK ("Java Development Kit" for Creating software in Java), you can get the JDK 2 Standard Edition (version 5) here.

    Also, get JCreator from here. JCreator is what you will be using to program. If you really want to feel special, and if you have a good enough computer, use NetBeans. I don't prefer to use JCReator or NteBeans, but I'm old school in that way.

    Once you have these things, you will want to do several things. First, install it (duh). Then start programming! You will first open whichever IDE (or "Integrated Development Environment") you downloaded (JCreator or NetBeans).

    Then, you'll type in:
    class classname
         public static void main (String[] args)
    "Hold on! What does all of this mean?!?"

    Well, you first say the "class" and what the name of it is. The "public static void main (String[] args)" thing don't worry too much about. You need that if you are programming something and it is not an applet...so it is to the command line, in other words.

    You will have to do it for every program you do, with the exception of applets (which will be part 2 of my tutorial).

    Now, what I assume is that you who are reading this are blissfully unaware of any programming. If you know some programming, skip the next few paragraphs.

    What you will write when you want to print something (anything) out on the screen is simple. If you want it to take up an entire line, its a little more work (only two extra letters!). Observe the following:
    System.out.println("HOW ARE YOU?!?");
    //and this is a comment that the compiler ignores
    With the first line we have, it simply writes HELLO! It continues on the first line and writes HOW ARE YOU?!? and then creates a new line.

    Note also that the quotations are double, and should not be interrupted (with some exception, we'll examine next).

    Comments are something new and unique. It is something I use to tell anyone who reads the code. The compiler, or program which turns the code into the final product, ignores comments when compiling. They can be very useful or very annoying. An example of each is if I state what each variable is going to be used for, or if I create art using slashes, characters, and question marks. The program itself is the art, not the comments.

    So, this is all straightforward right? One last comment I should add, if you are going to open a bracket, make sure you indent afterwards for aesthetics and to guarentee when checking that you have closed the brackets. Never leave a bracket open.

    1. Write a program that says "I don't want to set the world on fire..."
    2. Write another program, with comments, that produces
    "Somebody says: 'Of no school I am part,
    Never to living master lost my heart,
    Nor any more can I be said
    To have learned anything from the dead.'
    That statement - subject to appeal -
    Means 'I'm a self-made imbecile.'"

    Strings among other things
    This section is a little more challenging: don't give up! We are introducing strings and variables.

    A string is a preset phrase. For example:
         String arkimedes = "Jack and Jill went up the hill...";
    "arkimedes" can be thought of in terms of simple algebra. It is a variable, although this may be misleading to some since a variable in algebra represents a number. In java we have different types of variables.

    We have an int variable that holds integers (whole numbers, those without decimal places) also represents a number.

    We will be using a String variable that holds words.

    If we wanted to print it out on the screen, we would simply do the following:
    Thus it prints the string arkimedes, note the lack of quotation marks.

    The lack of quotations means that it goes and looks for "arkimedes" the variable (as opposed to "arkimedes" the programmer). It literally "points" to it (and the variable is stored in RAM).

    1. Do the last section exercises with strings.

    But what if we wanted to do something with inputs? There are two ways to do it: one is the easy way with EasyReader (part of the EasyClasses for java input and output), the other is the hard way. I assume you are lazy, and I know I am lazy, so we will do it the lazy way regardless to what you want.

    It is important to have the EasyRead.java (and if you're lazy, put the rest in too) in the same folder as your code. Otherwise there will be serious problems. The initial code for introducing EasyReader should be the following:
    EasyReader console;
    console = new EasyReader();
    Now when programming, and you want to ask a question to the user of the program, you use EasyReader. Suppose you wanted to know the name of someone who is having their birthday. You would use EasyReader.

    EasyReader console;
    console = new EasyReader();
    String birthday;
    System.out.println("Who's birthday is it?");
    The program has a string that is undefined, birthday, and it is defined by the response from the user.

    Exercise: given the previous knowledge given, write a program that "sings" happy birthday to whoever is having a birthday. You can also have a challenge and ask for when they were born (what year) and calculate their age (the lazy solution is to always reply "Boy, you ARE old.").

    This can also be used to check a yes or know question, but that is rushing ahead of ourselves. We will leave strings for the time being to look at its antithetical partner in crime: the variable!

    A variable, simply put, is a number. It is exactly what it was in algebra. But there are two sorts of numbers: integers and non-integers.

    If we wanted to deal with integers only, we would define a number as such
    int hello;
    We have defined the variable "hello" to be an integer, then defined it to be 5. But what if we wanted 5.1?

    We then use a double, or a number variable which uses a decimal place.
    It is then:
    double hello;
    It can be used the same way as the string with one exception: for it uses readInt() rather than readLine().

    Observe the two:
    EasyReader console = new EasyReader();
    int hello;
    String hi;
    System.out.println("Do you wish to say anything?"); //string
    System.out.println("How old are you?!"); //integer
    Have you ever read any of the adventures of Sherlock Holmes? He is a hell of a logician! He once explained it can be reduced to "If p, then q".

    Brilliant! It is called a conditional by logicians, so that's what I am calling it.

    If we wanted to check something, we can use conditionals. Consider:
    EasyReader console = new EasyReader();
    String answer;
    System.out.println("How do you like the tutorial so far?");
         System.out.println("Geez, you sure know how to hurt a guy...");
    else if(answer == " ")
        System.out.println("Spiteful words can hurt your feelings but silence breaks your heart");
        System.out.println("You have great taste!");
    Notice the if(p==q), what this means is if p and q are identical, then execute what is within the brakets. Otherwise ignore it. If one puts "!=" that means "is not equal". In most programming languages "!" means "not". We could also use ">", ">=", "<", "<=", etc. for integers.

    The else if statement says "Well, if the first condition isn't true, but this one is execute the braket". That last "else" statement is done if the other two have not been executed.

    We can stack these together too, consider:

    EasyReader console=new EasyReader();
    String strng;
    System.out.println("Are you happy?");
                    System.out.println("Don't worry, be happy :) ");
                    System.out.println("When your smiling, the world smiles with you...");
              System.out.println("When your smiling, the world smiles with you...");
         System.out.println("When your smiling, the world smiles with you...");

    1. Create a program that permits someone to be put under the socratic method. I.e. have the program ask "What are you thinking about?", then "What if it weren't true?", "Why is it true?", "Is there any alternative? Why is this better?", etc.

    2. Create a program that asks "Who is buried in Grant's tomb?"

    Appendix on Math
    Java really isn't the best program for math, but it can do it. There are several methods which are reserved for math especially.

    I will briefly go over what does what in this table:
    a.subtract(b);  //a - b
    a.add(b);  //a + b
    a.divide(b, rounding scale);   // a/b with rounding taken into effect
    a.multiply(b);  // a times b
    Math.methods.exp(double a); // Euler's e to the power of a
    There are also reserved doubles E and PI, which represent Euler's e and Archimedes' pi respectively. You can call pi by writing "Math.PI' just as you could call e by "Math.E" in your program.

    Other than that, there is nothing to it.

    Exercises: 1. Make a calculator.

  2. #2
    Join Date
    Nov 2005
    a) lack of basic information necessary when learning java (i.e. how to compile a file, how to name a file, etc.)
    b) the structure in which the information is presented (presenting strings prior to introducing primitive data types)
    c) errors in example code (strings are not compared using operators, the equals, compareTo, and etc. methods are used)
    d) the actual information presented is missing key java syntax explanations (such as import statements, the declaration of the main method, etc.)
    e) don't teach bad techniques to those learning java for the first time. (for example when you look for the word "yes" as an answer, you check for all cases of the word when you could've simply used a toLower method and just compared to the word "yes" once rather than three times. by the way, your logic on that part would not have worked.)
    f) simply telling them to download a the sdk and an ide is not enough. explain what each does, go through the steps of adding the java bin folder to the path if it not done so... don't even force them to use an ide, explain that any text editors serves just fine...

    so simply put, don't try to educate others on subjects which you are not familiar with yourself.

    import java.awt.*; // this is an import statement, it imports a java package, in this
    		   // case java.awt into your program, thus making that package's 
    		   // objects available, such as java.awt.Frame
    public class testClass { // this is a class declaration, the class is public and 
    			 // its name is testClass, thus you must have a file named
    			 // testClass.java
    	testClass() { // this is called the constructor, it executes the code within it
    		      // whenever a new a new instance of the testClass class is created
    		int x = 5; // this declares the variable x as an integer (whole real
    			   // number) and assigns the value 5 to it.
    		x = x + 1; // this takes the value of x, adds 1 to it, and sets the
    			   // new value to x, other common operators include -,*,/,
    			   // and etc. 
    		System.out.println("the value of x is: " + x);
    			   // the above statement outputs "the value of x is 6" to
    			   // the screen. anything in quotes is taken directly as text
    			   // and is outputed as written in the quotes. the + appends 
    			   // the value of x to the end of the text
    	public static void main() {
    		// this is the main method, when interpreted, this is 
    		// where java will begin executing code
    		new testClass(); // this creates a new instance of the testClass
    				 // class and executes the code within the constructor
    this is a rough outline of how detailed you should go with your explanations, don't assume the reader knows how everything works, explain it to them, no detail is too small to skip when teaching someone something for the first time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts