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!
public class CompSci { //open brace public static void main(String[] args) { System.out.println("Comp Sci!"); } } //close brace Braces – You gotta have ‘em! Every class and every method must have a { and a }.
© A+ Computer Science - 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.out frequently used methods NameUse 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"); referencecommand / method OUTPUT compsci
System.out.print("compsci"); OUTPUT compscicompsci
System.out.println("compsci"); OUTPUT compsci
System.out.println("compsci"); OUTPUTcompsci
System.out.println("c\tompsci " ); \nnewline \ttab \rcarriage return \bbackspace OUTPUT c ompsci
System.out.println("com\tpsci " ); OUTPUT com psci \nnewline \ttab \rcarriage return \bbackspace
System.out.println("comp\nsci"); OUTPUT comp sci \nnewline \ttab \rcarriage return \bbackspace
\\outs \ \"outs " \’outs ’ System.out.println( " \\compsci\ " / " ); OUTPUT \compsci"/
\\outs \ \"outs " \’outs ’ System.out.println("\\'comp\'sci\'/"); OUTPUT \'comp'sci'/
© A+ Computer Science - Escape Sequences frequently used combinations NameUse \ttabs over five spaces \nmoves to front of next line \bdeletes previous character \rmoves to front of current line \\nets one backslash \ \"nets one double quote " \’nets one single quote ’
© A+ Computer Science - //single-line comments /* */block comments //this line prints stuff on the screen System.out.println("stuff");
Review: AboutMe Part1 Create 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 - System.out.printf("%s","compsci\n"); OUTPUT compsci
format()
© A+ Computer Science -
Review: AboutMe Part2 Modify 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.