© Xiaoying Gao, Peter Andreae Variables, Constants, UI methods COMP 102 #4 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.

Slides:



Advertisements
Similar presentations
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Graphical Output,
Advertisements

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.
Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Methods COMP.
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.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
CMT Programming Software Applications
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Program Elements and Syntax.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
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.
© Xiaoying Gao, Peter Andreae UI methods, Variables, Constants COMP 102 #4 2015T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Create Objects,
©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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
First Java Program COMP 102 #2 2015T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
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 More Interfaces.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Call a Method,
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
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.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Files COMP 102.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Methods COMP.
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.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Classes, Objects, Fields,
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 Fields, Constructors.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
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.
2011-T1 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Rashina Hoda and Peter Andreae COMP 102 Rashina Hoda.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
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.
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 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.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
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.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Program Elements and Syntax.
Fields, Constructors COMP 102 # T2
Information and Computer Sciences University of Hawaii, Manoa
Introduction to Computer Science / Procedural – 67130
Programs with graphics output
Java Programming Review 1
Names of variables, functions, classes
Presentation transcript:

© Xiaoying Gao, Peter Andreae Variables, Constants, UI methods COMP 102 #4 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington

Menu Variables, data types Expressions, assignment statement Constants UI methods Graphical output Admin Class Rep Labs with low numbers: Optional tutorial Optional helpdesk COMP 102 1:2

© Xiaoying Gao, Peter Andreae COMP 102 4: 3 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: 4 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) Run(Call) a Method : a 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: 5 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 -> " yes or no (boolean) true, false Objects …

© Xiaoying Gao, Peter Andreae COMP 102 4: 6 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: 7 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: 8 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) %: remainder +: also for string concatenation method calls that return a value sub-expressions, using (… ) … + for Strings: "concatenates" the Strings

© Xiaoying Gao, Peter Andreae COMP 102 4: 9 A class can have constants Inside a class, outside a method import ecs100.*; public class Numbers{ public static final double minN = 0.0; public static final double maxN = 50.0; public void printNumbers() { double x = 11; x = x/2; UI.println(x); x = (x-minN)/(maxN-minN); UI.println("x is now " + x); }

© Xiaoying Gao, Peter Andreae COMP 102 4: 10 int vs double import ecs100.*; public class Numbers{ public static final int minN = 0; public static final int maxN = 50; public void printNumbers() { int x = 11; x = x/2; UI.println(x); x = (x-minN)/(maxN-minN); UI.println("x is now " + x); }

© Xiaoying Gao, Peter Andreae COMP 102 4: 11 What can the UI do? UI is a predefined object (strictly, it is a class with static methods, which acts like a predefined object ) Has methods for text input from the user eg UI.askString("What is your name?") text output eg UI.println(" * " + name + " * "; graphical output eg UI.drawRect(100, 100, 300, 150) making buttons, sliders, etc How do you find out about all the methods? How do your find out what arguments you need to provide?

© Xiaoying Gao, Peter Andreae COMP 102 4: 12 Read the Documentation! Full documentation for all the standard Java library code (the "API" : Application Programming Interface) Version of Java API documentation on course web site: "Java Documentation" in side bar Tailored for Comp 102 Includes documentation of the ecs100 library: (UI, Trace, etc,) puts most useful classes at the top of the list. Use the documentation while you are programming!

© Xiaoying Gao, Peter Andreae COMP 102 4: 13 Some UI methods: read the documentation Text: UI.clearText()UI.print(anything ) UI.println(anything ) UI.println() UI.printf( format-string, values…) UI.askString(prompt-string ) UI.askDouble(prompt-string ) UI.askInt(prompt-string ) UI.askBoolean(prompt-string ) UI.next()UI.nextInt()UI.nextDouble UI.nextLine() UI.hasNext() UI.hasNextIntUI.hasNextDouble() Graphics: UI.clearGraphics() UI.setColor(color ) UI.drawRect(left, top, wd, ht )UI.fillRect(left, top, wd, ht ) … UI.drawOval(left, top, wd, ht )UI.fillOval(left, top, wd, ht ) … UI.drawLine(x1, y1, x2, y2 )UI.drawImage(file, left, top )

© Xiaoying Gao, Peter Andreae COMP 102 4: 14 Draw some shapes with colors

© Xiaoying Gao, Peter Andreae COMP 102 4: 15 Version 1 import ecs100.*; import java.awt.Color; public class DrawShapes{ public void drawPicture(){ UI.setColor(Color.green); UI.fillRect(100,150,200,100); UI.setColor(Color.red); UI.fillOval(150,150, 100, 100); UI.setColor(Color.orange); UI.drawLine(0, 200, 400, 200); UI.fillArc(150, 150, 100, 100, 30, 120); }

© Xiaoying Gao, Peter Andreae COMP 102 4: 16 Version 2 import ecs100.*; import java.awt.Color; public class DrawShapes2{ public void drawPicture(){ double x = 200; //centr x double y = 200; //center y double d = 100; //diameter UI.setColor(Color.green); UI.fillRect(x-d, y-d/2, 2*d, d); UI.setColor(Color.red); UI.fillOval(x-d/2, y-d/2, d, d); UI.setColor(Color.orange); UI.drawLine(x-2*d, y, x+2*d, y); UI.fillArc(x-d/2, y-d/2, d, d, 30, 120); }

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