ICSE  and ISC computer science question with answer

ICSE and ISC computer science question with answer

 

ICSE  and ISC computer science question with answer

ICSE  and ISC computer science Question with Answer

An ABC Credit card company set the limit for spending 25000 to its customer. ABC Credit Card Company also offers a cashback to its customers according to the following criteria. Accept the amount spent by the user and display the cashback amount he is entitled to. The amount, Cash Back.

                            Cash Back

First     2000            150

Next     2000            230 + 2% of amount exceeding 2000

Next     5000            500 + 4% of amount exceeding 4000

Till        25000         1200 + 8% of the amount exceeding 9000

Write a java program to declare the class 'ABCCreditCompany' that takes in the name of the customer and the amount spent by him. Calculate the cashback amount and print it along with all the other details.

Using following methods

void input()                        // for inputting the spending and customer name

void calculate()                 // for calculating the cash back

void display()                     // print the spending, cashback, and customer name

Solution

/**

A ABC Credit card company

*/

import java.util.*;

public class ABCCreditCompany

{

    double spend, cashBack = 0.0;

    String name;    // customer name

    void input()

    {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter Customer Name :");

        name = sc.nextLine();

        System.out.println("Enter Spending amount :");

        spend = sc.nextDouble();

    }

 // started calculate method

    void calculate()

    {       

        if(spend > 25000)

        {

            // if spending more then 25000 then print a message and end the program

            System.out.println("Sorry Only Under Rs. 25000/- allowed");

            System.exit(0);  // End the program

        }

        else

        {

            if(spend<=2000)

            {

                cashBack = 150.0;

            }

            else if(spend>2000 && spend<=4000)

            {

                cashBack = 200.0 + (spend - 2000) * 0.02;

            }

            else if(spend>4000 && spend<=9000)

            {

                cashBack = 400.0 + (spend - 4000) * 0.04;

            }

            else

            {

                cashBack = 1200.0 + (spend - 9000) * 0.08;

            }

        }

    }

    void display()

    {

        System.out.println("Customer Name   :" + name);

        System.out.println("Spending        :" + spend);   

        System.out.println("Cash Back       :" + cashBack);

    }

 

    public static void main(String args[])

    {

      // Creating Objcet ob

        ABCCreditCompany ob = new ABCCreditCompany();

        ob.input();

        ob.calculate();

        ob.display();       

    }           

}

Sample output

Enter Customer Name :

Ramesh Das

Enter Spending amount :

8000

Customer Name   :Ramesh Das

Spending             :8000.0

Cash Back           :560.0

Java program


Merge two array and sort the merge array

Merge two array and sort the merge array

 

Merge two array and sort the merge array

Merge two arrays and sort the merge array

Q. Write a program in Java to accept two integer arrays from the user and join them into a third array.

After joining the arrays, sort the final array in ascending order using Bubble sort technique.

Sample Input: Array A [ ] = {10,2,23,4,15};

Array B [ ] = {60,7,80,99,10};

Sample Output: The final Array after

joining is :

10 2 23 4 15 60 7 80 99 10

The final Array after sorting is : 2 4 7 10 10 15 23 60 80 99

Solution:

In the following the program two array arr1[] and arr2[] are merge into a single array arr3[]. Here first array arra1[] with five values first added to one by one to the third arr3[] and then arr2[] five values are added to arr3[]. After merging we sort the array with the Bubble sort array technique. Finally, we print the sorted array.

public class MergeArrayAndSort

{

    public static void main(String args[])

    {

        int arr1[]={10,2,23,4,15};  // First Array

        int arr2[]={60,7,80,99,10}; // Second Array

        int arr3[]=new int[10];  // Third Array

        int i, k=0; // use as a counter for third array

        int j, temp;

        System.out.println("First Array");

        for(i=0; i<5; i++)

        {

            System.out.print(arr1[i] + " ");

        }

        System.out.println();

        System.out.println("Second Array");

        for(i=0; i<5; i++)

        {

            System.out.print(arr2[i] + " ");

        }

        System.out.println();

        // Merging of two array

        // First array into the third array

        for(i=0; i<5; i++)

        {

            arr3[k]=arr1[i];

            k++; //k=5

        }

 

        // Second array into the third array

        for(i=0; i<5; i++)

        {

            arr3[k]=arr2[i];

            k++;

        }

        System.out.println("After Merging of Two arrays into third array");

        for(i=0; i<k; i++)

        {

            System.out.print(arr3[i] + " ");

        }

        System.out.println(); 

 

        // Sort the Merge Array with Bubble sort

        for(i=0; i<k-1; i++)

        {

            for(j=0; j<k-1 - i; j++)

            {

                if(arr3[j]>arr3[j+1])

                {

                    temp = arr3[j];

                    arr3[j] = arr3[j+1];

                    arr3[j+1] = temp;

                }

            }

        }

        // Print the sorted order

        for(i=0; i<k; i++)

        {

            System.out.print(arr3[i] + " ");

        }

        System.out.println();

    }       

}

Java program

Type conversion in Java - java type conversion

Type conversion in Java - java type conversion

Type conversion in Java

Type conversion in Java with Examples

In a mixed java expression the data type get converted to a higher type without any intervention and this conversion converted into a single type is known as data type conversion.

 

What is typecasting?

When data type converted to another type by the user then that is called type casting.

 

How many types of type conversion?

There are two types of type conversion as follows

  • Implicit or Widening or Automatic Type Conversion
  • Explicit or Narrowing Conversion

 

Implicit or Widening or Automatic Type Conversion

In implicit or Widening type conversion the data type convert automatically from lower to higher

byte ------->char ------->short ------->int ------->long ------->float ------->double

int x;

float y;

double z;

z = x + y + x * y;

In the above code int and float automatically converted to double and this widening or implicit typecasting.

 

In the following code ‘variable a’ is defined as int and while division it converted to double and the result comes as 3.3333333333333335

public class ImplicitConversion

{

    public static void main(String args[])

    {

        int a;

        double b, c;

        a = 10;

        b = 3.0;

        c = a / b;

        System.out.println(c);

    }

}

 

Explicit or Narrowing Conversion

double ------->float ------->long ------->int ------->short ------->char ------->byte

 The explicit or narrowing type casting done by the user requirement.

 Say

double a;

int b;

now if we want (a that is double data type) to be converted into int type then we need do so as

b = (int) a;

double is converted to int in the above expression.

Example

public class ExplicitConversion

{

    public static void main(String args[])

    {

        int a;

        double c, b;

        c = 10.0;

        b = 3.0;

        a =(int)(c/b);

        System.out.println(a);

    }

}

In the above code, double is converted to int type and after the division it prints

3

Not 3.3333333333333335

 Conclusion

Explicit the conversion must be done properly otherwise error will be there or might be the exact result will not come.

Java program