Search This Blog

Loading...

Wednesday, January 30, 2013

SELECTION SORT

import java.io.*;
class selectionsort
{
public static void main(String args[])throws IOException
{
    int x[]=new int[10];
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println(" Enter 10 Integer values ");
    for(int i=0;i<10;i++)
    {
          x[i] = Integer.parseInt(br.readLine());      //input array of numbers
     }
     int h,p=0;
    for(int i=0;i<10;i++)
    {
        h=x[i]; 
        for(int j=i+1;j<10;j++)  //loop for comparison with next element
        {
            if(x[j]>h) //descending order
            {
                h=x[j];
                p=j;
            }
        }
        x[p]=x[i];  //swap
        x[i]=h;
    }
System.out.println(" After Sorting");
for(int i =0;i<=x.length-1;i++)
{
System.out.println(x[i]);
}
}
}

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

Wednesday, December 29, 2010

ICSE BOARD QUESTION 2008 - program to input a string and print out the text with the uppercase and lowercase letters reversed , but all other characters should remain the same as before.

/**ICSE BOARD QUESTION 2008
 * Write a program to input a string and print out the text
 * with the uppercase and lowercase letters reversed ,
 * but all other characters should remain the
 * same as before.
 * EXAMPLE: INPUT:  WelComE TO School
 *          OUTPUT: wELcOMe to sCHOOL
 *
 */
import java.io.*;
public class questionFIVE2008
{
    public static void main(String args[]) throws IOException
    {
        int ch;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("ENTER THE STRING ");
        String input=br.readLine();
        int len=input.length();
        int i;
        for(i=0; i<len; i++)
        {
            ch=input.charAt(i);
            if(ch>=65 && ch<=90)
            {
                ch=ch+32;
                System.out.print((char)ch);
            }
            else if(ch>=97 && ch<=122)
            {
                ch=ch-32;
                System.out.print((char)ch);
            }
            else
            {
                System.out.print((char)ch);
            }
        }
    }
}

OUTPUT:
ENTER THE STRING
WelComE TO School

wELcOMe to sCHOOL

FUNCTIONS BASED PROGRAM - void function & function with a return type

class aman
{
    public static void main(String args[])
    {
        func ob=new func();
        int ans=ob.sum(10,20); //function call
        System.out.println("the answer is = " +ans);
       
        ob.product(20,50);  //function call
       
       
    }
}
      

class func
{
    int sum(int a, int b)            //function with return type
    {
        int c=a+b;
        return c;
       
       
    }
    void product(int a, int b)     // function with no return type
    {
          int c=a*b;
          System.out.println("the product is = " +c);
    }
}

FUNCTIONS BASED PROGRAM - convert celcius into fahrenheit

class display
{
    public static void main(String args[])
    {
        temperature ob= new temperature();
        double temp=ob.convert(25.0);
        System.out.println("The temperature in fahrenheit is = "+temp);
    }
}
class temperature
{
    double convert(double celcius)
    {
        double far=1.8*celcius+32.0;
        return far;
    }
}

FUNCTIONS BASED PROGRAM - convert inches & feet into cm

class distance
{
    double distinches, distfeet;   
    void func(double inches, double feet)
    {
        distinches=inches;
        distfeet=feet;
    }
   
    void compute()
    {
        double cm;
        cm=2.54*distinches + 30.48*distfeet; //1 INCH=2.54 cm &1 feet=30.48 cm
       
        System.out.println("The total distance in centimetres is = " +cm);
    }
}
    

FUNCTIONS BASED PROGRAM

class a
{
    public static void main(String args[])
    {
        five ob1=new five();   //syntax: class_name objectname= new classname();
        five ob2= new five();
       
        ob1.func(100);
        ob2.func(300);
    }
}
class five
{
    void func(int mins)
    {
        int hours=mins/60;
        int minutes=mins%60;
       
        System.out.println("The time in hours and minutes is = " +hours + " hours and " +minutes + " minutes");
    }
}

OBJECTS AS PARAMETERS IN FUNCTIONS

public class time
{
    int hr, min;
    int totalhours, totalminutes;
    void input(int h, int m)
    {
        hr=h;
        min=m;
    }
    void addtime(time t1, time t2)
    {
        totalhours= t1.hr + t2.hr + (t1.min + t2.min)/60;
        totalminutes=(t1.min + t2.min)%60;
    }
   
    void display()
    {
        System.out.println(totalhours + " hrs " + totalminutes +" min " );  
    }
}

Monday, December 27, 2010

Define class employee with given data members & member methods - ICSE BOARD QUESTION 2008

/** Define a class employee   ICSE BOARD QUESTION 2008
 */
import java.io.*;
class questionFOUR2008
{
    public static void main(String args[]) throws IOException
    {
        employee ob=new employee();
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the PAN NUMBER");
        int pan=Integer.parseInt(br.readLine());
       
        System.out.println("Enter the NAME ");
        String name=br.readLine();
       
        System.out.println("Enter the TAX INCOME");
        double taxincome=Double.parseDouble(br.readLine());
       
       ob.input(pan, name, taxincome);
       ob.calc();
       ob.display();
    }
}
    


class employee  
{
    int pan;
    String name;
    double taxincome;
    double tax;
   
    void input(int p, String n, double ti)
    {
        pan=p;
        name=n;
        taxincome=ti;
    }
    void calc()
    {
        if(taxincome <= 100000)
        {
            tax =0;
        }
        else if(taxincome<=150000)
        {
            tax=(taxincome-100000)*0.10;
        }
        else if(taxincome<=250000)
        {
            tax=5000+(taxincome-150000)*0.20;
        }
       
        else
        {
            tax=25000+(taxincome-250000)*0.30;
        }
    }
    void display()
    {
        System.out.println("PAN NUMBER \t NAME \t TAX-INCOME \t TAX");
        System.out.println("===============================================");
        System.out.println(+pan +"\t" + name +"\t" + taxincome+"\t"+ tax);
    }
}


 OUTPUT:

Enter the PAN NUMBER
123456789
Enter the NAME
AMAN
Enter the TAX INCOME
300000


 
PAN NUMBER      NAME      TAX-INCOME      TAX
===============================================
123456789              AMAN         300000.0             40000.0

Define class salary with given data members & member methods - ICSE BOARD QUESTION 2007

/**
 * Define a class salary  - ICSE BOARD QUESTION 2007
 */ 
import java.io.*;   
class questionFOUR2007
{
    public static void main(String args[]) throws IOException
    {
        salary ob=new salary();
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       
        System.out.println("Enter the NAME of the teacher ");
        String name=br.readLine();
       
        System.out.println("Enter the ADDRESS of the teacher ");
        String address=br.readLine();
       
        System.out.println("Enter the PHONE number");
        int phone=Integer.parseInt(br.readLine());
       
        System.out.println("Enter the SUBJECT SPECIALISATION ");
        String subject=br.readLine();
       
        System.out.println("Enter the monthly salary ");
        double salary=Double.parseDouble(br.readLine());
       
        ob.accept(name, address, phone, subject, salary);
        ob.compute();
        ob.display();
    }
}
class salary
{
    String name,address, subject;
    int phone;
    double salary;
    double tax=0.0;
    void accept(String n, String add, int ph, String sub, double sal)
    {
        name=n;
        address=add;
        phone=ph;
        subject= sub;
        salary=sal;
      
    }
    void compute()
    {
        double annualsalary=salary*12;  //annual salary
        tax=0.05*(annualsalary-175000);
    }
    void display()
    {
        System.out.println("NAME \t ADDRESS \t PHONE \t SUBJECT SPECIALISATION \t MONTHLY SALARY \t INCOME TAX ");
        System.out.println( name + "\t" + address + "\t" + phone + "\t" + subject + "\t" + salary + "\t" + tax);
    }
}
   
OUTPUT:
Enter the NAME of the teacher
AMAN
Enter the ADDRESS of the teacher
CHANDIGARH
Enter the PHONE number
98157
Enter the SUBJECT SPECIALISATION
MBA FINANCE
Enter the monthly salary
90000
NAME      ADDRESS      PHONE      SUBJECT SPECIALISATION    SALARY     INCOME TAX
AMAN    CHANDIGARH    98157    MBA FINANCE                           90000.0          45250.0

Saturday, December 25, 2010

Board questions : OUTPUT BASED 2 marks

class boards
{
    void outputbasedquestions()
    {
        int x=5;
        System.out.println(5*++x);  //ICSE 2005
        x=5;
        System.out.println(5*x++);  //ICSE 2005
       
        int m=5;
        int n=2;
        m-=n;
        System.out.println("m is = " +m);
        m=5;
       n=2;
        n=m+m/n;
        System.out.println("n is =" +n);
       
        int a=0, b=30, c=40;
        a=--b+c+ ++b;                    //ICSE 2006
        System.out.println("a = " +a);
       
       
        int val=500;
        int sum;
        n=550;
        sum=n+val>1750?400:200;    //ICSE 2006
        System.out.println("sum is =" +sum);
       
        val=1600;
        sum=n+val>1750?400:200;   //ICSE 2006
        System.out.println("sum is =" +sum);
       
        System.out.println("four:" +4+2);   // ICSE 2007
        System.out.println("four:" +(2+2)); //ICSE 2007
       
       
        a=2;
        b=3;
        c=9;
        int out=a-(b++)*(--c);                //ICSE 2007
        System.out.println("OUTPUT:"  +out); 
       
        a=2;
        b=3;
        c=9;
        out=a*(++b)%c;                         //ICSE 2007
        System.out.println("OUTPUT:"  +out);
       
               
    }
    void morequestions()
    {
        double x=-9.99;
        System.out.println(Math.abs(x));     //ICSE 2008
       
        x=9.0;
        System.out.println(Math.sqrt(x));    //ICSE 2008
       
       
       
    }
   
   
}


OUTPUT ON THE TERMINAL WINDOW:

30
25
m is = 3
n is =7
a = 99
sum is =200
sum is =400
four:42
four:4
OUTPUT:-22
OUTPUT:8

9.99
3.0

Friday, December 17, 2010

Nested Loops - to print a pattern

/*
 * Write a program to print the following pattern:


ABCDCBA
ABC   CBA
AB        BA
A             A      
AB        BA
ABC   CBA
ABCDCBA

 */
class aman
{
    void pattern(int n)
    {
        int j,i,k=0;
        int num=n;
        for(j=65+num;j>=65;j--)
        {
            for(i=65;i<j;i++)
            {
                System.out.print((char)i);
            }
            for(i=1;i<k;i++)
            {
                System.out.print(" ");
            }
            for(i=j-1;i>64;i--)
            {
                if(i!=65+num-1)
                {
                    System.out.print((char)i);
                }
            }
            k=k+2;
            if(j!=66)
            {
            System.out.print("\n");
        }
        }
        k=num-1;
        for(j=67;j<=65+num;j++)
        {
            for(i=65;i<j;i++)
            {
                System.out.print((char)i);
            }
            for(i=k;i>0;i--)
            {
                System.out.print(" ");
            }
            for(i=j-1;i>64;i--)
            {
               
                    if(i!=65+num-1)
                {
                    System.out.print((char)i);
                }
               
            }
            k=k-2;
            System.out.print("\n");
        }
   
   
    }
}

       
          

Thursday, December 16, 2010

Sorting of city names in alphabetical order using the Bubble Sort technique - ICSE BOARD QUESTION 2008

/**
 * Define a class and store the given city names in a single dimensional array. Sort these names in alphabetical order
 * using the Bubble Sort technique only.
 * INPUT: Delhi, Bangalore, Agra, Mumbai, Calcutta
 * OUTPUT: Agra, Bangalore, Calcutta, Delhi, Mumbai
 */
import java.io.*;
class question2008
{
    public static void main(String args[]) throws IOException
    {
        String words[]= {"Delhi", "Bangalore", "Agra", "Mumbai", "Calcutta"};
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));       
       
        int len=words.length;
        int i,j;
        String t;
       
        for(i=0; i<len-1; i++)
        {
            for(j=0; j<len-1-i; j++)
            {
                if(words[j].compareTo(words[j+1])>0)
                {
                    t=words[j+1];
                    words[j+1]=words[j];
                    words[j]=t;
                }
            }
        }
        System.out.println("The cities in alphabetical order are : ");
        for(i=0; i<len; i++)
        {
            System.out.print(words[i]+ " ");
        }
    }
}

Tuesday, December 14, 2010

Arrays based program - ICSE BOARD QUESTION 2009

/*
 * The annual examination results of 50 students in a class is tabulated as follows:
 * Roll no.         Subject A            Subject B           Subject C
 * ......           .......              .......             .......
 *
 * Write a program to read the data, calculate & display the following:
 * a) Average marks obtained by each student
 * b) Print the roll no. & average marks of the students whose average mark is above 80
 * c) Print the roll no & average marks of the students whose average marks is below 40
 */
import java.io.*;
public class questionNINE2009
{
    public static void main(String args[]) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int roll[]=new int[50];
        int marksA[]=new int[50];
        int marksB[]=new int[50];
        int marksC[]=new int[50];
        float avg[]=new float[50];
        int i,j;
        for(i=1; i<=50; i++)
        {
            System.out.println("Enter roll number of the student");
            roll[i]=Integer.parseInt(br.readLine());
            System.out.println("Enter marks of the student in Subject A");
            marksA[i]=Integer.parseInt(br.readLine());
            System.out.println("Enter marks of the student in Subject B");
            marksB[i]=Integer.parseInt(br.readLine());
            System.out.println("Enter marks of the student in Subject C");
            marksC[i]=Integer.parseInt(br.readLine());
           
            avg[i]=(marksA[i]+marksB[i]+marksC[i])/3;
        }
        System.out.println("----------------------------------RESULT----------------------------------------------");
        System.out.println("Roll no. \t Subject A \t Subject B \t Subject C \t Average ");
       
        for(j=1; j<=50; j++)
        {
       System.out.println(roll[j] +"\t \t" + marksA[j] +"\t \t" + marksB[j] +"\t \t" + marksC[j] +"\t \t" + avg[j] );
        }
       
        System.out.println("\n roll no. & average marks of the students whose average mark is above 80");
        System.out.println("Roll no. \t  Average ");
        for(j=1; j<=50; j++)
        {
           if(avg[j]>80)
            System.out.println(roll[j] +"\t \t" +  avg[j]);
            else
            System.out.println("N.A. \t \t   N.A.");
        }
       
        System.out.println("\n roll no. & average marks of the students whose average mark is below 40 ");
        System.out.println("Roll no. \t  Average ");
        for(j=1; j<=50; j++)
        {
           if(avg[j]<40)
            System.out.println(roll[j] +"\t \t" +  avg[j] );
            else
            System.out.println("N.A. \t \t   N.A.");
        }
           
    }
}

Menu driven program to accept a number from the user and check * whether it a BUZZ number or to accept any two numbers & print GCD of them - ICSE BOARD QUESTION 2009

/**
 * Write a menu driven program to accept a number from the user and check
 * whether it a BUZZ number or to accept any two numbers & print GCD of them.
 */
import java.io.*;
class questionEIGHT2009
{
    public static void main(String args[]) throws IOException
    {
        int i,j;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("**************MENU*************");
        System.out.println("Type 1 to check if a number is a BUZZ number or not");
        System.out.println("Type 2 to print GCD of any two numbers");
       
        System.out.println("Enter your choice");
        int ch=Integer.parseInt(br.readLine());
       
        switch(ch)
        {
            case 1:
            System.out.println("Enter the number to check if it is buzz number or number");
            int n=Integer.parseInt(br.readLine());
           
            if(n%7==0 || n%10==7)
            {
                System.out.println("The number " +n+ " is a BUZZ number");
            }           
            else
            {
                System.out.println("The number " +n+ " is NOT a BUZZ number");
            }
            break;
           
            case 2:
            System.out.println("Enter any two numbers to print their GCD");
            int n1=Integer.parseInt(br.readLine());
            int n2=Integer.parseInt(br.readLine());
           
            int divisor, dividend;
            if(n1>n2)
            {
                dividend=n1;
                divisor=n2;
            }
            else
            {
                dividend=n2;
                divisor=n1;
            }
           
            int rem=1;
            while(rem!=0)
            {
                rem=dividend%divisor;
                if(rem==0)
                {
                    System.out.println("GCD is = " +divisor);
                }
                else
                {
                    dividend=divisor;
                    divisor=rem;
                }
            }
            break;
           
            default:
            System.out.println("Wrong choice");
        }
    }
}

Program to generate a triangle or an inverted triangle till n terms based upon the user's choice of triangle to be displayed - ICSE BOARD QUESTION 2009

/**
 * Write a program to generate a triangle or an inverted triangle
 * till n terms based upon the user's choice of triangle to be displayed.
 *
 * Example 1:
 * Input: Type 1 for a triangle and
 *        Type 2 for an inverted triangle
 *       
 *        1
 *        Enter the number of terms
 *        5
 *        OUTPUT:
 *        1
 *        2 2
 *        3 3 3
 *        4 4 4 4
 *        5 5 5 5 5
 *       
 */
import java.io.*;
class questionFIVE2009
{
    public static void main(String args[]) throws IOException
    {
        int i,j;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("**************MENU*************");
        System.out.println("Type 1 for a triangle and ");
        System.out.println("Type 2 for an inverted triangle");
       
        int ch=Integer.parseInt(br.readLine());
        System.out.println("Enter the number of terms");
        int terms=Integer.parseInt(br.readLine());
       
        switch(ch)
        {
            case 1:          
            for(i=1; i<=terms; i++)
            {
                for(j=1; j<=i; j++)
                {
                    System.out.print(i);
                }
                System.out.println("");
            }
            break;
    
            case 2:
            for(i=terms; i>=1; i--)
            {
                for(j=1; j<=i; j++)
                {
                    System.out.print(i);
                }
                System.out.println("");
            }
            break;
           
            default:
            System.out.println("Wrong choice");
        }
    }
}

Function Overloading - ICSE BOARD QUESTION 2009

/**
 * Design a class to overload a function num_calc() as follows:
 *
 * void num_calc(int num, char ch) with one integer argument and one char
 * argument, computes the square of integer argument if choice ch is 's'
 * otherwise find its cube.
 *
 * void num_calc(int a, int b, char ch) with two integer arguments and
 * character argument. It computes the product of integer arguments if ch
 * is 'p' else adds the integers.
 *
 * void num_calc(String s1, String s2) with two string arguments, which
 * prints whether the strings are equal or not
 *
 */
public class questionSEVEN2009
{
   public void num_calc(int num, char ch)
   {
       if(ch=='s')
       {
           int square=num*num;
           System.out.println("The square of the number is = "+ square);
        }
        else
        {
            int cube=num*num*num;
            System.out.println("The cube of the number is =  "+ cube);
        }
    }
    public void num_calc(int a, int b, char ch)
   {
       if(ch=='p')
       {
           int product=a*b;
           System.out.println("The product of the two numbers is = "+ product);
        }
        else
        {
            int sum=a+b;
            System.out.println("The sum of the two number is = "+ sum);
        }
    }
    public void num_calc(String s1, String s2)
   {
       if(s1.equals(s2))
       {
           System.out.println("The two strings are equal");
        }
        else
        {
          System.out.println("The two strings are not equal"); 
        }
    }
}

Input a sentence & print the number of characters found in the longest word of the given sentence - ICSE Board Question 2009

/**
 * Write a program to input a sentence & print the number of characters
   found in the longest word of the given sentence.
   For example if S= "India is my country" then the output should be 7
 */
import java.io.*;
class questionSIX2009
{
    public static void main(String args[]) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter a sentence :");
        String s=br.readLine();
       
        char ch;
        s=s+" ";
        int array[]=new int[50];
        int lenword=0;
        int lenstr=s.length();
        int j=0;
       
        System.out.println("The string entered by you is: " +s);
        for(int i=0; i<lenstr; i++)
        {
            ch=s.charAt(i);
            if(ch!=' ')
            {
                lenword++;
               
                System.out.print(ch);
            }
           
            if(ch==' ')
            {
                array[j]=lenword;
                j++;
                System.out.println("   The size is " + lenword + "characters");
                lenword=0;
                System.out.print("\n");
            }
        }
         int m=0;
        for(j=0; j<lenstr; j++)
        {
            if(array[j]>m)
            {
                m=array[j];
            }
        }
        System.out.println("The length of the longest word is = " +m);
    }
   
}