Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Assignment # 1

Objective:
 Assertion
 Exception Handling
 Static typing
 Genericity
Assertion:
Definition and explanation:
An assertion is a condition that is evaluated to be either true or false at runtime that
would indicate a faulty execution if evaluated as false. The main advantage of
having assertions is to identify defects in a program. It can help a programmer read
the code, help a compiler compile it, or help the program detect its own defects.
Embedded assertions have been recognized as a potentially powerful tool for
automatic runtime detection of software faults during debugging, testing,
maintenance and even production versions of software systems.
The question arises why we need assertion?
Systems cannot be tested to be hundred percent perfect. Production applications
might face some strange data may not have been used for testing.
Assertion can help us to track the data that the variables receive and helps to
understand why some scenarios fail or in simpler words we can say that assertion
gives us the answers of those unsolved mysteries which we are trying to resolve
but fail, because we do not actually the source of the data.
Routine preconditions express the requirements that clients must satisfy
whenever they call a routine. For example the designer of ACCOUNT may wish to
permit a withdrawal operation only if it keeps the account's balance at or above the
minimum. Preconditions are introduced by the keyword require .
Routine postconditions , introduced by the keyword ensure , express conditions
that the routine (the supplier) guarantees on return, if the precondition was satisfied
on entry.
A class invariant must be satisfied by every instance of the class whenever the
instance is externally accessible: after creation, and after any call to an exported
routine of the class. The invariant appears in a clause introduced by the keyword
invariant , and represents a general consistency constraint imposed on all routines
of the class.
Example:
Some real world examples of assertion.
“ If there is peace, then economies grow”
“When I will get a promotion, I will buy a new car”
Assertion code Example and Syntax:
The syntax of assertion contains two parts
Assert(expression1):”expression2”:
The first part contains expression 1 and is called simple assert. It should be a
Boolean expression.
The second part contains expression 2 and is called normal assert. This statement is
optional it only prints out when statement 1 is false.
Code:
Before assertions were available, many programmers used comments to indicate
their assumptions concerning a program's behavior. For example, we might have
written something like this to explain our assumption about an else clause in a
multi way if-statement:
if (i % 3 == 0) {
...
} else if (i % 3 == 1) {
...
} else { // We know (i % 3 == 2)
...
}
Now we can use an assertion whenever we would have written a comment that
asserts an invariant. For example, we should rewrite the previous if-statement like
this:

if (i % 3 == 0) {
...
} else if (i % 3 == 1) {
...
} else {
assert i % 3 == 2 : i;
...
}
Exception handling:
Definition and explanation:
Exception handling is the way of handling errors, when a user does something that
he is not suppose to do, as a result we do not want to get an error which can
shutdown our program or can cause our program to stop working like for example
when we divide something by zero it will result as undefined which can cause our
program to shutdown or if our user clicks somewhere, where he is not suppose to
click, and thus results in the form of error which causes un wanted eruption.
So in order to handle such unwanted eruptions we use exception handling which
makes our program more efficient and also user friendly because user hates
unwanted shutdowns.
Error vs Exception:
Error and exception quite alike but in programming they have the following
difference.
Error: An Error indicates serious problem that a reasonable application should
not try to catch.
Exception: Exception indicates conditions that a reasonable application might try
to catch.
Example:
Some real world examples of exception handling are as follows.
Arithmetic exceptions such as division by zero and so on.
Operating systems use exception handling to resolve deadlocks and to recover
from crash and so on.
Code Example:
public class ExceptionHandle {

public static void main(String args[]){

try{

// Something which can cause exception

int data=90/0;

}catch(ArithmeticException e)

{System.out.println(e);}

System.out.println("Undefined and the remaining

code...");

} }

Static typing:

Definition and explanation:


Static typed programming languages are those in which variables need not be
defined before they’re used. This implies that static typing has to do with the
explicit declaration (or initialization) of variables before they’re employed. Java is
an example of a static typed language; C and C++ are also static typed languages.

Static typing does not means that you have to declare all the variables first, before
you use them. Variables maybe initialized anywhere, but developers have to do so
before they use those variables anywhere.

Example:

A real world example of exception handling is as follows.

V8 (JavaScript engine for Chrome and Node.js) tries to achieve utmost


performance with its optimizing compiler. Techniques have been made to do great
even with limited compile time type information but by having predictable and
non-changing type we can help our programs perform even better.

Code example:

static intnum, sum; // explicit declaration


num = 5; // now use the variables
sum = 10;
sum = sum + num;

GENERICITY:
Java Generic methods and generic classes enable programmers to specify, with a
single method declaration, a set of related methods, or with a single class
declaration, a set of related types, respectively. Generics also provide compile-time
type safety that allows programmers to catch invalid types at compile time.
In computer Science Terminology Generic programming is a style of computer
programming in which algorithms are written in terms of types to-be-specified-
later that are then instantiated when needed for specific types provided as
parameters.
Generics in Java is similar to templates in C++.
Example:
Following is a real life example of genericity.
All the citizens of a country are all equal there is no discrimination of gender or
rich or poor , religion all are considered equal and everyone is equal in the eyes of
law.
Code:
classTest
{
// A Generic method example
static<T> voidgenericDisplay (T element)
{
System.out.println(element.getClass().getName() +
" = "+ element);
}

// Driver method
publicstaticvoidmain(String[] args)
{
// Calling generic method with Integer argument
genericDisplay(11);

// Calling generic method with String argument


genericDisplay("GeeksForGeeks");

// Calling generic method with double argument


genericDisplay(1.0);
}
}

You might also like