Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Program Elements and Syntax.

Slides:



Advertisements
Similar presentations
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
Advertisements

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
CMT Programming Software Applications
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
© Xiaoying Gao, Peter Andreae Class, method, statements COMP 102 #3 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
© Xiaoying Gao, Peter Andreae UI methods, Variables, Constants COMP 102 #4 2015T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
©Xiaoying Gao, Peter Andreae First Java Program COMP 102 #2 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
First Java Program COMP 102 #2 2015T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Input, Output, and Processing
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Call a Method,
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
C++ Programming: Basic Elements of C++.
Primitive Variables.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Exercise, printf,
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Primitive Variables.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods Calling Methods Return.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington UI methods, Graphical.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
© Xiaoying Gao, Peter Andreae Variables, Constants, UI methods COMP 102 #4 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Programs with Choice Booleans,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
© Peter Andreae Java Programs COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Program Elements and Syntax.
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Basic Elements of Java
C++ Programming Basics
Unit 3: Variables in Java
Presentation transcript:

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Program Elements and Syntax rules COMP T1 #4

© Peter Andreae COMP 102 4: 2 Menu More details about the Java Program Semantics (meaning) and Syntax (grammar rules) Methods Statements Variables, Types, Assignment, Values and Expressions Reading: L-D-C: Chapter 2 Announcements Submission: checking, deleting, completing…. Labs: how many need to sign up yet? Class Rep.

© Peter Andreae COMP 102 4: 3 TemperatureCalculator import ecs100.*; /** Program for converting between temperature scales */ public class TemperatureCalculator{ /** Convert from fahrenheit to celsius */ public void fahrenheitToCelsius(){ double fahren = UI.askDouble("Farenheit:"); double celsius = (fahren - 32) * 5 / 9; UI.println(fahren + “F is " + celsius + " C"); } /** Convert from celsius to fahrenheit */ public void celsiusToFahrenheit(){ double celsius = UI.askDouble("Celsius:"); double fahren = celsius * 9 / ; UI.println(celcius “C is " + fahren + " F"); } /** Print conversion formula */ public void printFormula ( ) { UI.println("Celsius = (Fahrenheit - 32) *5/9"); }

© Peter Andreae COMP 102 4: 4 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 Telling an object to perform some action Putting a value into a place

© Peter Andreae COMP 102 4: 5 Elements of the program Comments vs Code Keywords vs Identifiers vs Strings vs numbers vs operators and punctuation 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

© Peter Andreae COMP 102 4: 6 Writing your own programs How? Use other programs as models, and guess how to modify Very useful strategy Limiting Hard to work out what's wrong if you don’t understand it

© Peter Andreae COMP 102 4: 7 A new program Calculator to convert inches to meters and to feet and inches import ecs100.*; /** Program to convert centimeters to inches */ public class TemperatureCalculator{ /** Convert from fahrenheit to celsius */ public void fahrenheitToCelsius(){ double fahren = UI.askDouble("Farenheit:"); double celsius = (fahren - 32) * 5 / 9; UI.println(fahren + “F is " + celsius + " C"); }

© Peter Andreae COMP 102 4: 8 Writing your own programs How? Use other programs as models, and guess how to modify Very useful strategy Limiting Hard to work out what's wrong if you don’t understand it Understand the language – rules and vocabulary Unlimited Able to understand and reason about your program

© Peter Andreae COMP 102 4: 9 Syntax rules: Program structure First version 〈import statements 〉 public class 〈 method descriptions 〉 { } 〈 classname 〉 Comments can be added anywhere import ecs100.*; import java.awt.Color;

© Peter Andreae COMP 102 4: 10 Comments Three kinds of comments: Documentation comments eg /** Program for converting between temperature scales */ end-of-line comments eg double fahren = celsius * 9 / ; // compute answer anywhere comments eg /* double fahren = celsius * 9 / ; 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 …

© Peter Andreae COMP 102 4: 11 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

© Peter Andreae COMP 102 4: 12 “Statements” (instructions) (Single instructions are called “statements” for silly historical reasons!) Two important kinds of statements: method call statement: tell some object to perform one of its methods. eg: tell the UI object to ask the user for a number eg: tell the UI object to print out a string eg: tell the UI object to draw an oval assignment statement compute some value and put it in a place in memory.

© Peter Andreae COMP 102 4: 13 Method Calls /** Print out the conversion formulas */ public void printFormula(){ UI.println( "Celsius = (Fahrenheit - 32) *5/9" ); } Method call Statement: who. what ( data to use) ; UI. println ( “Celsius = (Fahre…” ) ; Meaning of Statement: Tell the object to perform the method using the argument values provided 〈 object 〉 〈 methodname 〉〈 arguments 〉.();

© Peter Andreae COMP 102 4: 14 Objects and their methods in Java What objects are there? Predefined eg: UIa "User Interface" window with several panes  println(….), askDouble(…), drawRect(…), clearGraphics() Systemrepresenting the computer system  currentTimeMillis( ) Mathmethods for mathematical calculations  random( ), sin(…) Others The object(s) defined by your program New objects that your program creates Some method calls return a value

© Peter Andreae COMP 102 4: 15 Values / Data There are lots of different kinds of values: Numbers Integers( int or long) real numbers ( double or float ) e-34 … Characters ( char )'X' '4' Text ( String ) " F -> " Colours ( Color )Color.red Color.green Other Objects …

© Peter Andreae COMP 102 4: 16 Variables /** Print out the conversion formulas (version 2) */ public void printFormula () { String formula; formula = "Celsius = (Fahrenheit - 32) *5/9" ; UI.println( formula ); } A variable is a place that can hold a value. Must specify the type of value that can be put in the variable ⇒ “Declare” the variable. Must put a value into a variable before you can use it ⇒ “assign” to the variable Can use the value by specifying the variable’s name Can change the value in a variable (unlike mathematical variable) "

© Peter Andreae COMP 102 4: 17 Assignment Statements /** Print out the conversion formulas (version 2) */ public void printFormula () { String formula; formula = "Celsius = (Fahrenheit - 32) *5/9" ; UI.println( formula ); } Assignment Statement: where = what ; name-of-place= specification-of-value ; formula= " Celsius = (Fahrenheit - 32) *5/9" Compute the value and put it in the place 〈 variable 〉〈 expression 〉 =; "

© Peter Andreae COMP 102 4: 18 Declarations and Assignments Variable must be declared before you use it Must be declared only once in a method Must be assigned a value before it is used in an expression Can declare variable before first assignment or Declare variable in the first assignment statement: /** Print out the conversion formulas (version 2) */ public void printFormula () { String formula = "Celsius = (Fahrenheit - 32) *5/9" ; UI.println( formula ); } " (strictly, once in a block)

© Peter Andreae COMP 102 4: 19 Expressions /** Convert from fahrenheit to celsius */ public void fahrenheitToCelsius(){ double fahren = UI.askDouble("Fahrenheit:" ); double celsius = (fahren - 32) * 5 / 9; UI.println(fahren + “F is " + celsius + “ C"); } Expressions describe how to compute a value. Expressions are constructed from values variables operators (+, -, *, /, etc) method calls that return a value sub-expressions, using (… ) … + for Strings: "concatenates" the Strings

© Peter Andreae COMP 102 4: 20 Method Definition: Like a pad of worksheets public void fahrenToCelsius(){ double fahren = UI.askDouble("Fahrenheit:"); double celsius = (fahren – 32) * 5 / 9; UI.println(fahren + “F is " + celsius + “ C"); } Calling a Method: tempCalc1.fahrenheitToCelsius(); ⇒ get a “copy” of the method worksheet ⇒ perform each action in the body. ⇒ throw the worksheet away (losing all the information on it) Method Calls: a (good) metaphor public void fahrenheitToCelsius(){ double fahren = UI.askDouble("Fahrenheit:"); double celsius = (fahren – 32) * 5 / 9; UI.println(fahren + “ is " + celsius + “ C"); } Fahrenheit: F is 30.0C

© Peter Andreae COMP 102 4: 21 Summary of Java program structure A Class specifies a type of object TemperatureCalculator.class describes TemperatureCalculator objects A Class contains a collection of methods each method is an action the objects can perform. TemperatureCalculator objects can do celsiusToFahrenheit, fahrenheitToCelsius, printFormula If you have an object, you can call its methods on it. A method definition specifies how to perform the action A method contains statements each statement specifies one step of performing the action Declaration and Assignment statements Method call statements

© Peter Andreae COMP 102 4: 22 More details: Expressions L-DP-C 2.3 Literal values numbers:9, 5, Strings:“ F ->”, "centigrade = (fahrenheit - 32) *5/9“ … Variables: cent Numerical expressions using arithmetic operators: +, -, *, /, (, ),6 + cent * 45 ordinary arithmetic (except “/” : truncates integers) % “remainder” String expressions using concatenate operator: + Concatenate values into a single String (converts numbers etc into strings)

© Peter Andreae COMP 102 4: 23 More Details: Types L-DP-C 2.3 A type is a kind/category of value Every variable must have a type Java has: Primitive types: double-3.495, 6.482E23 int-2358, 45, (integers from -2 billion to 2 billion) longintegers from to 2 63 char‘D’, ‘d’, ‘=‘ booleantrue, false byte, short, float: … (see the book for details) Object types: String“Hello John”, “600.45”, “F”, “true” Scanner PrintStream …. (lots of predefined types) TemperatureConverter…. Every class defines a type

© Peter Andreae COMP 102 4: 24 Signs of Computer Distress stiffness soreness discomfort weakness tightness heavy tingling numbness aching throbbing burning hot Sore ImmobilityNo blood flow No oxygen to tissue Stop moving Staying still makes muscle fatigue worse. Get moving!

© Peter Andreae COMP 102 4: 25 OOS Muscle fatigue/OOS is not an injury: it is a lack of blood supply to muscles Resting or Immobilising sore areas makes pain worse Physical exercise works  Get up and get moving!  Stretch frequently  Seek professional advice

© Peter Andreae COMP 102 4: 26 More Details: Expressions Creating a new object: new TemperatureConverter() Methods that return a value... (we will see more soon…) Expressions out of sub expressions (3 * size) + (weight / 2.5)

© Peter Andreae COMP 102 4: 27 Method Definitions: Parameters /** Convert from fahrenheit to centigrade */ public void fahrenToCelsius(){ double fahren = UI.askDouble("Fahrenheit:" ); double celsius= (fahren - 32) * 5 / 9; UI.println(“ -> " + celsius + “ C"); } A parameter specifies the type of a value the method needs a name for the place where the value will be put when the method is called (a kind of variable, but special) Parameters are in defined in the headers of method definitions

© Peter Andreae COMP 102 4: 28 Method Definition: Like a pad of worksheets public void fahrenToCent(int fahren){ int cent = (fahren – 32) * 5 / 9;.out.println(fahren + “ F -> " + cent + “ C"); } Calling a Method: tempConv1.fahrenToCent(86); ⇒ get a “copy” of the method worksheet ⇒ copy the argument(s) into the parameters(s) ⇒ perform the actions in the body. Method Calls public void fahrenToCent(int fahren){ int cent = (fahren – 32) * 5 / 9; System.out.println(fahren + “ F -> " + cent + “ C"); }