Friday, April 27, 2012

Abstract Classes and Methods

Example of Abstract class in java :
Abstract Classes : Shape , Two Dim , Three Dim
Concrete Classes : Circle , square , triangle , sphere
Download file

Inheritance

Example of Inheritance in java.
Super Class --> Person
Subclass --> Student , Employee
Download File from this link.

Wednesday, April 11, 2012

Matrix Operations in java


MATRIX Class:



package matrix;
import java.util.Scanner;

public class Matrix {
        private int ROWS;
        private int COLOUM;
        private int totalMatrix;
        public double mat[][][];
        Scanner input = new Scanner(System.in);
        public Matrix(int rows,int coloum,int matNo){
                ROWS = rows;
                COLOUM = coloum;
                totalMatrix = matNo;
                mat = new double[totalMatrix+1][ROWS][COLOUM];
        }
        public void add(int oprnd1,int oprnd2){
                for(int i = 0;i<ROWS;i++)
                        for(int j=0 ; j<COLOUM;j++)
                                mat[totalMatrix][i][j] = mat[oprnd1-1][i][j] + mat[oprnd2-1][i][j];
        }
        public void subtract(int oprnd1,int oprnd2){
                for(int i = 0;i<ROWS;i++)
                        for(int j=0 ; j<COLOUM;j++)
                                mat[totalMatrix][i][j] = mat[oprnd1-1][i][j] - mat[oprnd2-1][i][j];
        }
        public void multiplication(int oprnd1,int oprnd2){
                if(ROWS == COLOUM)
                for(int i = 0;i<ROWS;i++)
                        for(int j=0 ; j<COLOUM;j++)
                                for(int k = 0;k<ROWS;k++ )
                                        mat[totalMatrix][i][j] += (mat[oprnd1-1][i][k]*mat[oprnd2-1][k][j]);
        }
        public void transpose(int matNo){
                double matRes[][] = new double[ROWS][COLOUM];
                for(int j = 0;j<ROWS;j++)
                        for(int i=0 ; i<COLOUM ; i++)
                                matRes[i][j]=mat[matNo-1][i][j];
                for(int j = 0;j<ROWS;j++)
                        for(int i=0 ; i<COLOUM ; i++)
                                mat[matNo-1][i][j]=matRes[j][i];
        }
        public void setMat(int matNo){
                for(int i = 0;i<ROWS;i++)
                        for(int j=0 ; j<COLOUM;j++)
                        {
                                System.out.printf("Enter number for Matrix %d [%d][%d] : ",matNo,i+1,j+1);
                                mat[matNo-1][i][j] = input.nextDouble();
                        }
        }
        public void display(int matNo){
                for(int i = 0;i<ROWS;i++)
                {
                        for(int j=0 ; j<COLOUM;j++)
                                System.out.print(mat[matNo-1][i][j]+"\t");
                        System.out.println();
                }
        }
}


MATRIX Test Class:

package matrix;
import java.util.Scanner;

public class MatrixTest {

        public static void main(String[] args) {
                Scanner input = new Scanner(System.in);
                System.out.print("Enter [Rows][Coloum] : ");
                int rows = input.nextInt();
                int coloum = input.nextInt();
                System.out.print("Enter count of MATRICES to create : ");
                int matCount = input.nextInt();
                Matrix matrix = new Matrix(rows,coloum,matCount);
                while(true)
                {
                        System.out.print("1. Setting Matrices\n"
                                +"2. Operations\n"
                                +"3. Display\n"
                                +"4. Exit\n"
                                +"Enter your Choice : ");
                        switch (input.nextInt())
                        {
                                case 1:
                                        System.out.print("Enter Matrix number to set : ");
                                        int matNo = input.nextInt();
                                        matrix.setMat(matNo);
                                        break;
                                case 2:
                                        System.out.print("1. Addition\n"
                                                +"2. Subtraction\n"
                                                +"3. Multiplication\n"
                                                +"4. Transpose\n"
                                                +"Enter operation : ");
                                                int choice = input.nextInt();
                                               
                                                System.out.print("Enter matrices"
                                                        + " for operation : ");
                                                int mat1 = input.nextInt();
                                                int mat2 = input.nextInt();
                                        switch (choice)
                                        {
                                                case 1:
                                                        matrix.add(mat1, mat2);
                                                        matrix.display(matCount+1);
                                                        break;
                                                case 2:
                                                        matrix.subtract(mat1, mat2);
                                                        matrix.display(matCount+1);
                                                        break;
                                                case 3:
                                                        matrix.multiplication(mat1, mat2);
                                                        matrix.display(matCount+1);
                                                        break;
                                                case 4:
                                                        matrix.transpose(mat1);
                                                        matrix.display(mat1);
                                                        break;
                                        }  
                                        break;
                                case 3:
                                        System.out.print("Enter Matrix number to display : ");
                                        matrix.display(input.nextInt());
                                        break;
                                case 4:
                                        System.exit(0);
                        }
                }
        }
}


follow for appreciation.

Tuesday, April 3, 2012

OOP SALES Lab Task 3 - april - 12




< Sales.java >


public class Sales {
        public int PERSON = 4;
        public int ITEM = 6;
        private int matrix[][] = new int[PERSON+1][ITEM+1];
        public Sales(){
                for(int person = 0;person<this.PERSON;person++)
                matrix[person][0]= person+1;
               
        }/*
        public void personsale(){
                for(int person =0;person<this.PERSON;person++)
                        for(int item = 1;item<this.ITEM;item++)
                                matrix[person][item] = 10;
        }*/
        public void PersonSale(int person ,int item ,int ammount){
                matrix[person][item] = ammount;
        }
        public void salesPersonTotal(){
                for(int person =0 ; person<4 ; person++)

                        for(int item = 1; item < 6 ; item++)
                        {
                                matrix[person][6] = matrix[person][6]+matrix[person][item];
                        }
        }
        public void salesItemTotal(){
                for(int i=1;i<=this.ITEM;i++)
                for(int j=0;j<=this.PERSON-1;j++)
                matrix[4][i]=matrix[4][i]+matrix[j][i];
        }
       
        public void display(){
               
                this.salesPersonTotal();
                this.salesItemTotal();
                System.out.println("PERSON\tI1\tI2\tI3\tI4\tI5\ttotal");
                for(int person = 0; person<5 ; person++ )
                {
                        for(int row = 0;row<7;row++){
                                System.out.printf("%d\t",matrix[person][row]);
                        }
                        System.out.println();
                }
        }
}



< SalesTest.java >


import java.util.Scanner;

public class SalesTest {

        public static void main(String[] args) {
                Scanner input = new Scanner(System.in);
                Sales sales = new Sales();
                while(true)
                {
                        System.out.println("Enter person ID (1-4):");
                        int person = input.nextInt();
                        if(person<=0||person>sales.PERSON)
                                break;
                        System.out.println("Enter Item ID (1-5):");
                        int item = input.nextInt();
                        if(item<=0||item>sales.ITEM)
                                break;
                        System.out.println("Enter item ammount :");
                        int ammount = input.nextInt();
                       
                        sales.PersonSale(person-1, item, ammount);
                }
                sales.display();
        }
}






< OUTPUT >


PRSN I1 I2 I3 I4 I5 total
1 12 15 0 0 0 27
2 0 15 0 0 0 15
3 0 0 0 0 0 0
4 0 0 0 0 0 0
0 12 30 0 0 0 42