How do I create and run a program in Java?

Working on Windows we use an editor (Notepad) to create a file. 

Name of file: Something.java (inside a class with the same name). 

You then compile this to produce: Something.class (use javac). 

To run the program you say: java Something (if you have adequate main).

Java programs are made out of classes. In our example: one class, one file. 

I also asked you to tell me where you want to be at the end of this class. 

Today I will tell you where I want you to be. 

So let's go to another class website:

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

http://www.cs.indiana.edu/classes/c211-dgerman/sum2014/classNotes.html

http://silo.cs.indiana.edu:8346/c211/sum2014/tetris.phps

So play Tetris note the commands are confusing to Adrian. 

Look for something in our language:

https://www.google.com/?gws_rd=ssl#q=java+tetris

The first link is: 

http://zetcode.com/tutorials/javagamestutorial/tetris/

At this point I get really excited and we all do this: 

(a) open a new Notepad window

(b) put this code in and save it as Tetris.java:

package tetris;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;


public class Tetris extends JFrame {

    JLabel statusbar;


    public Tetris() {

        statusbar = new JLabel(" 0");
        add(statusbar, BorderLayout.SOUTH);
        Board board = new Board(this);
        add(board);
        board.start();

        setSize(200, 400);
        setTitle("Tetris");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
   }

   public JLabel getStatusBar() {
       return statusbar;
   }

    public static void main(String[] args) {

        Tetris game = new Tetris();
        game.setLocationRelativeTo(null);
        game.setVisible(true);

    } 
}

(c) Then do the same with Shape.java:

package tetris;

import java.util.Random;
import java.lang.Math;


public class Shape {

    enum Tetrominoes { NoShape, ZShape, SShape, LineShape, 
               TShape, SquareShape, LShape, MirroredLShape };

    private Tetrominoes pieceShape;
    private int coords[][];
    private int[][][] coordsTable;


    public Shape() {

        coords = new int[4][2];
        setShape(Tetrominoes.NoShape);

    }

    public void setShape(Tetrominoes shape) {

         coordsTable = new int[][][] {
            { { 0, 0 },   { 0, 0 },   { 0, 0 },   { 0, 0 } },
            { { 0, -1 },  { 0, 0 },   { -1, 0 },  { -1, 1 } },
            { { 0, -1 },  { 0, 0 },   { 1, 0 },   { 1, 1 } },
            { { 0, -1 },  { 0, 0 },   { 0, 1 },   { 0, 2 } },
            { { -1, 0 },  { 0, 0 },   { 1, 0 },   { 0, 1 } },
            { { 0, 0 },   { 1, 0 },   { 0, 1 },   { 1, 1 } },
            { { -1, -1 }, { 0, -1 },  { 0, 0 },   { 0, 1 } },
            { { 1, -1 },  { 0, -1 },  { 0, 0 },   { 0, 1 } }
        };

        for (int i = 0; i < 4 ; i++) {
            for (int j = 0; j < 2; ++j) {
                coords[i][j] = coordsTable[shape.ordinal()][i][j];
            }
        }
        pieceShape = shape;

    }

    private void setX(int index, int x) { coords[index][0] = x; }
    private void setY(int index, int y) { coords[index][1] = y; }
    public int x(int index) { return coords[index][0]; }
    public int y(int index) { return coords[index][1]; }
    public Tetrominoes getShape()  { return pieceShape; }

    public void setRandomShape()
    {
        Random r = new Random();
        int x = Math.abs(r.nextInt()) % 7 + 1;
        Tetrominoes[] values = Tetrominoes.values(); 
        setShape(values[x]);
    }

    public int minX()
    {
      int m = coords[0][0];
      for (int i=0; i < 4; i++) {
          m = Math.min(m, coords[i][0]);
      }
      return m;
    }


    public int minY() 
    {
      int m = coords[0][1];
      for (int i=0; i < 4; i++) {
          m = Math.min(m, coords[i][1]);
      }
      return m;
    }

    public Shape rotateLeft() 
    {
        if (pieceShape == Tetrominoes.SquareShape)
            return this;

        Shape result = new Shape();
        result.pieceShape = pieceShape;

        for (int i = 0; i < 4; ++i) {
            result.setX(i, y(i));
            result.setY(i, -x(i));
        }
        return result;
    }

    public Shape rotateRight()
    {
        if (pieceShape == Tetrominoes.SquareShape)
            return this;

        Shape result = new Shape();
        result.pieceShape = pieceShape;

        for (int i = 0; i < 4; ++i) {
            result.setX(i, -y(i));
            result.setY(i, x(i));
        }
        return result;
    }
}

(d) there are no more files so now I want to compile these.

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

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

 Directory of C:\Users\dgerman

06/24/2014  11:34 AM    <DIR>          .
06/24/2014  11:34 AM    <DIR>          ..
08/12/2013  11:20 AM    <DIR>          .android
08/10/2012  02:13 AM    <DIR>          .datastudio
07/19/2012  10:12 PM    <DIR>          AppData
06/24/2014  11:34 AM    <DIR>          Contacts
06/24/2014  12:10 PM    <DIR>          Desktop
06/24/2014  11:34 AM    <DIR>          Documents
06/24/2014  11:34 AM    <DIR>          Downloads
06/24/2014  11:34 AM    <DIR>          Favorites
06/24/2014  11:34 AM    <DIR>          Links
06/24/2014  11:34 AM    <DIR>          Music
06/24/2014  11:34 AM    <DIR>          Pictures
06/24/2014  11:34 AM    <DIR>          Saved Games
06/24/2014  11:34 AM    <DIR>          Searches
06/24/2014  11:34 AM    <DIR>          Videos
08/12/2013  11:20 AM    <DIR>          workspace
               0 File(s)              0 bytes
              17 Dir(s)  62,989,209,600 bytes free

C:\Users\dgerman>cd Desktop

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/24/2014  12:10 PM    <DIR>          .
06/24/2014  12:10 PM    <DIR>          ..
06/24/2014  12:10 PM             8,095 Board.java
06/24/2014  12:05 PM             2,839 Shape.java
06/24/2014  12:06 PM               730 Tetris.java
               3 File(s)         11,664 bytes
               2 Dir(s)  62,989,209,600 bytes free

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

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

C:\Users\dgerman\Desktop>javac
Usage: javac <options> <source files>
where possible options include:
  -g                         Generate all debugging info
  -g:none                    Generate no debugging info
  -g:{lines,vars,source}     Generate only some debugging info
  -nowarn                    Generate no warnings
  -verbose                   Output messages about what the compiler is doing
  -deprecation               Output source locations where deprecated APIs are used
  -classpath <path>          Specify where to find user class files and annotation processors
  -cp <path>                 Specify where to find user class files and annotation processors
  -sourcepath <path>         Specify where to find input source files
  -bootclasspath <path>      Override location of bootstrap class files
  -extdirs <dirs>            Override location of installed extensions
  -endorseddirs <dirs>       Override location of endorsed standards path
  -proc:{none,only}          Control whether annotation processing and/or compilation is done.
  -processor <class1>[,<class2>,<class3>...]Names of the annotation processors to run; bypasses default discovery process
  -processorpath <path>      Specify where to find annotation processors
  -d <directory>             Specify where to place generated class files
  -s <directory>             Specify where to place generated source files
  -implicit:{none,class}     Specify whether or not to generate class files for implicitly referenced files
  -encoding <encoding>       Specify character encoding used by source files
  -source <release>          Provide source compatibility with specified release

  -target <release>          Generate class files for specific VM version
  -version                   Version information
  -help                      Print a synopsis of standard options
  -Akey[=value]              Options to pass to annotation processors
  -X                         Print a synopsis of nonstandard options
  -J<flag>                   Pass <flag> directly to the runtime system


C:\Users\dgerman\Desktop>


I finally compile the three files as follows: 

C:\Users\dgerman\Desktop>javac -d . *.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/24/2014  12:17 PM    <DIR>          .
06/24/2014  12:17 PM    <DIR>          ..
06/24/2014  12:10 PM             8,095 Board.java
06/24/2014  12:05 PM             2,839 Shape.java
06/24/2014  12:17 PM    <DIR>          tetris
06/24/2014  12:06 PM               730 Tetris.java
               3 File(s)         11,664 bytes
               3 Dir(s)  62,988,034,048 bytes free

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

 Directory of C:\Users\dgerman\Desktop\tetris

06/24/2014  12:17 PM    <DIR>          .
06/24/2014  12:17 PM    <DIR>          ..
06/24/2014  12:17 PM             1,394 Board$TAdapter.class
06/24/2014  12:17 PM             5,298 Board.class
06/24/2014  12:17 PM             1,252 Shape$Tetrominoes.class
06/24/2014  12:17 PM             2,435 Shape.class
06/24/2014  12:17 PM               990 Tetris.class
               5 File(s)         11,369 bytes
               2 Dir(s)  62,988,034,048 bytes free

C:\Users\dgerman\Desktop>

Now all I have to do is start the program. 


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

 Directory of C:\Users\dgerman\Desktop

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/24/2014  12:17 PM    <DIR>          .
06/24/2014  12:17 PM    <DIR>          ..
06/24/2014  12:10 PM             8,095 Board.java
06/24/2014  12:05 PM             2,839 Shape.java
06/24/2014  12:17 PM    <DIR>          tetris
06/24/2014  12:06 PM               730 Tetris.java
               3 File(s)         11,664 bytes
               3 Dir(s)  62,988,042,240 bytes free

C:\Users\dgerman\Desktop>java Tetris
Exception in thread "main" java.lang.NoClassDefFoundError: Tetris
Caused by: java.lang.ClassNotFoundException: Tetris
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: Tetris.  Program will exit.

C:\Users\dgerman\Desktop>java tetris\Tetris
Exception in thread "main" java.lang.NoClassDefFoundError: tetris\Tetris (wrong name: tetris/Tetris)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
        at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: tetris\Tetris.  Program will exit.

C:\Users\dgerman\Desktop>java tetris.Tetris

C:\Users\dgerman\Desktop>

This runs and is just like on the website. 

http://zetcode.com/tutorials/javagamestutorial/tetris/ is OK. 

Read and think about La Two. 

Write thoughts/questions on minute paper and ask in class. 

import java.util.*;
/**
 * This program demonstrates console input.
 * @version 1.10 2004-02-10
 * @author Cay Horstmann
 */ 
public class InputTest
{
   public static void main(String[] args)
   {
      Scanner in;                                           // declaration of variable in (which is of type Scanner) 
 
      in = new Scanner(System.in);                          // initialization of variable in 
           /* notice how a new scanner is created with the new operator: scanners are objects
              notice also the argument given: System.in stands for the keyboard (basically)
                                      just as System.out stands for the monitor/screen (by and large)
              the scanner we have created reads from the keyboard; you can create scanners that read from a file or 
              read from the network, hence the need to be able to identify these things. System.in is the keyboard. 
            */ 
      // get first input 
      System.out.print("What is your name? "); // why do we use print (and not println) here? 
      String name = in.nextLine(); // in points to an object of type Scanner which comes equipped with a method: nextLine
                                   // when the method is called the scanner waits for us to type on System.in (keyboard) patiently
                                   // patiently means until we hit Enter. At that point the characters we typed are collected and returned
                                   // into the program, in the expression where the invocation occured, as a String. Here a variable name of
                                   // the required type is ready to assume that value (to be used later in the program).  
 
      // get second input 
      System.out.print("How old are you? ");
      int age = Integer.parseInt(in.nextLine());      // read a string, convert to an int so we can do math with it  
 
      // display output on console 
      System.out.println("Hello, " + name + ". Next year you'll be " + (age + 1) + ".");
   }
}

I write the following based on the above:

import java.util.Scanner;

class Two {
  public static void main(String[] args) {
    Scanner in; 
    in = new Scanner(System.in); 
    System.out.print("How hot is it outside: "); 
    int temp = in.nextInt();
    System.out.println( ( temp - 32 ) * 5 / 9 );    
  }
}

Here's how it works:


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/24/2014  12:38 PM    <DIR>          .
06/24/2014  12:38 PM    <DIR>          ..
06/24/2014  12:10 PM             8,095 Board.java
06/24/2014  12:38 PM             2,086 InputTest.java
06/24/2014  12:05 PM             2,839 Shape.java
06/24/2014  12:17 PM    <DIR>          tetris
06/24/2014  12:06 PM               730 Tetris.java
               4 File(s)         13,750 bytes
               3 Dir(s)  62,985,080,832 bytes free

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

C:\Users\dgerman\Desktop>java InputTest
What is your name? Dave
How old are you? 3
Hello, Dave. Next year you'll be 4.

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

C:\Users\dgerman\Desktop>java Two
Howdy.

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

C:\Users\dgerman\Desktop>java Two
How hot is it outside: 77
25

C:\Users\dgerman\Desktop>

So now you should be able to write the program for Lab Two. 

--