© a+ computer science - . public class compsci { } all java programs start with a class

30
© A+ Computer Science - www.apluscompsci.com

Upload: jerome-bates

Post on 17-Dec-2015

228 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

Page 2: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

public class CompSci{

}

All Java programs start with a class.

Page 3: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

public class CompSci{ public static void main(String[] args) { System.out.println("Comp Sci!"); }} OUTPUT

Comp Sci!

Page 4: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

public class CompSci{ //open brace

public static void main(String[] args) { System.out.println("Comp Sci!"); }} //close brace

Braces – You gotta have ‘em! Every classand every method must have a { and a } .

Page 5: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

public class CompSci{ public static void main(String[] args) { System.out.println("Comp Sci!"); }}You must put a semi-colon at the end of all Java program statements ( ; ).

Page 6: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

Never put a ; before an open { brace

;{ //illegal}; //legal

Page 7: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

public class CompSci{ public static void main(String[] args) { System.out.println("Comp Sci!"); }}

Indent all code 3 spaces to make it easier to read.

Page 8: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

Page 9: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

Page 10: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

System.outfrequently used methods

Name Use

print(x) print x and stay on the current line

println(x) print x and move to next line down

printf(s,x) print x according to s specifications

Page 11: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

System.out.print("compsci");

reference command / method

OUTPUTcompsci

Page 12: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

System.out.print("compsci");System.out.print("compsci");

OUTPUTcompscicompsci

Page 13: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

System.out.println("compsci");

OUTPUTcompsci

Page 14: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

System.out.println("compsci");System.out.println("compsci");

OUTPUTcompscicompsci

Page 15: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

Page 16: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

System.out.println("c\tompsci");

\n newline\t tab\r carriage return\b backspace

OUTPUTc ompsci

Page 17: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

System.out.println("com\tpsci");

OUTPUTcom psci

\n newline\t tab\r carriage return\b backspace

Page 18: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

System.out.println("comp\nsci");

OUTPUTcompsci

\n newline\t tab\r carriage return\b backspace

Page 19: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

Page 20: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

\\ outs \\" outs "\’ outs ’

System.out.println("\\compsci\"/");

OUTPUT\compsci"/

Page 21: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

\\ outs \\" outs "\’ outs ’

System.out.println("\\'comp\'sci\'/");

OUTPUT\'comp'sci'/

Page 22: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

Escape Sequencesfrequently used combinations

Name Use

\t tabs over five spaces

\n moves to front of next line

\b deletes previous character

\r moves to front of current line

\\ nets one backslash \

\" nets one double quote "

\’ nets one single quote ’

Page 23: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

// single-line comments/* */ block comments

//this line prints stuff on the screenSystem.out.println("stuff");

Page 24: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

// single-line comments/* */ block comments

/* this line prints stuff on the screen*/System.out.println("stuff");

Page 25: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

System.out.printf("%s","compsci\n");

OUTPUTcompsci

Page 26: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

Page 27: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

Syntax errors occur when you type something in wrong, causing the code to not compile.

//missing semicolon - ; expectedSystem.out.println("stuff")

//case problem – should be Systemsystem.out.println("stuff")

Page 28: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

Runtime errors occur when something goes wrong while the program is running.

//an out of bounds exception is thrownString s = "runtime_error";System.out.println( s.charAt(15) );

Page 29: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

Page 30: © A+ Computer Science - . public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com