© Xiaoying Gao, Peter Andreae Class, method, statements COMP 102 #3 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.

Slides:



Advertisements
Similar presentations
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Advertisements

Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Introduction to C Programming
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.
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.
Computer Science 101 Introduction to Programming.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Program Elements and Syntax.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
19/5/2015CS150 Introduction to Computer Science 1 Announcements  1st Assignment due next Monday, Sep 15, 2003  1st Exam next Friday, Sep 19, 2003  1st.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
© Xiaoying Gao, Peter Andreae UI methods, Variables, Constants COMP 102 #4 2015T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
©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.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
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,
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Exercise, printf,
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
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 Fields, Constructors.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
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.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output.
Topic 4 Expressions and variables Based on slides bu Marty Stepp and Stuart Reges from "Once a person has understood.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
© 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.
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Program Elements and Syntax.
Chapter Topics The Basics of a C++ Program Data Types
2.5 Another Java Application: Adding Integers
Primitive Data, Variables, Loops (Maybe)
Java Programming: From Problem Analysis to Program Design, 4e
Java Programming Review 1
C++ Programming Basics
Unit 3: Variables in Java
Problem 1 Given n, calculate 2n
Presentation transcript:

© Xiaoying Gao, Peter Andreae Class, method, statements COMP 102 #3 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington

© Xiaoying Gao, Peter Andreae COMP 102 3:2 Menu More examples and a slightly big program Semantics (meaning) and Syntax (grammar rules) Class, Methods, Statements Variables, Types, Expressions Announcements: Assignment 1 is due next Wednesday 11am. Use lab computer, BlueJ, submit assignments Go to the labs even you have not signed up. Lab A: Thursday 3-4, 4-5, Fri 12-1 Lab B: Monday 4-5, Tuesday 12-1, 2-3, Wed 9-10 Optional tutorial Friday 9-10, CO219 with Tim, starting week 3

© 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:3

© 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:4

© Xiaoying Gao, Peter Andreae COMP 102 3:5 A Java Program Task: Write a temperature conversion program: C ⇔ F 1: Specification: what is it supposed to do? Write a program that will let the user do three things: print out the conversion formula let user enter temperature in Fahrenheit, and print out in Celsius. let user enter temperature in Celsius, and print out in Fahrenheit 2: Design: For calculate actions: Ask user for the value to be converted Calculate new values out of old value (F-32)*5/9 or C*9/5+32 Print out the answer

© Xiaoying Gao, Peter Andreae COMP 102 3:6 Designing the Java program 3: Editing Need to write this design in the Java language. We need an object (a "temperature calculator") to do the actions ⇒ we need a class to describe the object The object needs three actions ⇒ define three methods ⇒ specify what the methods will do

© Xiaoying Gao, Peter Andreae Writing the Java code import ecs100.*; /** Program for converting between temperature scales */ public class TemperatureCalculator{ /** Print conversion formula */ public void printFormula ( ) { UI.println("Celsius = (Fahrenheit - 32) *5/9"); } /** Convert from fahrenheit to centigrade */ public void fahrenToCelsius(){ double fahren = UI.askDouble("Farenheit:"); double celsius = (fahren - 32) * 5 / 9; UI.println(" -> " + celsius + " C"); } /** Convert from centigrade to fahrenheit */ public void celsiusToFahren(){ double celsius = UI.askDouble("Celsius:"); double fahren = celsius * 9 / ; UI.println(" -> " + fahren + " F"); } COMP 102 3:7

© Xiaoying Gao, Peter Andreae Compiling and Running 4: Compiling If there are syntax errors (invalid Java) then the compiler will complain and list all the errors ⇒ read the error message to work out what's wrong ⇒ fixing syntax errors until it compiles without complaint BlueJ makes this process easier 5: Running and Testing Must run the program and test it on lots of different input. BlueJ makes it easy to run individual methods. COMP 102 3:8

© Xiaoying Gao, Peter Andreae COMP 102 4: 9 Writing your own programs How? Use other programs as models Very useful strategy Limiting Hard to work out what's wrong Understand the language – rules and vocabulary Unlimited Able to understand and reason about your program

© Xiaoying Gao, Peter Andreae COMP 102 4: 10 Class syntax pattern 〈import statements 〉 public class 〈 method descriptions 〉 { } 〈 classname 〉 Comments can be added anywhere import comp102.*; import java.awt.Color;

© Xiaoying Gao, Peter Andreae COMP 102 4: 11 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(" -> " + 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 …

© Xiaoying Gao, Peter Andreae COMP 102 4: 12 Method Definitions /** Print out the conversion formulas */ public void printFormula ( ) { UI.println("Celsius = (Fahrenheit - 32) *5/9"); } 〈 Comment 〉〈 Header 〉〈 Body 〉 {} public void () 〈 name 〉 instructions to perform this action Specifying the information the action needs Maybe empty

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

© Xiaoying Gao, Peter Andreae COMP 102 4: 14 Method Calls 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 〉.();

© Xiaoying Gao, Peter Andreae COMP 102 4: 15 Objects and their methods in Java What objects are there? Predefined UIa "User Interface" window with several panes  println(….), askString(…), 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

© Xiaoying Gao, Peter Andreae COMP 102 4: 16 Data types 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 -> " Objects …

© Xiaoying Gao, Peter Andreae COMP 102 4: 17 Variables int x = 100; UI.println(“x is ”, x); x = x + 1; UI.println(“x is ” +x); 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)

© Xiaoying Gao, Peter Andreae COMP 102 4: 18 Assignment Statements double fahren = UI.askDouble("Farenheit:"); double celsius = (fahren - 32) * 5 / 9; Assignment Statement: where = what ; name-of-place = specification-of-value ; double celsius = (fahren - 32) * 5 / 9; Compute the value and put it in the place 〈 variable 〉〈 expression 〉 =;

© Xiaoying Gao, Peter Andreae COMP 102 4: 19 Expressions /** Convert from fahrenheit to celsius */ public void fahrenToCelsius(){ double fahren = UI.askDouble("Fahrenheit:" ); double celsius= (fahren - 32) * 5 / 9; UI.println(“ -> " + 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

© Xiaoying Gao, Peter Andreae COMP 102 4: 20 Declarations and Assignments Variable must be declared before you use it Must be declared only once in a method (strictly, once in a block) 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 ); } "

© Xiaoying Gao, Peter Andreae COMP 102 4: 21 Method Definition: Like a pad of worksheets public void fahrenToCelsius(){ double fahren = UI.askDouble("Fahrenheit:"); double celsius = (fahren – 32) * 5 / 9; UI.println(“ -> " + celsius + “ C"); } Calling a Method: tempCalc1.fahrenToCelsius(); ⇒ 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 fahrenToCelsius(){ double fahren = UI.askDouble("Fahrenheit:"); double celsius = (fahren – 32) * 5 / 9; UI.println(“ -> " + celsius + “ C"); }

© Xiaoying Gao, Peter Andreae COMP 102 4: 22 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 celsiusToFahren, fahrenToCelsius, 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

© Xiaoying Gao, Peter Andreae COMP 102 4: 23 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)

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

© Xiaoying Gao, Peter Andreae COMP 102 4: 25 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!

© Xiaoying Gao, Peter Andreae COMP 102 4: 26 Myths False OOS/muscle fatigue is a crippling injury It will affect the rest of your life Once you have it, you will never get bett Muscle fatigue/OOS is not an injury: it is a lack of blood supply to muscles Get up and get moving! Seek professional advice True When people rested, pain got worse Immobilising sore areas makes pain worse Physical exercise works!

© Xiaoying Gao, Peter Andreae COMP 102 4: 27 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)