Docsity

Java Programming and Banana Nut Bread Recipe: A Comparative Study, Slides of Java Programming

Partial preview of the text

Download Java Programming and Banana Nut Bread Recipe: A Comparative Study and more Slides Java Programming in PDF only on Docsity!

Basic operations

Announcements

E-book?

Methods have parenthesis

(objects never do)

Banana Nut Bread

Directions

  1. Preheat the oven to 350°F (175°C).
  2. Mix butter into the mashed bananas in a large mixing bowl.
  3. Mix in the sugar, egg, and vanilla.
  4. Sprinkle the baking soda and salt over the mixture and mix in.
  5. Add the flour and nuts last, mix.
  6. Pour mixture into a buttered 4x8 inch loaf pan.
  7. Bake for 1 hour. Cool on a rack.

Banana Nut Bread

Java Directions

  1. oven.preheat(350);
  2. bowl.mix(butter, bananas);
  3. bowl.mix(sugar, egg, vanilla);
  4. bowl.sprinkle(baking soda, salt);
  5. bowl.mix(flour, nuts);
  6. bowl.pour(pan);
  7. pan.bake(60);
  8. pan.cool();

Banana Nut Bread

mashedBananas = bananas.mashed();

bowl.add(butter, mashedBananas);

same as:

bowl.add(butter, bananas.mashed());

Kitchen.bowl.add(butter, bananas.mashed());

hand.mix(butter, mashedBananas);

bowl.add(hand.mix(butter, mashedBananas));

Magic 8 ball

What a rip off!

Compiling

Java is a high level language

(human readable)

Compiling changes a high level

language into a low level language

that is easier for the computer

(computer cannot run high level)

Compiling

You must recompile the source code

every time you save a change

before running the program again

Your source code is the original

language you wrote your program

in (the Java code for us)

High level (Java)

public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, world!"); } }

Intermediate level (Java-Byte)

public class HelloWorld extends java.lang.Object{ public HelloWorld(); Code: 0: aload_ 1: invokespecial #1; //Method java/lang/Object."":()V 4: return public static void main(java.lang.String[]); Code: 0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #3; //String Hello, world! 5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 8: return }

Strings

“Hello world”

Can append strings/numbers to strings:

“Hello world ” + “I'm writing Java code”

(see AppendStrings.java)

Mystery program!

Ingredients Directions

Comments

Comments are ignored pieces of code

// denotes a single line that is commented

// (everything before hitting enter)

/* denotes the beginning of a comment

and the end of a comment is denoted by */

Java Rules

The name of the file must be the same

as the class it implements

Example: HelloWorld.java implements

HelloWorld class

The “main” method will always be run

(and must exist)