Monday, 9 January 2017

Write a Perl script that prints the numbers from 1 to 100, but for multiples of three print “Fast” instead of the number and for the multiples of seven print “Car”. For the numbers which are multiples of both three and seven print “FastCar”.

use warnings;
use strict;
my  $l=1;
for($l=1;$l<=100;$l++)
{
if( $l % 3 == 0 && $l % 7 == 0 ) {
                print "FastCar";
                print"\n";
               
              }
             elsif( $l % 3 == 0 && $l % 7 != 0 ) {
                    print "Fast";
                    print "\n";
                   
             }
             elsif( $l % 3 != 0 && $l % 7 == 0 ) {
                    print "Car";
                    print"\n";
                   
             }
             else {
                print $l;
                print "\n";
               
             }
}

Write a program in C++ that accepts a list of values as command line arguments and prints the sum of all the values. Make sure that the logic of the program contains a recursive method recurseAdd that accepts these list of values as the input to it and returns the sum of all the values.

#include <iostream>
#include <cstdlib>
using namespace std;

int recurseAdd(int *,int);
int main(int argc, char **argv)
{
   if (argc < 2) // the program's name is the first argument
   {
      std::cerr << "Not enough arguments!\n";
      return -1;
   }
 
   int *inArray = new int[argc-1];

   for(int i=0;i<argc-1;i++)
    inArray[i]=std::atoi(argv[i+1]);
   
   
   
    int result=recurseAdd(inArray,argc-1);
    cout<<result;  
   

   return 0;
}
int recurseAdd(int *arr, int size)
{
if(size==0)
return 0;
else
{
return (*(arr +0) + recurseAdd((arr+1),size-1));
}
}

Activity - Alumni Form You are helping your college build an alumni website. Build a HTML form that has the following fields: Name Email Gender - Male or Female - should show up as a radio control City - make it show a list of available options (atleast add 5 options to choose from) Company - current company where you are working

<html>
<head>
<title>XYZ Institute Of Engineering</title>
<h1 align="center">XYZ Institute Of Engineering</h1>
<hr/>
</head>
<body>
<table border="1" align="center">
<tr>
<td>Name</td>
<td><input type="text"/></td>
</tr>
<tr>
<td>Email</td>
<td><input type="email"/></td>
</tr>
<tr>
<td>Gender</td>
<td><input type="radio" name="g"/>Male &nbsp; <input type="radio" name="g"/>Female</td>
</tr>
<tr>
<td>City</td>
<td>
<select>
                <option value="-">--select--</option>
                <option value="Kolkata">Kolkata</option>
                <option value="Delhi">Delhi</option>
                <option value="Mumbai">Mumbai</option>
                <option value="Chennai">Chennai</option>
                <option value="Bangalore">Bangalore</option>
            </select>
</td>
</tr>
<tr>
<td>Company</td>
<td><input type="text"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit"/></td>
</tr>

</table>
</body>
</html>

XML Processing in Java using JAXP

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
 * Efficient event based XML parsing using a SAX parser
 */
 class SaxRetrieval {
  /**
   * Parse specified file using Sax Parser.
   * @param fileName
   */
  public static void parseFile(String fileName) {
    try {
      // Instantiate Sax Parser.
      SAXParserFactory factory = SAXParserFactory.newInstance();
      SAXParser saxParser = factory.newSAXParser();
      // Setup handler for Sax Parser.
      DefaultHandler handler = new DefaultHandler() {
boolean employee = false;
        boolean name = false;
        boolean salary = false;
        // Invoked when the Sax Parser hits a start tag.
        public void startElement(String uri, String localName,String qName,
                     Attributes attributes) throws SAXException {
            if (qName.equalsIgnoreCase("EMPLOYEE") ) {
            employee = true;
          }        
          if (qName.equalsIgnoreCase("NAME") ) {
            name = true;
          }
          if (qName.equalsIgnoreCase("SALARY")) {
            salary = true;
          }

        }
       
       
        // Invoked for each text node in a tag.
        public void characters(char ch[], int start, int length) throws SAXException {
       
   if (name && employee) {
System.out.println("Name : " + new String(ch, start, length));
}
         
if (salary) {
System.out.println("Salary : " + new String(ch, start, length));
salary = false;
}
        }

        public void endElement(String uri, String localName,String qName) throws SAXException {
                      if(qName.equalsIgnoreCase("employee")){
                      employee = false;
                      }
if(qName.equalsIgnoreCase("name") ) {
name = false;
}



}
     
      };
     
      saxParser.parse(fileName, handler);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public static void main(String argv[]) {
    SaxRetrieval.parseFile("activity.xml");


 }
}

Reflections in Java

public class Reflection  {
public static void runReflection(String packageName, String className) {
       try {
Class<?> c = Class.forName(className);
Point p=(Point)c.newInstance();
p.dynamicExecute();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {

e.printStackTrace();
} catch (IllegalAccessException e) {

e.printStackTrace();
}
   }
public static void main(String args[]){
Reflection r =new Reflection();
r.runReflection("Reflection", "Point");

}

}

Generics in Java

import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class GenericsCollectionPrinter {
    public static void printCollection(Collection collection){
        Class c=collection.getClass();
        System.out.print("Collection "+c.getSimpleName()+": [ ");
        Iterator itr = collection.iterator();
        while(itr.hasNext()){
            Object o = itr.next();
            System.out.print(o+" ");
        }
        System.out.print("]");
        System.out.println();
}

    public static void main(String args[]){
List<Integer> x=new ArrayList<Integer>();
x.add(5);
x.add(8);
x.add(7);
List<String> y=new LinkedList<String>();
y.add("five");
y.add("eight");
GenericsCollectionPrinter.printCollection(x);
GenericsCollectionPrinter.printCollection(y);
}
}

Build a map function that takes a list as input and a function as input and returns a list of items where the function is applied to each item in the input list

function square(number) {
  return number * number;
}

function map(list[], function) {
result = [];
    foreach(number in list) {
        result.push(function(number));
    }
    return result;
}

map([1,2,3,4], square)