java syntax and output java part 3. public class compsci { } all java programs start with a class

25
Java Syntax and Output Java Part 3

Upload: elinor-hunt

Post on 13-Jan-2016

253 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java Syntax and Output Java Part 3. public class CompSci { } All Java programs start with a class

Java Syntax and OutputJava Part 3

Page 2: Java Syntax and Output Java Part 3. public class CompSci { } All Java programs start with a class

public class CompSci{

}

All Java programs start with a class.

Page 3: Java Syntax and Output Java Part 3. public class CompSci { } All Java programs start with a class

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

Comp Sci!

Page 4: Java Syntax and Output Java Part 3. public class CompSci { } All Java programs start with a class

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: Java Syntax and Output Java Part 3. 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: Java Syntax and Output Java Part 3. public class CompSci { } All Java programs start with a class

Never put a ; before an open { brace

;{ //illegal}; //legal

Page 7: Java Syntax and Output Java Part 3. public class CompSci { } All Java programs start with a class

Indentation

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: Java Syntax and Output Java Part 3. public class CompSci { } All Java programs start with a class

Java Output

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 9: Java Syntax and Output Java Part 3. public class CompSci { } All Java programs start with a class

System.out.print("compsci");

reference command / method

OUTPUTcompsci

Page 10: Java Syntax and Output Java Part 3. public class CompSci { } All Java programs start with a class

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

OUTPUTcompscicompsci

Page 11: Java Syntax and Output Java Part 3. public class CompSci { } All Java programs start with a class

System.out.println("compsci");

OUTPUTcompsci

Page 12: Java Syntax and Output Java Part 3. public class CompSci { } All Java programs start with a class

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

OUTPUTcompscicompsci

Page 13: Java Syntax and Output Java Part 3. public class CompSci { } All Java programs start with a class

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

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

OUTPUTc ompsci

Page 14: Java Syntax and Output Java Part 3. public class CompSci { } All Java programs start with a class

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

OUTPUTcom psci

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

Page 15: Java Syntax and Output Java Part 3. public class CompSci { } All Java programs start with a class

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

OUTPUTcompsci

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

Page 16: Java Syntax and Output Java Part 3. public class CompSci { } All Java programs start with a class

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

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

OUTPUT\compsci"/

Page 17: Java Syntax and Output Java Part 3. public class CompSci { } All Java programs start with a class

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

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

OUTPUT\'comp'sci'/

Page 18: Java Syntax and Output Java Part 3. 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 19: Java Syntax and Output Java Part 3. 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 20: Java Syntax and Output Java Part 3. public class CompSci { } All Java programs start with a class

Review: AboutMe Part1Create an AboutMe app that displays

your first name and last initial, your instructor’s name, and your school name on 3 separate lines.

Below the personal information, display a phrase that encourages your school team, such as “Go Plainsmen!”. Be sure this phrase is enclosed in quotations marks in your output.

Page 21: Java Syntax and Output Java Part 3. public class CompSci { } All Java programs start with a class

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

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

Page 22: Java Syntax and Output Java Part 3. public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

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

OUTPUTcompsci

Page 23: Java Syntax and Output Java Part 3. public class CompSci { } All Java programs start with a class

format()

Page 24: Java Syntax and Output Java Part 3. public class CompSci { } All Java programs start with a class

© A+ Computer Science - www.apluscompsci.com

Page 25: Java Syntax and Output Java Part 3. public class CompSci { } All Java programs start with a class

Review: AboutMe Part2Modify your app to include your

timetable with start and end times for each class.

Include code to properly align the data into 2 columns with the courses left aligned and the times right aligned.

Use the format() method for this.