Monday, April 6, 2015

Pointers in C++

0 comments


Pointers in C++



 Introduction to Pointers in C and c++


Introduction to Pointers


o When we declare a variable some memory is allocated for it. The memory location can be referenced through the identifier “i”. Thus, we have two properties for any variable : its address and its data value. The address of the variable can be accessed through the referencing operator “&”. “&i” gives the memory location where the data value for “i” is stored.

o A pointer variable is one that stores an address. We can declare pointers as follows int* p; .This means that p stores the address of a variable of type int.



o Q: Why is it important to declare the type of the variable that a pointer points to? Aren’t all addresses of the same length?

o A: It’s true that all addresses are of the same length, however when we perform an operation of the type “p++” where “p” is a pointer variable, for this operation to make sense the compiler needs to know the data type of the variable “p” points to. If “p” is a character pointer then “p++” will increment “p” by one byte (typically), if “p” were an integer pointer its value on “p++” would be incremented by 2 bytes (typically).


o Summary of what was learnt so far:


n Pointer is a data type that stores addresses, it is declared 
as follows:  int* a; char* p; etc.

n The value stored in a pointer p can be accessed through 
the dereferencing operator “*”.

n The address of a memory location of a variable can be 
accessed through the reference operator “&”.

n Pointer arithmetic: only addition and subtraction are 
allowed.


 Introduction to Pointers in C and c++




Pointers and Arrays



o The concept of array is very similar to the concept of pointer. The identifier of an array actually a pointer that holds the address of the first element of the array.

o Therefore if you have two declarations as follows:

n int a[10];” “int* p;” then the assignment “p = a;” is 
perfectly valid

n Also “*(a+4)” and “a[4]”  are equivalent as are “*
(p+4)” and “p[4]” .

n The only difference between the two is that we can change the value of “p” to any integer variable address whereas “a” will always point to the integer array of length 10 defined.

o The concept of array is very similar to the concept of pointer. The identifier of an array actually a pointer that holds the address of the first element of the array.

o Therefore if you have two declarations as follows:

n int a[10];” “int* p;” then the assignment “p = 
a;” is perfectly valid

n Also “*(a+4)” and “a[4]”  are equivalent as are 
“*(p+4)” and “p[4]” .

n The only difference between the two is that we can change the value of “p” to any integer variable address whereas “a” will always point to the integer array of length 10 defined.


Character Pointers, Arrays and Strings



o What is a String?

n A string is a character array that is ‘\0’ terminated.
n E.g. “Hello”

o What is a Character array?

n It is an array of characters, not necessarily ‘\0’ terminated
n E.g. char test[4] = {‘a’, ‘b’, ‘c’, ‘d’}; <this char array is not zero terminated>

o What is a character pointer?

n It is a pointer to the address of a character variable.

n E.g. char* a; <this pointer is not initialized>

o How do we initialize a Character pointer

n Initialize it to NULL. char* a = NULL;

n Let it point to a character array.

o char* a; char b[100]; a = b

n Initialize to a character string.

o char* a = “Hello”; a pointer to the memory location where  ‘H’ is stored. Here “a” can be viewed as a character array of size 6, the only difference being that a can be reassigned another memory location.


Examples

o char* a = “Hello”;
n  a -> gives address of ‘H’
n *a -> gives ‘H’
n a[0] -> gives ‘H’
n a++ -> gives address of ‘e’
n *a++ -> gives ‘e’
n a = &b; where b is another char  variable is perfectly LEGAL. However “char a[100];” “a =&b;” where b is another char variable is ILLEGAL.


Read full post »

Saturday, April 4, 2015

APPLET LIFE CYCLE METHODS - JAVA APPLETS

0 comments


JAVA APPLETS


• Introduction Applest

Java programs that can be embedded in Hypertext Markup Language (HTML) documents.

The browser that executes an applet is generically known as the applet container
import java.awt.*;
import java.applet.*;
/*
<applet code=applet1 width=200 height=400>
</applet>
*/
public class applet1 extends Applet
{
 public void paint(Graphics g)
 {
  g.drawString("Hello Welcome to java", 40,100);
  }
 }



APPLET LIFE CYCLE


applet life cycle in java



public void init() 

 This method is used to initialize

 variables of applet. Called only once.


public void start()


▫ Called immediately after init and can be called

 repeatedly.

▫ Used to start animation threads


public void paint(Graphics g)


This method is called automatically after 

start() method when applet begins its 

execution.


public void stop()


▫ Called when the browser leaves the page

▫ Used to stop animation threads


public void destroy()


Called automatically by applet when it ends up

 its activity. It releases the resources used by 

applet.





/*

<applet code=app width=500 height=500>

</applet>

*/

public class app extends Applet

{

  String str

  public void init()

  {

    str +=" init";

}

public void start()

{

 str +=" start";

 }

public void stop()

{

 str +=" stop";

 } 

 public void paint(Graphics g)

 {

   g.drawString(str,10,20);  

   }

 }




GRAPHICS IN APPLETS


Drawing Lines


public void drawLine(int x1,int y1,int x2,int y2);


Drawing Rectangles


public void drawRect(int x,int y , int width , int height);

public void drawRoundRect(int x,int y , int width , int height,int arcw ,int arch);

public void draw3DRect(int x, int y , int width , int height , boolean raised);

public void fillRect(int x,int y , int width , int height);

public void fillRoundRect(int x,int y , int width , int height,int arcw ,int arch);

public void fill3DRect(int x, int y , int width , int height , boolean raised);

public void clearRect(int x, int y, int width , int height);

 {clears the rectangle and fills it with background color }



Drawing Ovals & Circles


public void drawOval(int x,int y , int width , int height);

public void fillOval(int x,int y , int width , int height);


Drawing Ovals & Circles


public void drawArc(int x ,int y , int width , int height ,int startangle , int arcangle);

public void fillArc(int x , int y , int width , int height ,int startangle , int arcangle);


Drawing Polygons


public void drawPolygon(int xpoints[ ] ,int ypoints[ ] , int nPoints);

public void fillPolygon(int xpoints[ ] ,int ypoints[ ] , int nPoints);

Public void drawPolygon(Polygon P); {Draw outline}



 Drawing PolyLines


To draw open-Ended polylines

public void drawPolyline(int xpoints[ ] ,int ypoints[ ] , int nPoints);


Colors In Graphics


Belongs to java.awt.color;
Some common color constants…..

Color.black
Color.blue
Color.cyan
Color.darkGray
Color.gray
Color.green
Color.lightGray
Color.magenta
Color.orange
Color.pink
Color.red
Color.white
Color.yellow



We can also generate colors by combination of

RGB [red , green & blue].


We can also generate colors by combination of

HSB [hue , saturation & brightness].


Read full post »