Java enum constructor

In Java, an enumeration is a special kind of class that represents a fixed number of predefined values. The values are defined as enum constants, which are static and final fields of the enumeration.

When you define an enum, you can also define a constructor for it, which is called when each enum constant is created. The constructor is passed the values of the constant's fields and can use those values to initialize the constant.

Here's an example of an enumeration called Size with a constructor that takes a single String argument:


In this example, the Size enumeration has four constants: SMALL, MEDIUM, LARGE, and EXTRA_LARGE. Each constant has an associated abbreviation, which is passed to the constructor when the constant is created. The constructor sets the value of the abbreviation field to the passed-in value.

You can call the constructor explicitly or it will be called automatically when enum constants are created, so the following code:

Size s = Size.SMALL;

is equivalent to

Size s = new Size("S");

When you define constructors for your enum, it is a best practice to make them private. So that no other classes can create an instance of your Enum. This way you can only use the predefined values of the Enum and not create any new value on runtime.

jstl if else string comparison

 In JavaServer Pages (JSP), the JSTL (Java Standard Tag Library) provides a set of tags that you can use to perform common tasks, such as iterating over a collection of data, performing conditional logic, and formatting text. The JSTL if tag is used to perform conditional logic in a JSP page. The if tag evaluates a boolean expression, and if the expression evaluates to true, the content inside the if tag is rendered to the response. If the expression evaluates to false, the content inside the if tag is not rendered.

JSTL (JavaServer Pages Standard Tag Library) is a collection of tags that provide common functionality when working with JSPs (JavaServer Pages). One of the tags provided by JSTL is the <c:if> tag, which can be used to conditionally include content in a JSP based on a Boolean expression.

Here's an example of how you might use the JSTL if tag to conditionally render content in a JSP page:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<c:if test="${someCondition}">

  <p>This content will be displayed if someCondition is true</p>

</c:if>

The JSTL if tag also provides an else branch which will be executed if the test conditions return false

jstl if else


Additionally, you can also use c:choose, c:when, c:otherwise tags for conditions where you have more than one test case.


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<c:choose>
  <c:when test="${someCondition}">
    <p>This content will be displayed if someCondition is true</p>
  </c:when>
  <c:when test="${anotherCondition}">
    <p>This content will be displayed if anotherCondition is true</p>
  </c:when>
  <c:otherwise>
    <p>This content will be displayed if neither someCondition nor anotherCondition is true</p>
  </c:otherwise>
</c:choose>
Please note that to use the above-mentioned tags, you need to include JSTL core library in your project.

C program to print table of 2 using for loop


#include <stdio.h>

int main() {

    int i;

The first line includes the standard input/output header file, which is needed for the printf function used later in the program.

The main function is the entry point for the program. It is the first function that is executed when the program runs.

The next line declares a variable i of type int. This variable will be used as the loop counter in the for loop.

    for (i = 1; i <= 10; i++) {

        printf("2 * %d = %d\n", i, 2 * i);

    }

This is the for loop. It starts with the for keyword, followed by parentheses that contain three statements separated by semicolons:

The initialization statement i = 1 sets the value of i to 1 before the loop starts.

The loop condition i <= 10 specifies that the loop will continue as long as the value of i is less than or equal to 10.

The iteration statement i++ increments the value of i by 1 after each iteration of the loop.

Inside the loop, the printf function is used to print a string to the console. The string contains two %d format specifiers, which are placeholders for two integer values that will be printed. The first %d is replaced by the value of i, and the second %d is replaced by the result of 2 * i. The \n at the end of the string is a newline character that causes the output to be printed on a new line.

Finally, the return 0; statement at the end of the main function indicates that the program has completed successfully.

C program to find the largest element in an array

C program that finds the largest element in an array :

  1. #include <stdio.h>
  2. int main() {
  3.   int n, i;
  4.   long long int t1 = 0, t2 = 1, nextTerm;

  5.   printf("Enter the number of terms: ");
  6.   scanf("%d", &n);

  7.   printf("Fibonacci Series: ");

  8.   for (i = 1; i <= n; ++i) {
  9.     printf("%lld, ", t1);
  10.     nextTerm = t1 + t2;
  11.     t1 = t2;
  12.     t2 = nextTerm;
  13.   }

  14.   return 0;
  15. }

  • This program first reads in n, the number of elements in the array. It then reads in n elements from the user and stores them in the array. 
  • Finally, it iterates through the array and compares each element to the first element (which is initially set to the largest). 
  • If it finds a larger element, it updates the value of the first element to be the larger value. At the end, the program prints out the largest element in the array.


  • This program first initializes an array a with 10 elements and assigns the value of the first element to the variable max.
  • It then loops through the array and compares each element with max. 
  • If an element is larger than max, max is updated with the new value. After the loop finishes, max will contain the largest element in the array.

Fibonacci series program in c

Here is a simple program that prints out the first n numbers in the Fibonacci series in C:

  1. #include <stdio.h>
  2. int main() {
  3.   int n, i;
  4.   long long int t1 = 0, t2 = 1, nextTerm;

  5.   printf("Enter the number of terms: ");
  6.   scanf("%d", &n);

  7.   printf("Fibonacci Series: ");

  8.   for (i = 1; i <= n; ++i) {
  9.     printf("%lld, ", t1);
  10.     nextTerm = t1 + t2;
  11.     t1 = t2;
  12.     t2 = nextTerm;
  13.   }

  14.   return 0;
  15. }

  • This program first prompts the user to enter the number of terms in the Fibonacci series to be printed. It then uses a for loop to iterate through the terms and prints each one. The loop starts at 1, and it continues until the value of i is greater than n. 
  • On each iteration of the loop, the next term in the series is calculated as the sum of the previous two terms, and the variables t1 and t2 are updated to store the previous two terms. The first two terms of the series are initialized to 0 and 1, respectively.
  • This program uses a for loop to iterate through the first 20 terms in the Fibonacci series. The variables t1 and t2 represent the previous two terms, and the variable nextTerm represents the next term in the series. 
  • The program prints out the value of t1 and then updates the values of t1, t2, and nextTerm for the next iteration of the loop.
Here is a C program that prints the first n numbers of the Fibonacci series:

fibonacci series in c

Enter the number of terms: 12
First 12 terms of Fibonacci series are:
0
1
1
2
3
5
8
13
21
34
55
89
  • This program prompts the user to enter a positive integer n and then generates the first n terms of the Fibonacci series using a loop.  with the first two terms being 0 and 1. The series starts with 0 and 1 and looks like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
  • This program prompts the user to enter the number of terms in the Fibonacci series they want to generate, then uses a loop to compute and print out each term. The first two terms of the series are hard-coded as 0 and 1, and the remaining terms are computed by adding the previous two terms together.
  • To understand how this program works, you can read the following explanation:
  • The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. In this program, the first two numbers of the Fibonacci series are initialized to 0 and 1.
  • The for loop is used to iterate over the numbers of the series. The loop starts from 0 and ends at n-1. At each iteration, the next number in the series is calculated by adding the first and second numbers. The if statement is used to handle the case where the current iteration is 0 or 1, in which case the next number is simply the current iteration.
  • After the next number is calculated, the first and second numbers are updated for the next iteration. Finally, the next number is printed to the screen.
  • I hope this helps! Let me know if you have any questions.

Introduction to C language

Introduction to C


     C is a general purpose programming language. Programming language is the means to communicate with the system to get our thing done.

Need for a programming language:

  We all of us know system can only understand binary representation i.e. in the form of 0's and 1's.

Suppose we have to program which adds two numbers and save the result.

Steps involved:
  • First we should write the values in the memory.
  • Move those values into the registers.(registers are also a kind of memory where the CPU can perform operation on the values stored in the registers.)
  • Add the values in the registers.
  • Save the result in the register to the memory.

Machine Language                                                                                                                                                                                                                                                                               At the beginning machine code is used to write the program. 

Machine level programming consists of binary or hexadecimal instructions on which an processor can respond directly.  

Structure of machine language instruction.
Opcode   Operand 

Opcode represents the operation to be performed by the system on the operands.

In machine code:
Memory     opcode     operand 
location                                                                     
0000:           55            e4     10   // 55 represent move instruction, move 10 to location e4          
0001:           89            e5     20
0003:           83            e4     e5
0006:           54            c4     e4    

Its difficult to read and write opcode for every instruction. So instead of numerical value for instruction there should be an alternative, Then comes the assembly code.

Assembly language:

 Mnemonics are used to represent the opcode.
An Assembly code:
MOV  A, $20           // move value 20 to register A (MOV is a mnemonics)
MOV  B, $10           // move value 10 to register B
ADD   A, B              // Add value in register in A and B
MOV  #30, A           // move value in register A to address 30

Assemblers converts the assembly language into binary instructions.
Assembly language is referred to as low level language,
Assembler languages are simpler than opcodes. Their syntax is easier to read than machine language but as the harder to remember. So, their is a need for other language which is much more easy to read and write the code.
Then comes the C programming language.

C language

In C language:
 int main()
{
     int a,b,c;
     a=10;
     b=20;
     c=a+b;
}

Compiler converts the C language code into assembly language code.

C language is easier to write and read. 

Factorial program in python

#write a python program for factorial of a given number.

num = int(input("Enter a number: "))
factorial = 1
if num < 0:
   print("factorial not exist for negative numbers")
elif num == 0:
   print("The factorial of 0 is 1")
else:
   for i in range(1,num + 1):
       factorial = factorial*i
   print("The factorial of",num,"is",factorial)

  • We store the number we give in the num variable and by using the if, elif, and else loop we are executing the program. if statement executes when the given condition is true and same for elif also. After the iteration, it finally returns the factorial of the number we are given.
  • Normally we write as 12!=12*11*10*9*8*7*6*5*4*3*2*1=479001600




Select Menu