Intro to Java Anatomy of a Class Terminology

  • Slides: 25
Download presentation
Intro to Java • Anatomy of a Class & Terminology • Running and Modifying

Intro to Java • Anatomy of a Class & Terminology • Running and Modifying a Program Comp. Sci 4 Intro to Java 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 Intro to Java 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 Intro to Java 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 Intro to Java 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 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 Intro to Java 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 Intro to Java 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 Intro to Java 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. Intro to Java 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 Intro to Java 2. 9

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

Move. Test. java String is a sequence of characters import java. awt. Rectangle; [] 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 Intro to Java 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 Intro to Java 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 Intro to Java 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 Intro to Java 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 Intro to Java 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 Intro to Java 2. 15

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

Move. Test. java Comments Ignored by compiler 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); } } Prints java. awt. Rectangle[x=20, y=35, width=20, height=30] Comp. Sci 4 Intro to Java 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 Intro to Java 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 Intro to Java 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 Intro to Java 2. 19

Coding Conventions v Capitalization q q q v Indentation q q v Class identifier

Coding Conventions v Capitalization q q q v Indentation q q v Class identifier Variable identifier Method identifier Braces Body of code (also called a code block) See course webpage for a complete description Comp. Sci 4 Intro to Java 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 Intro to Java 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 Intro to Java 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 Intro to Java 2. 23

Introduction to Java Downloading Source Code v Start up Eclipse q v (In ICC:

Introduction to Java Downloading Source Code v Start up Eclipse q v (In ICC: In applications folder on desktop) Set snarf site to: http: //www. cs. duke. edu/courses/spring 06/cps 004/snarf Comp. Sci 4 Intro to Java 2. 24

Assignment #1 v Create your Class Web Page q See assignment on web v

Assignment #1 v Create your Class Web Page q See assignment on web v Due Tuesday, 1/24 v (Assignment #0 Due Today!) Comp. Sci 4 Intro to Java 2. 25