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#

Sunday, 20 September 2015

C# Name Sorting Program

Its is a simple C# program that sort the student name in ascending order. The program take the student name and grade and sort the name in ascending order.

Source Code 


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

namespace sortStdNames
{
class Program
{
struct student
{
public string name;
public int id;
public char grade;
};
student[] stdInstance = new student[2];
int size;
static void Main(string[] args)
{
Program m = new Program();
m.size = 0;
welcomeScrean();
int j = 2;
Console.Write( "\t Enter Detail of 2 students " +"\n");
for (int i=0; i < j; i++)
{
getStudentsWithGrade(m);
}

displayAllStudents(m);
sortAndDisplayAllStudents(m);
String s = Console.ReadLine();
}

private static void sortAndDisplayAllStudents(Program m)
{

String[] temp= new String[m.size];
List<string> l = new List<string>();
for ( int h = 0 ; h<m.size ; h++ )
{
try
{
l.Add(m.stdInstance[h].name);
}
catch (IndexOutOfRangeException e){

}
}
l.Sort();
Console.Write("\n"
+ "\a"
+ "\t *******************************************************" + "\n"
+ "\t ** Sorted names are **" + "\n"
+ "\t *******************************************************" + "\n");
Console.Write("\t \n");
foreach (String c in l)
{
Console.WriteLine("\t"+c);
}
}

private static void displayAllStudents(Program m)
{
Console.Write("\n"
+ "\t Students added with following data" + "\n"
+ "\t ======================================" + "\n"
+ "\n");

for (int n = 0; n < m.size; n++)
{
try
{
Console.Write("\n\t Name: " + m.stdInstance[n].name + "\n"
+ "\t Grade: "+m.stdInstance[n].grade);
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine("IndexOutOfRangeException caught and " + e);
}
}
}

private static void getStudentsWithGrade(Program m)
{
Console.Write( "\n"
+"\a"
+"\t *******************************************************" +"\n"
+"\t ** Adding Student No # " +m.size +" **" +"\n"
+"\t *******************************************************" +"\n");

Console.Write( "\n"
+"\n\tEnter Name : ");


try
{
m.stdInstance[m.size].name = Console.ReadLine();

Console.Write( "\n\tEnter Grade ");

m.stdInstance[m.size].grade = Convert.ToChar(Console.ReadLine());//taking grade

m.stdInstance[m.size].id = m.size;//settind customer id
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine("IndexOutOfRangeException caught and " + e);
}
m.size++;
Console.Write( "\n"
+"\t Press Enter to continue . . ." +"\n");
String s= Console.ReadLine();
}

private static void welcomeScrean()
{

}
}
}

Output of the Program

Sort student name according to their names

Friday, 18 September 2015

Fibonacci sequence series in C#

The Fibonacci sequence is a series of numbers where a number is found by adding up the two numbers before it. Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so forth.

Source Code 

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

namespace evenFibonacci
{
class Program
{
static void Main(string[] args)
{
welcomeScrean();
evenFibonacci();
int wait = Convert.ToInt32(Console.ReadLine());
}

private static void welcomeScrean()
{
Console.Write("\a"
+ "\t*******************************************************" + "\n"
+ "\t** **" + "\n"
+ "\t** Welcome To **" + "\n"
+ "\t** **" + "\n"
+ "\t** Find Even Fibonacci **" + "\n"
+ "\t** **" + "\n"
+ "\t** and there sum **" + "\n"
+ "\t** **" + "\n"
+ "\t** Program By LEP **" + "\n"
+ "\t** **" + "\n"
+ "\t*******************************************************" + "\n");
}

private static void evenFibonacci()
{
int num, count = 0;
long previous = 1, next = 0, sum;
Console.Write("\t Enter Your Number to Find Fibonacci ");
num = Convert.ToInt32(Console.ReadLine());

Console.Write("\n\t First " + num + " Even Fibonacci Numbers Are \n");

do
{
if (previous % 2 == 0)
{
Console.Write("\n\t " + previous + " ");
count++;
}
sum = previous + next;
next = previous;
previous = sum;

} while (count != num);

}
}
}

Output of the Program

C# Program code for Fibonacci sequence series