Search This Blog

Sunday, October 21, 2012

Functions and Arrays for computation of result - JAVA & BLUE J PROGRAM

/*
 * Computer Science Project on result
Class : Result
Member Variables:
rollno, Name, ,avg  ,grade, array of marks in 5 subjects
Functions used :
Default constructor
Parameterized constructor
void new_student( int , String, int, int , int, int, int) // Function to accept rollno, name, eng, maths, science, art, geo marks from user.
void display_details(  )// Function to display the details
void calcavg() // to calculate the average marks and store in avg
void calcgrade()// to calculate the grade using the following formula
    avg         grade
    >90         A
    >70 &<= 90      B
    >60 &  <=70     C
    >40 & <=60      D
    <40         F  
double  return_average( ) // To return the average marks
String return_name()//To return the name
char return_grade( ) // To return the grade
Class Useresult
Write a main method which creates an object of the above class and call all the methods

 */




import java.io.*;
class useresult
{
    public static void main(String args[])throws IOException
    {
        result ob=new result();  //automatically calls default constructor
        int mks[]={89,99,90,88,78};
        result ob1=new result(007,"John",mks); //automatically calls parameterized constructor
       
        ob.new_student(1001, "Robin", 90, 98,95,90,96);
        ob.display_details();
        ob.calcavg();
        ob.calcgrade();
        System.out.println("\n**********FINAL RESULT**********");
        System.out.println("NAME   \t AVERAGE \t GRADE ");
        System.out.println(ob.return_name()+"\t "+ob.return_average()+"\t           "+ob.return_grade());

    }
}



class result
{
      int rollno;
      String name;
      double avg;
      char grade;
      int marks[]=new int[5];
      int i;
     
     
      result()
      {
          rollno=0;
          name="";
          marks[0]=0;
          marks[1]=0;
          marks[2]=0;
          marks[3]=0;
          marks[4]=0;
                }
     
      result(int rn, String n, int m[])
      {
          rollno=rn;
          name=n;
         
        for(i=0;i<5;i++)
        {
            marks[i]=m[i];
        }
      }
     
      void new_student(int r, String stu_name, int eng, int maths, int sci, int arts, int geo)
      {
          rollno=r;
          name=stu_name;
          marks[0]=eng;
          marks[1]=maths;
          marks[2]=sci;
          marks[3]=arts;
          marks[4]=geo;
         
      }
      void display_details()
      {
          System.out.println("ROLL NO\tNAME  \tENG\tMATHS\tSCI\tARTS\tGEO");
          System.out.println(+rollno+"\t"+name+"\t"+marks[0]+"\t "+marks[1]+"\t "+marks[2]+"\t "+marks[3]+"\t "+marks[4]);
        }
      void calcavg()
      {
          int sum=0;
          for(i=0;i<5;i++)
          {
              sum=sum+marks[i];
          }
          avg=sum/5;
      }
      void calcgrade()
      {
          if(avg>90)
          {
              grade='A';
          }
          else if(avg>70 && avg<=90)
          {
              grade='B';
          }
          else if(avg>60 && avg<=70)
          {
              grade='C';
          }
          else if(avg>40 && avg<=60)
          {
              grade='D';
          }
          else
          {
              grade='F';
          }
      }
      double return_average()
      {
          return avg;
      }
      String return_name()
      {
          return name;
      }
      char return_grade()
      {
          return grade;
      }
}

       
         
/*
 *
 *
OUTPUT:
ROLL NO    NAME      ENG    MATHS    SCI    ARTS    GEO
1001    Robin    90     98     95     90     96

**********FINAL RESULT**********
NAME        AVERAGE      GRADE
Robin     93.0               A

 */  
   

Sunday, October 14, 2012

disease detection using java & blue j

import java.io.*;
class usediagnose1
{
    public static void main(String args[]) throws IOException
    {
        String d1="Hypertension";
        String d2="Diabetes";
        String s1[]={"High BP", "Obesity", "Breathlessness", "Chest pain", "High pulse rate"}; //symptoms
        String s2[]={"Poor vision", "Belly fat", "Unusual thirst", "Fatigue", "Numbness"};
       
        diagnose1 ob=new diagnose1(d1,d2,s1,s2);
        diagnose1 ob2=new diagnose1();
        ob.new_diagnose(100, "Miss. Pooja");
        ob.ask();
        String rd=ob.return_disease();
        String nm=ob.return_name();
        System.out.println("Wish you speedy recovery "+nm+ " from the disease: " +rd);
       
       
    }
}
       


class diagnose1
{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

    int patient_id;
    String patient_name;
    String disease1,disease2;
    String symp1[]=new String[5];
    String symp2[]=new String[5];
    String disease_detected;
    int i;
    diagnose1() //default constructor
    {
        patient_id=0;
        patient_name="";
    }
   
    diagnose1(String dis1, String dis2, String s1[], String s2[])  //parametrised constructor
    {
       
        disease1=dis1;
        disease2=dis2;
       
        for(i=0;i<5;i++)
        {
            symp1[i]=s1[i];
            symp2[i]=s2[i];
        }
       
    }
   
   
    void new_diagnose(int id, String n)
    {
        patient_id=id;
        patient_name=n;
    }
   
    void ask()throws IOException
    {
        System.out.println("Enter any two symptoms of your illness out of the list of symptoms");
        System.out.println(" High BP,Obesity,Breathlessness,Chest pain,High pulse rate,\n Poor vision, Belly fat,Unusual thirst,Fatigue,Numbness");
        String one=br.readLine();
        String two=br.readLine();
        int j,k;
        int c1=0, c2=0;   //counters for symptoms
        for(j=0;j<5;j++)
        {
            if(symp1[j].equalsIgnoreCase(one))
            {
                c1++;
            }
            if(symp1[j].equalsIgnoreCase(two))
            {
                c1++;
            }
        }
        for(k=0;k<5;k++)
        {
            if(symp2[k].equalsIgnoreCase(one))
            {
                c2++;
            }
            if(symp2[k].equalsIgnoreCase(two))
            {
                c2++;
            }
        }
       
        if(c1==2)   //disease detected if any 2 symptoms match
        {
            disease_detected=disease1;
        }
        else if(c2==2)
        {
            disease_detected=disease2;
        }
        else
        {
       
            disease_detected="NONE";
        }
       
        System.out.println("Detected disease : " +disease_detected);
        display_details();
    }
    private void display_details()
    {
             if(disease_detected==disease1)
             {
                 System.out.println("Remedies for Hypertension are: Consume less salt, Lose weight, Exercise daily, Do not take stress");
             }
             else if(disease_detected==disease2)
             {
                 System.out.println("Remedies for Diabetes are: Consume less sugar, Lose excess weight, Brisk walk daily");
             }
             else
            
             {
                 System.out.println("You are fit and fine !");
             }
    } 
    String return_disease()

    {

        return disease_detected;

    }

    String return_name()

    {

        return patient_name;

    }
}
           



       

Place an order for books using functions & constructors - Java & Blue J program

import java.io.*;
class useorder
{
    public static void main(String args[]) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
        System.out.println("Welcome customer. Please enter the number of copies you want to order ! ");
        int copies=Integer.parseInt(br.readLine()); // input of number of copies ordered
       
        order ob=new order();
        order ob2=new order(3089, 9874, 3, 395);  //automatic call to parameterised constructor
       
        ob.fillarray(); //function call
        ob.disparr();   //function call
        ob.new_order(1009, 11, copies, 395); //parameterised function call
        ob.disp_details(); //function call
        ob.calc_bill();    //function call
       
        int numcopies=ob.return_copies(); //function call
        int pr=ob.return_price();         //function call
        int amount=ob.return_bill();      //function call
        System.out.println("The amount of bill to be paid for "+numcopies+ " copies at the price of "+pr+" per copy is equal to "+amount);
    }
}
       
       
  

class order
{
    int bookid, custid, numcopies, price, bill;
    int array[]=new int[5];
   
    order()
    {
        bookid=0;
        custid=0;
        numcopies=0;
        price=0;
        bill=0;
    }
   
    order(int b, int c, int cop, int p)
    {
        bookid=b;
        custid=c;
        numcopies=cop;
        price=p;
        bill=numcopies*price;
    }
   
   
    void fillarray()
    {
        array[0]=2;
        array[1]=3;
        array[2]=5;
        array[3]=7;
        array[4]=11;
      
    }
    void disparr()
    {
        int i;
        System.out.println("The first five prime numbers as elements of the array  are : ");
       
        for(i=0;i<5;i++)
        {
            System.out.println(array[i]);
        }
    }

    void new_order(int bi, int ci, int n, int p)
    {
        bookid=bi;
        custid=ci;
        numcopies=n;
        price=p;
    }
    void disp_details()
    {
        System.out.println("YOUR BOOKING ID IS = "+bookid);
        System.out.println("YOUR CUSTOMER ID IS = "+custid);
        System.out.println("The number of copies as per your order are = "+numcopies);
        System.out.println("The price per copy is = "+price);
    }
    void calc_bill()
    {
        bill=numcopies*price;
    }
    int return_bill()
    {
        return bill;
    }
    int return_copies()
    {
        return numcopies;
    }
    int return_price()
    {
        return price;
    }
}

       
       

Use of arrays, functions, constructors in JAVA & BLUE J

import java.io.*;
class usecanteen
{
    public static void main(String args[])throws IOException
    {
       canteen ob=new canteen();  //object creation
       
        int itemnumber=2;
        int quantity=3;
        canteen ob1=new canteen(itemnumber, quantity);  //calling parameterised constructor
       
        ob.new_order();
        ob.display_bill();
        int amount=ob.return_bill();
        System.out.println("Kindly pay a total of Rs. "+amount);
    }
}






class canteen
{

        String items[]={"TV", "DVD Player", "Tape recorder", "LG mobile"};  //items array
        int price[]={20000, 5000, 3000, 11000}; //rate array
        int q; //quantity
        int bill;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
        canteen()  //default constructor
        {
            q=0;
            bill=0;
        }
       
        canteen(int itemnum, int quant)  //parameterised constructor
        {
            bill=price[itemnum-1]*quant;
        }
       
        void new_order() throws IOException   //function to place a new order
        {
            System.out.println("************************MENU**********************");
            System.out.println("Enter 1 to order TV @20 000 Rs. ");
            System.out.println("Enter 2 to order DVD player @5000 Rs.");
            System.out.println("Enter 3 to order Tape recorder @3000 Rs.");
            System.out.println("Enter 4 to order LG mobile @11000 Rs.");
            System.out.println("****************************************************");
            int ch,i;
            String ans;
            int count=0;
           
            for(i=1;i<=4;i++)
            {
                count++;
                System.out.println("Please enter your choice of item(1-4)");
                ch=Integer.parseInt(br.readLine());
               
                if(ch>=1 && ch<=4)
                {
                    System.out.println("Please enter the number of " + items[ch-1] + " you want" );                   
                    q=Integer.parseInt(br.readLine());
                }
                else
                {
                    break;
                }
     
                bill=bill + price[ch-1]*q;
                if(count==4)
                {
                   break;
                }
                System.out.println("Do you want to place another order YES or NO?Please note you cannot order more than 4 items");
                ans=br.readLine();
                if(ans.equalsIgnoreCase("NO"))
                {
                    break;
                }
               
        
                           
            }
        }
        void display_bill()
        {
            System.out.println("ITEMISED BILL is of Rs."+bill);
        }
        int return_bill()
        {
            return bill;
        }
         
    }

Monday, September 3, 2012

To store first 10 prime numbers in an array

public class yps
{
    public static void main(String args[])
    {
       int p[]=new int[10];  //to store 10 prime numbers
       int num, i=0,j, count=0,c=0,a;
       
        for(num=2; num<100; num++)
        {
            for(j=1; j<=num; j++)
            {
                if(num%j==0)
                {
                    c++;
                }
            }
            if(c==2)
            {
                p[i]=num;
                i++;
                count++;
               
            }
            if(count==10)
            {
                break;
            }
            c=0;
        }
            System.out.println("First 10 Prime nums are: ");
         for(a=0;a<10;a++)
         {
             System.out.println(p[a]);
            
        }
       
    }
}

To accept 10 decimal values from the user on Terminal Window

import java.io.*;
public class yps
{
    public static void main(String args[]) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        double num[]=new double[10];
        int i;
        System.out.println("Enter 10 decimal values");
        for(i=0; i<10; i++)
        {
            num[i]=Double.parseDouble(br.readLine());
        }
        System.out.println("The 10 decimal values entered are:");
       for(i=0; i<10; i++)
        {
            System.out.println(num[i]);
        }
    }
}

Program to accept 5 strings on Terminal window

/*
 * Program to accept 5 strings on Terminal window
 */

import java.io.*;
public class yps1
{
    public static void main(String args[]) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String name[]=new String[5];
        int i,j;
        System.out.println("Enter five names");
        for(i=0; i<5; i++)
        {
           
            name[i]=br.readLine();
           
        }
        for(j=0; j<5; j++)
        {
           
            System.out.println("NAME  "+ (j+1) + " :"+ name[j]);           
        }
       
    }
}

Sunday, August 5, 2012

Passing object as parameter: Calculate distance between two points Coordinate Geometry

class sukhman
{
    public static void main(String args[])
    {
       point conobj=new point(1,1);  //parametrized constructor will get invoked
       point ob=new point();         //object 1   
       point ob1=new point();        //object 2
       ob.translator(-2,-3);
       ob1.translator(-4,4);
       double ans=ob.distance(ob1);   //passing object as parameter
       System.out.println("The distance between two points is = "+ans);
    }
}


class point
{
    int x,y;
    point()
    {
        x=0;
        y=0;
    }
    point(int a,int b)
    {
        x=a;
        y=b;
    }
    void translator(int xp, int yp)
    {
        x=xp;
        y=yp;
    }
    double distance(point obj)
    {
        double xs=Math.pow((obj.x-x),2);
        double ys=Math.pow((obj.y-y),2);
        double d=Math.sqrt(xs+ys);   //formula in coordinate geometry
        return d;
    }
}

OUTPUT:
The distance between two points is = 7.280109889280518

USE OF FUNCTIONS & CONSTRUCTORS_ROBIN

import java.io.*;
class usesales
{
    public static void main(String args[]) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        sales ob=new sales();
        sales ob1=new sales(001, "Robin", 3, 1000);
        System.out.println("Enter sales ID");
        int s=Integer.parseInt(br.readLine());
        System.out.println("Enter NAME");
        String n=br.readLine();
        System.out.println("Enter Quantity");
        int q=Integer.parseInt(br.readLine());
        System.out.println("Enter Price");
        int p=Integer.parseInt(br.readLine());
        ob.morning();
        ob.new_sales(s,n,q,p);
        ob.calcbill();
        int billret=ob.returnbill();
        String nameret=ob.returnname();
        System.out.println("The bill on your NAME: " +nameret+ " is =  " +billret);

        //now using the object created for parametrized constructor
        ob1.calcbill();
        billret=ob1.returnbill();
        nameret=ob1.returnname();
        System.out.println("The bill on your NAME: " +nameret+ " is =  " +billret);
    }
}
    
   

class sales
{
    int salesid;
    String name;
    int qty;
    int price;
    int bill;
   
    sales()
    {
        salesid=0;
        name="";
        qty=0;
        price=0;
    }
     sales(int sid, String n, int q, int p)
     {
        salesid=sid;
        name=n;
        qty=q;
        price=p;
    }
    void morning()
    {
        for(int i=1;i<=5;i++)
        {
            System.out.println("GOOD MORNING");
        }
    }
    void new_sales(int id, String nm, int qt, int pr)
    {
        salesid=id;
        name=nm;
        qty=qt;
        price=pr;
    }
    void calcbill()
    {
        bill=qty*price;
    }
    int returnbill()
    {
        return bill;
    }
    String returnname()
    {
        return name;
    }
}


OUTPUT:
Enter sales ID
007
Enter NAME
AMAN
Enter Quantity
10
Enter Price
500
GOOD MORNING
GOOD MORNING
GOOD MORNING
GOOD MORNING
GOOD MORNING
The bill on your NAME: AMAN is =  5000
The bill on your NAME: Robin is =  3000
       

Wednesday, May 16, 2012

Concept based question - icse

Q: What does the following statement signify?
                               int a=5;


Ans: The above statement signifies that there is memory location(variable) named a of type int which is used to store integer values and currently holds the value of 5