Java – Objects and Classes…

Java is an Object Oriented Language.It supports Object Oriented features like,

  • Polymorphism
  • Inheritance
  • Classes
  • Objects
  • Abstraction
  • Encapsulation
  • Overloading
  • Overriding
  • Reusability

Objects in Java:

Object Oriented concepts are implemented to match the real life entities.Object can be a man,desk,car or anything which has a

  • State
  • Identity
  • Behaviour

Classes in Java:

A class is an collection of objects which holds the properties of the objects.Its is an blue print of the objects behaviour.

Example:

A clear definition of a class would be,consider a class as a car.

  • Identity – A car has a color,number and unique body.
  • Behaviour – A car can move,move backward etc.
  • State – Car is idle,moving etc.

Creating a Class:

public Class Car

{

String carName;

int carNumber;

String carColor;

void moving();

void stop();

}

A Class can contain any number of variables,methods and objects.Here moving() and stop() are methods remaining things would fall as variables.There are types in variables we gonna have them later.

Creating an Object:

Object can hold values.It is used to match real time entities as i said earlier.Objects can be created using the ‘new’ keyword.Here the ‘new’ refers to a new memory allocation.

Object can be created in several ways.They are,

1.Declaration

2.Instantiation

3.Initialization

Declaration:

Syntax: ‘ClassName ObjectName;’

Instantiation:

Syntax: ‘ClassName ObjectName = new ClassName();’

Initialization:

Syntax: ‘ClassName ObjectName = new ClassName(“Value”);’

Struts – Struts Introduction…

Struts is a mature open source MVC(Model-View-Controller) frame work for web applications. With Struts we can easily integrate various technologies like J2EE , Hibernate, iBATIS, JSTL and JSF, Velocity Templates, XSLT etc. Apart from being a framework Struts also provides utilities and custom tags to cut down development time.

Struts is a very popular framework and has now become a de-facto standard for Java applications. The reasons for this popularity are given below

Advantages of Struts

Advantage MVC

Struts imposes the MVC framework so that no body goes off loop and does his own non standard implementation. Before Struts MVC implementations were development team’s perception of MVC which lead to lots of confusion, errors and increased the learning curve for that implementation. With Struts this human element is gone and a standard process of MVC is imposed on the developers

Advantage ConfigurationsStruts works completely on configurations in XML and property files rather than hard coding the components in the Java code. All the elements like Controller actions, redirects, validations, exceptions can be incorporated by configurations and can be easily tracked at a later stage.

Advantage Utilities

Struts provides lots of utilities derived from Apache Commons to make our life easier like ready made Form Beans for request parameters, Validation controls and Custom tags

Disadvantages of Struts

Disadvantage Performance

Frameworks mean bigger call stacks due to various function calls which results in lesser performance. However this is a performance sacrifice is offset by major advantage of standardized development process advantage.

Disadvantage Beginners

Though this is not an issue with experienced developers, the new developers have a bigger learning curve. The documentation at Apache is mostly targeted at the experienced audience.

Disadvantage Imposing

If you want to use Struts then you will have to just follow the rules set by this framework. Going in a different way is pretty tedious.

Disadvantage Small Programs

Struts is not always useful for a smaller programs because there is no point in wasting time on various Struts configurations when you can write your program in a jiffy. The program requirements should be carefully evaluated before using Struts.

iPhone – UITextField Delegate Methods…

So its a long time….Okkk.Let me tell you about the delegates and those methods.Here we go,

What is Delegate:

A delegate is an object which communicates with other objects when an event occurs.If you keep your fingers over a candle light you get a message to your brain which says to take back,its a kinda delegate.

UITextField Delegate Methods:

Every component has some delegate methods which will be called when an  event occurs.TextField delegate methods are,

*textFieldShouldBeginEditing

*textFieldShouldEndEditing

*textFieldDidBeginEditing

*textFieldDidEndEditing

*textField shouldChangeCharactersInRange

*textFieldShouldClear

*textFieldShouldReturn

Now Lets have a Deep look over them,

textFieldShouldBeginEditing:

This method asks  some textfield to become the first responder(Current TextField).So you can change the delegate information.

textFieldDidEndEditing:

This method asks  some textfield to resign the first responder(Current TextField).So you can change the delegate information.

textFieldDidBeginEditing:

This methods tells that some textfield has just became the first responder(Current TextField).So you can change the delegate information.

textFieldDidEndEditing:

This methods tells that some textfield has just resigned the first responder(Current TextField).So you can change the delegate information.

textField shouldChangeCharactersInRange:

This method is called whenever user types a new character or delete’s a character.

textFieldShouldClear:

This method is used to clear the textfield which is an build in method.

textFieldShouldReturn:

This method is called when the user enter’s the return button.So we can add some additional behaviour here.

 Example:

Step 1: As i have explained already i have created an Xcode project.

Step 2: The xib file looks like this.I have two textfields for UserName and Password with a button.

screen

Step 3: I have created outlet and set delegate to the textfields.

Setdelegate

Step 4: Implement UITextFieldDelegate in the .h file.

setDelegateinInterfaceStep 5: I have included the QuartzCore framework and imported the file in my .m file.Using this you can make some overlay colouring,modify border width and more.

import

Step 6: Implement your logic inside the delegate methods.

Now you are set to Go….

Code:

.h File:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITextFieldDelegate>

@property (retain, nonatomic) IBOutlet UITextField *userNameTextfield;

@property (retain, nonatomic) IBOutlet UITextField *passwordTextfield;

– (IBAction)submitClicked:(id)sender;

@end

 .m File:

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController ()

@end

@implementation ViewController

@synthesize userNameTextfield,passwordTextfield;

– (void)viewDidLoad

{

[super viewDidLoad];

}

– (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

}

– (void)dealloc {

[userNameTextfield release];

[passwordTextfield release];

[super dealloc];

}

– (IBAction)submitClicked:(id)sender {

if(userNameTextfield.text.length == 0 || passwordTextfield.text.length==0)

{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Please Enter UserName and Password” message:nil delegate:self cancelButtonTitle:@”Ok” otherButtonTitles:nil];

[alert show];

}

else

{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”You are learning about delegates….” message:nil delegate:self cancelButtonTitle:@”Ok” otherButtonTitles:nil];

[alert show];

}

}

#pragma  – mark Textfield Delegate Methods

– (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

textField.layer.borderWidth=2;

textField.layer.borderColor=[[UIColor redColor] CGColor];

textField.layer.cornerRadius=7;

return YES;

}

– (void)textFieldDidBeginEditing:(UITextField *)textField  {

}

– (BOOL)textFieldShouldEndEditing:(UITextField *)textField

{

textField.layer.borderColor = [[UIColor  clearColor] CGColor];

return YES;

}

– (void)textFieldDidEndEditing:(UITextField *)textField {

}

– (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  {

return YES;

}

– (BOOL)textFieldShouldClear:(UITextField *)textField  {

return NO;

}

– (BOOL)textFieldShouldReturn:(UITextField *)textField

{

if(textField == userNameTextfield)

{

[passwordTextfield becomeFirstResponder];

}

else if (textField == passwordTextfield)

{

[passwordTextfield resignFirstResponder];

}

return YES;

}

@end