Friday, 13 November 2015

Security Alarm System Project in C# for Beginners

This is the small interesting project for beginners to get motivation in programming. The program performs just like a security alarm system and generates different sounds and peeps on different situations.

Source Code 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("\t\t\tLEP SECURIETY SYSTEM - C#\n\n");
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine("\t1 : Police \n");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("\t2: Fire Alarm \n");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("\t3: Earthquake \n");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("\t Press the Button for help\n");
int s = int.Parse(System.Console.ReadLine());

Console.ForegroundColor = ConsoleColor.DarkRed;
switch (s) {
case 1:
for (int i = 0; i < 10; i++) {
Console.Write("\a");
Console.Write("\tPlz Help us...\t");

}
break;
case 2:
for (int i = 0; i < 10; i++) {
System.Media.SystemSounds.Exclamation.Play();
Console.Write("\tPlz Help us...\t");

}
break;
case 3:
for (int i = 0; i < 10; i++)
{
System.Media.SystemSounds.Hand.Play();
Console.Write("\tPlz Help us...\t");
}
break;
default:

break;
}
int s1 = Console.Read();
Console.WriteLine("\a");
}
}
}

Output of the Program

security alarm system in C#

Thursday, 12 November 2015

Java Code to Generate HTML form

This is the simple Java program that converts the user inputs in to HTML form (paragraph). You can use this HTML document in any web document. The basic purpose behind this code us to give a understanding to the beginners about the connectivity of Java with different web platform.

Source Code 


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lep;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

/**
 *
 * @author UzairYousafZai
 */
public class produceHTMLfromJava {

/**
     * @param args the command line arguments
     */
public static void main(String[] args) throws IOException {
start();
}

private static void start() throws IOException {
String html = generate_HTML_string();
generate_HTML_file(html);

}

private static String generate_HTML_string() throws IOException {
System.out.println("generateTheTicket()");
StringBuilder htmlBuilder = new StringBuilder();
htmlBuilder.append("<html>");
htmlBuilder.append("<head><title>Hello World</title></head>");
htmlBuilder.append("<body><p>"+getParagraph1()+"<br>"+ getParagraph2()+"</p></body>");
htmlBuilder.append("</html>");
String html = htmlBuilder.toString();
return html;
}

private static void generate_HTML_file(String html) throws FileNotFoundException, UnsupportedEncodingException {
PrintWriter writer = new PrintWriter("name.html", "UTF-8");
writer.println(html);
writer.close();
}

private static String getParagraph1() throws IOException {
System.out.println("Enter First Paragraph");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
return s;
}

private static String getParagraph2() throws IOException {
System.out.println("Enter Second Paragraph");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
return s;
}

}

Output of the Program

Java Code to Generate HTML form






Tuesday, 10 November 2015

Programming logical Questions and logic building conceptual code | Mini Games Project in Java

This is the simple but interesting game in Java for beginners to understand the programming logic. The logic behind the program is to match the same numbers in a row or column. The Simple Java program is a mini project for beginners as well to work on GUI.

Source Code 

package Game;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
import static java.util.Collections.*;

public class Game extends JFrame implements ActionListener {

/**
     *
     */
private static final long sVUID = 1L;
private JButton exitPressed, replayPressed;
private JButton[] allButtons = new JButton[16];
private ArrayList<Integer> list = new ArrayList<Integer>();
private int counter = 0;
private int[] id = new int[2];
private int[] value = new int[2];

public Game() {
initComponents();
initPanel();
setArrayListText();
setTitle("Game");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
}

public void initComponents() {
for (int i = 0; i < allButtons.length; i++) {
allButtons[i] = new JButton();
allButtons[i].setFont(new Font("Serif", Font.BOLD, 28));
allButtons[i].addActionListener(this);
}
exitPressed = new JButton("Exit");
exitPressed.addActionListener(this);
replayPressed = new JButton("Replay");
replayPressed.addActionListener(this);
}

public void initPanel() {
Panel gamePnl = new Panel();
gamePnl.setLayout(new GridLayout(4, 4));
for (int i = 0; i < allButtons.length; i++) {
gamePnl.add(allButtons[i]);
}

Panel buttonPnl = new Panel();
buttonPnl.add(replayPressed);
buttonPnl.add(exitPressed);
buttonPnl.setLayout(new GridLayout(1, 0));

add(gamePnl, BorderLayout.CENTER);
add(buttonPnl, BorderLayout.SOUTH);

}

public void setArrayListText() {
for (int i = 0; i < 2; i++) {
for (int ii = 1; ii < (allButtons.length / 2) + 1; ii++) {
list.add(ii);
}
}
shuffle(list);

int newLine = 0;
for (int a = 0; a < list.size(); a++) {
newLine++;
System.out.print(" " + list.get(a));
if (newLine == 4) {
System.out.println();
newLine = 0;
}
}
}

public boolean sameValues() {
if (value[0] == value[1]) {
return true;
}
return false;
}

@Override
public void actionPerformed(ActionEvent e) {
if (exitPressed == e.getSource()) {
System.exit(0);
}
if (replayPressed == e.getSource()) {
this.dispose();
new Game();
}
for (int i = 0; i < allButtons.length; i++) {
if (allButtons[i] == e.getSource()) {
allButtons[i].setText("" + list.get(i));
allButtons[i].setEnabled(false);
counter++;
if (counter == 3) {
if (sameValues()) {
allButtons[id[0]].setEnabled(false);
allButtons[id[1]].setEnabled(false);
} else {
allButtons[id[0]].setEnabled(true);
allButtons[id[0]].setText("");
allButtons[id[1]].setEnabled(true);
allButtons[id[1]].setText("");
}
counter = 1;
}
if (counter == 1) {
id[0] = i;
value[0] = list.get(i);
}
if (counter == 2) {
id[1] = i;
value[1] = list.get(i);
}
}
}
}

public static void main(String[] args) {
new Game();
}
}

Output of the Program


Mini Games Project in Java


Tuesday, 3 November 2015

Insertion Sort Algorithm in C#

This is the simple piece of code of insertion sort algorithm. Insertion sort algorithm is less efficient as compared to other advanced algorithms but for beginner student it is a good programming practice. The insertion sort algorithm build the final sorted array one item at a time.

Source Code 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace insertionSort
{
class Program
{
static void Main(string[] args)
{
welcomeScreen();
Sort();

}

private static void welcomeScreen()
{
Console.Write("\n"
+ "\t ======================================" + "\n"
+ "\t Welcome " + "\n"
+ "\t ======================================" + "\n"
+ "\n");
}

private static void Sort()
{
int i, j, s, temp;
int[] a = new int[20];

Console.Write("\n"
+ "\t Enter total elements: " + "\n"
+ "\t ---------------------" + "\n"
+ "\t");
s = Convert.ToInt32(Console.ReadLine());

Console.Write("\tEnter " + s + " elements: "+ "\n");
for (i = 0; i < s; i++)
{
Console.Write("\t");
a[i] = Convert.ToInt32(Console.ReadLine());
}
for (i = 1; i < s; i++)
{
temp = a[i];
j = i - 1;
while ((j >= 0)&&(temp < a[j]) )
{
a[j + 1] = a[j];
j = j - 1;
}
a[j + 1] = temp;
}

Console.Write("\tAfter sorting: ");
Console.Write("\n"
+ "\t After sorting: " + "\n"
+ "\t -------------- " + "\n"
+ "");
for (i = 0; i < s; i++)
{
Console.Write("\t" + a[i] + "\n");
}
int s1 = Convert.ToInt32(Console.ReadLine());
return;
}
}
}

Output of the Program


Insertion Sort Algorithm in C#