introduction to java thanks to dan lunney (shs). java basics file names the “main” method output...

8
Introduction to Introduction to Java Java Thanks to Thanks to Dan Lunney (SHS) Dan Lunney (SHS)

Upload: samson-mccormick

Post on 12-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()

Introduction to JavaIntroduction to Java

Thanks toThanks toDan Lunney (SHS)Dan Lunney (SHS)

Page 2: Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()

Java BasicsJava Basics

• File names

• The “main” method

• Output to screen

• Escape Sequence – Special Characters

• format() method

• Conventions

Page 3: Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()

File NamesFile Names

• In JCreator the file name and class name must be identical

• Always start class names with a capital letter• If more than one word in file name all names are

capitalized • Always begin java programs with:

public class FileName {Code goes here…

}

Page 4: Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()

The “main” methodThe “main” method

• Executes the main section of code

• All other methods “called” from here

• Syntax for main method is:public static void main(String[] args) { code in here…}

Page 5: Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()

Output to ScreenOutput to Screen

• To print to screen we use the line: System.out.println();

• What ever is in the () will print• Text must be in “” marks• There are two options

– println – prints a line and the enters to start new line– print – prints line and remains on same line

• Example: System.out.println(“Hello class”); prints Hello class on the screen

Page 6: Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()

Escape Sequence – Special Escape Sequence – Special CharactersCharacters

• By including special characters within the quote marks, java will perform special formatting

• \n – new line• \t – tab (8 spaces)• \\ - backslash• \” – double quote mark• Example: System.out.print(“Hello\t”);

System.out.print(“class”);prints Hello class

Page 7: Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()

format() methodformat() method

• Can format the look of text• Example: • System.out.format(“%-10s %-8s”, “Hello”, “class”);• Prints – Hello class (5 spaces between)

• Symbols– % - indicates start of format– “-” indicates left align (nothing for right align)– # indicates how many spaces for text– S indicates it’s a string

Page 8: Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()

Java ConventionsJava Conventions

• Always use in an introductory comment with name, date, and project

• All class names start with Upper case letters• Comments should be included before all

methods to explain them• All code statements should be indented from

method line• Place starting curly brace { on the same line as

classes and methods. Place the ending curly brace } on its own line with nothing else.