Ethiopia 2004 | MIT | MIT-AITI

Java Lab 0: Hello World

  1. Create a folder on your network drive to store the work you will do in this class. The name of the folder should have no spaces. We highly recommend you call it Java.
  2. Go to Start Menu · Programs · Accessories and click on Notepad. This opens up Notepad which is the editor we will use to write your source code for now. Type this code below into Notepad:
    public class HelloWorld { 
        public static void main(String[] args) {
            System.out.println("Hello, World!"); 
        } 
    }
    
  3. Save the file as HelloWorld.java in the Java directory you created in step 1. Browse the directory with Explorer and make sure that the file ends with .java. If it ends with .java.txt then delete the .txt part.
  4. Go to Start Menu · Programs · Accessories and click on Command Prompt or MSDOS Prompt. This is the command prompt where you will type the commands to compile and to run your programs. At the prompt, change to your Java directory by typing the following command and pressing enter (where X stands for the the letter of your network drive):
    cd X:\Java
    Obviously, if you named your folder something other than Java, you should use that folder name instead. Tip: To change to directories with spaces in their names, enclose the path in quotation marks. For instance, type cd "My Documents", not cd My Documents.
  5. Compile the source code you wrote in Step 2 by now typing the following command at the prompt and pressing enter:
    javac HelloWorld.java
    Note that javac stands for java compilier. If running this command prints any errors to the command prompt window, then you either did not copy the code in Step 2 correctly or you did not give the file the right name in Step 3. Make sure each is copied precisely, character by character, with correct capitalization, and then try to recompile. If it compiles successfully, it will print out nothing and simply show the next prompt.
    You can verify that your program sucessfully compiled by listing the contents of the directory using the command dir. You should see the file that you wrote in Notepad, HelloWorld.java, and a second file, the bytecode produced by the Java compiler, HelloWorld.class.
  6. Now run your program by executing the following command at the command prompt. You do not include the .class extension!
    java HelloWorld
    What do you see?
  7. Now change the HelloWorld code so that it prints out Goodbye, World! instead. This should be done by changing only one line of your program. Compile and run your program and see what it prints out.
  8. The command System.out.println prints out its argument and then starts a new line. Change your program so it prints out Hello, World! on one line and then prints out Goodbye, World! on the next line. Compile and run.
  9. Take a look at the code you have written. It begins by creating a class called HelloWorld. We�ll learn more about what a class is in later lectures. After the word HelloWorld in your code, there is an opening brace { which denotes the beginning of the class. Then all the way at the bottom of your code there is a closing brace } which denotes the end of the class.
  10. Every Java file contains a class with the same name as the file. See how the class we created called HelloWorld is in a file called HelloWorld.java? If we had a Java file named Racecar.java, in it you would find a class called Racecar. If we were to create a class called Student, we would create it in a file called Student.java.
  11. Inside some classes, you�ll find what�s called the main method. The main method always begins with the text:
    public static void main(String[] args)
    After this text you will see an opening brace { which denotes the beginning of the main method and the second-to-last closing brace } denotes the end of the main method. We will see a lot of things in Java begin and end with braces.
  12. Add these lines to your main method:
    String name = "AITI"; 
    System.out.print("Hello,");
    System.out.println(name);
    System.out.println("How are you today?");
    
    Compile and run. How does System.out.print differ from System.out.println?
  13. Change the text AITI to your name (for example, Gabreselassie) and compile and run your code again. How has the output changed?
  14. Change the line
    System.out.println(name);
    to the line
    System.out.println("name");
    Why are the outputs different?
  15. Play around with printing out different messages.

Lab 0 will not be graded. Thus, you should not submit anything for this lab. Be aware, however, that its concepts will be important in future labs and tests.

Hosted by www.Geocities.ws

1