Java Syntax and OutputJava 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!
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 } .
© 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 ( ; ).
Never put a ; before an open { brace
;{ //illegal}; //legal
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.
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
System.out.print("compsci");
reference command / method
OUTPUTcompsci
System.out.print("compsci");System.out.print("compsci");
OUTPUTcompscicompsci
System.out.println("compsci");
OUTPUTcompsci
System.out.println("compsci");System.out.println("compsci");
OUTPUTcompscicompsci
System.out.println("c\tompsci");
\n newline\t tab\r carriage return\b backspace
OUTPUTc ompsci
System.out.println("com\tpsci");
OUTPUTcom psci
\n newline\t tab\r carriage return\b backspace
System.out.println("comp\nsci");
OUTPUTcompsci
\n newline\t tab\r carriage return\b backspace
\\ outs \\" outs "\’ outs ’
System.out.println("\\compsci\"/");
OUTPUT\compsci"/
\\ outs \\" outs "\’ outs ’
System.out.println("\\'comp\'sci\'/");
OUTPUT\'comp'sci'/
© 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 ’
© A+ Computer Science - www.apluscompsci.com
// single-line comments/* */ block comments
//this line prints stuff on the screenSystem.out.println("stuff");
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.
// single-line comments/* */ block comments
/* this line prints stuff on the screen*/System.out.println("stuff");
© A+ Computer Science - www.apluscompsci.com
System.out.printf("%s","compsci\n");
OUTPUTcompsci
format()
© A+ Computer Science - www.apluscompsci.com
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.