Top Banner
Anatomy.1 Anatomy of a Class & Terminology
24

Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.

Jan 05, 2016

Download

Documents

Lucinda Riley
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.

Anatomy.1

Anatomy of a Class& Terminology

Page 2: Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.

Anatomy.2

The Plan

• Go over MoveTest.java from Big Java

• Basic coding conventions

• Review with GreeterTest.java

• More terminology with Greeter.java

• Homework 0 reminder

• Homework 1 assigned (due in 1 week)

Page 3: Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.

Anatomy.3

Why know the lingo?

• 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

Page 4: Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.

Anatomy.4

Terminology 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.println

Page 5: Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.

Anatomy.5

MoveTest.java

import java.awt.Rectangle;

public class MoveTest{ public static void main(String[] args) { Rectangle cerealBox = new Rectangle(5, 10, 20, 30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.out.println(cerealBox); }}

Printsjava.awt.Rectangle[x=20, y=35, width=20, height=30]

Page 6: Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.

Anatomy.6

MoveTest.java

import java.awt.Rectangle;

public class MoveTest{ public static void main(String[] args) { Rectangle cerealBox = new Rectangle(5, 10, 20, 30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.out.println(cerealBox); }}

Printsjava.awt.Rectangle[x=20, y=35, width=20, height=30]

abstract windowing toolkit package

Rectangle class

java package

import means to bring into the program classes or packages not in the package java.lang

import is a keyword – it is a word with a special meaning

Page 7: Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.

Anatomy.7

MoveTest.java

import java.awt.Rectangle;

public class MoveTest{ public static void main(String[] args) { Rectangle cerealBox = new Rectangle(5, 10, 20, 30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.out.println(cerealBox); }}

Printsjava.awt.Rectangle[x=20, y=35, width=20, height=30]

public means usable by everything, public is also a keyword

class means instruction and data for making objects,class is a keyword

MoveTest is the name of the classA class name must match the file name.Names are also called identifiers.Identifiers and keywords are mutually exclusive.

Page 8: Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.

Anatomy.8

MoveTest.java

import java.awt.Rectangle;

public class MoveTest

{

public static void main(String[] args)

{

Rectangle cerealBox = new Rectangle(5, 10, 20, 30);

// move the rectangle

cerealBox.translate(15, 25);

// print the moved rectangle

System.out.println(cerealBox);

}

}

class declaration

class definition.

class body

{} Starts and endsclass body

Page 9: Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.

Anatomy.9

MoveTest.java

import java.awt.Rectangle;

public class MoveTest{ public static void main(String[] args) { Rectangle cerealBox = new Rectangle(5, 10, 20, 30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.out.println(cerealBox); }}

Printsjava.awt.Rectangle[x=20, y=35, width=20, height=30]

Static means one per class

void means no return value

main is the nameof the method

Page 10: Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.

Anatomy.10

MoveTest.java

import java.awt.Rectangle;

public class MoveTest{ public static void main(String[] args) { Rectangle cerealBox = new Rectangle(5, 10, 20, 30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.out.println(cerealBox); }}

Printsjava.awt.Rectangle[x=20, y=35, width=20, height=30]

String is a sequence of characters

[] means an array

args is a parameter

Page 11: Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.

Anatomy.11

MoveTest.java

import java.awt.Rectangle;

public class MoveTest{ public static void main(String[] args) { Rectangle cerealBox = new Rectangle(5, 10, 20, 30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.out.println(cerealBox); }}

Printsjava.awt.Rectangle[x=20, y=35, width=20, height=30]

method declaration

method definition

method body{} Starts and endsmethod body

Page 12: Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.

Anatomy.12

MoveTest.java

import java.awt.Rectangle;

public class MoveTest{ public static void main(String[] args) { Rectangle cerealBox = new Rectangle(5, 10, 20, 30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.out.println(cerealBox); }}

Printsjava.awt.Rectangle[x=20, y=35, width=20, height=30]

Rectangle is a type (also a class)

cerealBox is a variable

Creates a RectangleCalls the constructor ofthe Rectangle class

Page 13: Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.

Anatomy.13

MoveTest.java

import java.awt.Rectangle;

public class MoveTest{ public static void main(String[] args) { Rectangle cerealBox = new Rectangle(5, 10, 20, 30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.out.println(cerealBox); }}

Printsjava.awt.Rectangle[x=20, y=35, width=20, height=30]

Declaring the cerealBox variable of type Rectangle

Initializing the cerealBox variable

Page 14: Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.

Anatomy.14

MoveTest.java

import java.awt.Rectangle;

public class MoveTest{ public static void main(String[] args) { Rectangle cerealBox = new Rectangle(5, 10, 20, 30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.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 side

Page 15: Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.

Anatomy.15

MoveTest.java

import java.awt.Rectangle;

public class MoveTest{ public static void main(String[] args) { Rectangle cerealBox = new Rectangle(5, 10, 20, 30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.out.println(cerealBox); }}

Printsjava.awt.Rectangle[x=20, y=35, width=20, height=30]

Arguments – order matters

Page 16: Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.

Anatomy.16

MoveTest.java

import java.awt.Rectangle;

public class MoveTest{ public static void main(String[] args) { Rectangle cerealBox = new Rectangle(5, 10, 20, 30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.out.println(cerealBox); }}

Printsjava.awt.Rectangle[x=20, y=35, width=20, height=30]

CommentsNot read by compiler

Page 17: Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.

Anatomy.17

MoveTest.java

import java.awt.Rectangle;

public class MoveTest{ public static void main(String[] args) { Rectangle cerealBox = new Rectangle(5, 10, 20, 30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.out.println(cerealBox); }}

Printsjava.awt.Rectangle[x=20, y=35, width=20, height=30]

Rectangle objectAlso a local variable

Local variables are declared inmethod bodies

Calling the translate methodOn the cerealBox object

Page 18: Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.

Anatomy.18

MoveTest.java

import java.awt.Rectangle;

public class MoveTest{ public static void main(String[] args) { Rectangle cerealBox = new Rectangle(5, 10, 20, 30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.out.println(cerealBox); }}

Printsjava.awt.Rectangle[x=20, y=35, width=20, height=30]

Calling the println methodFor console output

Page 19: Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.

Anatomy.19

Why know and follow theJava Coding Conventions

• Helps understand code– 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

Page 20: Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.

Anatomy.20

Coding Conventions

• Capitalization– Class indentifier

– Variable identifier

– Method identifier

• Indentation– Brackets

– Body of code (also called a code block)

• See course webpage for a complete description

Page 21: Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.

Anatomy.21

GreeterTest.java

public class GreeterTest

{

public static void main(String[] args)

{

Greeter worldGreeter = new Greeter("World");

System.out.println(worldGreeter.sayHello());

Greeter daveGreeter = new Greeter("Dave");

System.out.println(daveGreeter.sayHello());

}

}

Page 22: Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.

Anatomy.22

Greeter.java

public class Greeter{ public Greeter(String aName) { name = aName; }

public String sayHello() { String message = "Hello, " + name + "!"; return message; }

private String name;}

ConstructorUsed to initialize instance variablesHas no return type, not even voidName is the same as the class name

Declaration of instance variableOutside method bodyInside class body

Page 23: Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.

Anatomy.23

public class Greeter{ public Greeter(String aName) { name = aName; }

public String sayHello() { String message = "Hello, " + name + "!"; return message; }

private String name;}

Greeter.java

Empty parameter list

String concatenation

Private means only accessible within class body

Page 24: Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.

Anatomy.24

Homework

• Homework 0 due by 5pm today

• Homework 1 linked from website and due next week

• Keep up with the readings – should have already read first two chapters of Big Java