Ad Space (Insert Google AdSense Code)
 'use strict';
var http=require('http');
var port=8000;
http.createServer(function(req,res){
    console.log(req.method+' '+req.url);
     console.log(req.headers);
     console.log();
     res.writeHead(200,{'content-type':"text/plain"});
     res.end('headers logged to console!/n');
}).listen(port);
 console.log('listening at port '+port);



  hello.js 

  module.exports ={
SayHelloInEnglish:function(){
    return "Hello students";
},
 SayHelloInSpanish:function(){
    return "¡Hola, estudiantes!";
}
};

sayhello.js


  var hello=require('./Hello.js');
console.log(hello.SayHelloInEnglish());
console.log(hello.SayHelloInSpanish());


   const moment = require('moment'); 

console.log('current date and time: ');
console.log(moment().format('YYYY-MM-DD HH:MM:SS'));
  const EventEmmiter=require('events');
const eventEmmiter=new EventEmmiter();

eventEmmiter.on('calculateSal',(hra,sal,Da,ta)=>{

    let add=hra+sal+Da+ta;
    console.log(`sal: ${add}`);
});


eventEmmiter.emit('calculateSal',4000,5000,6000,7000);
Ad Space (Insert Google AdSense Code)
//Demonstrating basic file operations in node.js
const fs = require('fs');
const path = require('path');

//Define file path
const filepath = path.join(__dirname,'example.txt');

//Create and write to a file
fs.writeFileSync(filepath,'This is a demonstration of basic file operstions in node.js');
console.log('File created and written successfully');

//read the file
const filecontent = fs.readFileSync(filepath,'utf8');
console.log('File content:',filecontent);

//Append data to the file
fs.appendFileSync(filepath,'\n Appending some more content to the file');
console.log('Content appended to the file');

//Read the updated file
const updateContent = fs.readFileSync(filepath,'utf-8');
console.log('Updated file content',updateContent);

//Rename the file
const newfilepath = path.join(__dirname,'rename_example.txt');
fs.renameSync(filepath,newfilepath);
console.log('File renamed to rename_example.txt');

//Delete the file
fs.unlinkSync(newfilepath);
console.log('File deleted successfully!');
console.log('File Handling operation completed');
Ad Space (Insert Google AdSense Code)
1. Write a Java Program to demonstrate a Generic Class
      public class prac
{
    public T item;
    public prac(T item)
    {
        this.item=item;
    }
    public T print()
    {
        return item;
    }

    public static void main(String[] args)
    {
        prac  i1=new prac<>(33);
        System.out.println(i1.print());

         prac  s1=new prac<>("shiv");
        System.out.println(s1.print());
    }
}
    
2. Write a Java Program to demonstrate Generic Methods.
      public class prac{
    public static  void printArr(E[] arr){
        for(E num:arr){
            System.out.print(num+" ");
        }
        System.out.println();
    }

    public static void main(String args[]){
        Integer[] intarr={10,20,30};
        System.out.println("arr of int ");
        printArr(intarr);

        String[] strarr={"a","b","c"};
        System.out.println("arr of str ");
        printArr(strarr);
    }
}
    
3. Write a Java Program to demonstrate Wildcards in Java Generics.
  import java.util.*;
public class prac{
    public static Integer sum(List l1)//upper bounded
    {
        int sum=0;
        for(Number n:l1)
        {
          sum+=n.intValue();
        }
        return sum;
    }

    public static void main(String[] args)
    {
        List l1= new ArrayList<>();
        l1.add(10);
        l1.add(20);
        List l2=new ArrayList<>();
        l2.add(10.2);
        l2.add(20.2);
        System.out.println(sum(l1));
        System.out.println(sum(l2));
    }
}
1. Write a Java program to create List containing list of items of type String and use for-each loop to print the items of the list
  import java.util.*;
 public class prac{
    public static void main(String[] args)
    {
        List s1 =new ArrayList<>();
        s1.add("a");
        s1.add("b");
        s1.add("c");
        s1.add("d");

        for(String s:s1){
            System.out.println(s+" ");
        }
    }
 }
2. Write a Java program to create List containing list of items and use ListIterator interface to print items present in the list. Also print the list in reverse / backward direction.
  import java.util.*;
  public class prac{
    public static void main(String[] args) {
        List l1=new ArrayList<>();
        l1.add(10);
        l1.add(20);
        l1.add(30);

        ListIterator itr=l1.listIterator();

        while(itr.hasNext()){
            System.out.println(itr.next());
        }

         while(itr.hasPrevious()){
            System.out.println(itr.previous());
        }
    }
  }
1. Write a Java program to create a Set containing list of items of type String and print the items in the list using Iterator interface. Also print the list in reverse / backward direction
   import java.util.*;
   public class prac{
    public static void main(String[] args) {
        Set s1=new HashSet<>();
        s1.add("a");
        s1.add("b");
        s1.add("c");

        Iterator itr1=s1.iterator();
        while(itr1.hasNext()){
            System.out.println(itr1.next());
        }
        List s=new ArrayList<>(s1);
        ListIterator itr2=s.listIterator(s.size());
         while(itr2.hasPrevious()){
            System.out.println(itr2.previous());
        }
    }
   }
2. Write a Java program using Set interface containing list of items and perform the following operations: a. Add items in the set. b. Insert items of one set in to other set. c. Remove items from the set d. Search the specified item in the set
  import java.util.*;
public class prac{
    public static void main(String[] args){
        Set s1=new HashSet<>();
        s1.add("a");
        s1.add("b");
        s1.add("c");
System.out.println(s1);
        Set s2=new HashSet<>(s1);
        System.out.println(s2);
        s2.remove("c");
        System.out.println(s2);
        System.out.println(s1.contains("b"));
    }
}

← Back to Blogs