DOC PREVIEW
Duke CPS 004 - Intro to Java

This preview shows page 1-2 out of 7 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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

Duke CPS 004 - Intro to Java

Documents in this Course
Lecture

Lecture

18 pages

Chapter 7

Chapter 7

18 pages

Chapter 9

Chapter 9

15 pages

Java 1

Java 1

24 pages

Java 3

Java 3

11 pages

Lecture

Lecture

10 pages

Chapter 4

Chapter 4

28 pages

Chap 2

Chap 2

16 pages

Graphics

Graphics

20 pages

Lecture

Lecture

12 pages

HTML

HTML

16 pages

Java 1

Java 1

6 pages

Chapter 4

Chapter 4

16 pages

The Plan

The Plan

25 pages

Lecture

Lecture

16 pages

Chapter 6

Chapter 6

21 pages

Lecture

Lecture

18 pages

Lecture

Lecture

23 pages

Lecture

Lecture

16 pages

Lecture

Lecture

19 pages

Lecture

Lecture

12 pages

Lecture

Lecture

5 pages

Lecture

Lecture

26 pages

Lecture

Lecture

16 pages

Chapter 7

Chapter 7

23 pages

Lecture

Lecture

21 pages

Lecture

Lecture

4 pages

Lecture

Lecture

4 pages

Lecture

Lecture

8 pages

Lecture

Lecture

4 pages

Lecture

Lecture

10 pages

Chapter 4

Chapter 4

32 pages

Java

Java

4 pages

CompSci 4

CompSci 4

18 pages

Lecture

Lecture

26 pages

CompSci 4

CompSci 4

12 pages

HTML

HTML

17 pages

Lecture

Lecture

16 pages

Chapter 5

Chapter 5

22 pages

Lecture

Lecture

4 pages

Chapter 4

Chapter 4

10 pages

Chapter 2

Chapter 2

15 pages

Chapter 8

Chapter 8

14 pages

Lecture

Lecture

15 pages

Load more
Download Intro to Java
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Intro to Java and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Intro to Java 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?