Tuesday, 1 November 2016

Java Lecture note 3

=============================================
Lecture note,
Monday 19 September 2016 07:00:48 AM IST
====================================================
OVERLOADING OF METHODS
We can add another method with same name but different parameter list
Advantage is using the meaningful name as method name.
appropriate method is called with matching arguments.

Program - find area of circle and rectangle using method overloading
-------------------------------------------------------
public class Overload {

 /**
  * @param
  */
 public static void main(String[] args) {
  // TODO overload area
  System.out.println("Area of Circle is " +area(10.5));
  System.out.println("Area of rectangle is "+area(10,15.5));

 }
 static double area(double r){
  return r*r*Math.PI;
 }
 static double area(double a, double b){
  return (a*b);
 }

}
-----------------------------------------------------------------
Area of Circle is 346.3605900582747
Area of rectangle is 155.0
----------------------------------------------------------

CONSTRUCTORS
USED to INITIALIZE objects
SAME METHOD NAME AS CLASS NAME.
no RETURN TYPE
Invoked automatically.
Look at Circle c = new Circle();
Here c object is created and a constructor is called. This is default constructor. This is provided automatically.

Program
----------------------------------------

public class ConstructorDemo {

 double radius;

 ConstructorDemo(){
  radius = 1.0;
 }
 ConstructorDemo(double r){
  radius = r;
 }

 double area(){
  return radius*radius*Math.PI;
 }
 public static void main(String[] args) {
 // TODO simple constructor

  ConstructorDemo c = new ConstructorDemo();
  System.out.println( c.area());

  ConstructorDemo c2 = new ConstructorDemo(10.5);
  System.out.println( c2.area());

 }

}
3.141592653589793
346.3605900582747
---------------------------------------------------------------------
---------------------------------------------------------------------
It is MANDATORY to use a default constructor when ANY OTHER TYPE OF CONSTRUCTOR used.
===========================================================
Wednesday 21 September 2016 07:02:31 AM IST
//////////////////Lecture Note
===========================================================
Practice Problems
1. Create a class called Triangle with
 * Three sides as double types
 * A no-arg constructor with default side = 1
 * A constructor with specified values of sides
 * Method getArea() returns area.
 * Method getPerimeter() returns perimeter.
 Write Java Program to implement this class.
2. Create a class called FixedDeposit with
 * DATA fields amount , interest, years.
 * a no-arg cosntructor having default values 1000, 5.0, 1 respectvelly.
 * a constructor having specified values.
 * method totalReturn() returns the total return value.
 Implement and Test this class .
------------------------------------------------------------
this REFERENCE
___________________
THIS KEYWORD IS used to refer the object that refers to it. For ex we can refer the instance varable radius as this.radius . This is required if the method refers with same name as parameter list.
For Ex - void getRadius(double radius)
 {
  this.radius = radius; //where radius is a data field as well as a parameter.
 }
==================================================
COMPARE TWO OBJECTS
----------------------
How to compare radius of 2 circle objects

public class CompareObj {

 /**
  * @param args Author S K Nayak
  */
 double radius;

 CompareObj(){

 }
 CompareObj(double radius){
  this.radius = radius;
 }
 static void greater( CompareObj c1, CompareObj c2){
  if ( c1.radius > c2.radius)
   System.out.println("greater is "+c1.radius);
  else
   System.out.println("greater is "+c2.radius);
 }

 public static void main(String[] args) {
  // TODO Compare 2 objects
  CompareObj obj1 = new CompareObj(15.5);
  CompareObj obj2 = new CompareObj(20.5);
  greater(obj1, obj2);

 }

}greater is 20.5
------------------------------------------------------------------------
------------------------------------------------------------------------
INTERNAL QUESTIONS SET A
------------------------------------------------------------------------
1. Create a class called FacultyGuide with data fields -
 private String facultyName
 private String[] students - max 30 no of students
 private int noOfStudents
 use constructor to initialize facultyName and noOfStudents. use method addStudent to add a student name to facultyName. Test this class to add some students and display the names.

2. Describe relationship between the object and class. Describe object reference variable and object with example.
SET - B
1. Create a class called Election with data fields -
 private String partyName
 private String[] candidates - max 10 no of candidates
 private int noOfCandidates.
 use constructor to initialize partyName and noOfCanndidates. use method addCandidates to add a candidate name to partyName. Test this class to add some candidates and display names.
2. Explain four different visibility modes in java with examples.
---------------------------------------------------------------

=================================================================
Wednesday 19 October 2016 08:14:28 AM IST
Lecture note
=================================================================
INHERITANCE
New classes from existng classes.
Powerful feature of java for reusing the code.
In fact every class is inherited from Object class.
super class/ sub class,, base/ derived,, parent class/child class
private members are not inherited.
super class contains common set of attributes and methods that are applied to all sub classes. For ex a vehicle class contains regdno and ownerName which is applied to all vehicles.
keyword ---- extends

class Box {
 double length;
 double breadth;
 double height;

 Box()
 {
 }

 Box (double l, double b, double h){
  length = l;
  breadth = b;
  height = h;
 }
 double volume()
 {
  return length * breadth * height;
 }
}

class Boxw extends Box{
 double weight;
 Boxw()
 {
 }
 Boxw(double l, double b, double h, double w)
 {
  length = l;
  breadth = b;
  height = h;
  weight = w;
 }
}

public class BoxDemo {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // box volume demo
  Boxw bw = new Boxw(10.1,15.2, 20.2,5.2);
  System.out.println("volume of box is "+bw.volume());
 }

}
-----------------------------------------------------
volume of box is 3101.1039999999994
------------------------------------------------------

Tuesday 25 October 2016 07:26:50 AM IST
========================================================
using super to call super class constructors
super(arg list);
super --- first statement . what happens when Boxw(){ weight = 1;} used .
So constructors are executed on the order of derivation from super class to sub class,, called CONSTRUCTOR chaining.
SUPER can be used to refer super class member
OVERRIDING
A method in a sub class is said to override the superclass method having defined with same name ,same signature and same return type.
class A{
 void show();
}
class B extends A{
 void show();
}
super class object reference can refer sub class object and can achieve polymorphism.
Ex A r;
A a;
r=a
r=b


Protected data members accessed from subclass of same or different package.
Default is accessed from same package only.


============================================
Wednesday 26 October 2016 08:27:04 AM IST
============================================
WHY OVERRIDDEN METHODS ARE USED
To achieve RUN-TIME POLYMORPHISM or DYNAMIC BINDING.It is a powerful principle having ability of reusing the code during run time without recompiling the class. Hence to achieve flexibility.
--------------------------------------------
class Figure{
 double x;
 double y;

 Figure(){
 }
 Figure(double d1,double d2){
  x = d1;
  y = d2;
 }
 double area(){
  System.out.println("Area not defined");
  return 0;
 }
}

class Rectangle extends Figure{
 Rectangle(){

 }
 Rectangle(double a, double b){
  super(a,b);
 }
 double area(){
  return ( x * y);
 }
}
class Triangle extends Figure{
 Triangle(){

 }
 Triangle(double a,double b){
  super(a,b);
 }
 double area(){
  return(0.5 * x * y);
 }
}
public class Overriding {

 /**
  * Author SKNayak
  */
 public static void main(String[] args) {
  // TODO overriding example
  Figure f;
  Rectangle r = new Rectangle(10,20);
  f = r;
  System.out.println("Rectangle Area = "+f.area());
  Triangle t = new Triangle(15,12);
  f= t;
  System.out.println("Triangle Area = "+f.area());
 }

}
-----------------------------------
Rectangle Area = 200.0
Triangle Area = 90.0
----------------------------------
Tuesday 01 November 2016 07:13:51 AM IST
==================================================
Lecture Note
==================================================
PREVENTING EXTENDING BY using final class ,, ex Math class is a final class ,,can not be extended.
PREVENTING OVERRIDING - by using final method.

ABATRACT CLASSES
------------------
from sub class to super class -- classes are more general and less specific and
from super class to sub class -- classes are more specific
Sometimes super class is so general that and abstract that can not have specific instance. These classes are called
 abstract classes.
Abstract classes can not be instantiated. For Ex - GeometricObject having area method.
Abstract class contains abstract methods.
Abstract method is a method signature without IMPLEMENTATION.
Program
--------
 abstract class GeometricObject4 {
  GeometricObject4(){
 }
 public abstract double findArea();

}

 class Circle4 extends GeometricObject4{
 double radius;
 Circle4(){
 }
 Circle4(double r){
  radius = r;
 }

 public double findArea(){
  return radius*radius*Math.PI;
 }
}
 class Rectangle4 extends GeometricObject4{
 double a;
 double b;
 Rectangle4(){
 }
 Rectangle4(double a, double b){
  this.a = a;
  this.b = b;
 }
 public double findArea(){
  return a*b;
 }
}
class TestGeometric {

 /**
  * Test compare area geometric objects
  dynamically
  */
 public static void main(String[] args) {
  // TODO Abstract methods

  GeometricObject4 ob1;
  GeometricObject4 ob2;
  ob1 = new Circle4(10);
  ob2 = new Circle4(10);
  GeometricObject4 ob3;
  ob3 = new Rectangle4(10,20);


  System.out.println("ob1 == ob2 "+compareArea(ob1,ob2));
  System.out.println("ob1 == ob3 "+compareArea(ob1,ob3));
  }

  static boolean compareArea(GeometricObject4 obj1, GeometricObject4 obj2){
   return (obj1.findArea() == obj2.findArea());

         }

}
------------------
ob1 == ob2 true
ob1 == ob3 false

Abstract class contains abstract methods
Abstract methods can not be defined in non abstract class
Abstract class can contain concrete methods.
Subclass must implement abstract methods of its superclass or it must be declared as abstract.
Abstract class can not be instantiated using new operator.
Abstract class can have constructors.
A class that contain abstract methods must be declared abstract.
It is possible that a abstract class contains no abstract methods. In this case can not be instantiated.
Abstract class can have object reference.
A subclass can be abstract even if super class is concrete.


Wednesday, 14 September 2016

Java Lecture Note 2

ARITHMETIC Operators
FLOATING POINT ARITHMETIC
Why answer is not more accurate ? double values are more accurate
Why it is called floating point?
c = ( 5.0 / 9 ) * (f - 32) gives actual result.
SHORT HAAND OPERATORS
CHARACTER DATA TYPES
Java uses 16 bit char representation represents any possible character in the world is UNICODE character which also includes ASCII characters. it is represented by \u0000 to \uFFFF.
-------------------------------------------------------
public class NewUnicode {
/**S K Nayak
* @param args
*/
public static void main(String[] args) {
// TODO unicode character testing
System.out.println("\u03b1 \u03b2 \u03b3 \u0b05 \u0b06");
//unicode char for A
System.out.println( (byte)('A'));

}
}
α β γ ଅ ଆ
65
________________________________________________________check output _______________
*****************************
////////////////Lect - 5 Monday 08 August 2016 09:26:41 PM IST
=====================================================================
SHORTHAND OPERATORS
INCREMENT OPERATORS
ESCAPE SEQUENCES
STRING TYPES -  String is not exactly a datatype, rather a predefined class. But we use it like reference type and use like datatype. + is used for concatenation .
-------------------------------------------------
public class Concat {
/**
* @param args
*/
public static void main(String[] args) {
// TODO concat
int a = 10;
int b = 20;
System.out.println(a+b);
System.out.println(a +""+ b );
String s = "Hallo";
System.out.println(s);
}
}
30
1020
Hallo
-------------------------------------------------------
How to write in 2 lines of string in program
LOGICAL OPERATORS
RELATIONAL OPERATORS
ASSIGNMENT OPERATOR
CONDITIONAL OPERATOR
CONDITIONAL STATEMENTS - if...else
LOOPS
for loop is used when no of loops are known, while used when no of loops are not known, do- while used when statement to be used first.
EXPLAIN THE JAVA PROGRAM
Every java application contains at least one class. it should contain main() method.
----------------------------------------------------
Program-1 Find largest number using simple if;
Program-2 Check even/ odd
Program-3 Check Prime
Program-4 Fahrenheit to Celsius
SINE series x - x^3/3! + x^5/5! - x^7/7! + ...
--------------------------xxxxxxxxxxxxxxxxxxxxxxxxxxxxx-------------------------
Lecture note Tuesday 09 August 2016 10:01:27 PM IST
##################################################################
Introduction to Array
_____________________
Array is provided to store fixed size sequential collection of data of same type.
Declaration
datatype[] arrayname; arrayname is reference variable.
example int[] a;
It does not allocate any space for array. It only creates a storage location for the reference to array name.
You can create an array using arrayname = new datatype[size];
for ex int[] a;
a = new int[100];
It means that it crates an array and assigns the reference to the variable arrayname.
Also we can write  - int[] marks = new int[60];
Array can be INITIALIZED AS FOLLOWS
int[] a = { 10, 20, 30, 40, 50, 60 };
Here new operator is not required.
---------------------------------------------------------
public class OccurenceLetters {
/**
* @param args
*/
public static void main(String[] args) {
// TODO SMALLEST INDEX OF LARGEST ELEMENT IN AN AAARY
int[] a = { 10, 20, 70, 40, 50, 60, 70,10, 25, 63 };
int max = a[0];
int index = 0;
for (int i = 1; i < 10; i++)
{
if ( a[i] > max)
{
max = a[i];
index = i;
}

}
System.out.println("Value is "+index);
}
}
Value is 2
=======================================================================
=======================================================================
Lecture Note Sunday 28 August 2016 09:49:40 PM IST
=======================================================================
INPUT USING Scanner s = new Scanner( System.in);
String x = s.next()   ---- string
similarly s.nextByte(), s.nextShort() , s.nextInt();
OBJECT ORIENTED PROGRAMMING
Problem viewed as a set of objects and interaction between objects.
OBJECT is real world entity, Object occupies space. Object has identity that is state and behaviour. Example student, bankac, customer etc. So object consists of STATE/ DATA FIELD/INSTANCE VARIABLES AND BEHAVIOUR /METHODS. So data and methods are closely associated with each other.We say a circle is characterised by radius and method like area() and perimeter().
CLASS : Set of objects. A class is a blueprint which defines what will be the object's data and methods. A class does not occupy space. A class declares essential data and methods that will be shared among all objects.
So an object also called instance of a class. Ex customer is a class and Mr Roy is an object.
OBJECT ORIENTED CONCEPTS
ENCAPSULATION : Details of class is hidden and  user does not know. For ex We can create a circle object and compute area without knowing how area is calculated in detail.
ABSTRACTION : Separation of class implementation from use of a class. we can build a computer system by knowing how the components interact with each other.
CLASS
OBJECT
INHERITANCE
POLYMORPHISM
Reference variables
Objects can be accessed by using reference variables. Circle mycircle; mycircle =new Circle();you can combine also.
Object reference variable and objects are different. More correctly mycircle is a variable that contains reference to Circle object. Array name is actually an object.
DIAGRAM for circle class
Circle         -------------class name
________________________
radius:double -------------Data fields
________________________
getArea() -------------Methods / constructors
Circle()
_______________________
Object diagram
circle1:Circle
______________
radius = 10
______________
A class contains declaration for INSTANCE VARIABLES and definition for methods.ACCESS OBJECT's data and methods by dot , called object member access operator.
For Ex circle1.radius = 10; PROVIDED radius is public.
Then you can declare methods. Create objects of the class and access the members and methods by using dot operator.
PROGRAM
public class Circle {
double radius;
double getArea(){
return radius * radius * Math.PI;
}
public static void main(String[] args) {
// TODO Area of circle object
Circle myCircle = new Circle();
myCircle.radius = 10.1;
double area = myCircle.getArea();
System.out.println("Area = " +area);

}
}
Area = 320.4738665926948
======================================================
======================================================
Wednesday 14 September 2016 07:51:13 AM IST
Lecture note
As we discussed above how to use instance variables and instance methods.
However we can use normal methods like use main method, then other methods called normal methods. We can define main method , other methods and call the methods within main method. Since any java program resides within a class, methods must be static. Like main method as main is called by the operating system without the help of an object. similarly a STATIC METHOD can be called without the help of an object, that means inside main directly referring by the method name.So STATIC methods can be called directly WITHIN THE SAME CLASS.
So you can use instance variable, methods or normal methods as when required.
public class SimpleMethod {
/**
* @param args
*/
public static void main(String[] args) {
// TODO use simple mehtods
double r = 10.5;
double area = getArea(r);
System.out.println(area);
}

static double getArea(double radius){
return radius*radius*Math.PI;

}
}
346.3605900582747
PROGRAM - - Write a program get larger of x and y in a class where x and y are private instance variables of that class
public class PrivateDemo {
/**
* Find larger of two private members of a class
*/
private int x;
private int y;

public static void main(String[] args) {
// TODO make data private

PrivateDemo p = new PrivateDemo();
p.getValue(10, 99);
p.larger();


}

void getValue(int a,int b) {
x = a;
y = b;
}

void larger(){
if( x>y )
System.out.println(x);
else
System.out.println(y);
}
}
99

S K Nayak
Ph . 9938103308

Tuesday, 9 August 2016

Assignment Code-CSEL101, Date line - 16 Aug


  1. Compute SINE series  = x - x^3/3! + x^5/5! - x^7/7! + ... For given value of x and no of terms.
 Hints - input value of x
Convert to radian.
Multiply certain term by previous term to get the new term.

2. Pi value is given by the series = 4(1 - 1/3 + 1/5 - 1 / 7 ... )
Write a program to find value for 10000 no of terms.
Also find the no of terms the loop should be continued to get the value of pi as 3.14159.

Sunday, 7 August 2016

Java Lecture Note

LECT - 1
Introduction
Programming knowledge
Expert in C and data structures
What is computer - electronic data processing machine.
Algorithms - finite sequence of mathematical  steps may or may not take some data as input and produces well defined outputs.
Programs - set of executable instructions written with respect to a particular programming language used to solve a particular problem.
Machine level language - compiler's native language
Assembly language - mnemonics that is low level language.
High level languages- English like languages.
You should be acquainted with some elementary programs like find area of triangle, Celsius to Fahrenheit, salary calculations.

Lecture-2
Programming languages. - Basic, FORTRAN,PASCAL, COBOL, - Relied upon GOTO
More structured language - C programming language, highly efficient that retains power of assembly level programming and high level languages.
Why we need other languages - Mange complexity becomes an important issue. This was addressed by. Another language C++, C with Object oriented concepts.
Why we need Java - motivation was a platform independent programming language needed for using in consumer electronic devices.
Problem in C/ C++ is that it compiled for a particular target machine. Hence Java eliminating the problem of using under different platforms. Since java is used for creating two types of programs i.e. applications and applets,it is applet for which it became extremely important for Internet.
Applications are programs that run under the os of the same machine
Applets  - are small programs that run under internet.
Java is very powerful fully featured language used for creating so many mission critical applications. Java was used for developing and controlling robotic ROVER that rolled on Mars.
History
1991 - James Gosling(head), Naughon at Sun Microsystems created language called OAK, later on renamed to JAVA.
1995- Microsoft announced source licence for java for using inside IE. Consequently Borland, Netscape, Mitsubishi, Sybase, Symantec applied for source license.
2010 - Oracle acquired JAVA.

LECT - 3 Monday 01 August 2016 10:40:08 PM IST
Java language specification
So now we start with java language specification.
API application program interface contains predefined classes and interfaces
SE - java comes with three edition , developing client side standalone applications
EE -server side applications
ME -micro edition for mobile devices for example J2SE, J2EE, J2ME
Versions are released with a JDK
Dev tools - NETBIN, JBUILDER, TEXTPAD

Application - standalone programs run on any machine with JVM
applets - run from web browser
Servlets- run from web server, creates dynamic web contents
Compiler - source  code to target language code as a single unit
Interpreter - individual step one at a time and executed immediately after translation
Magic is bytecode not executable file
Bytecode interpreted by JVM
So you write code for JVM ,NOT OS
JVM interprets and executes in a way that is compatible with current hardware architecture and OS
Features of java
1. Java is simple - As you will see gradually java is simple than any other OO languages. Pointers and multiple inheritance make programs more complicated . These are eliminated in java
2. Java is Distributed - Java is made to make distributed computing easy where more than one computers are involved.
3. Interpreted - Java interpreter translates the bytecode into machine languages of the target machine
4. Robust - Robust means reliable, Since java checks many possible errors might come during run time. That is java does not support pointers which eliminates the possibility of overwriting memory and corrupting data. Another thing is Exceptions which forces the programmer to use it. Hence java can detect the exceptions and respond it during run time, thereby making program execution normally.
5. Java is secure - Although java works under internet and networking environments , it implements different security mechanisms to protect the machine.
6. Architecture neutral - Since java is interpreted , it enables architecture neutral and since compilation is not made for any particular computer. Thereby we can say platform independent language.
7. Multithreaded - Different parts of the program can run under networking environment.
8. Java is portable - since java does not need to be recompiled again , since java compiler is written in java
9. One thing is that java is never faster than c++ since bytecode is interpreted and not directly executed by the system. This issue is handled by using JIT compiler for jvm.
10. Dynamic- New classes and interface can be added to it without recompilation.

Tuesday 02 August 2016 07:54:37 PM IST
======================================================
Identifiers - certain names given in programming languages
like variables, constants, methods , classes etc.
identifier consists of letters, digits, underscore, dollar. can not start with digit.
Numeric data types -
byte - 8-bit signed
short - 16 bit signed
int - 32 bit signed
long - 64 bit signed
float - 32 bit IEEE 754
double - 64 bit IEEE 754
  standard defines also SPECIAL FLOATING POINT values.
_____________________________________________________
public class FloatingNumber {

/** Author is S K Nayak
* @param args
*/
public static void main(String[] args) {
// TODO Auto
System.out.println("Hallo this is first program");
System.out.println(1.0 / 0);
System.out.println(-1.0 / 0);
System.out.println(0.0 / 0);
System.out.println(1.0 - 0.25);
System.out.println(1.0 - 0.9);
}

}
Infinity
-Infinity
NaN
0.75
0.09999999999999998
___________________________________________________
ARITHMETIC Operators
FLOATING POINT ARITHMETIC
Why answer is not more accurate ? double values are more accurate
Why it is called floating point?
c = ( 5.0 / 9 ) * (f - 32) gives actual result.
SHORT HAAND OPERATORS
CHARACTER DATA TYPES
Java uses 16 bit char representation represents any possible character in the world is UNICODE character which aslo includes ASCII characters. it is represented by \u0000 to \uFFFF.
INCREMENT OPERATORS
ESCAPE SEQUENCES
STRING TYPES

                      _____________X_____________

C Programming

  Table of Contents ​ About the Author 3 ​ Introduction 4 ​ Introduction to Computers 4 1. What is a Computer? 4 2. History...