Download presentation
Presentation is loading. Please wait.
Published byAlicia McBride Modified over 8 years ago
1
CompSci 42.1Intro to Java Anatomy of a Class & Terminology Running and Modifying a Program
2
CompSci 42.2Intro to Java The 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)
3
CompSci 42.3Intro to Java 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
4
CompSci 42.4Intro to Java 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
5
CompSci 42.5Intro to Java MoveTester.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); } Prints java.awt.Rectangle[x=20, y=35, width=20, height=30]
6
CompSci 42.6Intro to Java 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); } Prints java.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
7
CompSci 42.7Intro to Java 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); } Prints java.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 class A class name must match the file name. Names are also called identifiers. Identifiers and keywords are mutually exclusive.
8
CompSci 42.8Intro to Java 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 ends class body
9
CompSci 42.9Intro to Java 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); } Prints java.awt.Rectangle[x=20, y=35, width=20, height=30] Static means one per class void means no return value main is the name of the method
10
CompSci 42.10Intro to Java 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); } Prints java.awt.Rectangle[x=20, y=35, width=20, height=30] String is a sequence of characters [] means an array args is a parameter
11
CompSci 42.11Intro to Java 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); } Prints java.awt.Rectangle[x=20, y=35, width=20, height=30] method declaration method definition method body {} Starts and ends method body
12
CompSci 42.12Intro to Java 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); } Prints java.awt.Rectangle[x=20, y=35, width=20, height=30] Rectangle is a type (also a class) cerealBox is a variable Creates a Rectangle Calls the constructor of the Rectangle class
13
CompSci 42.13Intro to Java 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); } Prints java.awt.Rectangle[x=20, y=35, width=20, height=30] Declaring the cerealBox variable of type Rectangle Initializing the cerealBox variable
14
CompSci 42.14Intro to Java 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); } Prints java.awt.Rectangle[x=20, y=35, width=20, height=30] Assignment operator Pronounced “gets” 1.Computes the right hand side 2.Assigns value to left hand side
15
CompSci 42.15Intro to Java 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); } Prints java.awt.Rectangle[x=20, y=35, width=20, height=30] Arguments – order matters
16
CompSci 42.16Intro to Java 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); } Prints java.awt.Rectangle[x=20, y=35, width=20, height=30] Comments Ignored by compiler
17
CompSci 42.17Intro to Java 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); } Prints java.awt.Rectangle[x=20, y=35, width=20, height=30] Rectangle object Also a local variable Local variables are declared in method bodies Calling the translate method On the cerealBox object
18
CompSci 42.18Intro to Java 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); } Prints java.awt.Rectangle[x=20, y=35, width=20, height=30] Calling the println method For console output
19
CompSci 42.19Intro to Java Why know and follow the Java 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
20
CompSci 42.20Intro to Java Coding Conventions Capitalization Class identifier Variable identifier Method identifier Indentation Braces Body of code (also called a code block) See course webpage for a complete description
21
CompSci 42.21Intro to Java 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()); }
22
CompSci 42.22Intro to Java Greeter.java public class Greeter { public Greeter(String aName) { name = aName; } public String sayHello() { String message = "Hello, " + name + "!"; return message; } private String name; } Constructor Used to initialize instance variables Has no return type, not even void Name is the same as the class name Declaration of instance variable Outside method body Inside class body
23
CompSci 42.23Intro to Java 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
24
CompSci 42.24Intro to Java Introduction to Java Downloading Source Code Set snarf site to: http://www.cs.duke.edu/courses/spring06/cps004/snarf
25
CompSci 42.25Intro to Java Introduction to Java Using Eclipse Starting Eclipse Making a project Importing files into a project Editing source code Compiling source code Executing source code Saving project
26
CompSci 42.26Intro to Java Starting Eclipse Double Click on Math Apps on the Desktop Double Click on eclipse.exe
27
CompSci 42.27Intro to Java Making a project File->New->Project Java->Java Project Click on Next For Project Name type Sample Click on Finish Switch to Java Perspective? Yes
28
CompSci 42.28Intro to Java Importing files into a project File->Import Zip file Click on Next From zip file Click on Browse Click on Desktop Click pong.jar Open For Into folder Click on Browse and select Sample ok Finish
29
CompSci 42.29Intro to Java Executing source code Click on + by Sample Click on + by pong Click on Pong.java Run->Run as->Java Application Play the game Using the mouse Using the keys ‘i’ and ‘m’ (click on red square near bottom right to end)
30
CompSci 42.30Intro to Java Editing source code Double Click on PongLoop.java Go to line 69 where it says position1.y-=2; (you’ll see 69:23 in the lower right hand corner as you move the cursor in the code) Change it to say position1.y-=8; Click on the disk icon to save (or File->Save)
31
CompSci 42.31Intro to Java Compiling source code Always be sure to save any changes before compiling (done in the previous step) (compile is done automatically when you save)
32
CompSci 42.32Intro to Java Executing the modified code Click on Pong.java Run->Run as->Java Application (or click on icon with white triangle in green circle) (this will repeat the last Run->Run as->Java Application) What changed?
33
CompSci 42.33Intro to Java Saving project During class projects are saved on the desktop Before leaving the lab be sure to save your project to your acpub account
34
CompSci 42.34Intro to Java Saving project Click on Sample File->Export Jar file Click on Next Click on Export java source files and resources For Select the export destination Browse ICC229 (My Computer) Select P drive Create and/or change into cps4 folder Use filename mypong.jar Click on Finish
35
CompSci 42.35Intro to Java Assignment #1 Create your Class Web Page See assignment on web Due Tuesday, 1/24 (Assoignment #0 Due Today!)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.