Presentation is loading. Please wait.

Presentation is loading. Please wait.

David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.

Similar presentations


Presentation on theme: "David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP."— Presentation transcript:

1 David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP 112 2014T1 #2

2 COMP 112 2: 2 BlueJ - an IDE for Java Java is Object Oriented and has extensive Libraries. BlueJ is an Integrated Development Environment BlueJ allows Objects to be built form a Class file Methods of an object to be executed Libraries to be easily integrated

3 COMP 112 2: 3 Objects Objects are a collection of: Private data (fields) Public methods Decomposing a problem into useful classes and objects is sometimes straightforward. Example a student record system might have: 1.A Student class and a Student object for each student. 2.A Course class and a Course object for each course 3.A course method could be Enroll that takes a student and enrolls them on the course.

4 COMP 112 2: 4 BlueJ an IDE Classes Right click (on class) New AddCounter (to make an object)

5 COMP 112 2: 5 BlueJ an IDE

6 COMP 112 2: 6 BlueJ an IDE Object Right click (to call methods)

7 COMP 112 2: 7 Values are Typed in Java There are lots of different kinds of values: Numbers Integers( int or long) 42 -194573203 real numbers ( double or float ) 16.43 6.626e-34 … Characters ( char )'X' '4' Text ( String ) " F -> " Colours ( Color )Color.red Color.green Integer (same as int but an object) …

8 COMP 112 2: 8 Java is Object Oriented Typed 1.Primitive a.int b.Boolean, … 2.Reference a.String b.ArrayList, … Compiled and runs on a Virtual machine Has garbage collection

9 COMP 112 2: 9 Primitives and Objects Car21 Car z = new Car(2); Ov 2 z Car21 int x = 1; x 1 x names a piec e of memory in which the value 1 is stored The value in z is the reference to the Object.

10 COMP 112 2: 10 Objects in Java Class defines Objects Used to structure code Objects contain: Fields (hold data) Methods (can be executed) Constructor methods Have same name as Class Have no return type Used to construct Object of the Class Best understood by an example.

11 COMP 112 2: 11 Simple Java Class Private FieldClass Name Class Declaration Public Method

12 COMP 112 2: 12 Java Method Variable Declaration Variable assignment Parameter TypeMethod nameReturn Type

13 COMP 112 2: 13 Java Program Library Constructor Main method

14 COMP 112 2: 14 Main - called from terminal

15 COMP 112 2: 15 Structure of the program Import statements Comment Class definition (defining the objects) Class header Method definitions (defining Actions the objects can do) Method header Instructions for performing the action

16 COMP 112 2: 16 Syntax rules: Program structure First version 〈import statements 〉 public class 〈 method descriptions 〉 { } 〈 classname 〉 Comments can be added anywhere Import ecs100; import java.awt.Color;

17 COMP 112 2: 17 Comments Three kinds of comments: Documentation comments eg /** Program for converting between temperature scales */ end-of-line comments eg double fahren = celsius * 9 / 5 + 32; // compute answer anywhere comments eg /* double fahren = celsius * 9 / 5 + 32; UI.println(celcius + “C is " + fahren + " F"); */ /** 〈 text of comment 〉 */ // 〈 text of comment 〉 Top of class, Before each method at end of any line /* 〈 text of comment 〉 */ multi-line, or middle of line, or …

18 COMP 112 2: 18 Method Definitions /** Print out the conversion formulas */ public void printFormula ( ) { UI.println("Celsius = (Fahrenheit - 32) *5/9"); } 〈 Doc Comment 〉〈 Header 〉〈 Body 〉 {} public void () 〈 name 〉 〈 parameters 〉 instructions to perform this action Specifying the information the action needs. May be empty

19 COMP 112 2: 19 Java Java is very portable Java byte code runs on a Java virtual machine 1.Name.java is source code 2.To compile into byte code > javac -classpath./ecs100.jar Name.java 3.Name.class is the byte code 4.To execute you run the JVM with the byte code >java -classpath ".:ecs100.jar" Name 5.To build executable build a.jar file


Download ppt "David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP."

Similar presentations


Ads by Google