Wednesday, August 26, 2020

JAVA PRACTICLES OF ALL TIMES

JAVA PRACTICLES OF ALL TIMES

 

 

Question:

Design a program to accept a day number (between 1 and 366), year (in 4 digits) from the user to generate and display the corresponding date. Also, accept ‘N’ (1 <= N <= 100) from the user to compute and display the future date corresponding to ‘N’ days after the generated date. Display an error message if the value of the day number, year and N are not within the limit or not according to the condition specified.

Test your program with the following data and some random data:

Example 1

INPUT:
DAY NUMBER: 255
YEAR: 2018
DATE AFTER (N DAYS): 22

OUTPUT:
DATE: 12 TH SEPTEMBER, 2018
DATE AFTER 22 DAYS: 4 TH OCTOBER, 2018

Example 2

INPUT:
DAY NUMBER: 360
YEAR: 2018
DATE AFTER (N DAYS): 45

OUTPUT:
DATE: 26 TH DECEMBER, 2018
DATE AFTER 45 DAYS: 9 TH FEBRUARY, 2019

Example 3

INPUT:
DAY NUMBER: 500
YEAR: 2018
DATE AFTER (N DAYS): 33

OUTPUT:
DAY NUMBER OUT OF RANGE.

Example 4

INPUT:
DAY NUMBER: 150
YEAR: 2018
DATE AFTER (N DAYS): 330

OUTPUT:
DATE AFTER (N DAYS) OUT OF RANGE.


Programming Code:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

/**

  * The class ISC2019_Q1 inputs a day number, year and number of days after

  * and prints the current date and the future date

  * @author : www.guideforschool.com

  * @Program Type : BlueJ Program - Java

  * @Question Year : ISC Practical 2019 Question 1

  */

import java.util.*;

class ISC2019_Q1

{

    int isLeap(int y) //function to check for leap year and return max days

    {

        if((y%400 == 0) || (y%100 != 0 && y%4 == 0))

            return 366;

        else

            return 365;

    }

 

    String postfix(int n) //function to find postfix of the number

    {

        int r = n%10;

        if(r == 1 && n != 11)

            return "ST";

        else if(r == 2 && n != 12)

            return "ND";

        else if(r == 3 && n != 13)

            return "RD";

        else

            return "TH";

    }

 

    void findDate(int d, int y) //function to find the date from day number

    {

        int D[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        String MO[] = {"", "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY",

                          "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"};

        if(isLeap(y)==366)

        {

            D[2] = 29;

        }

        int m = 1;

        while(d > D[m])

        {

            d = d - D[m];

            m++;

        }

        System.out.println(d+postfix(d)+" "+MO[m]+", "+y);

    }

     

    void future(int d, int y, int n) //function to find future date

    {

        int max = isLeap(y);

        d = d + n;

        if(d>max)

        {

            d = d - max;

            y++;

        }

        findDate(d,y);

    }

     

    public static void main(String args[])

    {

        ISC2019_Q1 ob = new ISC2019_Q1();

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the day number : ");

        int day = sc.nextInt();

        System.out.print("Enter the year : ");

        int year = sc.nextInt();

        int max = ob.isLeap(year);

        if(day > max)

        {

            System.out.println("DAY NUMBER OUT OF RANGE");

        }

        else if(year<1000 || year>9999)

        {

            System.out.println("YEAR OUT OF RANGE");

        }

        else

        {

            System.out.print("Enter the number of days after : ");

            int n = sc.nextInt();

            if(n<1 || n>100)

            {

                System.out.println("DATE AFTER (N DAYS) OUT OF RANGE");

            }

            else

            {

                System.out.print("DATE :\t\t\t");

                ob.findDate(day,year);

                System.out.print("DATE AFTER "+n+" DAYS :\t");

                ob.future(day,year,n);

            }

        }

    }      

}


Output:

Enter the day number : 360
Enter the year : 2019
Enter the number of days after : 80
DATE : 26TH DECEMBER, 2019
DATE AFTER 80 DAYS : 15TH MARCH, 2020

 

Question:

Prime-Adam integer is a positive integer (without leading zeros) which is a prirne as well as an Adam number.

Prime number : A number which has only two factors, i.e. 1 and the number itself.
Example: 2, 3, 5, 7 …etc.

Adam number: The square of a number and the square of its reverse are reverse to each other.
Exarnple: If n=13 and reverse of ‘n’ =31, then,
(13) = 169
(31) = 961 which is reverse of 169
thus 13, is an Adam number.

Accept two positive integers m and n, where m is less than n as user input. Display all Prime-Adam integers that are in the range between m and n (both inclusive) and output them along with the frequency, in the format given below:

Test your program with the following data and some random data:

Example 1

INPUT:

m=5
n=100

OUTPUT:

THE PRIME-ADAM INTEGERS ARE:
11 13 31
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3

Example 2

INPUT:
m=100
n=200

OUTPUT:

THE PRIME-ADAM INTEGERS ARE:
101 103 113
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3

Example 3

INPUT:
m=50
n=70

OUTPUT:

THE PRIME-ADAM INTEGERS ARE:
NIL
FREQUENCY OF PRIME-ADAM INTEGERS IS: 0

Example 4

INPUT:
m=700
n=450

OUTPUT: INVALID INPUT.


Programming Code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

/**

  * The class ISC2020_Q1 inputs a lower and an upper range

  * and prints all the Prime-Adam numbers within that range

  * @author : www.guideforschool.com

  * @Program Type : BlueJ Program - Java

  * @Question Year : ISC Practical 2020 Question 1

  */

import java.util.*;

class ISC2020_Q1 //the main class

{

    boolean isPrime(int n) //to check for prime number

    {

        int c = 0;

        for(int i=1; i<=n; i++)

        {

            if(n%i == 0)

            {

                c++;

            }

        }

        if(c == 2)

            return true;

        else

            return false;

    }

     

    int reverseNum(int n) //to reverse a number

    {

        int r = 0, d = 0;

        while(n > 0)

        {

            d = n%10;

            r = r*10 + d;

            n = n/10;

        }

        return r;

    }

     

    boolean isAdam(int n) //to check for Adam number

    {

        int rev = reverseNum(n);

        int sqn = n*n;

        int sqr = rev * rev; //square of reverse

        int rsqn = reverseNum(sqn); //reverse of square

        if(rsqn == sqr)

            return true;

        else

            return false;

    }

     

    public static void main(String args[]) //the main method

    {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the lower limit : ");

        int m = sc.nextInt();

        System.out.print("Enter the upper limit : ");

        int n = sc.nextInt();

 

        ISC2020_Q1 ob = new ISC2020_Q1();

 

        if(m<1 || n<1 || m>n) //checking for invalid input

        {

            System.out.println("INVALID INPUT");

        }

        else

        {

            int c = 0;

            System.out.println("THE PRIME-ADAM INTEGERS ARE:");

            for(int i=m; i<=n; i++)

            {

                if(ob.isPrime(i) && ob.isAdam(i)) //checking for prime-adam number

                {

                    c++;

                    System.out.print(i + "\t");

                }

            }

            if(c == 0)

            System.out.print("NIL");

            System.out.println("\nFREQUENCY OF PRIME-ADAM INTEGERS IS:" + c);

        }

    }

}


Output:

Enter the lower limit : 100
Enter the upper limit : 200
THE PRIME-ADAM INTEGERS ARE:
101 103 113
FREQUENCY OF PRIME-ADAM INTEGERS IS:3

 

 

Question:

Prime-Adam integer is a positive integer (without leading zeros) which is a prirne as well as an Adam number.

Prime number : A number which has only two factors, i.e. 1 and the number itself.
Example: 2, 3, 5, 7 …etc.

Adam number: The square of a number and the square of its reverse are reverse to each other.
Exarnple: If n=13 and reverse of ‘n’ =31, then,
(13) = 169
(31) = 961 which is reverse of 169
thus 13, is an Adam number.

Accept two positive integers m and n, where m is less than n as user input. Display all Prime-Adam integers that are in the range between m and n (both inclusive) and output them along with the frequency, in the format given below:

Test your program with the following data and some random data:

Example 1

INPUT:

m=5
n=100

OUTPUT:

THE PRIME-ADAM INTEGERS ARE:
11 13 31
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3

Example 2

INPUT:
m=100
n=200

OUTPUT:

THE PRIME-ADAM INTEGERS ARE:
101 103 113
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3

Example 3

INPUT:
m=50
n=70

OUTPUT:

THE PRIME-ADAM INTEGERS ARE:
NIL
FREQUENCY OF PRIME-ADAM INTEGERS IS: 0

Example 4

INPUT:
m=700
n=450

OUTPUT: INVALID INPUT.


Programming Code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

/**

  * The class ISC2020_Q1 inputs a lower and an upper range

  * and prints all the Prime-Adam numbers within that range

  * @author : www.guideforschool.com

  * @Program Type : BlueJ Program - Java

  * @Question Year : ISC Practical 2020 Question 1

  */

import java.util.*;

class ISC2020_Q1 //the main class

{

    boolean isPrime(int n) //to check for prime number

    {

        int c = 0;

        for(int i=1; i<=n; i++)

        {

            if(n%i == 0)

            {

                c++;

            }

        }

        if(c == 2)

            return true;

        else

            return false;

    }

     

    int reverseNum(int n) //to reverse a number

    {

        int r = 0, d = 0;

        while(n > 0)

        {

            d = n%10;

            r = r*10 + d;

            n = n/10;

        }

        return r;

    }

     

    boolean isAdam(int n) //to check for Adam number

    {

        int rev = reverseNum(n);

        int sqn = n*n;

        int sqr = rev * rev; //square of reverse

        int rsqn = reverseNum(sqn); //reverse of square

        if(rsqn == sqr)

            return true;

        else

            return false;

    }

     

    public static void main(String args[]) //the main method

    {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the lower limit : ");

        int m = sc.nextInt();

        System.out.print("Enter the upper limit : ");

        int n = sc.nextInt();

 

        ISC2020_Q1 ob = new ISC2020_Q1();

 

        if(m<1 || n<1 || m>n) //checking for invalid input

        {

            System.out.println("INVALID INPUT");

        }

        else

        {

            int c = 0;

            System.out.println("THE PRIME-ADAM INTEGERS ARE:");

            for(int i=m; i<=n; i++)

            {

                if(ob.isPrime(i) && ob.isAdam(i)) //checking for prime-adam number

                {

                    c++;

                    System.out.print(i + "\t");

                }

            }

            if(c == 0)

            System.out.print("NIL");

            System.out.println("\nFREQUENCY OF PRIME-ADAM INTEGERS IS:" + c);

        }

    }

}


Output:

Enter the lower limit : 100
Enter the upper limit : 200
THE PRIME-ADAM INTEGERS ARE:
101 103 113
FREQUENCY OF PRIME-ADAM INTEGERS IS:3

 

 

 

 

 

Question:

The result of a quiz competition is to be prepared as follows:

The quiz has five questions with four multiple choices (A, B, C, D), with each question carrying 1 mark for the correct answer. Design a program to accept the number of participants N such that N must be greater than 3 and less than 11. Create a double dimensional array of size (Nx5) to store the answers of each participant row-wise.

Calculate the marks for each participant by matching the correct answer stored in a single dimensional array of size 5. Display the scores for each participant and also the participant(s) having the highest score.

Example: If the value of N = 4, then the array would be:

https://i2.wp.com/www.guideforschool.com/wp-content/uploads/2017/02/quiz-competition-result-isc-2017-java.jpg?resize=476%2C222

Note: Array entries are line fed (i.e. one entry per line)

Test your program with the sample data and some random data:

Example 1

INPUT : N = 5

Participant 1 D A B C C
Participant 2 A A D C B
Participant 3 B A C D B
Participant 4 D A D C B
Participant 5 B C A D D

Key: B C D A A

OUTPUT : Scores :

Participant 1 D A B C C
Participant 1 = 0
Participant 2 = 1
Participant 3 = 1
Participant 4 = 1
Participant 5 = 2

Highest score: Participant 5

Example 2

INPUT : N = 4

Participant 1 A C C B D
Participant 2 B C A A C
Participant 3 B C B A A
Participant 4 C C D D B

Key: A C D B B

OUTPUT : Scores :

Participant 1 = 3
Participant 2 = 1
Participant 3 = 1
Participant 4 = 3

Highest score:
Participant 1
Participant 4

Example 3

INPUT : N = 12

OUTPUT : INPUT SIZE OUT OF RANGE.

Programming Code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

/**

 * The class QuizResult_ISC2017 inputs the answers of each participant row-wise

 * and calculates the marks for each participant

 * @author : www.guideforschool.com

 * @Program Type : BlueJ Program - Java

 * @Question Year : ISC Practical 2017 Question 2

 */

 

import java.util.*;

class QuizResult_ISC2017

{

    char A[][],K[];

    int S[],n;

     

    void input()

    {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter number of participants : ");

        n = sc.nextInt();

        if(n<4 || n>10)

        {

            System.out.println("INPUT SIZE OUT OF RANGE");

            System.exit(0);

        }

        A = new char[n][5]; // Array to store the answers of every participants

        K = new char[5]; // Array to store answer key

        S = new int[n]; // Array to store score of every participant

        System.out.println("\n* Enter answer of each participant row-wise in a single line *\n");

        for(int i = 0; i<n; i++)

        {

            System.out.print("Participant "+(i+1)+" : ");

            for(int j=0; j<5; j++)

            {

                A[i][j] = sc.next().charAt(0);

            }

        }

        System.out.print("\nEnter Answer Key : ");

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

        {

            K[i] = sc.next().charAt(0);

        }

    }

 

    void CalcScore() // Function to calculate score of every participant

    {

 

        for(int i = 0; i<n; i++)

        {

            S[i] = 0;

            for(int j=0; j<5; j++)

            {

                if(A[i][j] == K[j]) // Checking if Answer of the participants match with the key or not

                {

                    S[i]++;

                }

            }

        }

    }

 

    void printScore()

    {

        int max = 0;

        System.out.println("\nSCORES : ");

        for(int i = 0; i<n; i++)

        {

            System.out.println("\tParticipant "+(i+1)+" = "+S[i]);

            if(S[i]>max)

            {

                max = S[i]; // Storing the Highest Score

            }

        }

        System.out.println();

         

        System.out.println("\tHighest Score : "+max);

         

        System.out.println("\tHighest Scorers : ");

        for(int i = 0; i<n; i++) // Printing all those participant number who got highest score

        {

            if(S[i] == max)

            {

                System.out.println("\t\t\tParticipant "+(i+1));

            }

        }

    }

 

    public static void main(String args[])

    {

        QuizResult_ISC2017 ob = new QuizResult_ISC2017();

        ob.input();

        ob.CalcScore();

        ob.printScore();

    }

}

Output:

Enter number of participants : 4

 

* Enter answer of each participant row-wise in a single line *

 

Participant 1 : A C C B D

Participant 2 : B C A A C

Participant 3 : B C B A A

Participant 4 : C C D D B

 

Enter Answer Key : A C D B B

 

SCORES :

    Participant 1 = 3

    Participant 2 = 1

    Participant 3 = 1

    Participant 4 = 3

 

    Highest Score : 3

    Highest Scorers :

    Participant 1

    Participant 4

 

 

 

 

 

 

 

 

 

 

 

 

Question:

A company manufactures packing cartons in four sizes, i.e. cartons to accommodate 6 boxes, 12 boxes, 24 boxes and 48 boxes. Design a program to accept the number of boxes to be packed (N) by the user (maximum up to 1000 boxes) and display the break-up of the cartons used in descending order of capacity (i.e. preference should be given to the highest capacity available, and if boxes left are less than 6, an extra carton of capacity 6 should be used.)

Test your program with the sample data and some random data:

Example 1

INPUT : N = 726

OUTPUT :

48 x 15 = 720
6 x 1 = 6
Remaining boxes = 0
Total number of boxes = 726
Total number of cartons = 16

Example 2

INPUT : N = 140

OUTPUT :

48 X 2 = 96
24 x 1 = 24
12 x 1 = 12
6 x 1 = 6
Remaining boxes 2 x 1 = 2
Total number of boxes = 140
Total number of cartons = 6

Example 3

INPUT : N = 4296

OUTPUT : INVALID LENGTH

Programming Code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

/**

 * The class BoxPacking_ISC2017 inputs number of boxes to be packed

 * and display the break-up of the cartons used in descending order of capacity

 * @author : www.guideforschool.com

 * @Program Type : BlueJ Program - Java

 * @Question Year : ISC Practical 2017 Question 1

 */

 

import java.util.*;

class BoxPacking_ISC2017

{

    public static void main(String args[])

    {

        Scanner sc = new Scanner(System.in);

         

        System.out.print("Enter number of boxes to be packed : ");

        int N = sc.nextInt();

        if(N<1 || N > 1000)

        {

            System.out.println("INVALID INPUT");

        }

        else

        {

            int cart[] = {48, 24, 12, 6};

            int copy = N;

            int totalCart = 0,count = 0;

            System.out.println("OUTPUT :");

            for(int i=0; i<4; i++)

            {

                count = N / cart[i];

                if(count!=0)

                {

                    System.out.println("\t"+cart[i]+"\tx\t"+count+"\t= "+cart[i]*count);

                }

                totalCart = totalCart + count;

                N = N % cart[i];

            }

            if(N>0)

            {

                System.out.println("\tRemaining Boxes   "+N+" x 1 = "+N);

                totalCart = totalCart + 1;

            }

            else

            {

                System.out.println("\tRemaining Boxes\t\t= 0");

            }

            System.out.println("\tTotal number of boxes   = "+copy);

            System.out.println("\tTotal number of cartons = "+totalCart);

        }

    }

}

Output:

Enter number of boxes to be packed : 815

OUTPUT :

    48  x   16  = 768

    24  x   1   = 24

    12  x   1   = 12

    6   x   1   = 6

    Remaining Boxes   5 x 1 = 5

    Total number of boxes   = 815

    Total number of cartons = 20

 

Question:

Caesar Cipher is an encryption technique which is implemented as ROT13 (‘rotate by 13 places’). It is a simple letter substitution cipher that replaces a letter with the letter 13 places after it in the alphabets, with the other characters remaining unchanged.

https://i0.wp.com/www.guideforschool.com/wp-content/uploads/2017/02/caeser-cipher-java-program.jpg?resize=618%2C118

Write a program to accept a plain text of length L, where L must be greater than 3 and less than 100.

Encrypt the text if valid as per the Caesar Cipher.

Test your program with the sample data and some random data:

Example 1

INPUT : Hello! How are you?

OUTPUT : The cipher text is:
Uryyb? Ubj ner lbh?

Example 2

INPUT : Encryption helps to secure data.

OUTPUT : The cipher text is:
Rapelcgvba urycf gb frpher qngn.

Example 3

INPUT : You

OUTPUT : INVALID LENGTH

Programming Code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

/**

 * The class CaesarCipher_ISC2017 inputs a sentence and encrypts it by shifting

 * every alphabet 13 places ahead in a circular fashion

 * @author : www.guideforschool.com

 * @Program Type : BlueJ Program - Java

 * @Question Year : ISC Practical 2017 Question 3

 */

import java.util.*;

class CaesarCipher_ISC2017

{

    void rot13(String w)

    {

        char ch;

        int a = 0;

        String ans = "";

        for(int i = 0; i<w.length(); i++)

        {

            ch = w.charAt(i);

            if(Character.isLetter(ch))

            {

                a = ch + 13;

                 

                if((Character.isUpperCase(ch) && a>90) || (Character.isLowerCase(ch) && a>122))

                {

                    a = a - 26;

                }

                ch = (char)a;

            }

            ans = ans + ch;

        }

        System.out.println("OUTPUT : The cipher text is :\n"+ans);

    }

     

    public static void main(String args[])

    {

        CaesarCipher_ISC2017 ob = new CaesarCipher_ISC2017();

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a sentence : ");

        String s = sc.nextLine();

        int L = s.length();

        if(L<4 || L>99)

        {

            System.out.println("INVALID LENGTH");

        }

        else

        {

            ob.rot13(s);

        }

    }

}

Output:

Enter a sentence : Encryption helps to secure data.
OUTPUT : The cipher text is :
Rapelcgvba urycf gb frpher qngn.

 

 

 

 

 

 

 

 

 

Question:

Write a program to declare a square matrix A[][] of order (M x M) where ‘M’ must be greater than 3 and less than 10. Allow the user to input positive integers into this matrix. Perform the following tasks on the matrix:

(a) Sort the boundary elements in descending order using any standard sorting technique and rearrange them in the matrix.
(b) Calculate the sum of the boundary elements.
(c) Display the original matrix, rearranged matrix and sum of the boundary elements.

Test your program with the sample data and some random data:

Example 1

INPUT :M = 4

9        2        1        5        
8        13       8        4        
15       6        3        11       
7        12       23       8        

OUTPUT:
ORIGINAL MATRIX

9        2        1        5        
8        13       8        4        
15       6        3        11       
7        12       23       8        

REARRANGED MATRIX

23       15       12       11       
1        13       8        9        
2        6        3        8        
4        5        7        8        
 
The sum of boundary elements is = 105
         

Programming Code:

/**The class SortBoundary, sorts the boundary elements of a 2-D square matrix in descending order.
 * It also finds the sum of the boundary elements 
 * @author : www.guideforschool.com
 * @Program Type : BlueJ Program - Java
 */
 
import java.util.*;
class SortBoundary
{
    int A[][], B[], m, n; 
    static int sum=0;
 
    void input() //Function for taking all the necessary inputs
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the size of the square matrix : ");
        m=sc.nextInt();
        if(m<4 || m>10)
        {
            System.out.println("Invalid Range");
            System.exit(0);
        }
        else
        {
            A = new int[m][m];
            n = m*m;
            B = new int[n]; // 1-D Array to store Boundary Elements
 
            System.out.println("Enter the elements of the Matrix : ");
            for(int i=0;i<m;i++)
            {
                for(int j=0;j<m;j++)
                {
                    System.out.print("Enter a value : ");
                    A[i][j]=sc.nextInt();
                }
            }
        }
    }
 
    /* The below function is used to store Boundary elements 
     * from array A[][] to array B[] 
     */
    void convert()
    {
        int x=0;
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(i == 0 || j == 0 || i == m-1 || j == m-1) // Condition for boundary elements
                {
                    B[x] = A[i][j];
                    x++;
                    sum = sum + A[i][j]; // Finding sum of boundary elements
                }
            }
        }
    }
 
    void sortArray() //Function for sorting Boundary elements stored in array B[]
    {
        int c = 0;
        for(int i=0; i<n-1; i++)
        {
            for(int j=i+1; j<n; j++)
            {
                if(B[i]<B[j]) // for ascending use B[i]>B[j]
                {
                    c = B[i];
                    B[i] = B[j];
                    B[j] = c;
                }
            }
        }
    }
    
    /* Function fillSpiral is filling the boundary of 2-D array in spiral
     * way from the elements of 1-D array
     */
    void fillSpiral()
    {
        int R1=0, R2=m-1, C1=0, C2=m-1, x=0;
 
        for(int i=C1;i<=C2;i++) // accessing the top row
        {
            A[R1][i]=B[x++];
        }
        for(int i =R1+1;i<=R2;i++) // accessing the right column
        {
            A[i][C2]=B[x++];
        }
        for(int i =C2-1;i>=C1;i--) // accessing the bottom row
        {
            A[R2][i]=B[x++];
        }
        for(int i =R2-1;i>=R1+1;i--) // accessing the left column
        {
            A[i][C1]=B[x++];
        }
    }
 
    void printArray() //Function for printing the array A[][]
    {
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<m;j++)
            {
                System.out.print(A[i][j]+"\t");
            }
            System.out.println();
        }
    }
 
    public static void main(String args[])
    {
        SortBoundary ob = new SortBoundary();
        ob.input();
        System.out.println("*********************");
        System.out.println("The original matrix:");
        System.out.println("*********************");
        ob.printArray(); //Printing the original array
        ob.convert(); //Storing Boundary elements to a 1-D array
        ob.sortArray(); //Sorting the 1-D array (i.e. Boundary Elements)
        ob.fillSpiral(); //Storing the sorted Boundary elements back to original 2-D array
 
        System.out.println("*********************");
        System.out.println("The Rearranged matrix:");
        System.out.println("*********************");
        ob.printArray(); //Printing the rearranged array
        System.out.println("*********************");
        System.out.println("The sum of boundary elements is = "+sum); //Printing the sum of boundary elements
    }
}

See:

How to access Boundary Elements.

How to fill in a spiral format.

Output:

Enter the size of the square matrix : 4
Enter the elements of the Matrix : 
Enter a value : 9
Enter a value : 2
Enter a value : 1
Enter a value : 5
Enter a value : 8
Enter a value : 13
Enter a value : 8
Enter a value : 4
Enter a value : 15
Enter a value : 6
Enter a value : 3
Enter a value : 11
Enter a value : 7
Enter a value : 12
Enter a value : 23
Enter a value : 8
*********************
The original matrix:
*********************
9        2        1        5        
8        13       8        4        
15       6        3        11       
7        12       23       8        
*********************
The Rearranged matrix:
*********************
23       15       12       11       
1        13       8        9        
2        6        3        8        
4        5        7        8        
*********************
The sum of boundary elements is = 105
                 

Question:

Write a program to accept a sentence which may be terminated by either’.’, ‘?’or’!’ only. The words may be separated by more than one blank space and are in UPPER CASE.

Perform the following tasks:

(a) Find the number of words beginning and ending with a vowel.

(b) Place the words which begin and end with a vowel at the beginning, followed by the remaining words as they occur in the sentence.

Test your program with the sample data and some random data:

Example 1

INPUT: ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE.

OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL= 3

ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL

Example 2

INPUT: YOU MUST AIM TO BE A BETTER PERSON TOMORROW THAN YOU ARE TODAY.

OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL= 2

A ARE YOU MUST AIM TO BE BETTER PERSON TOMORROW THAN YOU TODAY

Example 3

INPUT: LOOK BEFORE YOU LEAP.

OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL= 0

LOOK BEFORE YOU LEAP

Example 4

INPUT: HOW ARE YOU@

OUTPUT: INVALID INPUT

Programming Code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

/**

 * The class ISC2016_Q3 inputs a sentence, and prints and counts the words

 * beginning and ending with a vowel, before other words

 * @author : www.guideforschool.com

 * @Program Type : BlueJ Program - Java

 * @Question Year : ISC Practical 2016 Question 3

 */

 

import java.util.*;

class ISC2016_Q3

{

    boolean isVowel(String w) // Function to check if a word begins and ends with a vowel or not

    {

        int l = w.length();

        char ch1 = w.charAt(0); // Storing the first character

        char ch2 = w.charAt(l-1); // Storing the last character

        if((ch1=='A' || ch1=='E' || ch1=='I' || ch1=='O' || ch1=='U') &&

        (ch2=='A' || ch2=='E' || ch2=='I' || ch2=='O' || ch2=='U'))

        {

            return true;

        }

        else

        {

            return false;

        }

    }

 

    public static void main(String args[])

    {

        ISC2016_Q3 ob = new ISC2016_Q3();

        Scanner sc = new Scanner(System.in);

 

        System.out.print("Enter a sentence : ");

        String s = sc.nextLine();

        s = s.toUpperCase();

        int l = s.length();

        char last = s.charAt(l-1); // Extracting the last character

 

        /* Checking whether the sentence ends with '.' or '?' or not */

        if(last != '.' && last != '?' && last != '!')

        {

            System.out.println("Invalid Input. End a sentence with either '.', '?' or '!' only");

        }

        else

        {

            StringTokenizer str = new StringTokenizer(s," .?!");

            int x = str.countTokens();

            int c = 0;

            String w = "", a = "", b = "";

 

            for(int i=1; i<=x; i++)

            {

                w = str.nextToken(); // Extracting words and saving them in w

 

                if(ob.isVowel(w))

                {

                    c++; // Counting all words beginning and ending with a vowel

                    a = a + w + " "; // Saving all words beginning and ending with a vowel in variable 'a'

                }

                else

                    b = b + w + " "; // Saving all other words in variable 'b'  

            }

            System.out.println("OUTPUT : \nNUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = " + c);

            System.out.println(a+b);

 

        }

    }

}

Output:

Enter a sentence : ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE.
OUTPUT :
NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 3
ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL

 

 

 

 

 

 

 

Question:

Write a program to declare a square matrix A[][] of order (M x M) where ‘M’ must be greater than 3 and less than 10. Allow the user to input positive integers into this matrix. Perform the following tasks on the matrix:

(a) Sort the non-boundary elements in ascending order using any standard sorting technique and rearrange them in the matrix.
(b) Calculate the sum of both the diagonals.
(c) Display the original matrix, rearranged matrix and only the diagonal elements of the rearranged matrix with their sum.

Test your program with the sample data and some random data:

Example 1

INPUT :M = 4

9   2   1   5

8   13  8   4

15  6   3   11

7   12  23  8

OUTPUT:

ORIGINAL MATRIX

9   2   1   5

8   13  8   4

15  6   3   11

7   12  23  8

REARRANGED MATRIX

9   2   1   5

8   3   6   4

15  8   13  11

7   12  23  8

DIAGONAL ELEMENTS

9           5

    3   6

    8   13

7           8

SUM OF THE DIAGONAL ELEMENTS = 59

 

Programming Code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

/**

* The class SortNonBoundary_ISC2016 inputs a square matrix and

* sorts the non-boundary elements in ascending order

* @author : www.guideforschool.com

* @Program Type : BlueJ Program - Java

* @Question Year : ISC Practical 2016 Question 2

*/

 

import java.util.*;

class SortNonBoundary_ISC2016

{

    int A[][],B[],m,n;

 

    void input() //Function for taking all the necessary inputs

    {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the size of the square matrix : ");

        m=sc.nextInt();

        if(m<4 || m>10)

        {

            System.out.println("Invalid Range");

            System.exit(0);

        }

        else

        {

            A = new int[m][m];

            n = (m-2)*(m-2);

            B = new int[n]; //Array to store Non-Boundary Elements

 

            System.out.println("Enter the elements of the Matrix : ");

            for(int i=0;i<m;i++)

            {

                for(int j=0;j<m;j++)

                {

                    System.out.print("Enter a value : ");

                    A[i][j]=sc.nextInt();

                }

            }

        }

    }

 

    /* The below function stores Non-Boundary elements

     * from array A[][] to array B[] if s = 1

     * else stores the Non-Boundary elements in array A[][] from array B[]

     */

    void convert(int s)

    {

        int x=0;

        for(int i=0;i<m;i++)

        {

            for(int j=0;j<m;j++)

            {

                if(i != 0 && j != 0 && i != m-1 && j != m-1)

                {

                    if(s==1)

                        B[x] = A[i][j];

                    else

                        A[i][j] = B[x];

                    x++;

                }

            }

        }

    }

 

    void sortArray() //Function for sorting Non-Boundary elements stored in array B[]

    {

        int c = 0;

        for(int i=0; i<n-1; i++)

        {

            for(int j=i+1; j<n; j++)

            {

                if(B[i]>B[j])

                {

                    c = B[i];

                    B[i] = B[j];

                    B[j] = c;

                }

            }

        }

    }

 

    void printArray() //Function for printing the array A[][]

    {

        for(int i=0;i<m;i++)

        {

            for(int j=0;j<m;j++)

            {

                System.out.print(A[i][j]+"\t");

            }

            System.out.println();

        }

    }

 

    void printDiagonal() //Function for printing the diagonal elements and their sum

    {

        int sum = 0;

        for(int i=0;i<m;i++)

        {

            for(int j=0;j<m;j++)

            {

                if(i==j || (i+j)==m-1)

                {

                    System.out.print(A[i][j]+"\t");

                    sum = sum + A[i][j];

                }

                else

                    System.out.print("\t");

            }

            System.out.println();

        }

        System.out.println("Sum of the Diagonal Elements : "+sum);

    }

 

    public static void main(String args[])

    {

        SortNonBoundary_ISC2016 ob = new SortNonBoundary_ISC2016();

        ob.input();

        System.out.println("*********************");

        System.out.println("The original matrix:");

        System.out.println("*********************");

        ob.printArray(); //Printing the original array

        ob.convert(1); //Storing Non-Boundary elements to a 1-D array

        ob.sortArray(); //Sorting the 1-D array (i.e. Non-Diagonal Elements)

        ob.convert(2); //Storing the sorted Non-Boundary elements back to original 2-D array

 

        System.out.println("*********************");

        System.out.println("The Rearranged matrix:");

        System.out.println("*********************");

        ob.printArray(); //Printing the rearranged array

        System.out.println("*********************");

        System.out.println("The Diagonal Elements:");

        System.out.println("*********************");

        ob.printDiagonal(); //Printing the diagonal elements and their sum

    }

}

 

Output:

Enter the size of the square matrix : 4

Enter the elements of the Matrix :

Enter a value : 9

Enter a value : 2

Enter a value : 1

Enter a value : 5

Enter a value : 8

Enter a value : 13

Enter a value : 8

Enter a value : 4

Enter a value : 15

Enter a value : 6

Enter a value : 3

Enter a value : 11

Enter a value : 7

Enter a value : 12

Enter a value : 23

Enter a value : 8

*********************

The original matrix:

*********************

9   2   1   5

8   13  8   4

15  6   3   11

7   12  23  8

*********************

The Rearranged matrix:

*********************

9   2   1   5

8   3   6   4

15  8   13  11

7   12  23  8

*********************

The Diagonal Elements:

*********************

9           5

    3   6

    8   13

7           8

Sum of the Diagonal Elements : 59

 

 

Question:

Circular Prime is a prime number that remains prime under cyclic shifts of its digits. When the leftmost digit is removed and replaced at the end of the remaining string of digits, the generated number is still prime. The process is repeated until the original number is reached again.
A number is said to be prime if it has only two factors I and itself.

Example:
131
311
113
Hence, 131 is a circular prime.

Test your program with the sample data and some random data:

Example 1

INPUT :N = 197
OUTPUT:
197
971
719
197 IS A CIRCULAR PRIME

Example 2

INPUT :N = 1193
OUTPUT:
1193
1931
9311
3119
1193 IS A CIRCULAR PRIME

Example 3

INPUT :N = 29
OUTPUT:
29
92
29 IS NOT A CIRCULAR PRIME

Programming Code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

/**

* The class CircularPrime_Q1_ISC2016 inputs a number and

* checks whether it is a Circular Prime or not

* @author : www.guideforschool.com

* @Program Type : BlueJ Program - Java

* @Question Year : ISC Practical 2016 Question 1

*/

 

import java.util.*;

class CircularPrime_Q1_ISC2016

{

    boolean isPrime(int n) // Function for checking whether a number is prime or not

    {

        int c = 0;

        for(int i = 1; i<=n; i++)

        {

            if(n%i == 0)

                c++;

        }

        if(c == 2)

            return true;

        else

            return false;

    }

     

    int circulate(int n) //Function for circulating the digits to form new number

    {

        String s = Integer.toString(n);

        String p = s.substring(1)+s.charAt(0);

        int a = Integer.parseInt(p);

        return a;

    }

     

    void isCircularPrime(int n) //Function to check for circular prime

    {

        int f = 0,a = n;

        do

        {

            System.out.println(a);

            if(isPrime(a)==false)

            {

                f = 1;

                break;

            }

            a = circulate(a);

        }while(a!=n);

         

        if(f==1)

            System.out.println(n+" IS NOT A CIRCULAR PRIME");

        else

            System.out.println(n+" IS A CIRCULAR PRIME");

    }

     

    public static void main(String args[])

    {

        CircularPrime_Q1_ISC2016 ob = new CircularPrime_Q1_ISC2016();

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number : ");

        int n = sc.nextInt();

        ob.isCircularPrime(n);

    }

}

Output:

Enter a number : 87
87
87 IS NOT A CIRCULAR PRIME

Enter a number : 1193
1193
1931
9311
3119
1193 IS A CIRCULAR PRIME

Enter a number : 123
123
231
312
123 IS NOT A CIRCULAR PRIME

 

 

 

Question:

Write a program to accept the year, month and the weekday name of the 1st day of that month and generate its calendar.

Example :

INPUT :
Year : 2016
Month : February
1st day of February : Monday

OUTPUT :

---------------------------------

           February 2016

---------------------------------

SUN MON TUE WED THU FRI SAT

---------------------------------

     1   2   3   4   5   6

---------------------------------

 7   8   9   10  11  12  13

---------------------------------

 14  15  16  17  18  19  20

---------------------------------

 21  22  23  24  25  26  27

---------------------------------

 28  29                 

---------------------------------

Programming Code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

/**

* The class CalendarProgram inputs a year, month and the weekday name

* of the 1st day of that month and generates its calendar

* @author : www.guideforschool.com

* @Program Type : BlueJ Program - Java

*/

 

import java.util.*;

class CalendarProgram

{

    //Function to match the given month and return its maximum days

    int findMaxDay(String mname, int y)

    {

        String months[] = {"","January", "February", "March", "April", "May", "June",

                            "July", "August", "September", "October", "November", "December"};

        int D[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

         

        if((y%400==0) || ((y%100!=0)&&(y%4==0)))

        {

            D[2]=29;

        }

        int max = 0;

        for(int i=1; i<=12; i++)

        {

            if(mname.equalsIgnoreCase(months[i]))

            {

                max = D[i];  //Saving maximum day of given month

            }

        }

        return max;

    }

     

    //Function to match the given weekday name and return its weekday no.

    int findDayNo(String wname)

    {

        String days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",

                            "Saturday"};

        int f = 0;

        for(int i=0; i<7; i++)

        {

            if(wname.equalsIgnoreCase(days[i]))

            {

                f = i;  //Saving week day no. given day (e.g. '0' for Sunday)

            }

        }

        return f;

    }

     

    //Function for creating the calendar

    void fillCalendar(int max, int f, String mname, int y)

    {

        int A[][] = new int[6][7];

        int x = 1, z = f;

     

        for(int i=0;i<6;i++)

        {

            for(int j=f; j<7; j++)

            {

                if(x<=max)

                {

                    A[i][j] = x;

                    x++;

                }

            }

            f = 0;

        }

         

        for(int j=0; j<z; j++) //Adjustment to bring last (6th) row elements to first row

        {

            A[0][j]=A[5][j];

        }   

         

        printCalendar(A, mname, y); //Calling function to print the calendar

    }

     

    //Function for printing the calendar

    void printCalendar(int A[][], String mname, int y)

    

        System.out.println("\n\t----------------------------------------------------");

        System.out.println("\t\t\t   "+mname+" "+y);

        System.out.println("\t----------------------------------------------------");

        System.out.println("\tSUN\tMON\tTUE\tWED\tTHU\tFRI\tSAT");

        System.out.println("\t----------------------------------------------------");

         

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

        {

            for(int j = 0; j < 7; j++)

            {

                if(A[i][j]!=0)

                    System.out.print("\t "+A[i][j]);

                else

                    System.out.print("\t ");

            }

            System.out.println("\n\t----------------------------------------------------");

        }

    }

     

    public static void main(String args[])

    {

        CalendarProgram ob = new CalendarProgram();

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the year : ");

        int y = sc.nextInt();

        System.out.print("Enter the month name (e.g. January) : ");

        String mname = sc.next();

        System.out.print("Enter the week day name (e.g. Sunday) of 1st day of "+mname+" : ");

        String wname = sc.next();

         

        int max = ob.findMaxDay(mname,y);

        int f = ob.findDayNo(wname);       

        ob.fillCalendar(max,f,mname,y);

    }

}

Output:

Enter the year : 2016

Enter the month name (e.g. January) : October

Enter the week day name (e.g. Sunday) of 1st day of October : Saturday

 

    ---------------------------------

               October 2016

    ---------------------------------

    SUN MON TUE WED THU FRI SAT

    ---------------------------------

     30  31                  1

    ---------------------------------

     2   3   4   5   6   7   8

    ---------------------------------

     9   10  11  12  13  14  15

    ---------------------------------

     16  17  18  19  20  21  22

    ---------------------------------

     23  24  25  26  27  28  29

    ---------------------------------

 

 

 

 

Question:

Accept a paragraph of text consisting of sentences that are terminated by either ‘.’ (full stop), ‘!’ (exclamation mark) or a ‘?’ (question mark). Assume that there can be maximum 10 sentences in a paragraph. Write a program to arrange the sentences in increasing order of their number of words.

Example :
INPUT : Please come and attend the party. Hello! How are you?
OUTPUT :
Hello = 1
How are you = 3
Please come and attend the party = 6

Programming Code:

/**
* The class sortParagraph inputs a paragraph and arranges the 
* sentences in ascending order of their number of words
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
 
import java.util.*;
class sortParagraph
{
    // Function to count no. of words in every sentence
    int countWords(String s)
    {
        StringTokenizer str = new StringTokenizer(s," .,?!");
        int c = str.countTokens();
        return c;
    } 
    
    // Function to sort the sentences in ascending order of their no. of words
    void sort(String w[], int p[]) 
    {
        int n = w.length, t1 = 0;
        String t2 = "";
        
        for(int i=0; i<n-1; i++)
        {
            for(int j=i+1; j<n; j++)
            {
                if(p[i]>p[j]) // for descending use p[i]<p[j]
                {
                    t1 = p[i];
                    p[i] = p[j];
                    p[j] = t1;
                    t2 = w[i];
                    w[i] = w[j];
                    w[j] = t2;
                }
            }
        }
        printResult(w,p);    // Calling function for printing the result
    }
    
    void printResult(String w[], int p[]) // Function to print the final result
    {
        int n = w.length;
        for(int i=0; i<n; i++)
        {
            System.out.println(w[i]+"\t=\t"+p[i]);
        }
    }
    
    public static void main(String args[])
    {
        sortParagraph ob = new sortParagraph();
        Scanner sc = new Scanner(System.in);
        
        System.out.print("Enter a paragraph : "); //Inputting a paragraph
        String pg = sc.nextLine();
        
        StringTokenizer str = new StringTokenizer(pg,".?!");
        int count = str.countTokens(); //Counting no. of sentences in the paragraph
        if(count > 10)
            System.out.println("A maximum of 10 sentences are allowed in the paragraph");
        else
        {
            String sent[] = new String[count]; //Array to store the sentences separately
            int p[] = new int[count]; //Array to store no. of words of each sentence
            
            for(int i=0; i<count; i++)
            {
                sent[i] = str.nextToken().trim(); // Saving sentences one by one in an array
                p[i] = ob.countWords(sent[i]); // Saving no. of words of every sentence
            }
            ob.sort(sent,p);
        }
    }
}

Output:

Enter a paragraph : Please come and attend the party. Hello! How are you?
OUTPUT :
Hello = 1
How are you = 3
Please come and attend the party = 6

 

 

 

 

 

 

 

 

 

 

Question:

Write a Program in Java to input a number and check whether it is a Bouncy Number or not.

Increasing Number : Working from left-to-right if no digit is exceeded by the digit to its left it is called an increasing number; for example, 22344.
Decreasing Number : Similarly if no digit is exceeded by the digit to its right it is called a decreasing number; for example, 774410.
Bouncy Number : We shall call a positive integer that is neither increasing nor decreasing a “bouncy” number; for example, 155349. Clearly there cannot be any bouncy numbers below 100.

Solution:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

/**

* The class BouncyNumber inputs a number and checks whether it is a Bouncy Number or not

* @author : www.guideforschool.com

* @Program Type : BlueJ Program - Java

*/

 

import java.util.*;

class BouncyNumber

{

    boolean isIncreasing(int n) //Function to check whether a number is Increasing

    {

        String s = Integer.toString(n);

        char ch;

        int f = 0;

        for(int i=0; i<s.length()-1; i++)

        {

            ch = s.charAt(i);

            if(ch>s.charAt(i+1))// If any digit is more than next digit then we have to stop checking

            {

                f = 1;

                break;

            }

        }

        if(f==1)

            return false;

        else

            return true;

    }

     

    boolean isDecreasing(int n) //Function to check whether a number is Decreasing

    {

        String s = Integer.toString(n);

        char ch;

        int f = 0;

        for(int i=0; i<s.length()-1; i++)

        {

            ch = s.charAt(i);

            if(ch<s.charAt(i+1))// If any digit is less than next digit then we have to stop checking

            {

                f = 1;

                break;

            }

        }

        if(f==1)

            return false;

        else

            return true;

    }

     

    void isBouncy(int n)

    {

        if(isIncreasing(n)==true)

            System.out.println("The number " + n + " is Increasing and Not Bouncy");

        else if(isDecreasing(n)==true)

            System.out.println("The number " + n + " is Decreasing and Not Bouncy");

        else

            System.out.println("The number " + n + " is bouncy");

    }

     

    public static void main(String args[])

    {

        Scanner sc = new Scanner(System.in);

        BouncyNumber ob = new BouncyNumber();

        System.out.print("Enter a number : ");

        int n = sc.nextInt();

        ob.isBouncy(n);

    }

}

Output:

Enter a number : 22344
The number 22344 is Increasing and Not Bouncy
Enter a number : 774410
The number 774410 is Decreasing and Not Bouncy
Enter a number : 155349
The number 155349 is bouncy

Question:

Write a Program in Java to input a number and check whether it is an Evil Number or not.

Evil Number : An Evil number is a positive whole number which has even number of 1’s in its binary equivalent.

Example: Binary equivalent of 9 is 1001, which contains even number of 1’s.
A few evil numbers are 3, 5, 6, 9….

Design a program to accept a positive whole number and find the binary equivalent of the number and count the number of 1’s in it and display whether it is a Evil number or not with an appropriate message. Output the result in format given below:

Example 1
INPUT : 15
BINARY EQUIVALENT : 1111
NO. OF 1’s : 4
OUTPUT : EVIL NUMBER

Example 2
INPUT : 26
BINARY EQUIVALENT : 11010
NO. OF 1’s : 3
OUTPUT : NOT AN EVIL NUMBER

Programming Code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

/**

* The class EvilNumber inputs a number and checks whether it is an Evil Number or not

* @author : www.guideforschool.com

* @Program Type : BlueJ Program - Java

* @ISC Computer Science Practical Specimen Paper - Question 1

*/

import java.util.*;

class EvilNumber

{

    String toBinary(int n) // Function to convert a number to Binary

    {

        int r;

        String s=""; //variable for storing the result

  

        char dig[]={'0','1'}; //array storing the digits (as characters) in a binary number system

  

        while(n>0)

            {

                r=n%2; //finding remainder by dividing the number by 2

                s=dig[r]+s; //adding the remainder to the result and reversing at the same time

                n=n/2;

            }

        return s;

    }

     

    int countOne(String s) // Function to count no of 1's in binary number

    {

        int c = 0, l = s.length();

        char ch;

        for(int i=0; i<l; i++)

        {

            ch=s.charAt(i);

            if(ch=='1')

            {

                c++;

            }

        }

        return c;

    }

     

    public static void main(String args[])

    {

        EvilNumber ob = new EvilNumber();

        Scanner sc = new Scanner(System.in);

         

        System.out.print("Enter a positive number : ");

        int n = sc.nextInt();

         

        String bin = ob.toBinary(n);

        System.out.println("Binary Equivalent = "+bin);

         

        int x = ob.countOne(bin);

        System.out.println("Number of Ones = "+x);

         

        if(x%2==0)

            System.out.println(n+" is an Evil Number.");

        else

            System.out.println(n+" is Not an Evil Number.");

    }

}

Output:

Enter a positive number : 26

Binary Equivalent = 11010

Number of Ones = 3

26 is Not an Evil Number.

 

Enter a positive number : 420

Binary Equivalent = 110100100

Number of Ones = 4

420 is an Evil Number.

 

Enter a positive number : 659

Binary Equivalent = 1010010011

Number of Ones = 5

659 is Not an Evil Number.

Question:

A class Admission contain the admission numbers of 100 students. Some of the data members/ member functions are given below:

Class name: Admission

Data member/instance variable:

Adno[ ]: Integer array to store admission numbers

Member functions/methods:

Admission(): constructur to initialize the array elements
void fillArray(): to accept the element of the array in ascending order
int binSearch(int l, int u, int v): to search for a particular admission number(v) using binary search and recursive technique and return 1 if found otherwise returns -1

Specify the class Admission giving details of the constructorvoid fillArrray() and int binSearch(int, int, int). Define the main() function to create an object and call the functions accordingly to enable task.

Programming Code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

/**

 * The class Admission searches for an Admission number using Binary Search

 * @author : www.guideforschool.com

 * @Program Type : BlueJ Program - Java

 * @Question Year : ISC 2015 (Theory) Question 8

 */

 

import java.util.*;

class Admission

{

    int Adno[]=new int[100];

    static Scanner sc = new Scanner(System.in);

 

    Admission() // Default constructor

    {

        for(int i=0; i<100; i++)

        {

            Adno[i]=0;

        }

    }

     

    void fillArray()throws Exception // Function to accept elements in ascending order

    {

        for(int i=0; i<100; i++)

        {

            System.out.print("Enter Admission no of student "+(i+1)+": ");

            Adno[i] = sc.nextInt();

        }

         

        /*Sorting the array in ascending order */

         

        int temp=0;

        for(int i=0; i<99; i++)

        {

            for(int j=i+1; j<100; j++)

            {

                if(Adno[i]>Adno[j])

                {

                    temp = Adno[i];

                    Adno[i] = Adno[j];

                    Adno[j] = temp;

                }

            }

        }           

    }

     

    int binSearch(int l, int u, int v) // Recursive function implementing binary search

    {

        int mid = (l + u)/2;

        if(u < l) // condition if the search is unsuccessful

        {

            return -1;

        }

        if(v==Adno[mid]) // condition if the search is successful

        {

            return 1;

        }

        else if(v>Adno[mid])

        {

            return binSearch(mid+1,u,v);

        }

        else

        {

            return binSearch(l,mid-1,v);

        }

    }

     

    public static void main(String args[])throws Exception

    {

        Admission ob = new Admission();

        System.out.println("Enter Admission number in ascending order");

        ob.fillArray();

        System.out.print("Enter an Admission number to search : ");

        int v = sc.nextInt();

        int f = ob.binSearch(0,99,v);

        System.out.println("*****************************");

        if(f == 1)

        {

            System.out.println("Admission Number found");

        }

        else

        {

            System.out.println("Admission Number Not found");

        }

    }

}

Output:

Note: The output has been taken for 5 elements in the array

Enter Admission number in ascending order
Enter Admission no of student 1: 205
Enter Admission no of student 2: 310
Enter Admission no of student 3: 670
Enter Admission no of student 4: 887
Enter Admission no of student 5: 952
Enter an Admission number to search : 887
*****************************
Admission Number found

Question:

Write a Program in Java to input a 2-D square matrix and check whether it is a Lower Triangular Matrix or not.

Lower Triangular Matrix : A Lower Triangular matrix is a square matrix in which all the entries above the main diagonal () are zero. The entries below or on the main diagonal themselves may or may not be zero.

Example:

5   0   0   0  

3   1   0   0  

4   9   4   0  

6   8   7   2  


Solution:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

/**

 * The class LowerTriangularMatrix inputs a Matrix and checks whether it is a Lower Triangular Matrix or not

 * @author : www.guideforschool.com

 * @Program Type : BlueJ Program - Java

 */

 

import java.util.*;

class LowerTriangularMatrix

{

    public static void main(String args[])throws Exception

    {

        Scanner sc=new Scanner(System.in);

        System.out.print("Enter the size of the matrix : ");

        int m=sc.nextInt();

        int A[][]=new int[m][m];

 

        /* Inputting the matrix */

        for(int i=0;i<m;i++)

        {

            for(int j=0;j<m;j++)

            {

                System.out.print("Enter an element : ");

                A[i][j]=sc.nextInt();

            }

        }

        // printing the matrix

        System.out.println("*************************");

        System.out.println("The Matrix Is : ");

        for(int i=0;i<m;i++)

        {

            for(int j=0;j<m;j++)

            {

                System.out.print(A[i][j]+"\t");

            }

            System.out.println();

        }

        System.out.println("*************************");

        //Checking for lower triangular matrix

        int p=0;

        for(int i=0;i<m;i++)

        {

            for(int j=i+1;j<m;j++)

            {

                if(A[i][j]!=0) //all elements above diagonal must be zero if any element is not zero then we stop

                {

                    p = 1;

                    break;

                }

            }

        }

 

        if(p == 0)

        {

            System.out.println("The Matrix is Lower Triangular");

        }

        else

        {

            System.out.println("The Matrix is NOT Lower Triangular");

        }

    }

}


Output:

Enter the size of the matrix : 4

Enter an element : 5

Enter an element : 0

Enter an element : 0

Enter an element : 0

Enter an element : 3

Enter an element : 1

Enter an element : 0

Enter an element : 0

Enter an element : 4

Enter an element : 9

Enter an element : 4

Enter an element : 0

Enter an element : 6

Enter an element : 8

Enter an element : 7

Enter an element : 2

*************************

The Matrix Is :

5   0   0   0  

3   1   0   0  

4   9   4   0  

6   8   7   2  

*************************

The Matrix is Lower Triangular

Question:

Write a Program in Java to input a 2-D square matrix and check whether it is an Upper Triangular Matrix or not.

Upper Triangular Matrix : An Upper Triangular matrix is a square matrix in which all the entries below the main diagonal () are zero. The entries above or on the main diagonal themselves may or may not be zero.

Example:

5 3 0 7

0 1 9 8

0 0 4 6

0 0 0 2


Solution:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

/**

 * The class UpperTriangularMatrix inputs a Matrix and checks whether it is a Lower Triangular Matrix or not

 * @author : www.guideforschool.com

 * @Program Type : BlueJ Program - Java

 */

 

import java.util.*;

class UpperTriangularMatrix

{

    public static void main(String args[])throws Exception

    {

        Scanner sc=new Scanner(System.in);

        System.out.print("Enter the size of the matrix : ");

        int m=sc.nextInt();

        int A[][]=new int[m][m];

 

        /* Inputting the matrix */

        for(int i=0;i<m;i++)

        {

            for(int j=0;j<m;j++)

            {

                System.out.print("Enter an element : ");

                A[i][j]=sc.nextInt();

            }

        }

        // printing the matrix

        System.out.println("*************************");

        System.out.println("The Matrix Is : ");

        for(int i=0;i<m;i++)

        {

            for(int j=0;j<m;j++)

            {

                System.out.print(A[i][j]+"\t");

            }

            System.out.println();

        }

        System.out.println("*************************");

        //Checking for upper triangular matrix

        int p=0;

        for(int i=0;i<m;i++)

        {

            for(int j=0;j<i;j++)

            {

                if(A[i][j]!=0) //all elements below main diagonal must be zero if not then we stop

                {

                    p = 1;

                    break;

                }

            }

        }

 

        if(p == 0)

        {

            System.out.println("The Matrix is Upper Triangular");

        }

        else

        {

            System.out.println("The Matrix is NOT Upper Triangular");

        }

    }

}


Output:

Enter the size of the matrix : 4

Enter an element : 5

Enter an element : 3

Enter an element : 0

Enter an element : 7

Enter an element : 0

Enter an element : 1

Enter an element : 9

Enter an element : 8

Enter an element : 0

Enter an element : 0

Enter an element : 4

Enter an element : 6

Enter an element : 0

Enter an element : 0

Enter an element : 0

Enter an element : 2

*************************

The Matrix is :

5 3 0 7

0 1 9 8

0 0 4 6

0 0 0 2

*************************

The matrix is Upper Triangular 

Question:

Write a Program in Java to input a 2-D square matrix and check whether it is a Scalar Matrix or not.

Scalar Matrix : A scalar matrix is a diagonal matrix in which the main diagonal () entries are all equal.

See : Java program to check for Diagonal Matrix

Example:

5 0 0 0

0 5 0 0

0 0 5 0

0 0 0 5


Solution:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

/**

* The class ScalarMatrix inputs a Matrix and checks whether it is a scalar matrix or not

* @author : www.guideforschool.com

* @Program Type : BlueJ Program - Java

*/

  

import java.util.*;

class ScalarMatrix

{

    public static void main(String args[])throws Exception

    {

        Scanner sc=new Scanner(System.in);

        System.out.print("Enter the size of the matrix : ");

        int m=sc.nextInt();

        int A[][]=new int[m][m];

          

        /* Inputting the matrix */

        for(int i=0;i<m;i++)

        {

            for(int j=0;j<m;j++)

            {

                System.out.print("Enter an element : ");

                A[i][j]=sc.nextInt();

            }

        }

  

        /* Printing the matrix */

        System.out.println("*************************");

        System.out.println("The Matrix is : ");

        for(int i=0;i<m;i++)

        {

            for(int j=0;j<m;j++)

            {

                System.out.print(A[i][j]+"\t");

            }

            System.out.println();

        }

        System.out.println("*************************");

          

        int p = 0, q = 0, x = A[0][0]; // 'x' is storing the 1st main diagonal element

          

        for(int i=0;i<m;i++)

        {

            for(int j=0;j<m;j++)

            {

                /* Checking that the matrix is diagonal or not */

                if(i!=j && A[i][j]!=0) // All non-diagonal elements must be zero

                {

                    p=1;

                    break;

                }

                /* Checking the matrix for scalarity */

                // All main diagonal elements must be equal to 'x' and non-zero

                if(i==j && (A[i][j]==0 || A[i][j]!=x))

                {

                    q=1;

                    break;

                }

            }

        }

          

        if(p==0 && q==0)

            System.out.println("The matrix is scalar");

        else

            System.out.println("The matrix is not scalar");

    }

}


Output:

Enter the size of the matrix : 4

Enter an element : 5

Enter an element : 0

Enter an element : 0

Enter an element : 0

Enter an element : 0

Enter an element : 5

Enter an element : 0

Enter an element : 0

Enter an element : 0

Enter an element : 0

Enter an element : 5

Enter an element : 0

Enter an element : 0

Enter an element : 0

Enter an element : 0

Enter an element : 5

*************************

The Matrix is :

5 0 0 0

0 5 0 0

0 0 5 0

0 0 0 5

*************************

The matrix is Scalar

Question:

Write a Program in Java to input a 2-D square matrix and check whether it is a Diagonal Matrix or not.

Diagonal Matrix : A diagonal matrix is a matrix (usually a square matrix) in which the entries outside the main diagonal () are all zero. The diagonal entries themselves may or may not be zero (but all diagonal entries cannot be zero).

Example:

5 0 0 0

0 1 0 0

0 0 0 0

0 0 0 7


Solution:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

/**

* The class DiagonalMatrix inputs a Matrix and checks whether it is a diagonal matrix or not

* @author : www.guideforschool.com

* @Program Type : BlueJ Program - Java

*/

  

import java.util.*;

class DiagonalMatrix

{

    public static void main(String args[])throws Exception

    {

        Scanner sc=new Scanner(System.in);

        System.out.print("Enter the size of the matrix : ");

        int m=sc.nextInt();

        int A[][]=new int[m][m];

          

        /* Inputting the matrix */

        for(int i=0;i<m;i++)

        {

            for(int j=0;j<m;j++)

            {

                System.out.print("Enter an element : ");

                A[i][j]=sc.nextInt();

            }

        }

  

        /* Printing the matrix */

        System.out.println("*************************");

        System.out.println("The Matrix is : ");

        for(int i=0;i<m;i++)

        {

            for(int j=0;j<m;j++)

            {

                System.out.print(A[i][j]+"\t");

            }

            System.out.println();

        }

        System.out.println("*************************");

  

        int p=0, q=0;

          

        for(int i=0;i<m;i++)

        {

            for(int j=0;j<m;j++)

            {

                if(i!=j && A[i][j]!=0) // Checking non-diagonal elements

                {

                    p=1;

                    break;

                }

                if(i==j && A[i][j]==0) // Checking diagonal elements

                {

                    q++;

                }

            }

        }

          

        if(p==0 && q<m)

            System.out.println("The matrix is Diagonal");

        else

            System.out.println("The matrix is not Diagonal");

    }

}


Output:

Enter the size of the matrix : 4

Enter an element : 5

Enter an element : 0

Enter an element : 0

Enter an element : 0

Enter an element : 0

Enter an element : 1

Enter an element : 0

Enter an element : 0

Enter an element : 0

Enter an element : 0

Enter an element : 0

Enter an element : 0

Enter an element : 0

Enter an element : 0

Enter an element : 0

Enter an element : 7

*************************

The Matrix is :

5 0 0 0

0 1 0 0

0 0 0 0

0 0 0 7

*************************

The matrix is Diagonal

Question:

Write a Program in Java to input two 2-D arrays and perform Matrix Multiplication:

Illustration:

matrix multiplication java program


Solution:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

/**

 * The class MatrixMultiplication inputs 2 Matrices and performs Matrix Multiplication on them

 * @author : www.guideforschool.com

 * @Program Type : BlueJ Program - Java

 */

import java.util.*;

class MatrixMultiplication

{

    void printMatrix(int P[][], int r, int c) // Funtion for printing an array

    {

        for(int i=0; i<r; i++)

        {

            for(int j=0; j<c; j++)

            {

                System.out.print(P[i][j]+"\t");

            }

            System.out.println();

        }

    }

 

    public static void main(String args[])

    {    

        MatrixMultiplication ob = new MatrixMultiplication();

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter no of rows for 1st array : "); //Inputting the rows of array A

        int r1=sc.nextInt();

        System.out.print("Enter no of columns for 1st array : "); //Inputting the columns of array A

        int c1=sc.nextInt();              

        System.out.print("Enter no of rows for 2nd array : "); //Inputting the rows of array B

        int r2=sc.nextInt();

        System.out.print("Enter no of columns for 2nd array : "); //Inputting the rows of array B

        int c2=sc.nextInt();

        if(c1 != r2)    //condition for multiplication to be possible

        {

            System.out.println("Matrix multiplication of the given order is not possible");

        }

 

        else

        {

 

            if(r1>0&&r2>0&&c1>0&&c2>0)

            {

                int A[][]=new int[r1][c1];

                int B[][]=new int[r2][c2];

                System.out.println("*************************");

                System.out.println("Inputting the 1st Matrix:"); //Accepting the values of array A

                System.out.println("*************************");

                for(int i=0;i<r1;i++)

                {

                    for(int j=0;j<c1;j++)

                    {

                        System.out.print("Enter any value : ");

                        A[i][j]=sc.nextInt();

                    }

                }

 

                System.out.println("*************************");

                System.out.println("Inputting the 2nd Matrix:"); //Accepting the values of array B

                System.out.println("*************************");

                for(int i=0;i<r2;i++)

                {

                    for(int j=0;j<c2;j++)

                    {

                        System.out.print("Enter any value : ");

                        B[i][j]=sc.nextInt();

                    }

                }

 

                //Matrix Multiplication begins here

                int C[][]=new int[r1][c2];

                int sum;

                for(int i=0;i<r1;i++)

                {

                    for(int j=0;j<c2;j++)

                    {

                        sum=0;

                        for(int k=0;k<c1;k++)

                        {

                            sum = sum + A[i][k]*B[k][j];

                        }

                        C[i][j] = sum;

                    }

                }

                //end of matrix multiplication

 

                System.out.println("*************************");

                System.out.println("The 1st Matrix is:"); //Printing the array A

                System.out.println("*************************");

                ob.printMatrix(A,r1,c1);

                System.out.println("*************************");

                System.out.println("The 2nd Matrix is:"); //Printing the array A

                System.out.println("*************************");

                ob.printMatrix(B,r2,c2);

                System.out.println("*************************");

                System.out.println("The Result of Multiplication is:"); //Printing the array A

                System.out.println("*************************");

                ob.printMatrix(C,r1,c2);

            }

            else

            {

                System.out.print("Enter positive no of rows and columns");

            }

        }

    }

}


Output:

Enter no of rows for 1st array : 2

Enter no of columns for 1st array : 2

Enter no of rows for 2nd array : 2

Enter no of columns for 2nd array : 2

*************************

Inoutting the 1st Matrix:

*************************

Enter any value : 1

Enter any value : 0

Enter any value : -3

Enter any value : 2

*************************

Inputting the 2nd Matrix:

*************************

Enter any value : -1

Enter any value : 4

Enter any value : 3

Enter any value : 5

*************************

The 1st Matrix is:

*************************

1   0  

-3  2  

*************************

The 2nd Matrix is:

*************************

-1  4  

3   5  

*************************

The Result of Multiplication is:

*************************

-1  4  

9   -2 

 

For more programs open the pdf

MORE PRACTICLES,PDF

 

Thank you**********

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Use phone As Rubber Ducky Against Another phone

  April 28, 2021 Android Tricks  /  Linux Use phone As Rubber Ducky Against Another phone Posted by VIVEK SHUKLA Use Android as Rubber Ducky...

Spyzone Form

Get Featured Updates, fill out the form now :
https://forms.gle/WLyXPLhp4ao7kqJP9

Contact Us

Name

Email *

Message *