Java Coding 4 (part2) David Davenport Computer Eng. Dept.,

Slides:



Advertisements
Similar presentations
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Example - The.
Advertisements

Java Coding OOP David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Towards Event-driven programming &
Everyday Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction to.
Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
Introduction to UML David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …Unified Modeling Language.
Object Orientated Concepts. Overview  The world is objects  Defining an object  Not a function, it’s a method  Lets inherit want we can  Objects.
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
Java Coding 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Decisions, decisions…!
Java Coding 3 David Davenport Computer Eng. Dept.,
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
Review: OOP & Arrays David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …from CS101.
Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey.
Java Coding 4 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Method madness.
1 Interactive Applications (CLI) Interactive Applications Command Line Interfaces Project 1: Calculating BMI Example: Factoring the Solution Reading for.
Passing Other Objects Strings are called immutable which means that once a String object stores a value, it never changes –recall when we passed a message.
Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
Java Coding 3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Over & over again!
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Syntax for Variables & Constants Input,
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Some important Java interfaces +
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
More Methods and Arrays Material from Chapters 5 to 7 that we didn’t cover in 1226.
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
Building java programs, chapter 3 Parameters, Methods and Objects.
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Interactive Programs Programs that get input from the user 1 In PowerPoint, click on the speaker icon then click the "Play" button to hear the narration.
Comp1004: Building Better Objects I Methods. Coming up Methods and Parameters – Why Parameterise? – Call by value, call by reference Return Types – Methods.
Phil Tayco Slide version 1.0 Created Sep 18, 2017
Java Coding – part 2 David Davenport Computer Eng. Dept.,
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Java Coding 4 David Davenport Computer Eng. Dept.,
Java Coding Extra David Davenport Computer Eng. Dept.,
Java Coding 3 – part2 David Davenport Computer Eng. Dept.,
Java Coding 3 David Davenport Computer Eng. Dept.,
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept.,
Java Coding 8 David Davenport Computer Eng. Dept.,
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
Introduction to Methods in java
Methods.
Classes and Objects 2nd Lecture
Boolean Expressions And if…else Statements.
CS139 – Fall 2010 How far we have come
CMSC 202 Exceptions.
Java Coding 4 David Davenport Computer Eng. Dept.,
Introduction to Classes and Methods
Reference Parameters.
Java Coding 6-extra David Davenport Computer Eng. Dept.,
Writing Functions.
Java Coding 6 – part2 David Davenport Computer Eng. Dept.,
Chapter 6 – Methods Topics are:
Methods in Java Methods & Parameters.
Java Coding 6_part3 David Davenport Computer Eng. Dept.,
Visibilities and Static-ness
CMSC 202 Exceptions.
Java Coding 6 David Davenport Computer Eng. Dept.,
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Java Coding 6 David Davenport Computer Eng. Dept.,
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept.,
Presentation transcript:

Java Coding 4 (part2) David Davenport Computer Eng. Dept., Be aware… BEWARE! David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Last updated: 4/11/2017 ~ separated out last part with global variables! Previously: from original 16/11/2016 NOTICE: This presentation deals almost exclusively with STATIC methods & structured programming. We move on to INSTANCE methods in a following presentation on OOP.

IMPORTANT… Students… Instructors… This presentation is designed to be used in class as part of a guided discovery sequence. It is not self-explanatory! Please use it only for revision purposes after having taken the class. Simply flicking through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn! Instructors… You are free to use this presentation in your classes and to make any modifications to it that you wish. All I ask is an email saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you, David.

Be aware Want methods to be reusable Methods that read inputs from keyboard or write output to console are difficult to reuse (e.g. in GUIs) So, pass inputs as parameters & return the output as the result User interaction is then done elsewhere, usually main method. E.g. if a sqrt method allows read its input from the keyboard, we could not use it to compute hypothenuse. Or if it prints output on console, we could not use it to output sqrt * 2 + 1, or some such. Ok, you can define methods specifically for getting data from user/keyboard, or for outputting data to the console in a neat form, but that is all they should do. Principle: each piece performs only one function… Cohesion & coupling… Method should do one task only & should rely on as few external methods/classes as possible (& should definitely not rely on their internal details!)

Beware a = 5; b = 3; d = 7; c = myMethod( a, b); Sys… ( a + “, ” + b + “, ” + d); What is output? In Java, (primitive) parameters are inputs only ~ “pass by value”. In other languages, parameters can be outputs & input/outputs too (indeed, Java object parameters behave differently! ~ “pass by reference”) d is normally assumed not to be changed by the method… (if defined locally in method then (hopefully) no problem but if “global” –static class variable- then dangerous, all bets are off. Only way to be sure in such cases is to trace method.. But tracing is error prone, especially if 10,000 lines long!) Note: global constants are ok, indeed encouraged! Of course, assumes one more thing… only one thread! Naturally assume method parameters are inputs only & method doesn’t have side effects! Global variables!

Global, Local, Params… public class MethodPlay { static int d; } global Scope: if local use it, else look to outer (global) context. Rule: any variable used in a method, that is not a parameter, must be defined locally… otherwise need to trace everything to see whether any side-effects change its value! public static int myMethod(…) { d = 999; } assigns 999 to global variable public static void main( String[] args) { d = 7; c = myMethod(…); Sys..( d); } assigns 7 to global variable prints 999 from global variable

Global, Local, Params… public class MethodPlay { static int d; } global Scope: if local use it, else look to outer (global) context. Rule: any variable used in a method, that is not a parameter, must be defined locally… otherwise need to trace everything to see whether any side-effects change its value! public static int myMethod(…) { d = 999; } assigns 999 to global variable public static void main( String[] args) { int d; d = 7; c = myMethod(…); Sys..( d); } local Note: - need global d here otherwise won’t compile (missing d in myMethod) - can get value (=999) from global variable using “MethodPlay.d” assigns 7 to local variable prints 7 from local variable

Global, Local, Params… public class MethodPlay { static int d; } global Scope: if local use it, else look to outer (global) context. Rule: any variable used in a method, that is not a parameter, must be defined locally… otherwise need to trace everything to see whether any side-effects change its value! public static int myMethod(…) { int d: d = 999; } local assigns 999 to local variable public static void main( String[] args) { d = 7; c = myMethod(…); Sys..( d); } Note: - need global d here otherwise won’t compile (missing d in myMethod) - can get value (=999) from global variable using “MethodPlay.d” assigns 7 to global variable prints 7 from global variable …unless something else changed it!

assigned to param (input only!) Global, Local, Params… public class MethodPlay { static int d; } global Scope: if local use it, else look to outer (global) context. Rule: any variable used in a method, that is not a parameter, must be defined locally… otherwise need to trace everything to see whether any side-effects change its value! public static int myMethod( int d) { d = 999; } param assigned to param (input only!) public static void main( String[] args) { int d; d = 7; c = myMethod(..); Sys..( d); } local

Global, Local, Params… public class MethodPlay { static int d; } global Scope: if local use it, else look to outer (global) context. Rule: any variable used in a method, that is not a parameter, must be defined locally… otherwise need to trace everything to see whether any side-effects change its value! public static int myMethod( …) { int d; d = 999; } local public static void main( String[] args) { int d; d = 7; c = myMethod(..); Sys..( d); } local

Example… static Scanner scan = new Scanner( System.in ); global public static int askForAndGetIntValue() { System.out.print( “Enter value: “); return scan.nextInt(); } Scanner scan ) { param Scanner scan = new Scanner( System.in ); local oops… undefined! Local: ok, but may be problem here due to shared resource? Param: ok, create in main & pass (but may have lots of params!) Global: um… for the really lazy!