Page 327 - Computer_Science_F5
P. 327

Computer Science  Writing and running a simple program
           When developing applications in Java, it is essential to start with a class name that
           matches the filename. As a practice, let you create a simple program that prints
           “Hello Tanzanians”. You can create your first Java file, named Tunalianzisha.java,
           using  any  text  editor  like  Notepad. The  file  should  contain  a  greeting  message:
          FOR ONLINE READING ONLY
           “Hello Tanzanians”.


                Activity 5.7:

             Writing a simple program-Hello Tanzanian


            Aim:
            To create a simple program that prints “Hello Tanzanians” to the console and
            learn how to compile the Java program using Command Prompt.


            Materials: Computers, text editors, Java Development Kit, Command prompt

            Instructions:

             Step 1:   Open a text editor, Notepad
             Step 2:   Type the following code:
                            class Tunalianzisha {
                            public static void main(String[] args) {

                            System.out.println(“Hello Tanzanians”);
                                }
                          }
             Step 3:  Save your first program in a Notepad as “Tunalianzisha.java” at any
                     location.


            Compiling a Java program:
             Step 4:   Open Command Prompt (cmd), write the path where you saved your file
                     then type “javac Main.java”:

                     (When  you open the  cmd.exe,  it  might  come  with  default  path  like
                     C:\Users\admin> ; if you saved your file on the desktop, change the
                     directory to Desktop using Help “cd Desktop” as seen in the Figure 5.3.
                     Here, you are going to use the JDK compiler (called javac) to change
                     your text file in a notepad into an executable program.



                                                 318
                                                                for Advanced Secondary Schools



     Computer Science Form 5.indd   318                                                     23/07/2024   12:34
   322   323   324   325   326   327   328   329   330   331   332