Galgotias College of Engineering and Technology: Web Technology Lab File

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

GALGOTIAS COLLEGE OF

ENGINEERING AND
TECHNOLOGY

WEB TECHNOLOGY LAB FILE

PREETAM KUMAR JAISWAL

1809710077

CSE-B
Web tech lab

22 – April – 2021

1) Program to print the duplicate elements of an array

import java.io.BufferedReader;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Scanner;

class Duplicate {
public static void main(String[] args) {
System.out.println("Enter element of array");
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
Hashtable<Integer, Integer> h1 = new Hashtable<>();
for (int i = 0; i < n; i++) {
int t=reader.nextInt();
if(h1.containsKey(t))
{
h1.put(t, h1.get(t)+1);
}
else
{
h1.put(t,1);
}
}
System.out.println("Duplicate elements are : ");
h1.forEach((k,v)->{
if(v>1)
{
System.out.println(k);
}
});
}
}
2) Program to print the elements of an array in reverse order

import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Scanner;
class ReverseOrder {
public static void main(String[] args) {
System.out.println("Enter element of array");
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
ArrayList<Integer> h1 = new ArrayList<>();
for (int i = 0; i < n; i++) {
int t = reader.nextInt();
h1.add(t);
}
System.out.println("Element in reverse order are : ");
for(int i=h1.size()-1;i>=0;i--)
{
System.out.println(h1.get(i));
}
}
}

 
3) Program to print the elements of an array present on even position

import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Scanner;

class EvenPrint {
public static void main(String[] args) {
System.out.println("Enter element of array");
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
ArrayList<Integer> h1 = new ArrayList<>();
for (int i = 0; i < n; i++) {
int t = reader.nextInt();
h1.add(t);
}
System.out.println("Element at even index : ");
for (int i = 0; i < h1.size(); i += 2) {
System.out.println(h1.get(i));
}
}
}

4) Program to left rotate the elements of an array

import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Scanner;

class LeftRotate {
public static void main(String[] args) {
System.out.println("Enter element of array");
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
ArrayList<Integer> h1 = new ArrayList<>();
for (int i = 0; i < n; i++) {
int t = reader.nextInt();
h1.add(t);
}
int f = h1.get(0);
for (int i = 1; i < h1.size(); i++) {
int t = h1.get(i);
h1.set(i - 1, t);
}
h1.set(n - 1, f);
System.out.println("Left rotated : ");
for (int i = 0; i < h1.size(); i += 1) {
System.out.println(h1.get(i));
}
}
}

5) Java Program to sort the elements of an array in ascending orde


import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Scanner;

class Sort {
public static void main(String[] args) {
System.out.println("Enter element of array");
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
ArrayList<Integer> h1 = new ArrayList<>();
for (int i = 0; i < n; i++) {
int t = reader.nextInt();
h1.add(t);
}
Collections.sort(h1);
System.out.println("Element sorted : ");
for (int i = 0; i < h1.size(); i++) {
System.out.println(h1.get(i));
}
}
}

6) Java Program to find Third Largest Number in an Array


import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Scanner;

class Third {
public static void main(String[] args) {
System.out.println("Enter element of array");
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
ArrayList<Integer> h1 = new ArrayList<>();
for (int i = 0; i < n; i++) {
int t = reader.nextInt();
h1.add(t);
}
Collections.sort(h1);
System.out.println("3rd largest element is : ");
System.out.println(h1.get(n - 3));
}
}

April 29, 2021

Write a program to elaborate the concept of method overloading and constructor overloading.

Method overloading:-

public class Sum {


public int sum(int x, int y)

return (x + y);

public int sum(int x, int y, int z)

return (x + y + z);

public double sum(double x, double y)

return (x + y);

// Driver code

public static void main(String args[])

Sum s = new Sum();

System.out.println(s.sum(10, 20));

System.out.println(s.sum(10, 20, 30));

System.out.println(s.sum(10.5, 20.5));

Constructor overloading:-

class Box

double width, height, depth;


Box(double w, double h, double d)

width = w;

height = h;

depth = d;

Box()

width = height = depth = 0;

double volume()

return width * height * depth;

// Driver code

public class Test

public static void main(String args[])

Box mybox1 = new Box(10, 20, 15);

Box mybox2 = new Box();


double vol;

vol = mybox1.volume();

System.out.println(" Volume of mybox1 is " + vol);

vol = mybox2.volume();

System.out.println(" Volume of mybox2 is " + vol);

2 write a program to elaborate the concept of inheritance in java

class Bicycle {

public int gear;

public int speed;

public Bicycle(int gear, int speed)

this.gear = gear;

this.speed = speed;

public void applyBrake(int decrement)

speed -= decrement;

public void speedUp(int increment)

{
speed += increment;

}]

public String toString()

return ("No of gears are " + gear + "\n"

+ "speed of bicycle is " + speed);

class MountainBike extends Bicycle {

public int seatHeight;

public MountainBike(int gear, int speed,

int startHeight)

super(gear, speed);

seatHeight = startHeight;

public void setHeight(int newValue)

seatHeight = newValue;

@Override public String toString()

return (super.toString() + "\nseat height is "

+ seatHeight);
}

// driver class

public class Test {

public static void main(String args[])

MountainBike mb = new MountainBike(3, 100, 25);

System.out.println(mb.toString());

3. write a program to illustrate the concept of method overriding

class Parent {

void show()

System.out.println("Parent's show()");

class Child extends Parent {

@Override

void show()

{
System.out.println("Child's show()");

// Driver class

public class Main {

public static void main(String[] args)

Parent obj1 = new Parent();

obj1.show();

Parent obj2 = new Child();

obj2.show();

4. write a program to illustrate the concept of interface

public class InterfaceEg {


public static void main(String[] args) {
IAnimal animal = new Cat();
System.out.println("name: " + animal.GetName());
System.out.println("dangerous: " + animal.IsDangerous());
System.out.println("extinct: " + animal.IsExtinct());
}
}

interface IAnimal {
boolean IsDangerous();

boolean IsExtinct();

String GetName();
}
class Cat implements IAnimal {

@Override
public boolean IsExtinct() {
return false;
}

@Override
public String GetName() {
return "Cat";
}

@Override
public boolean IsDangerous() {
return false;
}
}

                06 May 2021


 
Instructions:
Complete these programs by end of this lab and append them to the

4) A sandwich is two pieces of bread with something in between. Return the string that is between the first and
last appearance of    "bread" in the given string, or return the empty string "" if there are not two pieces of
bread.
 
   getSandwich("breadjambread") → "jam"
   getSandwich("xxbreadjambreadyy") → "jam"
   getSandwich("xxbreadyy") → "" 

1). Write a program to demonstrate to show NullPointerException.

// A Java program to demonstrate that invoking a method

// on null causes NullPointerException

import java.io.*;

class GFG
{

public static void main (String[] args)

String ptr = null;

try

if (ptr.equals("gfg"))

System.out.print("Same");

else

System.out.print("Not Same");

catch(NullPointerException e)

System.out.print("NullPointerException Caught");

2). Write a program for Counting sort. 


   -- Create input/output file by taking their name from user. "input.txt"
   -- Write some data to be sorted in input file.
   -- Read input from the input file and write the sorted output in output file.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class CSort {


static void sort(char arr[]) {
int n = arr.length;
char output[] = new char[n];

int count[] = new int[256];


for (int i = 0; i < 256; ++i)
count[i] = 0;

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


++count[arr[i]];

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


count[i] += count[i - 1];

for (int i = n - 1; i >= 0; i--) {


output[count[arr[i]] - 1] = arr[i];
--count[arr[i]];
}
for (int i = 0; i < n; ++i)
arr[i] = output[i];
}

public static void main(String args[]) throws FileNotFoundException {


File myObj = new File("n.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
System.out.println(data);
}
myReader.close();

char arr[] = { 'g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e',
'k', 's' };

sort(arr);

System.out.print("Sorted character array is ");


for (int i = 0; i < arr.length; ++i)
System.out.print(arr[i]);
}
}

3). Write a Program to show try catch example?

public class TryCatchExample2 {  

   public static void main(String[] args) {  

      try        {  

       int data=50/0; 
}
    catch(ArithmeticException e)  
{       System.out.println(e);  
    }  
        System.out.println("rest of the code");  
 }        
}

4). A sandwich is two pieces of bread with something in between. Return the string that is between the
first and last appearance of “bread” in the given string, or return the empty string “” if there are not two
pieces of bread.

getSandwich(“breadjambread”) - > “jam”

getSandwich(“xxbreadjambreadyy”) -> “jam”

getSandwich(“xxbreadyy”)-> “”

import java.util.Scanner;

public class Sandwitch {


public static void main(String[] args) {
System.out.println("Enter string");
var sc = new Scanner(System.in);

var str = sc.nextLine();


str=str.replaceAll("bread", "-");
var p=str.split("-")[1];
System.out.println(p);
}
}

Lab -4

Write a java applet for Calculator

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Calculator extends Applet implements ActionListener
{
TextField inp;
//Function to add features to the frame
public void init()
{
    setBackground(Color.white);
    setLayout(null);
    int i;
    inp = new TextField();
    inp.setBounds(150,100,270,50);
    this.add(inp);
    Button button[] = new Button[10];
    for(i=0;i<10;i++)
    {
     button[i] = new Button(String.valueOf(9-i));
     button[i].setBounds(150+((i%3)*50),150+((i/3)*50),50,50);
     this.add(button[i]);
     button[i].addActionListener(this);
    }
    Button dec=new Button(".");
    dec.setBounds(200,300,50,50);
    this.add(dec);
    dec.addActionListener(this);

    Button clr=new Button("C");


    clr.setBounds(250,300,50,50);
    this.add(clr);
    clr.addActionListener(this);

    Button operator[] = new Button[5];


    operator[0]=new Button("/");
    operator[1]=new Button("*");
    operator[2]=new Button("-");
    operator[3]=new Button("+");
    operator[4]=new Button("=");
    for(i=0;i<4;i++)
    {
     operator[i].setBounds(300,150+(i*50),50,50);
     this.add(operator[i]);
     operator[i].addActionListener(this);
    }
    operator[4].setBounds(350,300,70,50);
    this.add(operator[4]);
    operator[4].addActionListener(this);
}
String num1="";
String op="";
String num2="";
//Function to calculate the expression
public void actionPerformed(ActionEvent e)
{
    String button = e.getActionCommand();
char ch = button.charAt(0);
    if(ch>='0' && ch<='9'|| ch=='.')
    {
     if (!op.equals(""))
        num2 = num2 + button;
     else
        num1 = num1 + button;
     inp.setText(num1+op+num2);
    }
    else if(ch=='C')
    {
     num1 = op = num2 = "";
     inp.setText("");
    }
    else if (ch =='=')
    {
     if(!num1.equals("") && !num2.equals(""))
     {
        double temp;
        double n1=Double.parseDouble(num1);
        double n2=Double.parseDouble(num2);
        if(n2==0 && op.equals("/"))
        {
         inp.setText(num1+op+num2+" = Zero Division Error");
         num1 = op = num2 = "";
        }
        else
        {
         if (op.equals("+"))
         temp = n1 + n2;
         else if (op.equals("-"))
         temp = n1 - n2;
         else if (op.equals("/"))
        temp = n1/n2;
         else
         temp = n1*n2;
         inp.setText(num1+op+num2+" = "+temp);
         num1 = Double.toString(temp);
         op = num2 = "";
     }
}
     else
     {
        num1 = op = num2 = "";
        inp.setText("");
     }
}
    else
    {
     if (op.equals("") || num2.equals(""))
        op = button;
     else
     {
        double temp;
        double n1=Double.parseDouble(num1);
        double n2=Double.parseDouble(num2);
        if(n2==0 && op.equals("/"))
        {
         inp.setText(num1+op+num2+" = Zero Division Error");
         num1 = op = num2 = "";
        }
        else
        {
         if (op.equals("+"))
         temp = n1 + n2;
         else if (op.equals("-"))
         temp = n1 - n2;
         else if (op.equals("/"))
        temp = n1/n2;
         else
         temp = n1*n2;
         num1 = Double.toString(temp);
         op = button;
         num2 = "";
     }
}
     inp.setText(num1+op+num2);
}
}
}

2). Write a html page representing your cv.

<html>
    <head>Cv </head>
    
    <body>
        <h3>Vanshika Singh</h3>
        <a href = "galgotiascollege.edu">College Info</a><br>

        <table border="2px" color = "black" padding = "4px">
            <thead>Qualifications</thead>
            <tr>
                <td> Program </td>
                <td> College </td>
                <td>  Percentage   </td> 
            </tr>
            <tr>
                <td> B.Tech </td>
                <td> Galgotias College of Engineering and Technology </td>
                <td>  88  </td> 
            </tr>
            
        </table>
        <h4>Strengths</h4> 
        <ul>
            <li>
                Punctuality
            </li>
            <li>
                Hardworking
            </li>
            <li>
                Interest in new technology
            </li>
        </ul>
        <h4>Projects</h4>
            <ul>
                <li>Sentiment Analysis</li>
                <ul>
                    <li> Description :- Determine the emotion of the user using 
there comments</li>
                </ul>
            </ul>
    </body>
</html>

You might also like