Software Construction Week 1
9 min read
Lesson slides
1 / 16
Mansoura University
- First Semester- 2020-2021
- Department of Information System
- Faculty of Computers and Information
- [CS ---] Software Construction
- Grade: 4
- Lab : 1
- DR Omar El-zeki
- Abd El Rahman Gamal
Agenda
- Goals of Software Construction .
- Tools .
- Why we use Java in this course
- Understanding the java environment .
- Types of memory (Stack , Heap)
- Value type vs Reference type
- Static Checking
- Static typing
- 2
Goals of Software Construction
- Safe from bugs. Correctness (correct behavior right now) and defensiveness (correct
- behavior in the future).
- Easy to understand. Has to communicate to future programmers who need to
- understand it and make changes in it (fixing bugs or adding new features). That
- future programmer might be you, months or years from now. You'll be surprised how
- much you forget if you don't write it down, and how much it helps your own future self
- to have a good design.
- Ready for change. Software always changes. Some designs make it easy to make
- changes; others require throwing away and rewriting a lot of code
- 3
Programs have to be written with two goals in mind:
- communicating with the computer. First persuading the compiler that your program is sensible – syntactically correct and type-correct. Then getting the logic right so that it gives the right results at runtime.
- communicating with other people. Making the program easy to understand, so that when somebody has to fix it, improve it, or adapt it in the future, they can do so.
- 4
Tools
- We will use the java as programming language so we need to set up the environment .
- Using eclipse as IDE or you have the ability to use another IDE you prefer .
- 5
Why we use Java in this course
- Safety
-
- Java has static checking (primarily type checking, but other kinds of static checks too, like that your code returns values from methods declared to do so.
-
- good language for learning about good software engineering practices.
-
- Easier to understand what you need to do if you learn how in a safe, statically-checked language.
- Ubiquity
- -Java is widely used in research, education, and industry. Java runs on many platforms, not just Windows/Mac/Linux
- -Java has a wide array of interesting and useful libraries (both its enormous built-in library, and other libraries out on the net), and
- Excellent free tools for development (IDEs like Eclipse, editors, compilers, test frameworks, profilers, code coverage, style checkers)
- 6
Understanding the java environment
- JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. It was developed by James Gosling and Patrick Naughton. It is a simple programming language. Writing, compiling and debugging a program is easy in java.
- Before we start learning Java, lets get familiar with common java terms
- Java Virtual Machine (JVM)
- Java Development Kit(JDK)
- Java Runtime Environment(JRE)
- 7
Understanding the java environment
-
- Writing of the program
-
- Compilation of program is done by javac compiler, javac is the primary java compiler included in java development kit (JDK). It takes java program as input and generates java bytecode as output.
-
- In third phase, JVM executes the bytecode generated by compiler. This is called program run phase.
- The primary function of JVM is to execute the bytecode produced by compiler. Each operating system has different JVM, however the output they produce after execution of bytecode is same across all operating systems. That is why we call java as platform independent language.
- 8
Understanding the java environment
- bytecode
- javac compiler of JDK compiles the java source code into bytecode so that it can be executed by JVM. The bytecode is saved in a .class file by compiler.
- Java Development Kit(JDK)
- This is complete java development kit that includes JRE (Java Runtime Environment), compilers and various tools like JavaDoc, Java debugger etc.
- In order to create, compile and run Java program you would need JDK installed on your computer.
- 9
Understanding the java environment
- Java Runtime Environment(JRE)
- JRE is a part of JDK which means that JDK includes JRE. When you have JRE installed on your system, you can run a java program however you won’t be able to compile it. JRE includes JVM, browser plugins and applets support. When you only need to run a java program on your computer, you would only need JRE.
- 10
Lets summarise
- The Java Virtual machine (JVM) is the virtual machine that runs on actual machine (your computer) and executes Java byte code. The JVM doesn’t understand Java source code, that’s why we need to have javac compiler that compiles *.java files to obtain *.class files that contain the byte codes understood by the JVM. JVM makes java portable (write once, run anywhere). Each operating system has different JVM, however the output they produce after execution of byte code is same across all operating systems
- 11
Static checking
- Types
- Static Checking vs Dynamic Checking
- Arrays and Collections
- Iterating
- Methods
- Mutating Variables vs Reassigning Variables
- 12
What happened actually when we declare a primitive variable
- 13
Types of memory (Stack , Heap)
- 14
Hailstone sequence
- 15
Java vs python
- 16
Types
- 17
- The most important semantic difference between the Python and Java code above is the declaration of the variable n , which specifies its type: int .
- int (for integers like 5 and -200, but limited to the range ± 2^31, or roughly ± 2 billion)
- long (for larger integers up to ± 2^63)
- boolean (for true or false)
- double (for floating-point numbers, which represent a subset of the real numbers)
- char (for single characters like 'A' and '$' )
- Java also has object types , for example:
- String represents a sequence of characters, like a Python string.
- By Java convention, primitive types are lowercase, while object types start with a capital letter.
STATIC TYPING
- java is a statically-typed language . The types of all variables are known at compile time (before the program runs), and the compiler can therefore deduce the types of all expressions as well. If a and b are declared as int s, then the compiler concludes that a+b is also an int . The Eclipse environment does this while you’re writing the code, in fact, so you find out about many errors while you’re still typing.
- In dynamically-typed languages like Python, this kind of checking is deferred until runtime (while the program is running).
- .
- 18
Static Checking, Dynamic Checking, No Checking
- It’s useful to think about three kinds of automatic checking that a language can provide:
- Static checking : the bug is found automatically before the program even runs.
- Dynamic checking : the bug is found automatically when the code is executed.
- No checking : the language doesn’t help you find the error at all. You have to watch for it yourself, or end up with wrong answers.
- 19
Static checking
- syntax errors, like extra punctuation or spurious words. Even dynamically-typed languages like Python do
- this kind of static checking. If you have an indentation error in your Python program, you’ll find out before
- the program starts running.
- wrong names, like Math.sine(2). (The right name is sin.)
- wrong number of arguments, like Math.sin(30, 20).
- wrong argument types, like Math.sin("30").
- wrong return types, like return "30"; from a function that’s declared to return an int.
- 20
Dynamic checking
- illegal argument values. For example, the integer expression x/y is only erroneous when y is actually
- zero; otherwise it works. So in this expression, divide-by-zero is not a static error, but a dynamic
- out-of-range indexes, e.g., using a negative or too-large index on a string.
- 21
SURPRISE: PRIMITIVE TYPES ARE NOT TRUE NUMBERS
- Integer division. 5/2 does not return a fraction, it returns a truncated integer. So this is an example of
- where what we might have hoped would be a dynamic error (because a fraction isn’t representable as
- an integer) frequently produces the wrong answer instead.
- Integer overflow. The int and long types are actually finite sets of integers, with maximum and minimum
- values. What happens when you do a computation whose answer is too positive or too negative to fit
- in that finite range? The computation quietly overflows (wraps around), and returns an integer from
- somewhere in the legal range but not the right answer.
- special values in floating-point types. Floating-point types like double have several special values that
- aren’t real numbers: NaN (which stands for “Not a Number”), POSITIVE_INFINITY, and
- NEGATIVE_INFINITY. So when you apply certain operations to a double that you’d expect to produce
- dynamic errors, like dividing by zero or taking the square root of a negative number, you will get one
- of these special values instead. If you keep computing with it, you’ll end up with a bad final answer.
- 22
Pre-defined types
- 23
(a) static error (b) dynamic error (c) no error, wrong answer
- 24
ARRAYS AND List
- Arrays are fixed-length sequences of another type, but list is non-fixed-length.
- 25
Methods
- 26
Mutating Values vs. Reassigning Variables
- When you assign to the contents of a mutable value – such as an array or list – you’re changing references inside that value.
- Change is a necessary evil. Good programmers avoid things that change, because they may change unexpectedly.
- Immutability (immunity from change) is a major design principle in this course. Immutable types are types whose values can never change once they have been created. (At least not in a way that’s visible to the outside world – there are some subtleties there that we’ll talk more about in a future class about immutability.) Which of the types we’ve discussed so far are immutable, and which are mutable?
- Java also gives us immutable references: variables that are assigned once and never reassigned. To make a reference immutable, declare it with the keyword final:
- Final int num;
- 27