CompSci 4 2.1Intro to JavaIntrotoJava•Anatomy of a Class & Terminology•Running and Modifying a ProgramCompSci 4 2.2Intro to JavaThe Planÿ Go over MoveTest.javaþ Similar to Horstmann p. 48ÿ Basic coding conventionsÿ Review with GreeterTest.java (Horstmann)ÿ More terminology with Greeter.java (Horstmann)ÿ Homework 0 reminderÿ Homework 1 assigned (due in 1 week)CompSci 4 2.3Intro to JavaWhy know the lingo?ÿ It’s difficult to read the textbooks if you don’tunderstand the wordsÿ Your compiler error messages use specific wordswith specific meaningsÿ You need to be able to express your questions soothers can understand themÿ The answers you receive will use the lingoCompSci 4 2.4Intro to JavaTerminology to Knowÿ Packageÿ Classÿ Importÿ Keywordÿ Publicÿ Objectÿ Identifierÿ Declarationÿ Definitionÿ Bodyÿ Staticÿ Voidÿ Returnÿ Methodÿ Mainÿ Parameterÿ Stringÿ Arrayÿ Typeÿ Variableÿ Localÿ Constructorÿ Initializeÿ Assignÿ Argumentsÿ Commentsÿ Calling a methodÿ System.out.printlnCompSci 4 2.5Intro to JavaMoveTester.javaimport java.awt.Rectangle;public class MoveTest{public static void main(String[] args){Rectangle cerealBox = new Rectangle(5, 10, 20, 30);// move the rectanglecerealBox.translate(15, 25);// print the moved rectangleSystem.out.println(cerealBox);}}Printsjava.awt.Rectangle[x=20, y=35, width=20, height=30]CompSci 4 2.6Intro to JavaMoveTest.javaimport java.awt.Rectangle;public class MoveTest{public static void main(String[] args){Rectangle cerealBox = new Rectangle(5, 10, 20, 30);// move the rectanglecerealBox.translate(15, 25);// print the moved rectangleSystem.out.println(cerealBox);}}Printsjava.awt.Rectangle[x=20, y=35, width=20, height=30]abstract windowing toolkit packageRectangle classjava packageimport means to bring into theprogram classes or packages notin the package java.langimportisakeyword–itisaword with a special meaningCompSci 4 2.7Intro to JavaMoveTest.javaimport java.awt.Rectangle;public class MoveTest{public static void main(String[] args){Rectangle cerealBox = new Rectangle(5, 10, 20, 30);// move the rectanglecerealBox.translate(15, 25);// print the moved rectangleSystem.out.println(cerealBox);}}Printsjava.awt.Rectangle[x=20, y=35, width=20, height=30]public means usable by everything, public is also a keywordclass means instruction and data formaking objects,class is a keywordMoveTest is the name of the classA class name must match the file name.Names are also called identifiers.Identifiers and keywords are mutually exclusive.CompSci 4 2.8Intro to JavaMoveTest.javaimport java.awt.Rectangle;public class MoveTest{public static void main(String[] args){Rectangle cerealBox = new Rectangle(5, 10, 20, 30);// move the rectanglecerealBox.translate(15, 25);// print the moved rectangleSystem.out.println(cerealBox);}}class declarationclass definition.class body{} Starts and endsclass bodyCompSci 4 2.9Intro to JavaMoveTest.javaimport java.awt.Rectangle;public class MoveTest{public static void main(String[] args){Rectangle cerealBox = new Rectangle(5, 10, 20, 30);// move the rectanglecerealBox.translate(15, 25);// print the moved rectangleSystem.out.println(cerealBox);}}Printsjava.awt.Rectangle[x=20, y=35, width=20, height=30]Static means one per classvoid means no return valuemain is the nameof the methodCompSci 4 2.10Intro to JavaMoveTest.javaimport java.awt.Rectangle;public class MoveTest{public static void main(String[] args){Rectangle cerealBox = new Rectangle(5, 10, 20, 30);// move the rectanglecerealBox.translate(15, 25);// print the moved rectangleSystem.out.println(cerealBox);}}Printsjava.awt.Rectangle[x=20, y=35, width=20, height=30]String is a sequence of characters[] means an arrayargs is a parameterCompSci 4 2.11Intro to JavaMoveTest.javaimport java.awt.Rectangle;public class MoveTest{public static void main(String[] args){Rectangle cerealBox = new Rectangle(5, 10, 20, 30);// move the rectanglecerealBox.translate(15, 25);// print the moved rectangleSystem.out.println(cerealBox);}}Printsjava.awt.Rectangle[x=20, y=35, width=20, height=30]method declarationmethod definitionmethod body{} Starts and endsmethod bodyCompSci 4 2.12Intro to JavaMoveTest.javaimport java.awt.Rectangle;public class MoveTest{public static void main(String[] args){Rectangle cerealBox = new Rectangle(5, 10, 20, 30);// move the rectanglecerealBox.translate(15, 25);// print the moved rectangleSystem.out.println(cerealBox);}}Printsjava.awt.Rectangle[x=20, y=35, width=20, height=30]Rectangle is a type (also a class)cerealBox is a variableCreates a RectangleCalls the constructor ofthe Rectangle classCompSci 4 2.13Intro to JavaMoveTest.javaimport java.awt.Rectangle;public class MoveTest{public static void main(String[] args){Rectangle cerealBox = new Rectangle(5, 10, 20, 30);// move the rectanglecerealBox.translate(15, 25);// print the moved rectangleSystem.out.println(cerealBox);}}Printsjava.awt.Rectangle[x=20, y=35, width=20, height=30]Declaring the cerealBox variable of type RectangleInitializing the cerealBox variableCompSci 4 2.14Intro to JavaMoveTest.javaimport java.awt.Rectangle;public class MoveTest{public static void main(String[] args){Rectangle cerealBox = new Rectangle(5, 10, 20, 30);// move the rectanglecerealBox.translate(15, 25);// print the moved rectangleSystem.out.println(cerealBox);}}Printsjava.awt.Rectangle[x=20, y=35, width=20, height=30]Assignment operatorPronounced “gets”1. Computes the right hand side2. Assigns value to left hand sideCompSci 4 2.15Intro to JavaMoveTest.javaimport java.awt.Rectangle;public class MoveTest{public static void main(String[] args){Rectangle cerealBox = new Rectangle(5, 10, 20, 30);// move the rectanglecerealBox.translate(15, 25);// print the moved rectangleSystem.out.println(cerealBox);}}Printsjava.awt.Rectangle[x=20, y=35, width=20, height=30]Arguments – order mattersCompSci 4 2.16Intro to JavaMoveTest.javaimport java.awt.Rectangle;public class MoveTest{public static void main(String[] args){Rectangle cerealBox = new Rectangle(5, 10, 20, 30);// move the rectanglecerealBox.translate(15, 25);// print the moved rectangleSystem.out.println(cerealBox);}}Printsjava.awt.Rectangle[x=20, y=35, width=20, height=30]CommentsIgnored by compilerCompSci 4 2.17Intro to JavaMoveTest.javaimport java.awt.Rectangle;public class MoveTest{public static void main(String[] args){Rectangle cerealBox = new Rectangle(5, 10, 20, 30);// move the rectanglecerealBox.translate(15, 25);// print the moved
View Full Document