Thursday 19 July 2018

Write a program in VVM that will convert the BCD to Hex for example 111 = 7

in          //BCD TO HEX BY AAMIR JAMIL
sto 01 
brz 04  // if 0
br 07
lda 01 // load 0 input
out
hlt
lda 80 
sub 01
brz 11  //if 1
br 14
lda 87   //load 1
out
hlt
lda 81
sub 01
brz 18  // if 2
br  21
lda 88  //load 2
out
hlt
lda 82
sub 01
brz 25  // if 3
br 28
lda 89  // load 3
out
hlt
lda 83
sub 01
brz 32  //if 4
br 35
lda 90  //load 4
out
hlt
lda 84  //load 5
sub 01
brz 39  //if 5
br 42
lda 91  //load 5
out
hlt
lda 85 //load 6 = 110
sub 01
brz 46  //if 6
br 49
lda 92  // load 6
out
hlt
lda 86  // load 7 = 111
sub 01
brz 53 // if 7
br 57
lda 93 // load 7
out
hlt
hlt
*80
dat 001  //80    1
dat 010  //81    2
dat 011  //82    3
dat 100  //83    4
dat 101  //84    5
dat 110  //85    6
dat 111  //86    7
dat 001 //87
dat 002 //88
dat 003 //89
dat 004 //90
dat 005 //91
dat 006 //92
dat 007 //93

Friday 15 December 2017

VVM CREATE A PROGRAM FOR DIVISION WITH APPROXIMATE PART

Q)
Write a program in VVM Assembler which will divide two numbers, i.e. divide number A by number B. If B>A or B=0 or B is negative number, then the program should prompt for another input which is less than A (B<A). If the result involves decimal output then the program should take care of the decimal part to produce the approximate correct results.

in //input for a sto 90 //store a at 90 sto 94 //store a also at 94 in //input for b sto 91 //store b at 91 lda 91 //load b (91) brz 08 //if b = 0 read go for new input brp 09 //if b is negative go for new input jmp 03 //jump for b new input lda 90 //load a brz 70 //if a=0 then show directly zero lda 90 //load value 90 sub 91 //a-b brp 15 //if b>a go for new input jmp 03 //jump for b<a lda 90 //lda 90 brz 70 //branch if zero jump 70 sub 91 //subtract 90-91 sto 90 //store subtracted value at 90 brp 60 //branch if postive or zero jump 60 lda 94 //code fo approx : load 94 sub 95 //94-95 sto 95 //store subtracted value at 95 lda 96 //load 96 add 97 //add 96+97 sto 96 //store added value to 96 lda 95 //load 95 sub 93 //subtract 95-93 sto 95 //store at 95 brz 31 //branch if 95 is zero then jump 31 jmp 23 //jump 23 jmp 32 //:31 line no:jump 32 *32 //32 line no start lda 96 //load 96 brz 37 //branch if zero jump 37 line sub 91 //sub 96-91 sto 96 //store subtracted value at 96 brp 42 //branch if postive or zero then jump 42 line lda 98 //load 98 sub 99 //sub 98-99 brp 80 //branch if positive or zero jump 80 line br 70 //jump 70 *42 //42 line no starts lda 98 //load 98 add 93 //add 98+93 sto 98 //store at 98 jmp 32 //jump 32 *60 //60 line no starts lda 92 //lda 92 add 93 //add 93 sto 92 //sto added value 92 jmp 73 //jmp 73 *70 //70 line no starts lda 92 //load 92 out //output 92 hlt //exit or terminated program *73 //line 73 starts lda 91 //load 91 add 95 //add 91+95 sto 95 //store added value at 95 jmp 15 //jump 15 *80 //line no 80 starts lda 92 //load 92 add 93 //add 92+93 out //output added value hlt //exit program *90 //90 line no starts dat 000 //hardcoded value at 90(0) dat 000 //hardcoded value at 90(0) dat 000 //hardcoded value at 90(0) dat 001 //hardcoded value at 90(1) dat 000 //hardcoded value at 90(0) dat 000 //hardcoded value at 90(0) dat 000 //hardcoded value at 90(0) dat 010 //hardcoded value at 90(10) dat 000 //hardcoded value at 90(0) dat 005 //hardcoded value at 90(5)

Saturday 2 December 2017

Write a program in VVM Assembler which will divide two numbers, i.e. divide number A by number B

write a program in vvm which will divide two numbers i.e divide number A by number B if B>A or B=0 or B is negative then the program should prompt for another input for B<A, if A=0 then result should be zero and if a>b it should show result.
in //input for a sto 90 //store a at 90 in //input for b sto 91 //store b at 91 lda 91 // load b (91) brz 07 // if b = 0 read go for new input brp 08 //if b is negative go for new input jmp 02 // jump for b new input lda 90 // load a brz 30 //if a=0 then show directly zero lda 90 // load 90 value sub 91 // a-b brp 14 //if b>a go for new input jmp 02 //jump for b<a lda 90 //load 90 brz 30 //if 90 value = 0 then jump 30 sub 91 //subtract values 90-91 sto 90 //store at 90 position brp 20 //if 90 value postive or zero then jump 20 jmp 30 //jump to line no 30 *20 //line 20 start lda 92 //load 92 add 93 //add 92+93 sto 92 //store the added value at 92 location jmp 14 //jump 14 line no *30 //line 30 starts lda 92 //load 92 out //output result hlt //terminated program *90 //90 line number start
dat 000  //90 location there is 0 hardcoded
dat 000  //91 location there is 0 hardcoded
dat 000  //92 location there is 0 hardcoded
dat 001  //93 location there is 1 hardcoded

Friday 29 July 2016

Method Overriding & polymorphism

Method Overriding & polymorphism by Aamir jamil

OBJECTIVE

Study Method Overriding, Abstract class and Interface.

THEORY


Method Overriding  By aamirjamil 
In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass. The version of the method defined by the superclass will be hidden. Consider the following:

// Method overriding by aamir jamil
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
// display i and j
void show() {
System.out.println("i and j: " + i + " " + j);
}
}
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
// display k – this overrides show() in A
void show() {
System.out.println("k: " + k);
}
}
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B
}
}
Output:








When show( ) is invoked on an object of type B, the version of show( ) defined within B is used. That is, the version of show( ) inside B overrides the version declared in A.

If you wish to access the superclass version of an overridden function, you can do
so by using super. For example, in this version of B, the superclass version of show( ) is invoked within the subclass’ version. This allows all instance variables to be displayed.


class B extends A {  
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
void show() {
super.show(); // this calls A's show()
System.out.println("k: " + k);
}
}
If you substitute this version of A into the previous program, you will see the following output:

 







Polymorphism By aamirjamil
Polymorphism describes a language’s ability to process objects of various types and classes through a single, uniform interface.
Polymorphism in Java has two types: Compile time polymorphism (static binding) and Runtime polymorphism (dynamic binding). Method overloading is an example of static polymorphism, while method overriding is an example of dynamic polymorphism.
Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.

Using Abstract Classes  By aamirjamil
There are situations in which you will want to define a superclass that declares the structure of a given abstraction without providing a complete implementation of every
method. That is, sometimes you will want to create a superclass that only defines a generalized form that will be shared by all of its subclasses, leaving it to each subclass
to fill in the details. Such a class determines the nature of the methods that the subclasses must implement.

  1. An abstract method will force to make the class abstract.
  2. An abstract method cannot have a body. Only signature is define with ";".
  3. Abstract class can contain both abstract and non abstract methods.
  4. To use the non abstract methods of abstract class we must make a child class.
  5. The child class must provide the implementation of the parent abstract methods by overriding
  6. If a child cannot provide implementation of parent abstract methods the abstract methods will force to make the child class abstract. In that case we cannot make Object of child class
  7. Abstract class can be used as reference class but not as Object class.
  8. After providing implementations of all abstract methods in the hierarchy we can create Objects

// By aamirjamil
abstract class A
{
abstract void add()
{ //Abstract method with body will generate error 
                        System.out.println("Add of A");
            }

            abstract void add();
            abstract void sub();
            void mull()
          {
                        System.out.println("Multiply of A");
           }
            void div()
          {
                        System.out.println("Divide of A\n");
            }
}

// Class B is abstract because of not providing the implementation of abstract void sub() of A.

abstract class B extends A
{
void add()
{ //Class B providing implementation of abstract void add() of class A
                        System.out.println("Add of B");
            }
            void div()
          {
                        System.out.println("Divide of B\n");
             }
}

// Class C providing the implementation of abstract void sub() of A and overriding multiply of A.
class C extends B
{
            void sub()
          {
                        System.out.println("Subtract of C");
            }
            void mull()
          {
                        System.out.println("Multiply of C");
            }

}

public class Abstract2
{
      public static void main (String[] args)
      {

            //We can only make Object of class C
            A obj1 = new C(); //Reference class A
            B obj2 = new C(); //Reference class B
            C obj3 = new C();

            obj1.add();      //Add of B
            obj1.sub(); //Subtract of C
            obj1.mull(); //Multiply of C
            obj1.div(); //Divide of B

            obj2.add();      //Add of B
            obj2.sub(); //Subtract of C
            obj2.mull(); //Multiply of C
            obj2.div(); //Divide of B

            obj3.add();      //Add of B
            obj3.sub(); //Subtract of C
            obj3.mull(); //Multiply of C
            obj3.div(); //Divide of B
      }
}
OUTPUT

 











Interfaces
Using the keyword interface, you can fully abstract a class’ interface from its implementation. That is, using interface, you can specify what a class must do, but not how it does it. Interfaces are syntactically similar to classes, but they lack instance variables, and their methods are declared without any body. In practice, this means that you can define interfaces which don’t make assumptions about how they are implemented. Once it is defined, any number of classes can implement an interface. Also, one class can implement any number of interfaces.

To implement an interface, a class must create the complete set of methods defined by the interface. However, each class is free to determine the details of its own implementation. By providing the interface keyword, Java allows you to fully utilize the “one interface, multiple methods” aspect of polymorphism.

Program 1: Interface1
                                                            Interface X
                                                                                    (Abstract add and sub)
             

                                                Class A Implements X
                                                                                    (Implements add and sub)


//// By aamirjamil
//Methods of Interface are by default abstract and  public 
//We can only define their signatures

interface X{
            void add();
            void sub();
}

//A child class implements (not extends) X interface
//A child class will provide implementation of both abstract methods
//Methods overriding will be used and access control must be public

class A implements X{
            public void add(){
                        System.out.println("Add of A");
            }
            public void sub(){
                        System.out.println("Subtract of A");
            }
}

public class Interface1{
            public static void main (String[] args){
                        A obj1 = new A();
                        X obj2 = new A(); //Interface reference can be used to perform //polymorphism
                        System.out.println("\nAdd and Subtract of A\n");
                        obj1.add ();
                        obj1.sub ();
                        System.out.println("\add and Subtract  of A on Interface reference\n");
                        obj2.add ();
                        obj2.sub ();
            }

}

Study inheritance and using super to call superclass constructors.

Inheritance by Aamir jamil

OBJECTIVE

Study inheritance and using super to call superclass constructors.

 

THEORY

Inheritance is one of the cornerstones of object-oriented programming because it 

allows the creation of hierarchical classifications. using inheritance, you can create a
general class that defines traits common to a set of related items. this class can then
be inherited by other, more specific classes, each adding those things that are unique to it. in the terminology of java, a class that is inherited is called a superclass. the class that does the inheriting is called a subclass. therefore, a subclass is a specialized version of a superclass. it inherits all of the instance variables and methods defined by the superclass and adds its own, unique elements.

Inheritance Basics
To inherit a class, you simply incorporate the definition of one class into another by using the extends keyword. To see how, let’s begin with a short example. The following program creates a superclass called A and a subclass called B. Notice how the keyword extends is used to create a subclass of A.

// A simple example of inheritance by aamir jamil

// Create a superclass.
class A
{
int i, j;
void showij()
{
                  System.out.println("i and j: " + i + " " + j);
            }
}

// Create a subclass by extending class A.
class B extends A
{
int k;
void showk()
{
       System.out.println("k: " + k);
}
void sum()
{
       System.out.println("i+j+k: " + (i+j+k));
}
}

class SimpleInheritance
{
public static void main(String args[])
{
          A superOb = new A();
                      B subOb = new B();
 // The superclass may be used by itself.
                      superOb.i = 10;
                      superOb.j = 20;
                      System.out.println("Contents of superOb: ");
                      superOb.showij();
                      System.out.println();
/* The subclass has access to all public members of
its superclass. */
                      subOb.i = 7;
                      subOb.j = 8;
                      subOb.k = 9;
                      System.out.println("Contents of subOb: ");
                      subOb.showij();
                      subOb.showk();
                      System.out.println();
                      System.out.println("Sum of i, j and k in subOb:");
                      subOb.sum();
            }
}

Output:















Using super

super has two general forms. The first calls the superclass’ constructor. The second
is used to access a member of the superclass that has been hidden by a member of a
subclass.

Using super to Call Superclass Constructors

A subclass can call a constructor method defined by its superclass by use of the
following form of super:

super(parameter-list);

Here, parameter-list specifies any parameters needed by the constructor in the superclass. super( ) must always be the first statement executed inside a subclass’ constructor.

// by aamir jamil

class Box {
private double width;
private double height;
private double depth;
// construct clone of an object
Box(Box ob) { // pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}

// constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}

// compute and return volume
double volume() {
return width * height * depth;
}
}


// BoxWeight now fully implements all constructors.
class BoxWeight extends Box {
double weight; // weight of box
// construct clone of an object
BoxWeight(BoxWeight ob) { // pass object to constructor
super(ob);
weight = ob.weight;
}
// constructor when all parameters are specified
BoxWeight(double w, double h, double d, double m) {
super(w, h, d); // call superclass constructor
weight = m;
}
}

class DemoSuper {
public static void main(String args[]) {
BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);
BoxWeight myclone = new BoxWeight(mybox1);
double vol;

vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
System.out.println("Weight of mybox1 is " + mybox1.weight);
System.out.println();

vol = myclone.volume();
System.out.println("Volume of myclone is " + vol);
System.out.println("Weight of myclone is " + myclone.weight);
System.out.println();
}
}

Output: