Saturday, 25 March 2017

ICSE Project Accessory Programs

ICSE Project Accessory Programs

These Programs are meant to help you develop your project by easing the trouble to type less and progress more.
If you have any suggestions about what other kind of Programs might ease out the trouble of developing programs, I will be delighted and will try my best to tackle the problem.
I. Program to convert text to Ascii java System.out.println() format. Just go to Patorjk and type the text you want to display in Ascii Art. Then count and enter the Number of lines your art has and paste the line one by one and you will get the System.out.println() format of the art.

import java.util.Scanner;
class AsciiToJava
{
    Scanner S = new Scanner(System.in);
    public void main()
    {
        Scanner SC = new Scanner(System.in);
        System.out.println("Enter the no of lines");
        int nol = S.nextInt();
        String ascii[] = new String[nol];
        String kscii[] = new String[nol];
        for(int i = 0; i<nol;i++)
        {
            System.out.println("Enter "+(i+1)+"th Line");
            ascii[i] = SC.nextLine();
            kscii[i] = "";
            for(int o = 0;o<ascii[i].length();o++)
            {
                kscii[i] = kscii[i] + ascii[i].charAt(o);
                if(ascii[i].charAt(o)=='\\')
                kscii[i] = kscii[i] + '\\';
            }
        }
        for(int j =0;j<nol;j++)
        System.out.println("System.out.println(\"              "+kscii[j]+"\");");
    }
}

I personally found it confusing to work with StringTokenizer class Objects so I have defined a method to convert tokenizer to array.

    String[] Tokenizer_To_Array(StringTokenizer s1)
    {
        String result[] = new String[s1.countTokens()];
        int n1 = s1.countTokens();
        for(int i=0;i<n1;i++)
            result[i] = s1.nextToken();
        return result;

    }

I am going to add more of such programs soon...


Monday, 13 March 2017

ICSE 2014 Programming Solution

IcSe 2014 PrOgRaMmInG SoLuTiOn

Question 4.
Define a class named movieMagic with the following description:
Instance variables/data members:
int year            –           to store the year of release of a movie
String title       –           to store the title of the movie.
float rating      –           to store the popularity rating of the movie.
(minimum rating = 0.0 and maximum rating = 5.0)
Member Methods:
(i)         movieMagic()              Default constructor to initialize numeric data members to 0 and String data member to “”.
(ii)        void accept()               To input and store year, title and rating.
(iii)       void display()              To display the title of a movie and a message based on the rating as per the table below.
RatingMessage to be displayed
0.0 to 2.0Flop
2.1 to 3.4Semi-hit
3.5 to 4.5Hit
4.6 to 5.0Super Hit
 
Write a main method to create an object of the class and call the above member methods
Solution : 
import java.util.Scanner;
class movieMagic
{
    Scanner S = new Scanner(System.in);
    Scanner StringScanner = new Scanner(System.in);
    int year;
    String title;
    float rating;
    movieMagic()
    {
        year = 0;
        rating = 0.0f;
        title = "";
    }
    void accept()
    {
        System.out.println("Enter the name, year and rating of the movie.");
        title = StringScanner.nextLine();
        year = S.nextInt();
        rating = S.nextFloat();
    }
    void display()
    {
        System.out.print("Title :"+title+"\nYear :"+year+"\nRating :"+rating+"\nMessage : Movie was a ");
        if(rating<=2.0)
        System.out.print("Flop");
        if((rating>2.0)&&(rating<=3.4))
        System.out.print("Semi-Hit");
        if((rating>3.4)&&(rating<=4.5))
        System.out.print("Hit");
        if((rating>4.5)&&(rating<=5.0))
        System.out.print("Super Hit");
    }
    void main()
    {
        accept();
        display();
    }
}


Question 5.
A special two-digit number is such that when the sum of its digits is added to the product of its digits, the result is equal to the original two-digit number.
Example:         Consider the number 59.
Sum of digits = 5 + 9 = 14
Product of its digits = 5 x 9 = 45
Sum of the sum of digits and product of digits= 14 + 45 = 59
Write a program to accept a two-digit number. Add the sum of its digits to the product of its digits. If the value is equal to the number input, output the message “Special 2-digit number” otherwise, output the message “Not a Special 2-digit number”.        
Solution :
import java.util.Scanner;
class Question5
{
    Scanner S = new Scanner(System.in);
    public boolean isSpecial(int x)
    {
        int prod = 1;
        int r = x;
        int sum = 0;
        while(x!=0)
        {
            int d = x%10;
            prod = prod*d;
            sum = sum +d;
            x=x/10;
        }
        if((prod+sum)==r)
        return true;
        else 
        return false;
    }
    void main()
    {
        System.out.println("Enter the number");
        int no = S.nextInt();
        if(isSpecial(no))
        System.out.println("It is a Special No.");
        else
        System.out.println("Not a Special No.");
    }
}


Question 6.
Write a program to assign a full path and file name as given below. Using library functions, extract and output the file path, file name and file extension separately as shown.
Input: C:Users\admin\Pictures\flowers.jpg
Output:
Path: C:Users\admin\Pictures
File name: flowers
Extension: jpg
Solution : 
import java.util.Scanner;
class Question6
{
    Scanner S = new Scanner(System.in);
    String input,path,extension,filename,temp;
    void method1(String str)
    {
        path = str.substring(0,(str.lastIndexOf('\\')));
        extension = str.substring((str.lastIndexOf('.')));
        filename = str.substring((str.lastIndexOf('\\')+1),(str.lastIndexOf('.')));
        System.out.println("Path : "+path+"\nFile name: "+filename+"\nExtension: "+extension);
    }
    void main()
    {
        System.out.println("Enter the String ");
        input = S.nextLine();
        method1(input);
    }
    
}


Question 7.
Design a class to overload a function area() as follows:
(i)   double area(double a, double b, double c) with three double arguments, returns the area of a scalene triangle using the formula: area = (s — a)(s- b)(s - c)
Where s = (a+b+c)/2
(ii)  double area(int a, int b, int height) with three integer arguments, returns the area of a trapezium using the formula:
area =       0.5  X  heightx(a+b)
(ii)  double area(double diagonal 1, double diagonal 2) with two double arguments, returns
the area of a rhombus using the formula:
Area =       0.5X(diagonal1 x diagonal2)        [15]
Solution :

import java.util.Scanner;
class Question6
{
    Scanner S = new Scanner(System.in);
    String input,path,extension,filename,temp;
    void method1(String str)
    {
        path = str.substring(0,(str.lastIndexOf('\\')));
        extension = str.substring((str.lastIndexOf('.')));
        filename = str.substring((str.lastIndexOf('\\')+1),(str.lastIndexOf('.')));
        System.out.println("Path : "+path+"\nFile name: "+filename+"\nExtension: "+extension);
    }
    void main()
    {
        System.out.println("Enter the String ");
        input = S.nextLine();
        method1(input);
    }
    
}


 Question 8.
Using the switch statement, write a menu driven program to calculate the maturity amount of a Bank Deposit. The user is given the following options:

(i) Term Deposit
(ii) Recurring Deposit

For option :-
(i) accept principal(P), rate of interest(r) and time period in years(n). Calculate
Amount and output the maturity amount (A) receivable using the formula A.= P[1 +(i/100)]  
(ii) accept Monthly Installment (P), rate of interest(r) and time period in months (n). Calculate and output the maturity amount(A) receivable using the formula A=(Pxn)+((Pxnx(n+1)xr)/ (2x100x12) For an incorrect option, an appropriate error message should be displayed. [15]

Solution :
import java.util.Scanner;
class Question8
{
    Scanner S = new Scanner(System.in);
    void RecurringDeposit()
    {
        System.out.println("Enter the Monthly Deposit");
        double P = S.nextDouble();
        System.out.println("Enter the Rate of Interest");
        double r = S.nextDouble();
        System.out.println("Enter the time in months");
        int n = S.nextInt();
        double A = (P*n) + ((P*n*(n+1)*r))/(100*12*2);
        System.out.println("Maturity Amount : "+A);
    }
    void TermDeposit()
    {
        System.out.println("Enter the Principal Ammount");
        double P = S.nextDouble();
        System.out.println("Enter the Rate of Interest");
        double r = S.nextDouble();
        System.out.println("Enter the time in years");
        int n = S.nextInt();
        double A = Math.pow(((1+(r/100))),n);
        A = A*P;
        System.out.println("Maturity Amount : "+A);
    }
    void main()
    {
        System.out.println("Press 1 for Term Deposit\nPress 2 for Recurring Deposit\nPress 3 to exit");
        int ch = S.nextInt();
        switch(ch)
        {
            case 1 : TermDeposit();
            break;
            case 2 : RecurringDeposit();
            break;
            case 3 : System.exit(0);
            break;
            default : System.out.println("Wrong choice Entered!");
            System.exit(0);
        }
    }
}
       
Question 9.
Write a program to accept the year of graduation from school as an integer value from the user. Using the Binary search technique on the sorted array of Integers given below, output the message "Record exists" if the value input is located in the array. If not, output the message "Record does not exist". { 1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010)   [15]

Solution :
import java.util.Scanner;
class Question9
{
    Scanner S = new Scanner(System.in);
    public boolean BinarySearch(int Array[],int search)//this Binary Search Method can be used in any question in Integer Arrays[].
    {
        int len = Array.length;
        boolean flag = false;
        int lowerIndex=0,higherIndex=len-1,middleIndex;
        while(lowerIndex<=higherIndex)
        {
            middleIndex=(lowerIndex+higherIndex)/2;
            if (search==Array[middleIndex])
            {
                System.out.println("Element "+search+" found at "+(middleIndex+1)+" position");
                flag= true;
                break;
            }
            else if(search<Array[middleIndex])
            higherIndex=middleIndex-1;
            else
            lowerIndex=middleIndex+1;
        }
        return flag;
    }
    void main()
    {
        System.out.println("Enter the year to be searched");
        int syear = S.nextInt();
        int year[] = {1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010};
        if(BinarySearch(year,syear))
        System.out.println("Record Exists");
        else
        System.out.println("Record does not Exist");
    }
}

    
Download the printable PDF file >>HERE<<


----------------x---------------------x----------------x---------------------x----------------x---------------------x----------------x---------------------x

Sunday, 12 March 2017

ICSE 2013 Programming Solution

IcSe 2013 PrOgRaMmInG SoLuTiOn


Question 4:
Define a class called FruitJuice with the following description: [15]
Instance variables/data members:
int product_code – stores the product code number
String flavour – stores the flavor of the juice.(orange, apple, etc)
String pack_type – stores the type of packaging (tetra-pack, bottle etc)
int pack_size – stores package size (200ml, 400ml etc)
int product_price – stores the price of the product
Member Methods:
(i)FriuitJuice() – default constructor to initialize integer data members
     to zero and string data members to “”.
(ii)void input() – to input and store the product code, flavor, pack type,
     pack size and product price.
(iii)void discount() – to reduce the product price by 10.
     void display() – to display the product code, flavor, pack type,
     pack size and product price.
Solution :

import java.util.Scanner;
class FruitJuice
{

    int product_code;
    String flavour;
    String pack_type;
    int pack_size;
    int product_price;
    public FruitJuice()
    {
        product_code = 0;
        flavour = "";
        pack_type = "";
        pack_size = 0;
        product_price = 0;
    }
    public void input()
    {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter productcode: ");
        product_code = scanner.nextInt();
        System.out.print("Enter flavour:");
        flavour = scanner.next();
        System.out.print("Enter pack type:");
        pack_type = scanner.next();
        System.out.print("Enter pack size:");
        pack_size = scanner.nextInt();
        System.out.print("Enter productprice: ");
        product_price = scanner.nextInt();
    }
    public void discount()
    {
        product_price = (int) ((90/100) *product_price);
    }
    public void display()
    {
        System.out.println("Product Code:" + product_code+"\nFlavour: "+flavour+"\nPack Type:"+pack_type+"\nPack Size: "+pack_size+"\nProduct Price:"+product_price);
    }
}

Question 5:

The International Standard Book Number (ISBN) is a unique numeric book identifier which is printed on every book. The ISBN is based upon a 10-digit code. The ISBN is legal if:
1xdigit1 + 2xdigit2 + 3xdigit3 + 4xdigit4 + 5xdigit5 + 6xdigit6 + 7xdigit7 + 8xdigit8 + 9xdigit9 + 10xdigit10 is divisible by 11.
Example: For an ISBN 1401601499
Sum=1×1 + 2×4 + 3×0 + 4×1 + 5×6 + 6×0 + 7×1 + 8×4 + 9×9 + 10×9 = 253 which is divisible by 11.
Write a program to:
(i) input the ISBN code as a 10-digit integer.
(ii) If the ISBN is not a 10-digit integer, output the message “Illegal ISBN” and terminate the program.
(iii) If the number is 10-digit, extract the digits of the number and compute the sum as explained above.
If the sum is divisible by 11, output the message, “Legal ISBN”. If the sum is not divisible by 11, output the message, “Illegal ISBN”. [15]
Solution :
import java.util.Scanner;
class Question5
{
    Scanner S = new Scanner(System.in);
    public boolean isISBN(long x)
    {
        long c = x;
        long sum = 0;
        for(int i = 1;i<=10;i++)
        {
            long d = c%10;
            c=c/10;
            sum = sum + (d*i);
        }
        if((sum%11)==0)
        return true;
        else 
        return false;
    }
    void main()
    {
        System.out.println("Enter an ISBN number");
        long no = S.nextLong();
        if(isISBN(no))
        System.out.println("Legal ISBN");
        else
        System.out.println("Illegal ISBN");
    }
}

Question 6:
Write a program that encodes a word into Piglatin. To translate word into Piglatin word, convert the word into uppercase and then place the first vowel of the original word as the start of the new word along with the remaining alphabets. The alphabets present before the vowel being shifted towards the end followed by “AY”.
Sample Input(1): London Sample Output(1): ONDONLAY
Sample Input(2): Olympics Sample Output(2): OLYMPICSAY [15]
Solution :

import java.util.Scanner;
class Question6
{
    Scanner S = new Scanner(System.in);
    String str,pigword="",temp="";
    char c,cc;
    public boolean isVowel(char ch)
    {
        ch = Character.toUpperCase(ch);
        if((ch=='A')||(ch=='E')||(ch=='I')||(ch=='O')||(ch=='U'))
        return true;
        else
        return false;
    }
    public void piglatin()
    {
        System.out.println("Enter the Word to be converted in Piglatin :-");
        str = S.nextLine();
        str = str.toUpperCase();
        for(int i = 0;i<str.length();i++)
        {
            c=str.charAt(i);
            if(isVowel(c))
            {
                pigword = str.substring(i,str.length());
                pigword = pigword+temp+"AY";
                break;
            }
           
            temp = temp+c;
        }
        System.out.println("Piglatin String : "+pigword);
    }
}
                

Question 7:
Write a program to input 10 integer elements in an array and sort them
In descending order using bubble sort technique. [15]
Solution :
import java.util.Scanner;
class Question7
{
    Scanner S = new Scanner(System.in);
    int lol[] = new int[10];
    void BubbleSort(int Array[])//mmethod 1 to sort elements in Ascending order.
    {
        int n = Array.length;
        int c = 0, f = 1;
        for(int i=0; (i<n-1) && (f==1); i++)
        {
            f = 0;
            for(int j=0; j<n-1-i; j++)
            {
                if(Array[j] > Array[j+1])
                {
                    c = Array[j];
                    Array[j] = Array[j+1];
                    Array[j+1] = c;
                    f = 1;
                }
            }
            if(f == 0)
                break;
        }
        System.out.println("The Array Sorted in Descending order is : ");
        for(int i = (n-1);i>=0;i--)
        System.out.println(Array[i]);
    }
    void Bubbles(int Array[])// Method 2 to actually sort the Array in descending oreder and printing it as it is.
    {
        int len = Array.length;
        for(int i = 0;i<len;i++)
        {
           for(int j = 1;j<len;j++)
           {
               if(Array[j-1]<Array[j])
               {
                   int c = Array[j];
                   Array[j] = Array[j-1];
                   Array[j-1] = c;
                }
            }
        }
        System.out.println("The Array Sorted in Descending order is : ");
        for(int i = 0;i<len;i++)
        System.out.println(Array[i]);
    }
    void main()
    {
        System.out.println("Enter the array to be sorted in decending order");
        for(int i=0;i<10;i++)
        lol[i] = S.nextInt();
        BubbleSort(lol);
        Bubbles(lol);//For this question, this method should be used as it is clearly said to SORT the array in descending order and print it.
    }

Question 8:
Design a class to overload a function series() as follows: [15]
(i) double series(double n) with one double argument and
returns the sum of the series.
sum = 1/1 + 1/2 + 1/3 + ….. 1/n
(ii) double series(double a, double n) with two double arguments
and returns the sum of the series.
sum = 1/a
2 + 4/a5 + 7/a8 + 10/a11... till n terms
Solution :

class Question8
{
    double series(double n)
    {
        double sum = 0;
        for(int i = 1;i<=n;i++)
        sum = sum + 1.0/i;
        return sum;
    }
    double series(double a, double n)
    {
        double sum = 0;
        int k = 1;
        for(int i = 1;i<=n;i++)
        {
            sum = sum + (k++/(Math.pow(a,(k++))));
            k++;
        }
        return sum;
    }
}

Question 9:

Using the switch statement, write a menu driven program: [15]
(i) To check and display whether a number input by the user is
a composite number or not (A number is said to be a composite, if it
has one or more then one factors excluding 1 and the number itself).
Example: 4, 6, 8, 9…
(ii) To find the smallest digit of an integer that is input:
Sample input: 6524
Sample output: Smallest digit is 2
For an incorrect choice, an appropriate error message should be displayed.
Solution :
import java.util.Scanner;
class Question9
{
    Scanner S = new Scanner(System.in);
    int no,ch;
    void main()
    {
        System.out.println("Press 1 to check for Composite Number.");
        System.out.println("Press 2 to find smallest digit in the Input.");
        ch = S.nextInt();
        switch(ch)
        {
            case 1: Prime();
            break;
            case 2: Dig();
            break;
            default: System.out.println("Wrong choice entered");
        }
    }
    void Dig()//My method using Arrays[]. I prefer this method because we can perform various actions and alterations in the array containing the digits of number easily...Useful for higher studies.
    {
        System.out.println("Enter the Number");
        no = S.nextInt();
        int copy = no;
        int len = Integer.toString(no).length();//finding the number of digits in the input.
        int digits[] = new int[len];
        int j = 0;
        int min = 9;
        while(copy>0)//extracting digits and puting them in array.
        {
            int d = copy%10;
            copy = copy/10;
            digits[j] = d;
            j++;
        }
        for(int i =0;i<len;i++)
        {
            if(digits[i]<min)
            min = digits[i];
        }
        System.out.println("Smallest digit is = "+min);
    }
    void Prime()
    {
        System.out.println("Enter the Number to be checked");
        no = S.nextInt();
        if(isPrime(no))
        System.out.println("Not a Composite Number");
        else
        System.out.println("It is a Composite Number");
           
    }
    public boolean isPrime(int x)
    {
        int killme = 0;
        for(int i = 2;i<x;i++)
        {
            if(x%i==0)
            killme++;
            else
            continue;
        }
        if(killme>0)
        return false;
        else
        return true;
    }
    void Digits()
    {
        System.out.println("Enter the Number");
        no = S.nextInt();
        int copy = no;
        int min = 9;
        while(copy>0)
        {
            int d = copy%10;
            copy = copy/10;
            if(d<min)
            min = d;
        }
        System.out.println("Smallest digit is = "+min);
    }
}

Download the printable PDF file >>HERE<<


----------------x---------------------x----------------x---------------------x----------------x---------------------x----------------x---------------------x