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;
        }
         
    }