©Xiaoying Gao, Peter Andreae First Java Program COMP 102 #2 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington
©Xiaoying Gao, Peter Andreae COMP 102 2:2 Menu Simple Java Programs Cycle: design, edit, compile, test Program elements Reading: Text Book Chapter 1 Admin: Assignment 1 is handed out. Sign up for two lab sessions! (Labs start tomorrow) Voting for a Class Rep Put a message about yourself on the forum if you want to be class representative; the class will vote next week. Trouble with passwords? Go to school office: CO 358 School Study Help: CO253
©Xiaoying Gao, Peter Andreae COMP 102 2:3 First Java Program import ecs100.*; public class FirstProgram { public void firstMethod() { UI.println( “Hello World" ); }
©Xiaoying Gao, Peter Andreae COMP 102 2:4 Machine & Assembly Language What the computer can understand Different for each computer Very detailed, low-level control of the computer Horrible to read : : copy the contents of memory location 143 into register 1. add the contents of memory location 116 to the contents of register 1. copy the contents of register 1 to memory location 181. : LD d1 143 AD d1 116 ST d1 181 :
©Xiaoying Gao, Peter Andreae COMP 102 2:5 High Level Programming Languages designed for people to use designed to be translated into machine language Must be Precise: no ambiguity about what to do Expressive:must be able to specify whatever you want done. Readable: People must be able to read the instructions. Translatable:able to be translated into machine language Concise: not “long-winded” or redundant Smalltalk ML Ada C++ Eiffel Prolog Haskell Miranda Java C# Python Scratch GameMaker Alice FORTRAN LISP Algol COBOL Basic C Pascal Simula Modula PHP Javascript
©Xiaoying Gao, Peter Andreae COMP 102 2:6 Java A high-level Object-Oriented programming language Designed by Sun Microsystems, early-mid 1990's. Widely used in teaching and industry. Related to C++, but simpler. Similar to C#. Good for interactive applications. Supports implementation in Web environment (applets). Extensive libraries of predefined classes to support, UIs, graphics, databases, web applications,... Very portable between kinds of computers.
© Xiaoying Gao, Peter Andreae COMP 102 3:7 Constructing Programs Design Edit typing in the Java code Compile Translating to executable instructions Run Testing the program to see that it works Specification syntax errors logic errors
© Xiaoying Gao, Peter Andreae COMP 102 3:8 BlueJ BlueJ is an IDE for Java (Integrated Development Environment) Class manager, for keeping track of the files in your program Editor for entering and modifying the program Built-in compiler interface to help compile and fix the syntax errors Special interface to make it easy to construct objects and call methods on them.
© Xiaoying Gao, Peter Andreae COMP 102 3:9 Using BlueJ for Java Programs Simple use of BlueJ for simple programs: 1.Edit the class file(s) to define the methods 2.Compile the class 3.Create an object of the class right click on the rectangle representing the class select “new…..” ⇒ a red square representing the object 4.Call methods on the object right click on the square representing the object select the method.
© Xiaoying Gao, Peter Andreae Example 1 import ecs100.*; public class Example1{ public void welcomeMessage(){ String name = "Sharon"; String course = "COMP102"; UI.println(“My name is " + name); UI.println("Welcome to " + course); } COMP 102 3:10
© Xiaoying Gao, Peter Andreae Example 2 /** An example for simple string Inout/Output. The way to generate loginName is much more simplified */ import ecs100.*; public class Example2{ public void printMessage(){ String course = "COMP102"; String name = UI.askString("type your first name: "); String family = UI.askString("your family name? "); UI.println("Hello "+ name + ", Welcome to " + course); String loginName = family.substring(0,2) + name; UI.println("your Login name is " + loginName); } COMP 102 3:11
© Xiaoying Gao, Peter Andreae DrawLabel import ecs100.*; public class DrawLabel { public void drawSimple() { double x = 100; double y = x + 200; double w = 223; double h = w/2; UI.drawRect(100,200,50,50); UI.drawRect(x, y, w, h); } COMP 102 3:12
© Xiaoying Gao, Peter Andreae COMP 102 3:13 Elements of the program Comments Keywords : words with special meaning in the Java Language, eg: public, class, if, while mostly to do with the structure of the program Identifiers : other words, used to refer to things in the program. mostly made up by the programmer, some are predefined. Strings : bits of text that the program will manipulate. always surrounded by “ and ” numbers operators and punctuation : + - * / ;, ( ) { } [ ] all have precise meanings and rules for use
© Xiaoying Gao, Peter Andreae COMP 102 3:14 Elements of the program Program Structure: Import list the library files you will use. Class Top level components of program Describes a class of objects Specifies the set of actions this kind of object can perform (Can also specify information the objects can hold) Note name, and conventions for naming. Methods Main elements of a class Each method describes one action that the objects of this class can perform
© Xiaoying Gao, Peter Andreae Variables A place to hold a value Type Name Value int x = 100; COMP 102 3:15
© Xiaoying Gao, Peter Andreae Actions in a program Method calls telling an object to do one of its methods, passing the necessary information: UI.println(“Hello"); What are the objects? what are the methods. UI object has methods for printing asking reading drawing …. Assignment statements putting a value in a place Int x = y/2; COMP 102 3:16