Begin Java - PowerPoint PPT Presentation

About This Presentation
Title:

Begin Java

Description:

Begin Java Pepper Objectives What is a program? Learn the basics of your programming tool: BlueJ Write a first Java program and see it run Make some changes and see ... – PowerPoint PPT presentation

Number of Views:90
Avg rating:3.0/5.0
Slides: 18
Provided by: JoanWT
Learn more at: https://home.adelphi.edu
Category:
Tags: begin | java | programming | with

less

Transcript and Presenter's Notes

Title: Begin Java


1
Begin Java
  • Pepper

2
Objectives
  • What is a program?
  • Learn the basics of your programming tool BlueJ
  • Write a first Java program and see it run
  • Make some changes and see them run
  • Understand basic program flow - Create two
    methods in one program

3
What Is Programming?
  • Computers cannot do all the wonderful things that
    we expect without instructions telling them what
    to do.
  • Program a set detailed of instructions telling
    a computer what to do
  • Programming designing and writing computer
    programs
  • Programming language a language used to express
    computer programs. (Syntax Vocab)

4
A Very Simple Java Program
Open braces mark the beginning
Class header
Method header
public class JavaWorld public static void
main(String args) System.out.println ("T
his is my first Java program.")
statements
Close braces mark the end
5
A First Program What Does It Do?
System.out.println ("This is my first Java
program.")
Prints the message This is my first Java program.
Ends the line
6
Structure of Java programs
  • public class ltnamegt
  • public static void main(String args)
  • ltstatementgt
  • ltstatementgt
  • ...
  • ltstatementgt
  • Every executable Java program consists of a class
  • that contains a method named main
  • that contains the statements (commands) to be
    executed

7
Steps to create a program
Design
Code Compile
Run
Test
8
Make the Program Run
  • Compile (translate to java byte code)
  • Run (interpreter for that OS reads java byte code
    and translates for machine)

9
Development Environment
  • Blue Jay
  • Lets you enter code and run it to get results
  • Download at
  • http//www.bluej.org/download/download.html
  • Instructions on syllabus

10
Exercise
  • Add the line Welcome to Java
  • When you run the program, you will see
  • This is my first Java Program. Welcome to
    Java.
  • Extra
  • Info print() does not go to the next line
  • Try splitting Welcome to Java into two
    statements Welcome and then to Java.

11
Comments
  • // -gt comment line ex // this is a comment
  • / xxx / ? comment between marks ex
  • / these are a bunch of comments
  • xy
  • that line above is meaningless /
  • Space liberally
  • For you to do Add some comments to your program
    and see it makes no changes

12
Add a method
  • What a set of statements that has a name you can
    call.
  • Why
  • deal with complex code
  • repeatedly call the same statements
  • Divide responsibility among programmers
  • How
  • Add a method header with following
  • Ex public static void method1()
  • Write the statements inside the .
  • Call the method a few times from your main
    routine
  • ex method1()

13
Example of a new method
  • public class JavaWorld
  • public static void main(String args)
  • System.out.println
  • ("This is my first Java program.")
  • method1()
  • method1()
  • public static void method1()
    System.out.println
  • (I love Java.")
  • YOU TRY add method1 to your program and have
    your main method call method1 a few times. Have
    it call method1 before and after the This is my
    first java program line. Add another print
    statement to method1 and see what happens.

14
Escape Characters 1
  • BUT, I really want a double quote inside my
    string!
  • \ is -? abc\def -? abcdef
  • \\ is \ -? abc\def -? abc\def

15
Escape Characters 2
  • How do I get new lines and tabs?
  • \n new line (go to beginning of next line)
  • \r carriage return (go to beginning of this
    line)
  • \t tab (go to next tab stop)

16
Escape characters in your program
  • Insert \t inside the quotes of a print statement
    in your program ? what prints?
  • Insert a \\ inside the quotes of a print
    statement in your program ? what prints?
  • Now remove the \ and see that the program wont
    even compile.

17
Your toolset
  • Basic setup
  • Create and call methods
  • Print to screen including escape commands
  • Comments
  • Here are the programming steps you explored
Write a Comment
User Comments (0)
About PowerShow.com