Beruflich Dokumente
Kultur Dokumente
CERTIFICATE
Page
No. Title Date Sign
No.
Create an application to draw simple text
1
& perform various operations
Create an application to handle multiple
2
forms
Create an application to demonstrate
3
timers
Create an application to demonstrate use
4
of buffering
Create an application to demonstrate
5
bouncing ball
Create an application to demonstrate
6
different I / P control
Create an application to demonstrate a
7
dialog box
Create an application to demonstrate
8
bitmap image
Create an application to demonstrate
9
various types of events
Roll No: 22
Practical 1
P1_1. Introduction to J2ME.
Package javax.microedition.lcdui
¾ The UI API provides a set of features for implementation of user interfaces for
MIDP applications.
Built In:
Classes used:
User Defined:
1) HelloWorld:
¾ This class is used to display a message hello world in the TextBox .
¾ It also implements CommandListener to handle a quit command action
event.
PROGRAM:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
txtBox.addCommand(quitCommand);
txtBox.setCommandListener(this);
display.setCurrent(txtBox);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command c,Displayable d)
{
if(c == quitCommand)
{
destroyApp(false);
notifyDestroyed();
}
}
}
OUTPUT:
P1_3. Create an application to draw a line & rectangle using
J2ME.(DrawShape.java)
Classes used:
User Defined:
1) DrawShape:
¾ It is derived from MIDlet class.
¾ This class is used to display two lines & rectangle on the screen.
¾ It also uses a canvas derived class for drawing graphics.
2) MyCanvas:
¾ It is derived from Canvas class.
¾ This class is used to draw two lines & rectangle on the screen.
¾ It also implements command listener to handle the quit command event.
Methods Used:
1) drawLine:
public void drawLine(int x1,int y1,int x2,int y2)
¾ Draws a line between the coordinates (x1,y1) and (x2,y2) using the current
color and stroke style.
¾ Parameter x1 is the x coordinate of the start of the line, y1 is the y
coordinate of the start of the line, x2 is the x coordinate of the end of the
line, y2 is the y coordinate of the end of the line.
2) drawRect
public void drawRect(int x, int y, int width, int height)
¾ Draws the outline of the specified rectangle using the current color and
stroke style.
¾ The resulting rectangle will cover an area (width + 1) pixels wide by (height
+ 1) pixels tall.
¾ If either width or height is less than zero, nothing is drawn.
Parameters:
x - the x coordinate of the rectangle to be drawn
y - the y coordinate of the rectangle to be drawn
width - the width of the rectangle to be drawn
height - the height of the rectangle to be drawn
PROGRAM:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public DrawShape()
{
canvas = new MyCanvas(this);
}
public void startApp()
{
d = Display.getDisplay(this);
d.setCurrent(canvas);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void exitButton()
{
destroyApp(false);
notifyDestroyed();
}
}
MyCanvas(DrawShape d)
{
ds = d;
display = Display.getDisplay(ds);
quitCommand = new Command("Quit",Command.SCREEN,0);
addCommand(quitCommand);
setCommandListener(this);
}
public void paint(Graphics g)
{
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,255);
g.drawRect(50,50,150,150);
g.drawLine(50,50,200,200);
g.drawLine(200,50,50,200);
}
OUTPUT:
P1_4. Create an application to create a button. On click event of this
button, display the rectangle. (ShapeDemo.java)
Classes used:
User Defined:
1) ShapeDemo:
¾ It is derived from MIDlet class.
¾ This class is used to display the rectangle on the done button click.
¾ It also uses a canvas derived class for drawing rectangle.
¾ It also implements command listener to handle the done command event.
3) ShpCanvas:
¾ It is derived from Canvas class.
¾ This class is used to draw rectangle on the screen.
¾ It also implements command listener to handle the quit command event.
PROGRAM:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
import java.lang.*;
public ShapeDemo()
{
canvas = new ShpCanvas(this);
form = new Form("Rectangle");
doneCommand = new Command("Done",Command.SCREEN,0);
form.addCommand(doneCommand);
form.setCommandListener(this);
}
public void startApp()
{
d = Display.getDisplay(this);
d.setCurrent(form);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void exitButton()
{
destroyApp(false);
notifyDestroyed();
}
public void commandAction(Command c,Displayable ds)
{
if(c == doneCommand)
{
canvas.isFirstPaint = true;
d.setCurrent(canvas);
}
}
}
OUTPUT:
Roll No: 22
Practical 2
Study of the use of different input controls
i) Create an application to draw a geometrical figure by using Radio Button
in J2ME. (RadioButtonDemo.java)
Classes used:
Built In:
1) ChoiceGroup
public class ChoiceGroup extends Item implements Choice
User Defined:
1) RadioButtonDemo:
¾ It is derived from MIDlet class.
¾ This class is used to display the rectangle on selection of Rectangle radio
button and circle on selection of circle radio button .
¾ It also uses a canvas derived class for drawing rectangle and circle.
¾ It also implements command listener to handle the done command event.
2)ShpCanvas:
¾ It is derived from Canvas class.
¾ This class is used to draw rectangle on the screen.
¾ It also implements command listener to handle the quit command event.
Methods used:
Built In:
1) getString
public String getString(int elementNum)
Gets the String part of the element referenced by elementNum.
Specified by:
getString in interface Choice
Parameters:
elementNum - the index of the element to be queried
Returns:
the string part of the element
Throws:
IndexOutOfBoundsException - if elementNum is invalid
2) getSelectedIndex
public int getSelectedIndex()
¾ Returns the index number of an element in the ChoiceGroup that is
selected.
¾ For ChoiceGroup objects of type EXCLUSIVE and POPUP there is
at most one element selected, so this method is useful for
determining the user's choice. Returns -1 if there are no elements in
the ChoiceGroup.
¾ For ChoiceGroup objects of type MULTIPLE, this always returns -1
because no single value can in general represent the state of such a
ChoiceGroup. To get the complete state of a MULTIPLE Choice, see
getSelectedFlags.
Specified by:
getSelectedIndex in interface Choice
Returns:
index of selected element, or -1 if none
3) append
public int append(String stringPart,
Image imagePart)
Appends an element to the ChoiceGroup.
Specified by:
append in interface Choice
Parameters:
stringPart - the string part of the element to be added
imagePart - the image part of the element to be added, or null if there is no
image part
Returns:
the assigned index of the element
Throws:
NullPointerException - if stringPart is null
4) drawArc
public void drawArc(int x,int y,int width,int height,int startAngle,int arcAngle)
¾ Draws the outline of a circular or elliptical arc covering the specified
rectangle, using the current color and stroke style.
¾ The resulting arc begins at startAngle and extends for arcAngle
degrees, using the current color. Angles are interpreted such that
0 degrees is at the 3 o'clock position.
¾ A positive value indicates a counter-clockwise rotation while a
negative value indicates a clockwise rotation.
¾ The center of the arc is the center of the rectangle whose origin is
(x, y) and whose size is specified by the width and height arguments.
¾ The resulting arc covers an area width + 1 pixels wide by height + 1
pixels tall. If either width or height is less than zero, nothing is
drawn.
¾ The angles are specified relative to the non-square extents of the
bounding rectangle such that 45 degrees always falls on the line from
the center of the ellipse to the upper right corner of the bounding
rectangle.
¾ As a result, if the bounding rectangle is noticeably longer in one axis
than the other, the angles to the start and end of the arc segment will
be skewed farther along the longer axis of the bounds.
Parameters:
x - the x coordinate of the upper-left corner of the arc to be drawn
y - the y coordinate of the upper-left corner of the arc to be drawn
width - the width of the arc to be drawn
height - the height of the arc to be drawn
startAngle - the beginning angle
arcAngle - the angular extent of the arc, relative to the start angle
User Defined:
1) getOptionString(String s)
¾ this method is used to get the selected text of the radio buttons using
the methods getstring and getSelectedIndex.
Program:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
import java.lang.*;
public RadioButtonDemo()
{
canvas = new ShapeCanvas(this);
form = new Form("Select Shape");
shpChoice = new ChoiceGroup("Shape",Choice.EXCLUSIVE);
doneCommand = new Command("Done",Command.SCREEN,0);
alert = new Alert("My Alert",null,null,AlertType.INFO);
shpChoice.append("Rectangle",null);
shpChoice.append("Circle",null);
form.append(shpChoice);
form.addCommand(doneCommand);
form.setCommandListener(this);
}
public void startApp()
{
d = Display.getDisplay(this);
d.setCurrent(form);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void exitButton()
{
destroyApp(false);
notifyDestroyed();
}
public void commandAction(Command c,Displayable ds)
{
if(c == doneCommand)
{
canvas.getOptionString(shpChoice.getString(shpChoice.getSelectedIndex()
));
canvas.isFirstPaint = true;
d.setCurrent(canvas);
}
}
}
ShapeCanvas(RadioButtonDemo s)
{
shp = s;
display = Display.getDisplay(shp);
quitCommand = new Command("Quit",Command.SCREEN,0);
optionCommand = new Command("Option",Command.BACK,0);
addCommand(quitCommand);
addCommand(optionCommand);
setCommandListener(this);
}
if(str == "Rectangle")
g.drawRect(50,50,140,140);
if(str == "Circle")
g.drawArc(80,80,80,80,0,360);
}
public void commandAction(Command c,Displayable d)
{
if(c == quitCommand)
{
shp.exitButton();
}
if(c == optionCommand)
{
display.setCurrent(shp.form);
}
}
}
OUTPUT:
ii) Create an application to fill the rectangle with the selected colors in
check box in J2ME.(ColorDemo.java)
Classes used:
User Defined:
1) ColorDemo:
¾ It is derived from MIDlet class.
¾ This class is used to fill the rectangle with selected colors.
¾ It also uses a canvas derived class for drawing rectangle using selected
colors
¾ It gives choice group as multiple ie check boxes to select multiple color
choices.
¾ It also implements command listener to handle the done command event.
2) ColorCanvas:
¾ It is derived from Canvas class.
¾ This class is used to draw rectangle on the screen with selected colors from
the check boxes.
¾ It also implements command listener to handle the quit command event.
¾ It defines three variables rcol, gcol, bcol to store color values selected
according to red, green and blue colors.
Program:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
import java.lang.*;
public ColorDemo()
{
canvas = new ColorCanvas(this);
form = new Form("Select Color");
colorChoice = new ChoiceGroup("Color",Choice.MULTIPLE);
doneCommand = new Command("Done",Command.SCREEN,0);
colorChoice.append("Red",null);
colorChoice.append("Green",null);
colorChoice.append("Blue",null);
form.append(colorChoice);
form.addCommand(doneCommand);
form.setCommandListener(this);
}
public void startApp()
{
d = Display.getDisplay(this);
d.setCurrent(form);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void exitButton()
{
destroyApp(false);
notifyDestroyed();
}
public void commandAction(Command c,Displayable ds)
{
if(c == doneCommand)
{
if(colorChoice.isSelected(0) == true)
canvas.rcol = 255;
else
canvas.rcol = 0;
if(colorChoice.isSelected(1) == true)
canvas.gcol = 255;
else
canvas.gcol = 0;
if(colorChoice.isSelected(2) == true)
canvas.bcol = 255;
else
canvas.bcol = 0;
canvas.isFirstPaint = true;
d.setCurrent(canvas);
}
}
}
ColorCanvas(ColorDemo s)
{
shp = s;
display = Display.getDisplay(shp);
quitCommand = new Command("Quit",Command.SCREEN,0);
optionCommand = new Command("Option",Command.BACK,0);
addCommand(quitCommand);
addCommand(optionCommand);
setCommandListener(this);
}
OUTPUT:
iii) Create an application to change the font style of the defined text in the
text box in J2ME.(FontDemo.java)
Classes used:
Built In:
1) List
public class List extends Screen implements Choice
2) Font
public final class Font extends Object
• STYLE_PLAIN
• SIZE_MEDIUM
• FACE_SYSTEM
¾ Values for other attributes are arranged to have disjoint bit patterns in order
to raise errors if they are inadvertently misused .However, the values for the
different attributes are not intended to be combined with each other.
FACE_SYSTEM
public static final int FACE_SYSTEM
¾ The "system" font face.
¾ Value 0 is assigned to FACE_SYSTEM.
STYLE_BOLD
public static final int STYLE_BOLD
¾ The bold style constant.
¾ This may be combined with the other style constants for mixed
styles.
¾ Value 1 is assigned to STYLE_BOLD.
STYLE_ITALIC
public static final int STYLE_ITALIC
¾ The italicized style constant. This may be combined with the other
style constants for mixed styles.
¾ Value 2 is assigned to STYLE_ITALIC.
STYLE_UNDERLINED
public static final int STYLE_UNDERLINED
¾ The underlined style constant. This may be combined with the other
style constants for mixed styles.
¾ Value 4 is assigned to STYLE_UNDERLINED.
SIZE_LARGE
public static final int SIZE_LARGE
¾ The "large" system-dependent font size.
¾ Value 16 is assigned to SIZE_LARGE.
User Defined:
1) FontDemo:
¾ It is derived from MIDlet class.
¾ This class is used to change the font attributes of the text displayed on the
screen.
¾ It also uses a canvas derived class for changing font.
¾ It provides the list of font styles like bold, italic, underline etc as
IMPLICIT.
¾ It also implements command listener to handle the done command event.
3) FontCanvas:
¾ It is derived from Canvas class.
¾ This class is used to draw rectangle on the screen with selected colors from
the check boxes.
¾ It also implements command listener to handle the quit command event.
¾ The option command event is used to display the list of font styles again for
the selection.
¾ Uses setFont function of graphics class to set the font of the text as per the
selection of the style in the FontList.
Methods Used:
Built In:
1) setFont
public void setFont(Font font)
¾ Sets the font for all subsequent text rendering operations.
¾ If font is null, it is equivalent to setFont(Font.getDefaultFont()).
Parameters:
font - the specified font
Program:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
import java.lang.*;
public FontDemo()
{
canvas = new FontCanvas(this);
canvas.getOptionString(fontList.getString(fontList.getSelectedIndex()));
canvas.isFirstPaint = true;
d.setCurrent(canvas);
}
}
}
FontCanvas(FontDemo f)
{
fnt = f;
display = Display.getDisplay(fnt);
quitCommand = new Command("Quit",Command.SCREEN,0);
optionCommand = new Command("Option",Command.BACK,0);
addCommand(quitCommand);
addCommand(optionCommand);
setCommandListener(this);
}
if(str == "Plain")
g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.S
IZE_LARGE));
if(str == "Underline")
g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_UNDERLINE
D,Font.SIZE_LARGE));
if(str == "Bold")
g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SI
ZE_LARGE));
if(str == "Italic")
g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_ITALIC,Font.
SIZE_LARGE));
g.drawString("KIRTI",100,90,Graphics.TOP|Graphics.LEFT);
}
public void commandAction(Command c,Displayable d)
{
if(c == quitCommand)
{
fnt.exitButton();
}
if(c == optionCommand)
{
display.setCurrent(fnt.fontList);
}
}
}
Roll No: 22
Practical 3
PROGRAM:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
import java.lang.*;
public FormDemo()
{
form1 = new Form("Personal Info");
form2 = new Form("Educational Info");
form3 = new Form("Student Info");
form1.append(txtName);
form1.append(txtCity);
form1.append(txtTel);
form1.addCommand(cmdNextForm1);
form2.append(chkStream);
form2.addCommand(cmdNextForm2);
form3.addCommand(cmdBack);
form3.append(txtDesc);
form1.setCommandListener(this);
form2.setCommandListener(this);
form3.setCommandListener(this);
}
public void startApp()
{
d = Display.getDisplay(this);
d.setCurrent(form1);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void exitButton()
{
destroyApp(false);
notifyDestroyed();
}
public void commandAction(Command c,Displayable ds)
{
if(c == cmdNextForm1)
d.setCurrent(form2);
if(c == cmdNextForm2)
{
str = “Name:"+txtName.getString()+"\n\n"+"City:"+txtCity.getString()+"\n\n"
+"Tel.:"+txtTel.getString()+"\n\n";
str=str+"Stream:"+chkStream.getString(chkStream.getSelectedIndex());
txtDesc.setString(str);
d.setCurrent(form3);
}
if(c == cmdBack)
d.setCurrent(form1);
}
}
OUTPUT:
Roll No: 22
Practical 4
Create an application to demonstrate timers. (Animation. java)
i) Draw a filled rectangle.
ii) Change the color of the rectangle after the specified interval of time.
Classes Used:
Built In:
1) Random
public class Random extends Object
2) Math
public final class Math extends Object
¾ The class Math contains methods for performing basic numeric operations.
3) Thread
public class Thread extends Object implements Runnable
¾ A thread is a thread of execution in a program.
¾ The Java Virtual Machine allows an application to have multiple threads of
execution running concurrently.
¾ Every thread has a priority.
¾ Threads with higher priority are executed in preference to threads with
lower priority.
¾ There are two ways to create a new thread of execution.
¾ One is to declare a class to be a subclass of Thread. This subclass should
override the run method of class Thread.
¾ An instance of the subclass can then be allocated and started.
User Defined:
1) AnimDemo:
¾ It is derived from MIDlet class.
¾ This class is used to change the color of the rectangle at random.
¾ It also uses a canvas derived class for changing rectangle color.
¾ It also implements command listener to handle the done command event.
4) AnimCanvas:
¾ It is derived from Canvas class.
¾ This class is used to fill rectangle on the screen with random generated
using Random function and stored in x, y, z variables in paint().
¾ It also implements command listener to handle the quit command event.
¾ Creates a thread object and overrides the run function of runnable interface
in which the random numbers are generated.
¾ Uses setColor function of graphics class to set the random color of the
Rectangle as reapint() is called.
Methods Used:
Built In:
1) start
public void start()
¾ Causes this thread to begin execution; the Java Virtual Machine calls
the run method of this thread.
¾ The result is that two threads are running concurrently: the current
thread (which returns from the call to the start method) and the other
thread (which executes its run method).
Throws:
IllegalThreadStateException - if the thread was already started.
2) sleep
public static void sleep(long millis) throws InterruptedException
¾ Causes the currently executing thread to sleep (temporarily cease
execution) for the specified number of milliseconds.
¾ The thread does not lose ownership of any monitors.
Parameters:
millis - the length of time to sleep in milliseconds.
Throws:
InterruptedException - if another thread has interrupted the current thread.
The interrupted status of the current thread is cleared when this exception is
thrown.
3) run
public void run()
¾ When an object implementing interface Runnable is used to create a
thread, starting the thread causes the object's run method to be called
in that separately executing thread.
¾ The general contract of the method run is that it may take any action
whatsoever.
4) nextInt
public int nextInt()
¾ Returns the next pseudorandom, uniformly distributed int value from
this random number generator's sequence.
¾ The general contract of nextInt is that one int value is
pseudorandomly generated and returned.
¾ All 232 possible int values are produced with (approximately) equal
probability.
¾ The method nextInt is implemented by class Random as follows:
public int nextInt() { return next(32); }
Returns:
the next pseudorandom, uniformly distributed int value from this random
number generator's sequence.
5) abs
public static int abs(int a)
¾ Returns the absolute value of an int value.
¾ If the argument is not negative, the argument is returned.
¾ If the argument is negative, the negation of the argument is returned.
¾ Note that if the argument is equal to the value of
Integer.MIN_VALUE, the most negative representable int value, the
result is that same value, which is negative.
Parameters:
a - an int value.
Returns:
the absolute value of the argument.
PROGRAM:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
import java.lang.*;
public AnimDemo()
{
canvas = new AnimCanvas(this);
form = new Form("Want to draw Rect");
doneCommand = new Command("Done",Command.SCREEN,0);
form.addCommand(doneCommand);
form.setCommandListener(this);
}
public void startApp()
{
d = Display.getDisplay(this);
d.setCurrent(form);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void exitButton()
{
destroyApp(false);
notifyDestroyed();
}
public void commandAction(Command c,Displayable ds)
{
if(c == doneCommand)
{
canvas.isFirstPaint = true;
d.setCurrent(canvas);
}
}
}
AnimCanvas(AnimDemo s)
{
shp = s;
display = Display.getDisplay(shp);
quitCommand = new Command("Quit",Command.SCREEN,0);
optionCommand = new Command("Option",Command.BACK,0);
addCommand(quitCommand);
addCommand(optionCommand);
setCommandListener(this);
t = new Thread(this);
rand = new Random();
t.start();
}
public void paint(Graphics g)
{
if(isFirstPaint)
{
isFirstPaint = false;
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
}
g.setColor(x,y,z);
g.fillRect(50,50,140,140);
}
public void commandAction(Command c,Displayable d)
{
if(c == quitCommand)
{
shp.exitButton();
}
if(c == optionCommand)
{
display.setCurrent(shp.form);
}
}
public void run()
{
try
{
x = Math.abs(rand.nextInt()%100);
y = Math.abs(rand.nextInt()%100);
z = Math.abs(rand.nextInt()%255);
repaint();
t.sleep(100);
t.run();
}catch(Exception e){}
}
}
OUTPUT:
Roll No: 22
Practical 5
Create an application to demonstrate use of buffering (back and double)
i) Draw a circle using a single buffer and
ii) Fill that circle using another buffer.
Classes Used:
Built In:
1) Image
public class Image extends Object
¾ The Image class is used to hold graphical image data. Image objects exist
independently of the display device.
¾ They exist only in off-screen memory and will not be painted on the display
unless an explicit command is issued by the application (such as within the
paint() method of a Canvas) or when an Image object is placed within a
Form screen or an Alert screen and that screen is made current.
¾ Images are either mutable or immutable depending upon how they are
created.
¾ Immutable images are generally created by loading image data from
resource bundles, from files, or from the network.
¾ They may not be modified once created.
¾ Mutable images are created as blank images containing only white pixels.
¾ The application may render on a mutable image by calling getGraphics() on
the Image to obtain a Graphics object expressly for this purpose.
¾ Images may be placed within Alert, Choice, Form, or ImageItem objects.
¾ The high-level user interface implementation may need to update the
display at any time, without notifying the application.
¾ In order to provide predictable behavior, the high-level user interface
objects provide snapshot semantics for the image.
¾ That is, when a mutable image is placed within an Alert, Choice, Form, or
ImageItem object, the effect is as if a snapshot is taken of its current
contents.
5) System
public final class System extends Object
¾ The System class contains several useful class fields and methods. It
cannot be instantiated.
User Defined:
1) BufferDemo:
¾ It is derived from MIDlet class.
¾ This class is used to change the color of the rectangle at random.
¾ It also uses a canvas derived class for changing rectangle color.
¾ It also implements command listener to handle the done command event.
6)BufferCanvas:
¾ It is derived from Canvas class.
¾ This class is used to fill Circle on the screen in paint().
¾ It also implements command listener to handle the quit command event.
¾ Creates a thread object and overrides the run function of runnable interface
in which the according to time in millis it changes the image on the screen.
¾ It also implements command listener to handle the quit command event.
Methods Used:
Built In:
1) currentTimeMillis
public static long currentTimeMillis()
Returns the current time in milliseconds.
Returns:
the difference, measured in milliseconds, between the current time and
midnight, January 1, 1970 UTC.
2) callSerially
public void callSerially(Runnable r)
¾ Causes the Runnable object r to have its run() method called later,
serialized with the event stream, soon after completion of the repaint
cycle.
¾ The call to run() will be serialized along with the event calls into the
application.
¾ The run() method will be called exactly once for each call to
callSerially().
¾ Calls to run() will occur in the order in which they were requested by
calls to callSerially().
¾ If the current Displayable is a Canvas that has a repaint pending at
the time of a call to callSerially(), the paint() method of the Canvas
will be called and will return, and a buffer switch will occur (if
double buffering is in effect), before the run() method of the
Runnable is called.
¾ If the current Displayable contains one or more CustomItems that
have repaints pending at the time of a call to callSerially(), the
paint() methods of the CustomItems will be called and will return
before the run() method of the Runnable is called. Calls to the run()
method will occur in a timely fashion, but they are not guaranteed to
occur immediately after the repaint cycle finishes, or even before the
next event is delivered.
¾ The callSerially() method may be called from any thread. The call to
the run() method will occur independently of the call to
callSerially(). In particular, callSerially() will never block waiting for
r.run() to return.
¾ The callSerially() facility may be used by applications to run an
animation that is properly synchronized with the repaint cycle. A
typical application will set up a frame to be displayed and then call
repaint(). The application must then wait until the frame is actually
displayed, after which the setup for the next frame may occur. The
call to run() notifies the application that the previous frame has
finished painting.
3) setGrayScale
public void setGrayScale(int value)
¾ Sets the current grayscale to be used for all subsequent rendering
operations.
¾ For monochrome displays, the behavior is clear.
¾ For color displays, this sets the color for all subsequent drawing
operations to be a gray color equivalent to the value passed in.
¾ The value must be in the range 0-255.
Parameters:
value - the desired grayscale value
Throws:
IllegalArgumentException - if the gray value is out of range
4) drawImage
public void drawImage(Image img, int x, int y, int anchor)
¾ Draws the specified image by using the anchor point.
¾ The image can be drawn in different positions relative to the anchor
point by passing the appropriate position constants.
¾ If the source image contains transparent pixels, the corresponding
pixels in the destination image must be left untouched.
¾ If the source image contains partially transparent pixels, a
compositing operation must be performed with the destination pixels,
leaving all pixels of the destination image fully opaque.
¾ If img is the same as the destination of this Graphics object, the
result is undefined. For copying areas within an Image, copyArea
should be used instead.
Parameters:
img - the specified image to be drawn
x - the x coordinate of the anchor point
y - the y coordinate of the anchor point
anchor - the anchor point for positioning the image
Throws:
IllegalArgumentException - if anchor is not a legal value
NullPointerException - if img is null
Program:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
}
OUTPUT:
Roll No: 22
Practical 6
Menu
______________
1. Start / Stop
2. About
Where,
Start / Stop menu starts/stops the bouncing of the ball.
About menu tells the information about the programmer.
Classes Used:
User Defined:
1) GameDemo:
¾ It is derived from MIDlet class.
¾ It also uses a canvas derived class for adding new elements of balls array.
¾ Inturn AreaCanvas uses object of ball class.
7)AreaCanvas:
¾ It is derived from Canvas class.
¾ This class is used to create objects of ball class.
¾ It also implements command listener to handle the start,stop and help
command event.
¾ keyReleased is overridden to check the status of keys like left, right, up and
down.
3) Ball
¾ Creates a thread object and overrides the run function of runnable interface
in which the according to random numbers generated it draws the circle at
random positions on the screen.
¾ On left key creates new object of ball , on right key deletes the previous
object of ball , on up key increases the speed and on down key decreases the
speed.
¾ For all these this class contains variables like x, y, lastX, lastY, delay=20,
rc, gc ,bc and boolean down, right to store the values of ball object last
created.
Methods Used:
Built In:
1) setTimeout
public void setTimeout(int time)
¾ Set the time for which the Alert is to be shown.
¾ This must either be a positive time value in milliseconds, or the
special value FOREVER.
Parameters:
time - timeout in milliseconds, or FOREVER
Throws:
IllegalArgumentException - if time is not positive and is not FOREVER
2) getClipX
public int getClipX()
¾ Gets the X offset of the current clipping area, relative to the
coordinate system origin of this graphics context.
¾ Separating the getClip operation into two methods returning integers
is more performance and memory efficient than one getClip() call
returning an object.
Returns:
X offset of the current clipping area
3) getClipY
public int getClipY()
¾ Gets the Y offset of the current clipping area, relative to the
coordinate system origin of this graphics context.
¾ Separating the getClip operation into two methods returning integers
is more performance and memory efficient than one getClip() call
returning an object.
Returns:
Y offset of the current clipping area
4) getClipWidth
public int getClipWidth()
¾ Gets the width of the current clipping area.
Returns:
width of the current clipping area.
5) getClipWidth
public int getClipWidth()
¾ Gets the width of the current clipping area.
Returns:
width of the current clipping area.
6) getGameAction
public int getGameAction(int keyCode)
¾ Gets the game action associated with the given key code of the
device.
¾ Returns zero if no game action is associated with this key code.
¾ The mapping between key codes and game actions will not change
during the execution of the application.
Parameters:
keyCode - the key code
Returns:
the game action corresponding to this key, or 0 if none
7) keyReleased
protected void keyReleased(int keyCode)
¾ Called when a key is released.
¾ The getGameAction() method can be called to determine what game
action, if any, is mapped to the key.
¾ Class Canvas has an empty implementation of this method, and the
subclass has to redefine it if it wants to listen this method.
Parameters:
keyCode - the key code of the key that was released
Program:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
import java.lang.*;
public GameDemo()
{
canvas = new AreaCanvas(this);
}
public void startApp()
{
d = Display.getDisplay(this);
d.setCurrent(canvas);
canvas.start();
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void exitButton()
{
destroyApp(false);
notifyDestroyed();
}
}
g.fillRect(g.getClipX(),g.getClipY(),g.getClipWidth(),g.getClipHeight());
for(i=0;i<balls;i++)
ball[i].paint(g);
g.setColor(0,0,0);
g.drawRect(0,0,getWidth()-1,getHeight()-1);
}
public void commandAction(Command c,Displayable d)
{
if(c == quitCommand)
{
gd.exitButton();
}
if(c == helpCommand)
{
display.setCurrent(helpAlert);
}
if(c == startCommand)
{
if(gameStopped==false)
{
stop();
gameStopped=true;
}
else
{
start();
gameStopped=false;
}
}
}
public void start()
{
Thread t;
for(i=0;i<balls;i++)
{
ball[i].stop = false;
t=new Thread(ball[i]);
t.start();
}
}
public void stop()
{
for(i=0;i<balls;i++)
ball[i].stop = true;
}
public void keyReleased(int keyCode)
{
int button;
button = getGameAction(keyCode);
switch(button)
{
case LEFT:
if(balls>0)
{
ball[balls-1].stop=true;
ball[balls-1] = null;
balls--;
repaint();
}
else
balls=0;
break;
case RIGHT:
ball[balls] = new Ball(this);
new Thread(ball[balls]).start();
balls++;
break;
case UP:
if(delay > 0)
delay-=10;
else
delay=0;
break;
case DOWN:
if(delay < 200)
delay+=10;
else
delay=200;
break;
}
}
}
class Ball implements Runnable
{
private int i = 0,x,y,lastX,lastY,delay=20,rc,gc,bc;
boolean down=true,right=true;
AreaCanvas c;
boolean stop=false;
Random random;
Ball(AreaCanvas c)
{
super();
this.c = c;
random = new Random();
lastX=c.getWidth();
lastY=c.getHeight();
x=Math.abs(random.nextInt()%(lastX-10));
y=Math.abs(random.nextInt()%(lastY-10));
rc=Math.abs(random.nextInt()%255);
gc=Math.abs(random.nextInt()%255);
bc=Math.abs(random.nextInt()%255);
}
if(down)
y++;
else
y--;
if(x==0)
right=true;
if(x==lastX-10)
right=false;
if(y==0)
down=true;
if(y==lastY-10)
down=false;
c.repaint(x-1,y-1,12,12);
try
{
Thread.sleep(c.delay);
}
catch(Exception e){}
}
}
}
OUTPUT:
Roll No: 22
Practical 7
Create an application to take input from user & show it on the Dialog Box
using J2ME
Classes Used:
User Defined:
1) Alert Demo:
¾ This class is derived from MIDlet and implements Command Listener.
¾ Has two command buttons back and show.
¾ Provides choices as exclusive for gender male or female and choices as
multiple for qualification SSC, BSC, MSC .
¾ The all description information taken is finally displayed in a text field
as a string.
Methods Used:
Built In:
1) setString
public void setString(String str)
¾ Sets the text string used in the Alert.
¾ If the Alert is visible on the display when its contents are updated through a
call to setString, the display will be updated with the new contents as soon
as it is feasible for the implementation to do so.
Parameters:
str - the Alert's text string, or null if there is no text
2) isSelected
public boolean isSelected(int elementNum)
¾ Gets a boolean value indicating whether this element is selected.
Specified by:
isSelected in interface Choice
Parameters:
elementNum - the index of the element to be queried
Returns:
selection state of the element
Throws:
IndexOutOfBoundsException - if elementNum is invalid
Program:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
import java.lang.*;
public AlertDemo()
{
form = new Form("Personal Info");
genChoice = new ChoiceGroup("Gender",Choice.EXCLUSIVE);
qualChoice = new
ChoiceGroup("Qualification",Choice.MULTIPLE);
showCommand = new Command("Show",Command.SCREEN,0);
backCommand = new Command("Back",Command.SCREEN,0);
txtDesc = new TextField("Description",null,50,TextField.ANY);
alert = new Alert("My Alert",null,null,AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
genChoice.append("Male",null);
genChoice.append("Female",null);
qualChoice.append("H.S.C.",null);
qualChoice.append("B.Sc.",null);
qualChoice.append("M.Sc.",null);
form.append(genChoice);
form.append(qualChoice);
form.append(txtDesc);
form.addCommand(showCommand);
form.setCommandListener(this);
}
public void startApp()
{
d = Display.getDisplay(this);
d.setCurrent(form);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void exitButton()
{
destroyApp(false);
notifyDestroyed();
}
public void commandAction(Command c,Displayable ds)
{
if(c == showCommand)
{
str = "Gender:"+genChoice.getString(genChoice.getSelectedIndex())+
"\n\n"+"Description:"+txtDesc.getString()+"\n\n"+"Qual:";
for(i=0;i<3;i++)
{
if(qualChoice.isSelected(i) == true)
str = str+qualChoice.getString(i)+",";
}
alert.setString(str);
d.setCurrent(alert);
}
}
}
OUTPUT:
Roll No: 22
Practical 8
Demonstration of various graphics objects
1) Colors
i. Create an application to change the background color in
J2ME. (ChangeBgColor.java)
ii. Create an application to change the text color in J2ME.
iii. Create an application to change the background as well as text
color at a time in J2ME.
Classes Used:
User Defined:
1) changeRect:
¾ It is extends MIDlet class.
¾ This class contains the object of MyCanvas1 which changes the color of the
text as well as of the background.
2) MyCanvas1:
¾ This class contains a paint method and a button on click of that button
the color of text and background changes randomly.
¾ Uses the object of Random class to generate the random numbers.
Methods Used:
Built In:
1) drawString
public void drawString(String str, int x, int y, int anchor)
¾ Draws the specified String using the current font and color.
¾ The x,y position is the position of the anchor point.
Parameters:
str - the String to be drawn
x - the x coordinate of the anchor point
y - the y coordinate of the anchor point
anchor - the anchor point for positioning the text
Throws:
NullPointerException - if str is null
IllegalArgumentException - if anchor is not a legal value
Program:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
public ChangeRect()
{
canvas = new MyCanvas1(this);
}
public void startApp()
{
d = Display.getDisplay(this);
d.setCurrent(canvas);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void exitButton()
{
destroyApp(false);
notifyDestroyed();
}
}
MyCanvas1(ChangeRect d)
{
ds = d;
display = Display.getDisplay(ds);
quitCommand = new Command("Quit",Command.SCREEN,0);
colorCommand = new Command("Color",Command.BACK,0);
rand = new Random();
addCommand(quitCommand);
addCommand(colorCommand);
setCommandListener(this);
}
}
public void commandAction(Command c,Displayable d)
{
if(c == quitCommand)
{
ds.exitButton();
}
if(c == colorCommand)
{
repaint();
}
}
}
OUTPUT:
P3 (Program).
2) Bitmap
i. Create an application to create a mutable bitmap in J2ME.
Classes Used:
User Defined:
1) MutableImage
¾ It is extends MIDlet class.
¾ This class contains the object of MyCanvas which displays the mutable
image on the screen.
2)MyCanvas:
¾ This class contains a paint method that creates mutable image object.
¾ Uses exitMidlet to close application.
Program:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class MutableImage extends MIDlet
{
private Display display;
private MyCanvas canvas;
public MutableImage ()
{
display = Display.getDisplay(this);
canvas = new MyCanvas (this);
}
try
{
image = Image.createImage(70, 70);
Graphics graphics = image.getGraphics();
graphics.setColor(255, 0, 0);
graphics.fillArc(10, 10, 60, 50, 180, 180);
}
catch (Exception error) { }
}
OUTPUT:
ii) Create an application to create an immutable bitmap in J2ME.
Classes Used:
User Defined:
1) ImmutableImage
¾ It is extends MIDlet class.
¾ This class contains the object of MyCanvas which displays the immutable
image on the screen.
2)MyCanvas:
¾ This class contains a paint method that creates immutable image object.
¾ Uses exitMidlet to close application.
Program:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ImmutableImage extends MIDlet
{
private Display display;
private MyCanvas canvas;
public ImmutableImage ()
{
display = Display.getDisplay(this);
canvas = new MyCanvas (this);
}
try
{
image = Image.createImage("/jc_frame_0.png");
}
catch (Exception error) { }
}
Practical 9
Classes Used:
User Defined:
1) TextDemo :
¾ This Class is used to create the TxtCanvas class object to show
keypressed on the keyboard.
¾ It also defines a method for exitbutton to destroy an application
which is called in TxtCanvas.
2) TxtCanvas:
¾ This class overrides the method KeyPressed to get the keyCode of
the key currently pressed and convert it into character string to be
displayed on the screen.
¾ Also contains the quit button to close application.
¾ And clear command to clear the screen content.
Methods used:
Built In:
1) keyPressed
protected void keyPressed(int keyCode)
¾ Called when a key is pressed.
Parameters:
keyCode - the key code of the key that was pressed
Program:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
import java.lang.*;
public TextDemo()
{
canvas = new TxtCanvas(this);
}
public void startApp()
{
d = Display.getDisplay(this);
d.setCurrent(canvas);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void exitButton()
{
destroyApp(false);
notifyDestroyed();
}
}
TxtCanvas(TextDemo b)
{
bm = b;
display = Display.getDisplay(bm);
quitCommand = new Command("Quit",Command.SCREEN,0);
clearCommand = new Command("Clear",Command.SCREEN,0);
addCommand(quitCommand);
addCommand(clearCommand);
setCommandListener(this);
}
CERTIFICATE
Page
No. Title Date Sign
No.
1 Single-Channel Queuing Model
5 Continuous Distribution
6 Measure of Performance
Practical 1
Single – Server Queuing System
1) A small grocery store has only one checkout counter. Customers arrive at
random from 1 to 8 minutes. Each possible value of interarrival time has the same
probability of occurrence. The service times vary from 1 to 6 minutes with the
probabilities shown in following table.
Service-Time distribution.
Service Time Cumulative Random-Digit
(Minutes) Probability Probability Assignment
1 0.10 0.10 01-10
2 0.20 0.30 11-30
3 0.30 0.60 31-60
4 0.25 0.85 61-85
5 0.10 0.95 86-95
6 0.05 1.00 96-00
Create the distribution table of inter-arrival time. Give the random numbers to 20
customers and write the Interarrival time and service time duration for the
individual customer according to the random digit range and prepare the
simulation table and analyze the system by simulating the arrival and services of
20 customers. From the table find (i) The average waiting time for a customer. (ii)
The average service time (iii) The average time between arrivals (iv) The average
waiting time for the customer who is waiting. (v) The average time the customer
spends in the system.
(vi) The probabilities that (a) a customer has to wait (b) of an idle server
2) with the intearrival time distribution for the time from 1 to 10. Give the random
numbers according to the random digit range and prepare the simulation table and
analyze the system by simulating the arrival and services of 20 customers.
3) Taking the same data as that example 1), but now take the service distribution
be
Service Time 1 2 3 4 5 6
(Minutes)
Probability 0.05 0.10 0.20 .30 0.25 0.10 Develop
the simulation table & analysis for 20 customers. What is the effect of changing
the service-time distribution?
Single-Server Queuing System
1. Data
Simulation
2.Data
Simulation
3.Data
Simulation
Source Code
x = Rnd() * 1000
x = WorksheetFunction.Round(x, 0)
Sheet1.Cells(i + 32, 3) = x
j=0
While (Sheet1.Cells(j + 7, 2) <> "")
a = Val(Sheet1.Cells(j + 7, 5))
b = Val(Sheet1.Cells(j + 7, 6))
If (x > a And x <= b) Then
Sheet1.Cells(i + 32, 4) = Sheet1.Cells(j + 7, 2)
End If
j=j+1
Wend
Next
For i = 1 To 20
x = Rnd() * 100
x = WorksheetFunction.Round(x, 0)
Sheet1.Cells(i + 32, 6) = x
j=0
While (Sheet1.Cells(j + 22, 2) <> "")
a = Val(Sheet1.Cells(j + 22, 5))
b = Val(Sheet1.Cells(j + 22, 6))
If (x > a And x <= b) Then
Sheet1.Cells(i + 32, 7) = Sheet1.Cells(j + 22, 2)
End If
j=j+1
Wend
Next
End Sub
Roll No: 22
Practical 2
What percent of time A & B individually where busy over total service time.?
What percent of arrivals had to wait.?
What is the average waiting time for all the customer.?
What is the average waiting time for the customer who had to wait.?
Draw your conclusion regarding the system and cost of waiting
2) By using above data but the change arriving distribution of cars as follows
Time between Arrivals 0 1 2 3 4
(Minutes)
Probability 0.10 0.20 0.35 0.20 0.15
Develop the simulation table and subsequent analysis for the period of
1 hour. What is the effect of changing the arrival distribtion.
3) Using the above data with B getting the preference when both the carhops
are idle and the service distribution for A is changed to
Service Time(Minutes) 1 2 3 4
Probability 0.30 0.30 0.25 0.15
Shortcuts Used:
1) R no for arrival = Random number for Inter Arrival Time
2) IAT = Inter Arrival Time
3) TAT = Total Arrival Time
4) R no for service = Randum number for Service Time
5) STARTT = Start time of Able/Baker
6) SERT = Service Time of Able/Baker
7) ENDT = End Service Time of Able/Baker
Formulas Used:
2) STARTT of Able =
IF(TAT >= MAX(ENDT of Able))
TAT
ELSE
IF(TAT>=MAX(ENDT of Baker))
""
ELSE
IF(MAX(ENDT of Able)>MAX(ENDT of Baker)
""
ELSE
MIN(MAX(ENDT of Able),MAX(ENDT of Baker))
3) SERT of Able =
IF(STARTT of Able="")
""
ELSE
SERT
4) ENDT of Able =
IF(STARTT of Able="")
""
ELSE
STARTT of Able + SERT of Able
5) STARTT of Baker =
IF(STARTT of Able="")
IF(TAT>MAX(ENDT of Baker))
TAT
ELSE
IF(TAT>MAX(ENDT of Able))
""
ELSE
MIN(MAX(STARTT of Baker),MAX(STARTT of Able))
ELSE
""
6) SERT of Baker =
IF(STARTT of Baker="")
""
ELSE
SERT
7) ENDT of Baker =
IF(STARTT of Baker="")
""
ELSE
STARTT of Baker + SERT of Baker
8) QUIT of Customer =
IF(STARTT of Able="")
IF(STARTT of Baker>TAT)
STARTT of Baker-TAT
ELSE
0
ELSE
IF(STARTT of Able>TAT)
STARTT of Able -TAT
ELSE
0
1-2)
Percent of time Baker were busy over total service time of baker * 100
=
total service time end service time of Able
29 * 100
=
45
= 64.44%
(ii)
Percent of arrivals had to wait number arrivals who wait * 100
=
numbers of arrivals
8 * 100
=
20
= 40%
(iii)
Average waiting time for total waiting time of customers
=
all customers numbers of customers
10
=
20
= 0.5
(iv)
Average waiting time for total waiting time of customers who wait
=
all customers who wait numbers of customers who wait
10
=
8
= 1.25
2-1)
Interarrival Distribution of Cars
Percent of time Able were busy over total service time of Able * 100
=
total service time end service time of Able
45 * 100
=
47
= 95.74%
Percent of time Baker were busy over total service time of baker * 100
=
total service time end service time of Able
31 * 100
=
43
= 72.09%
(ii)
Percent of arrivals had to wait number arrivals who wait * 100
=
numbers of arrivals
8 * 100
=
20
= 40%
(iii)
Average waiting time for total waiting time of customers
=
all customers numbers of customers
11
=
20
= 0.55
(iv)
Average waiting time for total waiting time of customers who wait
=
all customers who wait numbers of customers who wait
11
=
8
= 1.375
3-1)
Interarrival Distribution of Cars
Practical No. 3
Prepare the simulation table for purchase of 70 newspapers and find out the
total profit.
Total Profit = (revenue from sales) – ( cost of newspapers) – (lost profit from
excess demand) + (salvage from sale of scrap papers)
News Paper Seller’s Problem
Data
Simulation
Source Code
For i = 0 To 6
a = Cells(7 + i, 2)
a1 = a * 100
Cells(7 + i, 8) = "'" & Val(b) & "-" & Val(a1)
b = a1 + 1
Next i
b=1
For i = 0 To 5
a = Cells(7 + i, 3)
a1 = a * 100
Cells(7 + i, 9) = "'" & Val(b) & "-" & Val(a1)
b = a1 + 1
Next i
b=1
For i = 0 To 4
a = Cells(7 + i, 4)
a1 = a * 100
Cells(7 + i, 10) = "'" & Val(b) & "-" & Val(a1)
b = a1 + 1
Next i
End Sub
For i = 0 To 2
a = Cells(18 + i, 3)
a1 = a * 100
Cells(18 + i, 9) = "'" & Val(b) & "-" & Val(a1)
b = a1 + 1
Next i
End Sub
Roll No: 22
Practical No. 4
Discrete Distribution
1) Write a program to find the Probability Mass Function (pmf) -> p( X), mean
- > E ( X ) and Variance -> V( X ), for the Bernouilli Distribution. Accept
the probability of success p from the user and display the value of pmf.
2) Write a program to find the Probability Mass Function (pmf) -> p(X), mean
-> E(X) and variance -> V(X), for the Binomial Distribution. Accept the
probability of success p, no. of trials to achieve the first success is x from
the user and display the value of pmf.
3) Write a program to find the Probability Mass Function (pmf) -> p( X), mean
- > E ( X ) and Variance -> V( X ), for the Geometric Distribution. Accept
the probability of success p, no. of trials n from the user and display the
value of pmf.
4) Write a program to find the Probability Mass Function (pmf) -> p(X), mean
-> p(X) for the Poisson Distribution. Accept the mean and variance from
the user and display the value of pmf. Also find the cumulative Poisson
distribution table.
Discrete Distribution:
Code:
End Sub
Binomial.Cells(12, 3) = n
Binomial.Range("c10").Value = x
m=n*p
Binomial.Cells(17, 3) = p
v=n*p*q
Binomial.Cells(19, 3) = v
MSChart1.RowCount = n + 1
MSChart1.ColumnCount = 1
MSChart1.Column = 1
For i = 0 To n
Range("E" & i + 6).Value = i
MSChart1.Row = i + 1
MSChart1.Data = pmf
Range("g6:g8").Clear
Next i
End Sub
For i = 0 To 25
For i = lb To ub
sum = sum + Range("F" & i + 6).Value
Next i
Range("B26").Value = "P(" & lb & "<= X <=" & ub & ")"
Range("C26").Value = Math.Round(sum, 2)
End Sub
Geometric Distribution:
Code:
Dim p, q, a, n, x, y, z As Double
Private Sub cmdCal_Click()
Dim i, j As Integer
Dim sum As Double
Geometric.Cells(12, 3) = n
MSChart1.RowCount = n
MSChart1.ColumnCount = 1
MSChart1.Column = 1
Range("E5", "F100").ClearContents
For i = 1 To n
Range("E" & i + 6).Value = i
pmf = q ^ (i - 1) * p
Range("F" & i + 6).Value = Round(pmf, 5)
MSChart1.Row = i
MSChart1.Data = pmf
Next i
Geometric.Cells(14, 3) = Range("F" & i + 6 - 1).Value
Geometric.Cells(16, 3) = 1 / p
Geometric.Cells(18, 3) = q / (Math.Sqr(p))
End Sub
Poisson Distribution:
Code:
MSChart2.RowCount = x + 1
MSChart2.ColumnCount = 1
MSChart2.Column = 1
cdf = 0
For i = 0 To x
Range("E" & i + 6).Value = i
Range("H5").Formula = "=power(" & mv & "," & i & ")"
Range("H6").Formula = "=fact(" & i & ")"
pmf = Exp(-mv) * Range("H5").Value / Range("H6").Value
Range("F" & i + 6).Value = Round(pmf, 5)
MSChart1.Row = i + 1
MSChart1.Data = pmf
MSChart2.Row = i + 1
MSChart2.Data = cdf
Next i
End Sub
Practical No. 5
Continuous Distribution
Code:
Private Sub CommandButton1_Click()
Sheet7.Activate
End Sub
Private Sub CommandButton2_Click()
Sheet8.Activate
End Sub
Private Sub CommandButton3_Click()
Sheet6.Activate
End Sub
Private Sub CommandButton4_Click()
Sheet5.Activate
End Sub
Private Sub CommandButton5_Click()
Sheet4.Activate
End Sub
Private Sub CommandButton6_Click()
Sheet3.Activate
End Sub
Uniform Distribution:
Code:
Dim a As Integer
Dim b As Integer
'CDF
If i < a Then
Sheet7.Cells(i + 4, 3) = 0
End If
If i >= b Then
Sheet7.Cells(i + 4, 3) = 1
End If
Sheet7.Cells(i + 4, 1) = i
Next i
MSChart1.RowCount = 25
MSChart1.ColumnCount = 1
MSChart2.RowCount = 25
MSChart2.ColumnCount = 1
MSChart1.Column = 1
MSChart2.Column = 1
For i = 1 To 25
MSChart1.Row = i
MSChart1.Data = Sheet7.Cells(i + 4, 2)
MSChart2.Row = i
MSChart2.Data = Sheet7.Cells(i + 4, 3)
Next i
End Sub
Exponential Distribution:
Code:
For i = 1 To 40
'PDF
If i >= 0 Then
Sheet8.Cells(i + 4, 2) = Val(lamda) * Exp(-Val(lamda) * x)
Else
Sheet8.Cells(i + 4, 2) = 0
End If
'CDF
If i >= 0 Then
Sheet8.Cells(i + 4, 3) = 1 - Exp(-Val(lamda) * x)
Else
Sheet8.Cells(i + 4, 3) = 0
End If
Sheet8.Cells(i + 4, 1) = x
x = x + 0.1
Next i
MSChart1.ColumnCount = 1
MSChart1.RowCount = 40
'MSChart1
MSChart2.ColumnCount = 1
MSChart2.RowCount = 40
MSChart1.Column = 1
For i = 1 To 40
MSChart1.Row = i
MSChart1.Data = Sheet8.Cells(i + 4, 2)
Next i
MSChart2.Column = 1
For i = 1 To 40
MSChart2.Row = i
MSChart2.Data = Sheet8.Cells(i + 4, 3)
Next i
End Sub
Normal Distribution:
Code:
For i = 1 To 40
'PDF
Sheet6.Cells(i + 5, 2) = WorksheetFunction.NormDist(x, mean,
variance, False)
'CDF
Sheet6.Cells(i + 5, 3) = WorksheetFunction.NormDist(x, mean,
variance, True)
Sheet6.Cells(i + 5, 1) = Round(x, 1)
x = x + 0.1
z = (i - mean) / Sqr(variance)
Range("D" & i + 5).Value = z
Next i
MSChart1.ColumnCount = 1
MSChart1.RowCount = 40
'MSChart1
MSChart2.ColumnCount = 1
MSChart2.RowCount = 40
MSChart1.Column = 1
For i = 1 To 40
MSChart1.Row = Range("D" & i + 5).Value
MSChart1.Data = Sheet6.Cells(i + 5, 2)
Next i
MSChart2.Column = 1
For i = 1 To 40
MSChart2.Row = i
MSChart2.Data = Sheet6.Cells(i + 5, 3)
Next i
End Sub
Weibull Distribution:
Code:
a = InputBox("Enter alpha:")
b = InputBox("Enter beta:")
c = InputBox("Enter neue:")
For i = 1 To 20
'PDF
If i >= c Then
Sheet5.Cells(i + 5, 2) = b / a * ((i / a) ^ (b - 1)) * Exp(-(i / a) ^ b)
Sheet5.Cells(i + 5, 3) = 1 / a * (Exp(-(i / a)))
Else
Sheet5.Cells(i + 5, 2) = 0
Sheet5.Cells(i + 5, 3) = 0
End If
Sheet5.Cells(i + 5, 1) = i
Next i
MSChart1.RowCount = 40
MSChart1.ColumnCount = 1
MSChart1.Column = 1
MSChart2.RowCount = 40
MSChart2.ColumnCount = 1
MSChart2.Column = 1
For i = 1 To 40
MSChart1.Row = i
MSChart1.Data = Sheet5.Cells(i + 5, 2)
MSChart2.Row = i
MSChart2.Data = Sheet5.Cells(i + 5, 3)
Next i
End Sub
Triangular Distribution:
Code:
Dim a As Double
Dim b As Double
Dim c As Double
Private Sub CommandButton1_Click()
a = Val(InputBox("enter value of a"))
b = Val(InputBox("enter value of b"))
c = Val(InputBox("enter value of c"))
'pdf
For i = 0 To 20
If a <= i And i <= b Then
Sheet4.Cells(i + 4, 2) = 2 * (i - a) / ((b - a) * (c - a))
ElseIf b < i And i <= c Then
Sheet4.Cells(i + 4, 2) = 2 * (c - i) / ((c - b) * (c - a))
Else
Sheet4.Cells(i + 4, 2) = 0
End If
If i <= a Then
Sheet4.Cells(i + 4, 3) = 0
ElseIf a < i And i <= b Then
Sheet4.Cells(i + 4, 3) = ((i - a) ^ 2) / ((b - a) * (c - a))
MSChart1.RowCount = 20
MSChart1.ColumnCount = 1
MSChart1.Column = 1
For i = 4 To 20
MSChart1.Row = i - 2
MSChart1.RowLabel = Sheet4.Cells(i, 1)
MSChart1.Data = Sheet4.Cells(i, 2)
Next i
MSChart2.RowCount = 20
MSChart2.ColumnCount = 1
MSChart2.Column = 1
For i = 4 To 20
MSChart2.Row = i - 2
MSChart2.RowLabel = Sheet4.Cells(i, 1)
MSChart2.Data = Sheet4.Cells(i, 2)
Next i
End Sub
End Sub
Lognormal Distribution
Code:
Sheet3.Cells(i, 2) = 0
End If
Sheet3.Cells(i, 1) = x
Next i
MSChart1.RowCount = 20
MSChart1.ColumnCount = 1
MSChart1.Column = 1
For i = 4 To 24
MSChart1.Row = Sheet3.Cells(i, 1)
MSChart1.Data = Sheet3.Cells(i, 2)
Next i
End Sub
Roll No: 22
Practical No. 6
Measure of Performance
p0 = 1 / (sum1 + sum2)
Range("H17").Value = Round(p0, 3)
Range("H20").Value = Round(findPN(K), 3)
sum1 = 0
For i = 0 To K
sum1 = sum1 + i * findPN(i)
Next i
Range("H23").Value = Round(sum1, 3)
sum1 = 0
For i = c + 1 To K
sum1 = sum1 + (i - c) * findPN(i)
Next i
Range("H26").Value = Round(sum1, 3)
sum1 = 0
For i = 0 To K
sum1 = sum1 + (K - i) * lamda * findPN(i)
Next i
Range("H29").Value = Round(sum1, 3)
Range("H31").Value = Round(Range("H23").Value / Range("H29").Value, 3)
Range("H34").Value = Round(Range("H26").Value / Range("H29").Value, 3)
Range("H37").Value = Round(Range("H29").Value / (c * mue), 3)
Range("I15", "I20").ClearContents
End Sub
End Function
MG1ii :
Dim lamda, mue, sd, su, LQ, p0, WQ As Double
End Sub
MG1ii :
Dim lamda, mue, sd, su, LQ, p0, WQ, n As Double
Private Sub CommandButton1_Click()
lamda = Val(InputBox("Enter the Arrival rate (lamda):", "Parameters"))
mue = Val(InputBox("Enter the Service rate (mue):", "Parameters"))
n = Val(InputBox("Enter the number of customers:", "Parameters"))
Range("H7").Value = lamda
Range("H9").Value = mue
Range("H11").Value = 1 / mue
su = lamda / mue
Range("H13").Value = su
Range("H15").Value = su / (1 - su)
Range("H18").Value = su * su / (1 - su)
Range("H24").Value = 1 / (mue * (1 - su))
Range("H27").Value = su / (mue * (1 - su))
Range("E21").Value = "Probability of having " & n & " customers"
Range("I21").Formula = "=power(" & su & "," & n & ")"
Range("H21").Value = (1 - su) * Range("I21").Value
Range("I21").ClearContents
End Sub
MMcii :
Dim lamda, mue, sd, su, LQ, p0, WQ, n, c, pL, L As Double
Range("I15").ClearContents
Range("I16").ClearContents
LQ = (su * pL / (1 - su))
Range("H24").Value = LQ
L = c * su + LQ
Range("H21").Value = L
Range("H27").Value = L / lamda
Range("H30").Value = Range("H27").Value - (1 / mue)
End Sub
MGiii :
Dim lamda, mue, sd, su, LQ, p0, WQ, n, c, pL, L As Double
Private Sub CommandButton1_Click()
lamda = Val(InputBox("Enter the Arrival rate (lamda):", "Parameters"))
mue = Val(InputBox("Enter the Service rate (mue):", "Parameters"))
c = Val(InputBox("Enter the number of servers:", "Parameters"))
Range("H7").Value = lamda
Range("H9").Value = mue
Range("H11").Value = c
p0 = Exp(-lamda / mue)
Range("H15").Value = p0
Dim sum As Double
sum = 0
For i = 0 To c
Range("I15").Formula = "=power(" & lamda / mue & "," & i & ")"
Range("I16").Formula = "=fact(" & i & ")"
sum = sum + p0 * Range("I15").Value / Range("I16").Value
Next i
Range("H18").Value = sum
Range("I15").ClearContents
Range("I16").ClearContents
Range("H21").Value = lamda / mue
Range("H24").Value = 0
Range("H27").Value = 1 / mue
Range("H30").Value = 0
End Sub
MMcNi :
Dim lamda, mue, sd, su, LQ, p0, WQ, n, c, pL, L As Double
Private Sub CommandButton1_Click()
lamda = Val(InputBox("Enter the Arrival rate (lamda):", "Parameters"))
mue = Val(InputBox("Enter the Service rate (mue):", "Parameters"))
c = Val(InputBox("Enter the number of servers:", "Parameters"))
n = Val(InputBox("Enter the system capacity:", "Parameters"))
Range("H7").Value = lamda
Range("H9").Value = mue
Range("H11").Value = c
Range("H13").Value = n
Range("H15").Value = lamda / mue
su = lamda / (c * mue)
Range("H17").Value = su
For i = c + 1 To n
Range("I15").Formula = "=power(" & su & "," & i - c & ")"
sum2 = sum2 + Range("I15").Value
Next i
Range("I15").Formula = "=power(" & lamda / mue & "," & c & ")"
Range("I16").Formula = "=fact(" & c & ")"
p0 = 1 / (1 + sum1 + (Range("I15").Value / Range("I16").Value) * sum2)
Range("H19").Value = Round(p0, 3)
Range("I15").Formula = "=power(" & lamda / mue & "," & n & ")"
Range("I17").Formula = "=power(" & c & "," & n - c & ")"
Range("H22").Value = Round(Range("I15").Value * p0 / (Range("I16").Value *
(Range("I17").Value)), 3)
Range("I15").Formula = "=power(" & lamda / mue & "," & c & ")"
'Range("I16").Formula = "=fact(" & c & ")"
Range("I17").Formula = "=power(" & su & "," & n - c & ")"
LQ = Round(p0 * Range("I15").Value * su * (1 - Range("I17").Value - (n - c) *
Range("I17").Value * (1 - su)) / (Range("I16").Value * (1 - su) * (1 - su)), 3)
Range("H25").Value = LQ
Range("I15").ClearContents
Range("I16").ClearContents
Range("I17").ClearContents
Range("H28").Value = Round(lamda * (1 - Range("H22").Value), 3)
Range("H30").Value = Round(LQ / Range("H28").Value, 3)
Range("H33").Value = Round(Range("H30").Value + 1 / mue, 3)
Range("H36").Value = Round(Range("H33").Value * Range("H28").Value, 3)
End Sub
MMcKK :
Dim a, lamda, mue, sd, su, LQ, p0, WQ, c, pL, L, pn As Double
Dim i As Integer
Dim K As Integer
p0 = 1 / (sum1 + sum2)
Range("H17").Value = Round(p0, 3)
Range("H20").Value = Round(findPN(K), 3)
sum1 = 0
For i = 0 To K
sum1 = sum1 + i * findPN(i)
Next i
Range("H23").Value = Round(sum1, 3)
sum1 = 0
For i = c + 1 To K
sum1 = sum1 + (i - c) * findPN(i)
Next i
Range("H26").Value = Round(sum1, 3)
sum1 = 0
For i = 0 To K
sum1 = sum1 + (K - i) * lamda * findPN(i)
Next i
Range("H29").Value = Round(sum1, 3)
Range("H31").Value = Round(Range("H23").Value / Range("H29").Value, 3)
Range("H34").Value = Round(Range("H26").Value / Range("H29").Value, 3)
Range("H37").Value = Round(Range("H29").Value / (c * mue), 3)
Range("I15", "I20").ClearContents
End Sub
End Function
Roll No: 22
Practical No. 7
Using the examples / data taken in your theory session find the sequence of n
digited random number using
Code :
Main Program :
Chart:
Code :
Linear :
i=o
newRandom:
If i = 0 Then
x0 = (Range("e7").Value * Range("e5").Value + Range("e9").Value) Mod
Range("e11").Value
Else
x0 = (Range("e7").Value * Range("h" & i + 4).Value + Range("e9").Value)
Mod Range("e11").Value
End If
For h = 0 To i
If Linear.Cells(h + 5, 8) = x0 Then
MsgBox "Numbers repeated"
GoTo printChart
End If
Next h
Linear.Cells(i + 5, 8) = x0
i=i+1
GoTo newRandom
printChart:
MSChart1.ColumnCount = i
MSChart1.RowCount = 1
MSChart1.RowLabel = "Linear Congruential Method"
For h = 1 To i
MSChart1.Column = h
MSChart1.Data = Linear.Cells(h + 4, 8)
Next h
End Sub
Chart :
Multiplicative Congruential Method :
Program :
i=o
newRandom:
If i = 0 Then
x0 = (Range("e8").Value * Range("e6").Value) Mod Range("e10").Value
Else
x0 = (Range("e8").Value * Range("h" & i + 5).Value) Mod
Range("e10").Value
End If
For h = 0 To i
If multiplicative.Cells(h + 6, 8) = x0 Then
MsgBox "Numbers repeated"
GoTo printChart
End If
Next h
multiplicative.Cells(i + 6, 8) = x0
i=i+1
GoTo newRandom
printChart:
MSChart1.ColumnCount = i
MSChart1.RowCount = 1
MSChart1.RowLabel = "Multiplicative Congruential Method"
For h = 1 To i
MSChart1.Column = h
MSChart1.Data = multiplicative.Cells(h + 5, 8)
Next h
End Sub
Private Sub cmdClear_Click()
Range("e5", "e11").ClearContents
Range("h5", "h11").ClearContents
End Sub
Program :
End Sub
MSChart1.ColumnCount = rownum + 1
MSChart1.RowCount = 1
Range("d14").Value = actualNum
MidSquare.Cells(rownum + 6, 7) = actualNum
MSChart1.Column = rownum + 1
rownum = rownum + 1
MSChart1.Data = actualNum
End Sub
Public Function findDigits(num As Long) As Integer
findDigits = 0
While num > 0
num = num / 10
findDigits = findDigits + 1
Wend
End Function
Chart:
Code:
Private Sub cmdCal_Click()
Dim x10, x20, x30, a1, a2, a3, m1, m2, m3 As Double
Dim i, k, j As Integer
MSChart1.RowCount = Range("d3").Value
MSChart1.ColumnCount = 1
For j = 0 To Range("b3").Value
combined.Cells(13, j + 1) = combined.Cells(5, j + 1)
Next j
For j = 1 To Range("d3").Value
combined.Cells(13 + j, 1) = j
Next j
For j = 0 To Range("d3").Value – 1
Sum = 0
For k = 1 To Range("b3").Value
Cells(14 + j, k + 1).Value = Cells(6, 1 + k) * Cells(14 + j - 1, k + 1) Mod
Cells(7, 1 + k)
Sum = Sum + power(k - 1) * Cells(14 + j, k + 1).Value
Next k
MSChart1.Row = j + 1
MSChart1.Column = 1
MSChart1.RowLabel = "R" & j + 1
MSChart1.Data = Range("F" & 14 + j).Value
Next j
End Sub
Range("b5", "b7").ClearContents
Range("c5", "c7").ClearContents
Range("d5", "d7").ClearContents
End Sub
power = 1
For i = 1 To pow
power = power * (-1)
Next i
End Function
Roll No: 22
Practical No. 8
minVal = 0
maxVal = 0.1
For j = 4 To 13
count = 0
For i = 4 To 53
If Sheet1.Cells(i, 1) >= minVal And Sheet1.Cells(i, 1) < maxVal Then
count = count + 1
End If
Next i
Sheet1.Cells(j, 3) = count
minVal = minVal + 0.1
maxVal = maxVal + 0.1
Next j
count1 = 0
i=4
Do Until Sheet1.Cells(i, 1) = ""
count1 = count1 + 1
i=i+1
Loop
N = count1
count2 = 0
k=4
Do Until Sheet1.Cells(k, 2) = ""
count2 = count2 + 1
k=k+1
Loop
n1 = count2
For l = 4 To 13
Sheet1.Cells(l, 4) = N / n1
Next l
For m = 4 To 13
Sheet1.Cells(m, 5) = (Sheet1.Cells(m, 3) - Sheet1.Cells(m, 4)) *
(Sheet1.Cells(m, 3) - Sheet1.Cells(m, 4))
Next m
Sheet1.Cells(14, 6) = 0
For y = 4 To 13
Sheet1.Cells(y, 6) = Sheet1.Cells(y, 5) / Sheet1.Cells(y, 4)
Sheet1.Cells(14, 6) = Sheet1.Cells(14, 6) + Sheet1.Cells(y, 6)
Next y
a = Sheet1.Cells(14, 6)
z = Val(InputBox("Enter the table value for chi-square "))
If a < z Then
MsgBox ("Z is Accepted")
Else
MsgBox ("Z is Rejected")
End If
End Sub
End Sub
Kolmogorov- Smirnov Test :
Data:
Code:
Kolmogorov-Smirnov Test :
Program :
For k = 1 To j
Kolmogorov.Cells(k + 6, 5) = Val(Range("B" & k + 6).Value / j)
Next k
For k = 1 To j
Kolmogorov.Cells(k + 6, 6) = Val((Range("B" & k + 6).Value - 1) / j)
Next k
For k = 1 To j
Kolmogorov.Cells(k + 6, 7) = Kolmogorov.Cells(k + 6, 5) - Kolmogorov.Cells(k +
6, 4)
Next k
For k = 1 To j
Kolmogorov.Cells(k + 6, 8) = Kolmogorov.Cells(k + 6, 4) - Kolmogorov.Cells(k +
6, 6)
Next k
For k = 1 To j
If Range("G" & k + 6).Value > max Then
max = Range("G" & k + 6).Value
End If
Next k
Kolmogorov.Cells(13, 7) = max
max = 0
For k = 1 To j
If Range("H" & k + 6).Value > max Then
max = Range("H" & k + 6).Value
End If
Next k
Kolmogorov.Cells(14, 7) = max
Runs Test
Runs Up And Runs Down -:
Data:
Program :
Dim i As Integer
Count = 1
For i = 4 To 42
If RunUpDown.Cells(i, 2) = RunUpDown.Cells(i + 1, 2) Then
Count = Count
Else: Count = Count + 1
End If
Next i
RunUpDown.Cells(5, 5) = Count
End Sub
Program :
Program :
'Option Explicit
Dim l As Integer
Dim M As Integer
Dim sum As Double
AutoCo.Cells(3, 4) = Val(n)
i = Val(InputBox("Enter the starting no.i="))
AutoCo.Cells(5, 4) = Val(i)
l = Val(InputBox("Enter the lag l="))
AutoCo.Cells(7, 4) = Val(l)
sum = 0
For k = 0 To Val(M)
sum = sum + AutoCo.Cells(Val(i) + k * Val(l), 1) * AutoCo.Cells(Val(i) + (k + 1)
* Val(l), 1)
Next k
r = ((1 / (Val(M) + 1) * sum) - 0.25)
AutoCo.Cells(11, 4) = Val(r)
Zo = Val(r) / Val(s)
AutoCo.Cells(15, 4) = Val(Zo)
End Sub
Practical No. 9
Code:
Private Sub CommandButton1_Click()
Sheet2.Activate
End Sub
Private Sub CommandButton2_Click()
Sheet3.Activate
End Sub
Private Sub CommandButton3_Click()
Sheet4.Activate
End Sub
Private Sub CommandButton4_Click()
Sheet5.Activate
End Sub
For i = 1 To no
Range("D" & i + 13).Formula = "=round(rand(),5)"
Range("C" & i + 13).Value = i
m = Range("D" & i + 13).Value
x = -((1 / mean) * (Math.Log(1 - m)))
Cells(13 + i, 5) = x
Next i
End Sub
Weibull Distribution:
Code:
Dim alpha, beta, no, i As Integer
Dim m, x As Double
m=0
For i = 1 To no
Range("D" & i + 12).Formula = "=round(rand(),5)"
Range("C" & i + 12).Value = i
m = Range("D" & i + 12).Value
Cells(12 + i, 4) = m
x = alpha * (-(Math.Log(1 - m))) ^ (1 / beta)
Cells(12 + i, 5) = x
Next i
End Sub
Triangular Distribution:
Code:
Dim no, i, j As Integer
Dim r, x, y As Double
For i = 1 To no
Range("D" & i + 12).Formula = "=round(rand(),5)"
Range("C" & i + 12).Value = i
r = Range("D" & i + 12).Value
If (0 <= r And r <= 0.5) Then
x = Math.Sqr(2 * r)
End If
If (0.5 <= r And r <= 1) Then
x = 2 - (Math.Sqr(2 * (1 - r)))
End If
If (r > 1) Then
x=0
MsgBox ("Invalid")
End If
Cells(12 + i, 5) = x
Next i
End Sub
Empirical Continuous:
Code:
Dim f(20) As Double
Dim p(20) As Double
Dim c(20) As Double
Dim c_i(20) As Double
Dim a(20) As Double
nci = Val(TextBox1.Text)
u = Val(TextBox3.Text)
l = Val(TextBox2.Text)
r = Val(TextBox4.Text)
'add freq.
For i = 0 To nci - 1
f(i) = InputBox("Enter " & i & " th frequency ")
Sheet6.Cells(i + 6, 8) = f(i)
'find total freq.
s = s + f(i)
Next
Sheet6.Cells(nci + 7, 8) = s
'Prob p(i)
For i = 0 To nci - 1
p(i) = f(i) / s
Sheet6.Cells(i + 6, 9) = p(i)
Next
c_i(0) = l
Count = (u - l) / nci
For i = 0 To nci - 1
c_i(i + 1) = c_i(i) + Count
Sheet6.Cells(i + 6, 6) = c_i(i)
Sheet6.Cells(i + 6, 7) = c_i(i + 1)
Next
For i = 1 To nci - 1
a(i) = (c_i(i + 1) - c_i(i)) / (c(i) - c(i - 1))
Next
For i = 0 To nci - 1
Sheet6.Cells(i + 6, 11) = a(i)
Next
For i = 1 To nci - 1
If (r > c_i(i - 1) & r <= c_i(i)) Then
k = c_i(i - 1) + (a(i) * (r - c(i - 1)))
End If
Next
Sheet6.Cells(13, 4) = k
End Sub
Private Sub CommandButton2_Click()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
Sheet6.Cells(13, 4) = ""
i=6
While (Sheet6.Cells(i, 6) <> "")
Sheet6.Cells(i, 6) = ""
Sheet6.Cells(i, 7) = ""
Sheet6.Cells(i, 8) = ""
Sheet6.Cells(i, 9) = ""
Sheet6.Cells(i, 10) = ""
Sheet6.Cells(i, 11) = ""
i=i+1
Wend
Sheet6.Cells(i + 1, 8) = ""
Sheet1.Activate
End Sub
Roll No: 22
Practical No. 10
Code:
Sheet2.Cells(i + 6, 5) = n1
r(i) = Rnd()
Sheet2.Cells(i + 6, 6) = r(i)
oldp = p
p = p * r(i)
Sheet2.Cells(i + 6, 7) = p
Sheet2.Cells(i + 6, 8) = e
If p < e Then
Sheet2.Cells(i + 6, 9) = "Accept"
Sheet2.Cells(i + 6, 10) = "N = " & n1
p = oldp
Else
Sheet2.Cells(i + 6, 9) = "Reject"
Sheet2.Cells(i + 6, 10) = ""
p = oldp * r(i)
n1 = n1 + 1
End If
Next
End Sub
i=6
While (Sheet2.Cells(i, 5) <> "")
Sheet2.Cells(i, 5) = ""
Sheet2.Cells(i, 6) = ""
Sheet2.Cells(i, 7) = ""
Sheet2.Cells(i, 8) = ""
Sheet2.Cells(i, 9) = ""
Sheet2.Cells(i, 10) = ""
i=i+1
Wend
Sheet1.Activate
End Sub
Gamma Distribution:
Code:
For i = 0 To N - 1
Sheet3.Cells(i + 6, 6) = a
Sheet3.Cells(i + 6, 7) = b
r1 = Rnd()
r2 = Rnd()
Sheet3.Cells(i + 6, 8) = r1
Sheet3.Cells(i + 6, 9) = r2
v = r1 / (1 - r1)
x = bt * (v ^ a)
Sheet3.Cells(i + 6, 10) = x
c = b + ((bt * a) + 1) * (WorksheetFunction.Ln(v) -
WorksheetFunction.Ln(r1 ^ 2 * r2))
Sheet3.Cells(i + 6, 11) = c
If x > c Then
Sheet3.Cells(i + 6, 12) = "Reject"
Else
Sheet3.Cells(i + 6, 12) = "Accept"
End If
x = x / (bt * tht)
Next
End Sub
i=6
While (Sheet3.Cells(i, 6) <> "")
Sheet3.Cells(i, 6) = ""
Sheet3.Cells(i, 7) = ""
Sheet3.Cells(i, 8) = ""
Sheet3.Cells(i, 9) = ""
Sheet3.Cells(i, 10) = ""
Sheet3.Cells(i, 11) = ""
Sheet3.Cells(i, 12) = ""
i=i+1
Wend
Sheet1.Activate
End Sub