Category Archives: Miscellaneous

Solved ICSE Computer Applications 2014

Solved ICSE Computer Applications 2014 (Section B)

Question 4.

Define a class 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:

  1. movieMagic( ): Default constructor to initialize numeric data members to 0 and String data member to “”.
  2. void accept( ): To input and store year title and rating.
  3. void display( ): To display the title of a movie and a message based on the rating as per the table.
Β Rating Message to be displayed
0.0 to 2.0 Flop
2.1 to 3.4 Semi-hit
3.5 to 4.5 Hit
4.6 to 5.0 Super Hit

Write a main( ) method to create an object of the class and call the above member methods.

Solution:

import java.util.*;
class movieMagic{
    int year;
    String title;
    float rating;
    public movieMagic(){
        year = 0;
        title = "";
        rating = 0;
    }
    public void accept(){
        Scanner sc = new Scanner( System.in );
        System.out.print("Enter title: ");
        title = sc.nextLine();
        System.out.print("Enter year: ");
        year = sc.nextInt();
        System.out.print("Enter rating: ");
        rating = sc.nextFloat();
    }
    public void display(){
        String message = "";
        System.out.print("Title: "+title);
        if( rating >= 0.0f && rating <= 2.0 ) message="Flop";
        else if( rating >= 2.1f && rating <= 3.4 ) message="Semi-hit";
        else if( rating >= 3.5f && rating <= 4.5 ) message="Hit";
        else if( rating >= 0.0f && rating <= 2.0 ) message="Super Hit";
        System.out.println("Result: "+ message );
    }
    public static void main( String args[] ){
        movieMagic obj = new movieMagic( );
        obj.accept( );
        obj.display( );
    }
}

 


Question 5.

A special two-digit number is such that when the sum of the 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 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-two digit number” otherwise, output the message “Not a special two-digit number”.

Solution:

import java.util.*;
class Special{
    public static void main(String args[]){
        Scanner sc = new Scanner( System.in );
        System.out.print("Enter a two digit number: ");
        int num = sc.nextInt();
        if( num < 10 || num > 99 ){
            System.out.println( "Invalid input" );
        }else{
            if( isSpecialTwoDigitNumber( num ) )
            System.out.println( "Special two-digit number" );
            else
            System.out.println( "Not a Special two-digit number" );
        }
    }
    public static boolean isSpecialTwoDigitNumber( int num ){
        int first, second;
        first = num /10;
        second = num%10;
        return ( first+second + first*second == num );
    }
}

 


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: flower
Extension: jpg

Solution

import java.util.*;
class MyFile{
    public static void main( String args[] ){
        Scanner sc = new Scanner( System.in );
        String fullPath, path, filename, extension;
        System.out.println("Enter full path: ");
        fullPath = sc.nextLine();
        int lastSlash, lastDot;
        lastSlash = fullPath.lastIndexOf("\\");
        lastDot = fullPath.lastIndexOf(".");
        path = fullPath.substring( 0, lastSlash + 1 );
        filename = fullPath.substring( lastSlash+1, lastDot);
        extension = fullPath.substring( lastDot+1 );
        System.out.println( "Path: " + path );
        System.out.println( "File name: "+ filename );
        System.out.println( "Extension: " + extension );
    }
}

 


Question 7.

Design a class to overload a function area as follows:

  1. double area( double a, double b, double c ) with three double arguments, returns the area of a scalene triangle using the formula:
    area=sqrt{ s(s-a)(s-b)(s-c)}where s={a+b+c}/2
  2. double area( int a, int b, int height ) with three integer arguments, returns the area of a trapezium using the formula:
    area = {1/2}height(a+b)
  3. double area( double diagonal1, double diagonal2 ) with two double arguments, returns the area of a rhombus using the formula:
    area =(diagonal1 * diagonal2)

Solution:

class Area{
    public double area( double a, double b, double c ){
        double s = ( a+b+c )/2;
        return Math.sqrt( s * (s-a) * (s-b) * (s-c) );
    }
    public double area( int a , int b, int height ){
        return 1.0/2.0 * height * ( a + b );
    }
    public double area( double diagonal1, double diagonal2 ){
        return 1.0/2.0 * ( diagonal1 * diagonal2 );
    }
}

 


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:

  1. Term Deposit
  2. Recurring Deposit.

For option 1. accept principal(P), rate of interest(r) and time period in years(n). Calculate and output the maturity amount(A) receivable using the formula:

A=P{{[} 1+{r/100}{]}}^n

For option 2. 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 = P * n + P * { {n(n+1)}/2 } * { r/100 } * { 1/12 }

For an incorrect option, an appropriate error message should be displayed.

Solution

import java.util.*;
class BankDeposit{
    public static void main( String args[] ){
        float A=0, P, n, r;
        int choice;
        Scanner sc = new Scanner(System.in);
        System.out.println( "1. Term Deposit\n2.Recuring Deposit\nEnter choice(1,2)" );
        choice = sc.nextInt( );
        if( choice2 ){
            System.out.println( "Invalid choice" );
        }else{
            System.out.println( "Enter P: " );
            P = sc.nextFloat( );
            System.out.println( "Enter r: " );
            r = sc.nextFloat( );
            System.out.println( "Enter n: " );
            n = sc.nextFloat( );
            switch( choice ){
                case 1:
                    A = P * (float) Math.pow( ( 1 + r / 100), n );
                case 2:
                    A = P *n + P * (n*(n+1)/2) *(r/100) * (1.0f/12.0f);
            }
            System.out.println( "Maturity Amount: " + A );
        }
    }
}

 


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 of 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}
Solution

import java.util.*;
class Graduation{
    public static boolean binarySearch( int item ){
        int year[] ={ 1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010 };
        int lowerBound = 0, upperBound = year.length-1, middle;
        while( lowerBound <= upperBound ){
            middle = ( lowerBound + upperBound ) / 2;
            if( year[ middle ] == item) return true;
            else if( item < year[ middle ] ) upperBound = middle - 1;
            else lowerBound = middle + 1;
        }
        return false;
    }
    public static void main( String args[] ){
        Scanner sc = new Scanner( System.in );
        System.out.print( "Enter year of graduation: " );
        int year = sc.nextInt( );
        if( binarySearch( year ) ){
            System.out.print( "Record exists" );
        }else{
            System.out.print( "Record does not exist" );
        }
    }
}