Presentation is loading. Please wait.

Presentation is loading. Please wait.

First Java Program COMP 102 #2 2015T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.

Similar presentations


Presentation on theme: "First Java Program COMP 102 #2 2015T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington."— Presentation transcript:

1 First Java Program COMP 102 #2 2015T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington

2 ©Xiaoying Gao, Peter Andreae Admin Sign up for two lab sessions (Labs start today!) Lab time on MyAllocator is fixed Thursday 4-5 lab is cancelled Assignment 1 is handed out. 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 Reading: Done: Course outline Start: Assignment 1 Start: Text Book Chapter 1

3 ©Xiaoying Gao, Peter Andreae COMP 102 2:3 Menu Simple Java Programs Cycle: design, edit, compile, test Program elements

4 ©Xiaoying Gao, Peter Andreae COMP 102 2:4 First Java Program import ecs100.*; public class FirstClass { public void firstMethod() { UI.println( “Hello World!" ); }

5 ©Xiaoying Gao, Peter Andreae COMP 102 2:5 Machine & Assembly Language What the computer can understand Different for each computer Very detailed, low-level control of the computer Horrible to read : 00011001 01001111 01101001 00111010 00101001 10110101 : 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 :

6 ©Xiaoying Gao, Peter Andreae COMP 102 2:6 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

7 ©Xiaoying Gao, Peter Andreae COMP 102 2:7 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.

8 ©Xiaoying Gao, Peter Andreae Constructing Programs COMP 102 3:8 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

9 ©Xiaoying Gao, Peter Andreae 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. 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. COMP 102 3:9

10 ©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

11 ©Xiaoying Gao, Peter Andreae Example 2 /** An example for simple string Input/Output. The way to generate loginName is much more simplified Be aware this is not your real login name */ 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

12 ©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

13 ©Xiaoying Gao, Peter Andreae 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 COMP 102 3:13

14 ©Xiaoying Gao, Peter Andreae 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 COMP 102 3:14

15 ©Xiaoying Gao, Peter Andreae Variables A place to hold a value Type Name Value int x = 100; COMP 102 3:15

16 ©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 = 100 + y/2; COMP 102 3:16


Download ppt "First Java Program COMP 102 #2 2015T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington."

Similar presentations


Ads by Google