slide1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
java PowerPoint Presentation
Download Presentation
java

Loading in 2 Seconds...

  share
play fullscreen
1 / 11
Download Presentation

java - PowerPoint PPT Presentation

alexia
396 Views
Download Presentation

java

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. java

  2. Java • Java is a programming language developed by Sun Microsystems in 1995. • Java is one of the first languages to be platform independent. • Java is compiled to byte-code that runs on aJava interpreter

  3. Java VM • A Java program never executes directly (i.e., natively) on a machine; instead the Java interpreter reads the byte code and executes the corresponding native machine instructions.

  4. Java API • To run Java programs on a computer, all that is needed is the interpreter (java virtual machine) and some library routines (the API)

  5. The Java API • The Java API is the code that comes with the java interpreter (a.k.a. the java virtual machine) when you download java. • The java API is a collection of Java classes---take a look at the API for Java SE 6 • See if you can find the String class.

  6. A Simple Java Program public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } • This program prints Hello World! to the monitor • Things to notice: • All programs are classes • All programs have a method called main() that has one array of Strings parameter.

  7. Running Java outside of Processing Follow these steps to run HelloWorld • Download HelloWorld.java and save it to your Desktop. • Open a terminal window. • In the terminal window, use the command cd to change to the Desktop directory where HelloWorld.java is stored. • In the terminal window, type javac HelloWorld.java to compile your program to byte code • In the terminal window, type java HelloWorld to run your compiled program on the java virtual machine.

  8. In-class exercise • Run the following program and explain what happens; how is the output generated? • The program requires 3 class: Pets.java, Cat.java, and Dog.java • These classes are shown on the following slides.

  9. Cat Class public class Cat { public void hiss () { System.out.println ("Hiss!"); } public void scratch (Dog victum) { System.out.println ("I'm scratching the dog"); victum.growl (); } public void bite (Dog sillyDog) { System.out.println ("I'm bitting the dog"); sillyDog.yelp (); scratch (sillyDog); } }

  10. Dog Class public class Dog { public void bark () { System.out.println ("Arf!"); System.out.println ("Arf!"); } public void growl () { System.out.println ("Grrrr!"); } public void yelp () { System.out.println ("Awooo!"); } public void beenBittenBy (Cat sillyCat) { System.out.println ("I've been bitten by a cat with a mean hiss:"); sillyCat.hiss (); } }

  11. Pets Class public class Pets { /* Creates a Cat and a Dog */ public static void main (String [] args) { Cat tom = new Cat (); // create Cat object Dog spike = new Dog (); // create Dog object // demonstrate Cat behavior tom.bite (spike); System.out.println (); // Skip a line of output // demonstrate Dog behavior spike.beenBittenBy (tom); } }