Sie sind auf Seite 1von 9

Java Basics

Lab 1 : CH.1 : Introduction to Java Applications lab document created by Rakan N Al-Enezi

Platform Used : Eclipse Classic - To download check this link: http://www.eclipse.org/downloads/

Chapter 1 Objectives:

Learn How to in the Eclipse editor software .


Create new Java project . Create Class . Write your first program . Run your Program . Modifying your Program .

Exercise 1 - Run Eclipse And Create Project


Run the Eclipse editor from the Start Menu of the lab computer (or run it from your own laptop, if you brought one).

In Eclipse, create a new Java program: Click File New Java Project from the top menu. An empty white text window should appear in Eclipse.

Exercise 2 - Create Class

Recall from lecture: A Java program must be has at least one user-defined class. So for that we need to create class , to create class : Right Click src New Class

Exercise 3 - A First Program in Java

Write the following program into class :

1 2// Text-printing program. 3 4public class myfirstapp { 5 6 // main method begins execution of Java application 7 public static void main( String args[] ) 8 { 9 System.out.println( "Hello world!" ); 10 System.out.println( "Welcome to Java Programming!" ); 11 System.out.println( "This is my very first program." ); 12 13 } // end method main 14 15} // end class myfirstapp

Exercise 4 - Running our Program

We can now test run our program in Eclipse. In the tool-bar, click the Run button as shown in Picture

This should produce some output in the Console window like Console output.

Advanced: Escape sequences

An escape sequence inserts a special character into a println statement.


Escapesequence Description New-line tab (indents output by roughly 8 spaces) quotation mark backslash

\n \t \" \\

Example:
System.out.println( "I said \"hello\" to Rakan.");

Exercise 5 - Modifying Our First Program

Modify your Program file to produce the following console output.


Hello world! Welcome to Java Programming!

Exercise 6 - Displaying Text with (printf)

System.out.printf Displays formatted data

Format specifier %s placeholder for a string


System.out.printf( "%s\n%s\n", "Welcome to", "Java Programming!");

The Output should be like this :


Welcome to Java Programming!

Das könnte Ihnen auch gefallen