Wednesday, April 28, 2010

Java and JDeveloper Learnings

After a LOOOONG break, I am starting again in Java programming with JDeveloper.

Here are some fresh learnings-

1. Using JDeveloper beginner- Great video-
http://jdeveloperzerotopro.com/

2. How to enable input from the keyboard in Jdeveloper-
  1. Go to project properties(Right Click on the Project),
  2. click on Run/Debug settings,
  3. select your Run configuration (Default) and Click Edit.
  4. Locate "Tool settings" and check "Allow program input" in Additional Runner Options.
  5. Save settings and run your program again.
  6. Now you will see under the console input field for your inputs.
JDeveloper follows [Application --> Project --> Java Src file] architecture.

On a Java src file, first --> Make and then --> Run.

My first program- tried first on Console & then on JDeveloper-

________________________________________
package learnjavaproject;

public class Learn1 {
public Learn1() {
super();
}
public static void main (String [] args){
System.out.println("hello");
}
}
________________________________________

Then am working now on an App - "PayRoll"

These are the specs of this App-

Base class - Employee. [Properties - First Name, Last name, Base Salary ]

Three extended classes- Software Engineer, Project Leader, Project manager.

Factors affecting salary of these employees-
Software Engineer - skill
Project Leader - No_Team_Members
Project_manager
Project manager

________________________________________
Expected behavior --> By giving a type of employee and the parameter, the salary should be given back.
________________________________________


Employee Class-->
________________________________________

package learnjavaproject;// Project name is LearnJavaProject.

public class Employee {
private String FirstName;
private String LastName;

public Employee() {
super();
}
public Employee(String FirstName, String LastName){
SetFirstName(FirstName);
SetFirstName(LastName);
}
public void SetFirstName(String FirstName){
this.FirstName = FirstName;
}
public void SetLastName(String LastName){
this.LastName = LastName;
}
public String GetFirstName(){
return FirstName;
}
public String GetLastName(){
return LastName;
}
public String FullName(){
return FirstName +" "+LastName;
}
}
________________________________________

And the driver file -

package learnjavaproject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import learnjavaproject.Employee;


public class PayRoll {
public PayRoll() {
super();
}
public static void main (String [] args) throws Exception{
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));

String FirstName;
String LastName;

System.out.println("Enter First name: ");
String fName = br.readLine();
System.out.println("Enter last name: ");
String lName = br.readLine();

Employee e = new Employee(fName,lName);
System.out.println(e.GetFirstName());
}
}

________________________________________

But the output is null at this time. Let me try to debug this.. Will write again later...:)

Oh , It was a case Sensitive issue. I had put Firstname instead of FirstName...... OMG, what !!!!!
Now it works, Thanks to Anna who trouble-shooted this.

Tuesday, December 8, 2009

Collections in C#

Collections available in C#-
(
In System.Collections namespace.) -
ArrayList, BitArray, HashTable, SortedList, Queue ,Stack, Linked List etc


STACK

Stack class is generally referred to as LIFO (Last In First Out). They are used mostly in recursive functions.The best example where a stack is used would be to solve expressions.

The methods used are-
push()-Adds the element at the top of the stack,
pop()- Removes the element from the
top of the stack,
ToString()- Returns a string that represents the current object.
Peek()- Just presents the element.

Mechanism of stack -

Infix- (2+4)*(6/5)-12
Postfix- 24+65/*12-

1. read() 2 - push (2)
2. read() 4 push (4)

3. read - + --> opd2 = pop() opd1=pop()
res = opd1 opt opd2 push (res) // res= 6

4. read() 6 - push (6)
5. read() 5 push (5)

6. read - / --> opd2 = pop() opd1=pop()
res = opd1 opt opd2 push (res) // res= 1(6/5)

7. read() *- opd2 = pop() opd1=pop()
res = opd1 opt opd2 push (res) // res= 6
(6*1)

8. read() 12 - push (12)
9. read() -
opd2 = pop() opd1=pop()
res = opd1 opt opd2 push (res) // res= -6
(6-12)

QUEUE
A queue is referred to as FIFO (First in First Out).

Methods used are- Enqueue(), Dequeue(). Peek() etc.
Best example woul dbe a Printer Service.


Array -- int[] intArray = new int[3]; Have to define the size.


Array List --
ArrayList myList = new ArrayList();

An ArrayList is better than Array to use when you have no knowledge in advance about elements number.
ArrayList are slower than Arrays. So, if you need efficiency try to use Arrays.


LinkedList --
in each record/node there is a field that contains a reference (i.e., a link) to the next record in the sequence. It can be single - having one way reference por Double - Having reference to the previous & the next reference.
linkedlist is much more flexible and lets you insert, add and remove elements from both sides of your collection - it can be used as queue and even double-ended queue!




Monday, December 7, 2009

OOPS -1

Class- Representation of similar things in real world.

OOPS - Determine the entities from the problem definition. Identifying real life things/entities.

  • Inheritance
  • Encapsulation
  • Polymorphism

Abstraction - Mechanism of focusing on the similarities & ignoring the differences to define class.

Class diagram-

name
------
Data member
-------------
Member Function


Objects- Instance of a class - Identity, Behavior, State.

  • Difference of Abstraction (analysis level- Identifying classes) Vs Encapsulation (design level-define the class structure- data members & member functions in the same capsule)


  • Inheritance- Deriving /creating a new class from the base/ existing class.

Employee -- Parent class
^
|
Manager -- Child class

  • Polymorphism - static and dynamic
Static Polymorphism- Function or method Overloading - same name of methods but different number of parameters.
Dynamic or run time polymorphism - Function over-riding, using base class reference.


Data members should not be accessible outside the class. So, usually are private but then cannot use in the derived class. So, make it protected . So that they can be used in the derived clases.

To access them from outside, use property- Getters & Setters-

Trivia Info-
  • To override any method, the method in the base class should be virtual.
  • When assigning value to a data member via argument of a method, the data member should be prefixed with "this."
  • the base constructor is referred to as 'base' in the constructors in the derived class. Same as super in java.
  • e1==e2 ---> Checks the Reference Vs e1.Equals (e2) - Checks the values.

Difference of foreach & for loop-

foreach (Employee e in emplist)
e.cal_sal();

foreach - the type of the list has to be mentioned.

Abstract class- not to allow instantiating, only derived class can instantiate.

Interfaces - for multiple inheritance - programming by contract - define the interface.

Sealed class - classes cannot be inherited, same as final in java, opposite of abstract class.

Association (Linking) of different classes - Aggregation & Composition.
difference of aggregation and composition>

  • Aggregation - Grouping of classes- like Library and Book--> part of the whole
  • Composition - Strong Aggregation-Interdependent- Car & partContents of the whole.


Inner class - a class within a class.

Exception - Run time error - use the try - catch block to avoid system crashes.

try {}
catch (Exception ex){
Console.WriteLine(ex.Message);
}

class myException :Exception {
.........
}

Dot Net starters

Why Dot Net / Why C sharp ? Vs Java development- C# 3.0, VS 3.5

----------------------------------------------------------

1. language Interoperability - In Dot Net any language can be used, which is then compiled by the CLR. This is also cross platform compatible.

Here is the of Net framework diagram-


C++ C# VB.net Perl
-----------------------------

Windows Forms
Mobile Internet
Web Form
Web Services
------------------------------
ADO , .Net AND XML – Data Access Layer
------------------------------
.Net Class Library
------------------------------
CLR – Different for diff OS- same as JVM in Java
------------------------------
OS
------------------------------


Responsibility of CLR-

  • Memory management- Reference count, If not incremented, then garbage collected.
  • CTS- Common Type System -Language Interoperability
  • Life Cycle Monitoring
After interpreting & compiling, we get the .exe file.

Source Code-(Compilation) -> MSIL -->JIT compiler --> Native Code


Some more learning-
  • MSIL- Microsoft Intermediate language
  • To run the app from command prompt, either give the path in the environment variable or use the command prompt from the the VS folder.
  • To run a specific file after compiling, right click on the solution & change the "startup project" to the project that you want to run.
  • Native Executable vs Portable Executable -
    Native executable is a normal executable file which is directly executed from the OS- like notepad.exe .
    Portable executable -executed by CLR on top of OS.
  • Managed Vs unmanaged code -
    Managed - which is interpreted by the CLR,
    Unmanaged - To use Legacy code including a COM component.
  • Difference of Build vs Debug-
    Build- .exe file is created
    Debug- Executes line by line but exe file is not created.
  • In 1 application, there should be only 1 entry point.


See Sharp - C# odyssey begins...

Hi Friends,

Previously, I was a web designer/ developer working with HTML, JavaScript,AJAX, Photo shop, Dream-weaver etc .

I was fortunate to get an opportunity to work with a team designing an automated test framework for Windows Mobile devices. The frame work got ready with the hard work of the whole team but this triggered an interest in me to explore more on the Dot net environment especially to learn more about OOPS model- Object Oriented Programming using C sharp.

With a full time job & a family to take care of , I could not afford to go to real time class. So, I tried to find some online classes providing Dot net Training so that I could do my learning in late hours after all chores are done. I was surprised to find that there were zillions of online classes providing this type of service. I just had to search for them on Sulekha. Some were a bit more expensive than the others but I was happy that I could get a good online training center within my budget.

We used "gotomeeting.com" meeting tool to view the teachers screen from where he showed us the power point presentation, the code samples on the Visual Studio screen or Command Prompt and the results or errors of compilation.

I am very happy to learn this technology. I have done a lot of programming in Java, Perl etc previously on different occasions but never had a clear understanding of the technologies and its fundamentals. here I will share with you some of my learning that I have understood (or at least I think I do..) and the related code samples.

Plz feel free to comment of there is any thing missing from the content or if you want to know about some thing more.

Let's go for it !