27-02-2013, 09:49 AM
ADVANCED PROBLEM SOLVING
ADVANCED PROBLEM.ppt (Size: 210 KB / Downloads: 69)
Classes and Objects
Main actors in a Java pgm are objects.
Objects store data and provide methods for accessing and modifying this data.
Every object is an instance of a class.
Class defines type of data the object stores, all kinds of operations that can act on that data.
Members of Java class are:
Instance variables/Fields these represent data of java objects. They can be base type or objects of other classes.
Methods these refer to the operations that act on data. Defines behavior of objects.
Variable Declarations
Java Characters are 16-bit long, so no.of characters represented are 65,536.
Character literal is a character surrounded with single quotes. char ch=‘q’;
Integer literal is a sequence of decimal numbers(0-9). int i=123;
Octal number literal is 0 followed by sequence of digits 0-7. int j=0123;
Hexadecimal number literal is 0x followed by sequence of numbers 0-9 and A-F.
Literal Integers are 32-bit long.
Loops
Initialization part may also declare variables.
Loop can be exited before all statements in its body are executed using unlabeled break stmt.
With nested loops, break will exit the current loop and outer loop can be continued.
Continue causes the loop to skip remainder of its body and begin the next iteration.
Exception Handling
No.of catch clauses is not limited to one, can be any.
While executing try block, if an exception occurs, control is transferred to catch clauses, first catch that matches the exception type is executed.
Then program continues with stmt following try-catch stmt, unless it contains the throw clause, that forces the caller to handle it.
Throws clause in heading of the method is a warning to the user of the method that particular exception can occur, if not handled, pgm crashes.
Object-Oriented Programming
Encapsulation:
Objects make the connection between data and methods much tighter and more meaningful.
In non-OOLs, declarations of data and definitions of functions could be spread throughout the entire program, only pgm documentation indicates connection between them.
Strong coupling of data and operations causes modeling real world events much better.
First OOL was Simula.