C212/A592 Inroduction to Software Systems

  http://www.cs.indiana.edu/classes/c212

Adrian German dgerman@indiana.edu

silo.cs.indiana.edu

Connect with PuTTY

Unix commands:

   pwd, cd, cp, ls, ps, grep, du, 
   man, cat, pico, chmod, javac, java

http://kb.iu.edu/data/afsk.html

Office Hours

Reading Assignments

OnCourse

Java programs are made of classes. 

Open Notepad and start writing the program: 

  (a) I need to define a class
      Every class has name. I give this one the name of Dog. 

        public class Dog {

        } 

      Be aware of the coding guidelines: http://www.horstmann.com/ccj2/app01.pdf

      Save this as Dog.java and compile it. 

      Part of the coding guidelines is that class name stats with uppercase.

  (b) Type cmd and open a command prompt window.
 
      By default I am placed in C:\Users\dgerman 
 
      I want to go to the Desktop: cd Desktop

      I then type dir and after I see my file I try compiling it: javac Dog.java

  (c) Do you have Java installed on this machine? 

        http://docs.oracle.com/javase/tutorial/getStarted/cupojava/

      Download and install Java or call us if you don't have it. 

      We have it here. It's located in: C:\Program Files\Java\jdk1.6.0_33

      Here's what I did to compile:

        C:\Users\dgerman\Desktop>javac Dog.java
        'javac' is not recognized as an internal or external command, operable program or batch file.

        C:\Users\dgerman\Desktop>"C:\Program Files\Java\jdk1.6.0_33\bin\javac.exe" Dog.java

        C:\Users\dgerman\Desktop>dir
         Volume in drive C has no label.
         Volume Serial Number is 42B8-DA49

         Directory of C:\Users\dgerman\Desktop

        06/17/2013  12:28 PM    <DIR>          .
        06/17/2013  12:28 PM    <DIR>          ..
        06/17/2013  12:28 PM               180 Dog.class
        06/17/2013  12:17 PM                34 Dog.java
                       2 File(s)            214 bytes
                       2 Dir(s)  103,254,536,192 bytes free

        C:\Users\dgerman\Desktop>"C:\Program Files\Java\jdk1.6.0_33\bin\java.exe" Dog
        Exception in thread "main" java.lang.NoSuchMethodError: main

        C:\Users\dgerman\Desktop>

      My class needs a main method, otherwise it won't run. 

  (d) Add a main method to the class:

        public class Dog {
  
          public static void main(String[] args) {

            System.out.print( "This is" ); 
            System.out.println( "an example." ); 
            System.out.println( "Here's \" and \\ too." ); 

          } 

        } 

      Inside the method print three strings of characters. 

      Note the special role of " and \ and the rules of escaping them. 

  (c) Set the PATH on Windows:

        C:\Users\dgerman\Desktop>PATH=%PATH%;"C:\Program Files\Java\jdk1.6.0_33\bin"

        C:\Users\dgerman\Desktop>javac Dog.java

        C:\Users\dgerman\Desktop>java Dog
        This isan example.
        Here's " and \ too.

        C:\Users\dgerman\Desktop>

      Now you can access javac and java without typing the full path. 

  (d) On Unix your session would look like this:

     -bash-4.1$ mkdir c212

     -bash-4.1$ cd c212

     -bash-4.1$ mkdir 06172013

     -bash-4.1$ cd 06172013/

     -bash-4.1$ pico -w Dog.java

     -bash-4.1$ javac Dog.java

     -bash-4.1$ ls -l
     total 8
     -rw-r--r-- 1 dgerman faculty 491 Jun 17 12:48 Dog.class
     -rw-r--r-- 1 dgerman faculty 221 Jun 17 12:48 Dog.java

     -bash-4.1$ java Dog
     This isan example.
     Here's " and \ too.

     -bash-4.1$ cat Dog.java
     public class Dog {

       public static void main(String[] args) {

         System.out.print( "This is" );
         System.out.println( "an example." );
         System.out.println( "Here's \" and \\ too." );

       }

     }

     -bash-4.1$ clear
     -bash-4.1$

Notice on Unix we don't need to set up the PATH (for this anyway). 

There are alternatives to using Notepad or pico and javac/java. 

Later this week we will look at DrJava and BlueJ. 

See you in lab!

--