Anatomy of a Class Terminology Comp Sci 4

  • Slides: 24
Download presentation
Anatomy of a Class & Terminology Comp. Sci 4 2. 1

Anatomy of a Class & Terminology Comp. Sci 4 2. 1

The Plan v Go over Move. Test. java q v v v Similar to

The Plan v Go over Move. Test. java q v v v Similar to Horstmann p. 48 Basic coding conventions Review with Greeter. Test. java (Horstmann) More terminology with Greeter. java (Horstmann) Homework 0 reminder Homework 1 assigned (due in 1 week) Comp. Sci 4 2. 2

Why know the lingo? v v It’s difficult to read the textbooks if you

Why know the lingo? v v It’s difficult to read the textbooks if you don’t understand the words Your compiler error messages use specific words with specific meanings You need to be able to express your questions so others can understand them The answers you receive will use the lingo Comp. Sci 4 2. 3

Terminology to Know v v v v Package Class Import Keyword Public Object Identifier

Terminology to Know v v v v Package Class Import Keyword Public Object Identifier Declaration Definition Body Static Void Return Method Comp. Sci 4 v v v v Main Parameter String Array Type Variable Local Constructor Initialize Assign Arguments Comments Calling a method System. out. println 2. 4

Move. Tester. java import java. awt. Rectangle; public class Move. Test { public static

Move. Tester. java import java. awt. Rectangle; public class Move. Test { public static void main(String[] args) { Rectangle ceral. Box = new Rectangle(5, 10, 20, 30); // move the rectangle ceral. Box. translate(15, 25); // print the moved rectangle System. out. println(cereal. Box); } } Prints java. awt. Rectangle[x=20, y=35, width=20, height=30] Comp. Sci 4 2. 5

java package Move. Test. java import java. awt. Rectangle; abstract windowing toolkit package Rectangle

java package Move. Test. java import java. awt. Rectangle; abstract windowing toolkit package Rectangle class public class Move. Test { public static void main(String[] args) { Rectangle cereal. Box = new Rectangle(5, 20, into 30); import means to 10, bring the // move the rectangle program classes or packages not cereal. Box. translate(15, 25); in the package java. lang // print the moved rectangle System. out. println(cereal. Box); import is a keyword – it is a } } word with a special meaning Prints java. awt. Rectangle[x=20, y=35, width=20, height=30] Comp. Sci 4 2. 6

public means usable by everything, public is also a keyword Move. Test. java class

public means usable by everything, public is also a keyword Move. Test. java class means instruction and data for making objects, class is a keyword import java. awt. Rectangle; public class Move. Test { public static void main(String[] args) { Rectangle cereal. Box = new Rectangle(5, 10, 20, 30); // move the rectangle cereal. Box. translate(15, 25); // print the moved rectangle System. out. println(cereal. Box); } } Move. Test is the name of the class A class name must match the file name. Prints Names are also called identifiers. java. awt. Rectangle[x=20, y=35, width=20, height=30] Identifiers and keywords are mutually exclusive. Comp. Sci 4 2. 7

Move. Test. java class declaration import java. awt. Rectangle; public class Move. Test {

Move. Test. java class declaration import java. awt. Rectangle; public class Move. Test { public static void main(String[] args) { Rectangle cereal. Box = new Rectangle(5, 10, 20, 30); // move the rectangle cereal. Box. translate(15, 25); // print the moved rectangle System. out. println(cereal. Box); } } {} Starts and ends class body Comp. Sci 4 class definition. class body 2. 8

Move. Test. java Static means one per class import java. awt. Rectangle; void means

Move. Test. java Static means one per class import java. awt. Rectangle; void means no return value public class Move. Test { public static void main(String[] args) { Rectangle cereal. Box = new Rectangle(5, 10, 20, 30); // move the rectangle cereal. Box. translate(15, 25); // print the moved rectangle main is the name System. out. println(cereal. Box); of the method } } Prints java. awt. Rectangle[x=20, y=35, width=20, height=30] Comp. Sci 4 2. 9

Move. Test. java import java. awt. Rectangle; String is a sequence of characters []

Move. Test. java import java. awt. Rectangle; String is a sequence of characters [] means an array public class Move. Test { public static void main(String[] args) { Rectangle cereal. Box = new Rectangle(5, 10, 20, 30); // move the rectangle args is a parameter cereal. Box. translate(15, 25); // print the moved rectangle System. out. println(cereal. Box); } } Prints java. awt. Rectangle[x=20, y=35, width=20, height=30] Comp. Sci 4 2. 10

Move. Test. java method declaration import java. awt. Rectangle; public class Move. Test {

Move. Test. java method declaration import java. awt. Rectangle; public class Move. Test { public static void main(String[] args) { Rectangle cereal. Box = new Rectangle(5, 10, 20, 30); // move the rectangle {} Starts and ends cereal. Box. translate(15, 25); method body // print the body moved rectangle System. out. println(cereal. Box); } } Prints java. awt. Rectangle[x=20, y=35, width=20, height=30] method definition Comp. Sci 4 2. 11

Move. Test. java Rectangle is a type (also a class) import java. awt. Rectangle;

Move. Test. java Rectangle is a type (also a class) import java. awt. Rectangle; public class Move. Test cereal. Box is a variable { public static void main(String[] args) { Rectangle cereal. Box = new Rectangle(5, 10, 20, 30); // move the rectangle cereal. Box. translate(15, 25); // print the moved rectangle Creates a Rectangle System. out. println(cereal. Box); Calls the constructor } } the Rectangle class of Prints java. awt. Rectangle[x=20, y=35, width=20, height=30] Comp. Sci 4 2. 12

Move. Test. java import java. awt. Rectangle; Declaring the cereal. Box variable of type

Move. Test. java import java. awt. Rectangle; Declaring the cereal. Box variable of type Rectangle public class Move. Test { public static void main(String[] args) { Rectangle cereal. Box = new Rectangle(5, 10, 20, 30); // move the rectangle cereal. Box. translate(15, 25); // print the moved rectangle System. out. println(cereal. Box); } } Initializing the cereal. Box variable Prints java. awt. Rectangle[x=20, y=35, width=20, height=30] Comp. Sci 4 2. 13

Move. Test. java import java. awt. Rectangle; public class Move. Test { public static

Move. Test. java import java. awt. Rectangle; public class Move. Test { public static void main(String[] args) { Rectangle cereal. Box = new Rectangle(5, 10, 20, 30); // move the rectangle cereal. Box. translate(15, 25); // print the moved rectangle System. out. println(cereal. Box); } } Assignment operator Pronounced “gets” Prints 1. Computes the right hand java. awt. Rectangle[x=20, y=35, width=20, height=30] side 2. Assigns value to left hand side Comp. Sci 4 2. 14

Move. Test. java import java. awt. Rectangle; Arguments – order matters public class Move.

Move. Test. java import java. awt. Rectangle; Arguments – order matters public class Move. Test { public static void main(String[] args) { Rectangle cereal. Box = new Rectangle(5, 10, 20, 30); // move the rectangle cereal. Box. translate(15, 25); // print the moved rectangle System. out. println(cereal. Box); } } Prints java. awt. Rectangle[x=20, y=35, width=20, height=30] Comp. Sci 4 2. 15

Move. Test. java import java. awt. Rectangle; Comments Ignored by compiler public class Move.

Move. Test. java import java. awt. Rectangle; Comments Ignored by compiler public class Move. Test { public static void main(String[] args) { Rectangle cereal. Box = new Rectangle(5, 10, 20, 30); // move the rectangle cereal. Box. translate(15, 25); // print the moved rectangle System. out. println(cereal. Box); } } Prints java. awt. Rectangle[x=20, y=35, width=20, height=30] Comp. Sci 4 2. 16

Move. Test. java import java. awt. Rectangle; Local variables are declared in method bodies

Move. Test. java import java. awt. Rectangle; Local variables are declared in method bodies public class Move. Test { public static void main(String[] args) { Rectangle cereal. Box = new Rectangle(5, 10, 20, 30); // move the rectangle cereal. Box. translate(15, 25); // print the moved rectangle System. out. println(cereal. Box); } Calling the translate method } On the cereal. Box object Rectangle object Also a local variable Prints java. awt. Rectangle[x=20, y=35, width=20, height=30] Comp. Sci 4 2. 17

Move. Test. java import java. awt. Rectangle; Calling the println method For console output

Move. Test. java import java. awt. Rectangle; Calling the println method For console output public class Move. Test { public static void main(String[] args) { Rectangle cereal. Box = new Rectangle(5, 10, 20, 30); // move the rectangle cereal. Box. translate(15, 25); // print the moved rectangle System. out. println(cereal. Box); } } Prints java. awt. Rectangle[x=20, y=35, width=20, height=30] Comp. Sci 4 2. 18

Why know and follow the Java Coding Conventions v Helps understand code q q

Why know and follow the Java Coding Conventions v Helps understand code q q q v v makes purpose of identifiers clear delineates separate pieces of code assists in avoiding syntax errors Expected if source code is to be viewed at any time by anyone other than the original author Helps standardize Comp. Sci 4 2. 19

Coding Conventions v Capitalization q q q v Indentation q q v Class indentifier

Coding Conventions v Capitalization q q q v Indentation q q v Class indentifier Variable identifier Method identifier Braces Body of code (also called a code block) See course webpage for a complete description Comp. Sci 4 2. 20

Greeter. Test. java public class Greeter. Test { public static void main(String[] args) {

Greeter. Test. java public class Greeter. Test { public static void main(String[] args) { Greeter world. Greeter = new Greeter("World"); System. out. println(world. Greeter. say. Hello()); Greeter dave. Greeter = new Greeter("Dave"); System. out. println(dave. Greeter. say. Hello()); } } Comp. Sci 4 2. 21

Greeter. java public class Greeter { public Greeter(String a. Name) { Constructor name =

Greeter. java public class Greeter { public Greeter(String a. Name) { Constructor name = a. Name; } Used to initialize instance variables Has no return type, not even void Name is the same as the class name public String say. Hello() { String message = "Hello, " + name + "!"; return message; } private String name; } Comp. Sci 4 Declaration of instance variable Outside method body Inside class body 2. 22

Greeter. java public class Greeter { public Greeter(String a. Name) { name = a.

Greeter. java public class Greeter { public Greeter(String a. Name) { name = a. Name; } Empty parameter list String concatenation public String say. Hello() { String message = "Hello, " + name + "!"; return message; } private String name; } Comp. Sci 4 Private means only accessible within class body 2. 23

Homework v v v Homework 0 due by 5 pm today Homework 1 linked

Homework v v v Homework 0 due by 5 pm today Homework 1 linked from website and due next week Keep up with the readings – should have already read (? ) first two chapters of Head First Java Comp. Sci 4 2. 24