Dan Leightley

Dan Leightley's Blog Dan is a developer from Manchester, UK specialising in a range of languages such as Java/C++, ASP.NET and PHP/MySQL. I am a recent graduate of Manchester Metropolitan University studying Information and Communications BSc.

11 May 2012 ~ 0 Comments

PHP Security

Currently for my MSc project I have been exploring cross-virtual machine attacks and come across a very interesting article. The article by PHPMaster.Com (See link below) focuses on XSS attacks and interesting points a finger at lazy coders who fail to adequately code to prevent such basic misuses.

PHP Security Issues – XSS

26 December 2011 ~ 0 Comments

Student Class – Java

One important aspect of Java is inheritance, and how we do this in Java is to use the keyword Extend. This example, following on from assignment that was set is for the Student Class with Extends the Person class. As seen in the Person class – Java post.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
public class Student extends Person {
//Protected variables
protected Date dateEnrolled;
protected String course;
protected int year;

//Constructors
/**
* Set default values and create the student
*/
Student()
{
super();
dateEnrolled = new Date();
course = "No course yet added!";
year = 0;
}

/**
* Allow input for the creation of a student
*/
Student(String name, char sex, Date dob, String insceNumber, Date enrolled)
{
super(name, sex, dob);
natInsceNo = insceNumber;
dateEnrolled = new Date(enrolled);
course = "No course yet added!";
year = 0;
}

/**
* Allows for the cloning of a student
*/
public Student(Student other)
{//need to call the super class clone
super(other);
dateEnrolled = other.dateEnrolled;
course = other.course;
year = other.year;
}

//End of Constructors

//Start Transformers

/**
* Set the course
*/
public void setCourse(String s)
{
course = s;
}

/**
* Set the year
*/
public void setYear(int yr)
{
year = yr;
}

//Start of copy method
public void copy(Student other)
{
super.copy(other);
dateEnrolled = other.dateEnrolled;
course = other.course;
year = other.year;
}
//End of copy method

//End of Transformers

//Start of Accessors
//Get accessors
public Date getDate()
{
return dateEnrolled;
}

/**
* @return
*/
public String getCourse()
{
return course;
}

/**
* @return
*/
public int getYear()
{
return year;
}
//End of get Accessors

//Start of toString
public String toString()
{
return super.toString() + ", Course: " + course + ", Date Enrolled: " + dateEnrolled + ", Year:" + year;
}
//End of toString
//End of Accessors
}

So what we are doing here is inheriting all of the Person Class methods and using them to create a student. As we can have multiple students which inherit from a Person. Such as a Teacher is a person, therefore could extend from Person as well. Take a look at the code and try to work out what it is actually doing and focus on the Date dateEnrolled aspect.

22 October 2011 ~ 0 Comments

Employee Class – Java

This post relates to Person Class – Java, with Employee class extending from Person (Basically inheriting from Person). I’ll update this post soon.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
public class Employee extends Person {

	private Date dateStarted;
	private float salary;
	private int id;

	public Employee(String nme, char sex, int howOld, int number, Date start)
	{
		super(nme, sex, howOld);
		id = number;
		dateStarted = new Date(start);
		salary = 0;

	}

	//calls data from Person
	public boolean equals(Employee other)
	{
		return super.equals(other) && id == other.id && salary == other.salary && dateStarted.equals(other.dateStarted);
	}

	//allows for the setting of a salary with an input
	public void setSalary(float newSalary)
	{
		salary = newSalary;
	}

	public Employee (Employee other)
	{ //needs to match employee constructor and the same format and allows for the creator of another employee
		this(other.name, other.gender, other.age, other.id, other.dateStarted);
	}

	//Need to have all the variables to copy
	public void copy(Employee other)
	{
		dateStarted.copy(other.dateStarted);
		salary = other.salary;
		id = other.id;
		name = other.name;
		gender = other.gender;
		age = other.age;
	}

	public Date getDateStarted()
	{
		return dateStarted;
	}

	public String toString()
	{
		return "Employee Number: " + id + "Salary" + salary + "Started: " + dateStarted.toString();
	}
}

04 October 2011 ~ 0 Comments

Person class – Java

Hey guys,

Well I’ve not updated this site is a long time, been way too busy. My studies have been processing rapidly, currently working on Java inheritance. So, starting basic I’ve been working on a Person class, which will allow for the creation of a Person (Note that this is a basic version and you would have to make changes for it to be of the correct standard and conventions of java). Here is it:-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
public class Person {
 //variables
  protected int age, serial;
  protected char gender;
  protected String name;
  protected String address;
  private static int counter;

  //constructor
  public Person (String nme, char sex, int howOld)
  {
	  	age = howOld;
   		gender = sex;
   		name = nme;
   		address = "";
   		counter ++;
   		serial = counter + 1001;

  }

  public Person(Person other)
  {
	  name = other.name;
	  age = other.age;
	  gender = other.gender;
	  address = other.address;
  }

  //count every instance of person
  public static int count()
  {
	  return counter;
  }

  public void copy (Person other)
  {
	   age = other.age;
	   gender = other.gender;
	   name = other.name;
	   address = other.address;

  }

// accessor methods
  public int getAge()
   {
    return age;

   }//end get age

  public String toString()
  {
	  return name + " " + address;
  }

   //get gender
   public String getGender()
   {
    if(gender == 'm' || gender == 'M')
    	return "male";
    else
    	if (gender == 'f' || gender == 'F')
    	return "female";
    else
    	return "unknown";
   }
   //check to see if older
   public boolean isOlderThan(Person other)
   {
	   {
		if (age >= other.age)
			return true;
		else
			return false;
	   }

   }

	//checking to see if the same gender
	public boolean sameGender(Person other)
	{
		return gender == other.gender;
	}

	//Input a value to check age
	public boolean olderThan(int otherAge)
	{
		return age >= otherAge;
	}

	//check to see if female
	public boolean isFemale()
	{
		return gender == 'f' || gender == 'F';
	}

	public boolean equals (Person other)
	{
		return name.equals(other.name) && gender == other.gender && age == other.age && address.equals(other.address);
	}

   //transformer methods
   public void incrementAge()
   {
    age ++;
   }

   //get new address
   public void setAddress(String newAddress)
   {
    address = newAddress;
   }

}

So what does the code do? It allows for the adding of a instance called Person, with a name, gender and how old they are being set by the user via the constructor. Everything else is very self explanatory , expect the boolean equals (Person other) which checks two [persons to see if they match, and returns true if they do.

09 June 2011 ~ 0 Comments

Example Java – Multiplications Workout

Hey guys,

Well I present one of my little coding examples that I’ve been working on – It’s very basic and does the job (I plan on adding a GUI at some point). What it does is allow you to enter a number such as 7, and then displays the 7 times table! It uses a for loop to increment the times table and display the result.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//Work out the times tables of what is inputted
import java.util.*;
public class TimesTable {</code>

<code>public static void main(String[] args) {</code>

<code>int num, result;
Scanner keyboard = new Scanner(System.in);
System.out.println("***Times Tables***");
System.out.println("What Times Table would you like? ");
num = keyboard.nextInt(); //input number
System.out.println("Times Table of " + num + " is :");
//start the for loop
for (int a = 1; a <= 12; a++) //go up to 12 with loop
{
System.out.print( + a  + " x " + num); //display the multiplication
result = num * a;  // work out the result
System.out.println(" = " + result); //display result
}
}
}

Looking for any comments on how to make it better :-)

23 May 2011 ~ 0 Comments

Basic Computer Ordering – Java

Hey guys,

Well I’ve got another example for you. This example allows for a user to buy a computer, and then select a monitor and then allows them to select the a special promotional item to go with it. The Java coding uses an If…else and Switch statement to get the desired result.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.util.*; //Basic order system with multiple suggestions - upsell
public class ComputerOrder {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner(System.in);
double total, computer, screen1, screen2, selection, dvd, printer;
computer = 375.99; //cost of computer
screen1 = 75.99; //cost of screen
screen2 = 99.99; //cost of screen
total = 0;
System.out.println("You have ordered the basic model costing £" + computer);
System.out.println("If you would you like a 38cm screen? Press 1: Costing: £" + screen1);
System.out.println("If you woud like  43cm screen? Press 2: Costing: £" + screen2);
selection = keyboard.nextDouble(); //input users selection
if ( selection == 1) // if 1 do this
{
System.out.println("You have selected the 38cm screen");
total = computer + screen1; // work out the cost
}
else
{
if (selection == 2) // work out selection 2
{
System.out.println("You have selected the 43cm");
total = computer + screen2; // cost of selection
}
}
System.out.println("**Extra Items**");
System.out.println("DVD/CD Write - Cost 65.99 - Press A");
System.out.println("Printer - 125.00 - Press B");

dvd = 65.99; //declare cost and dvd
printer = 125.00; // declare printer and cost
char upsell; // used for switch
upsell = keyboard.next().charAt(0);
switch(upsell)
{
case 'A' : total = total + dvd; System.out.println("The total cost is £" + total);
break;
case 'B' : total = total + printer; System.out.println("The total cost is £" + total);
break;
default : System.out.println("Error - We could not find yoru suggestion!");

}

}
}

So as you can see, it first of all asks the user to select which monitor they would like, and then which add-on (printer or dvd player) and then displays the total price. A good programme that actually has some real world application!

23 May 2011 ~ 0 Comments

User sum test – Java

Well, it’s been a busy day! I’ve been busy going through “Java in Two Semester” and have just coded a program that asks a user to enter two numbers and then asks the user to guess the sum of the two numbers. If they get it right, a well done message is displayed, if its wrong then a sympathy message is shown:-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.util.*; // Ask for a user to input two numbers, work out the sum and ask for a guess

public class AskSum {
public static void main(String[] args)
	{
	Scanner keyboard = new Scanner(System.in);
	int num1, num2, sum, guess;
	System.out.println("***Guess the number game***");
	System.out.println();
	System.out.println("Please enter the first number: ");
	num1 = keyboard.nextInt();
	System.out.println("Please enter the second number: ");
	num2 = keyboard.nextInt();

	System.out.println("Now guess the total: ");
	guess = keyboard.nextInt();

	sum = num1 + num2;
	if (guess == sum)
	{
		System.out.println("Well done, yu guessed right, the answer was: " + sum );
		System.out.println("With your guess off: " + guess);
	}

	else
	{
		System.out.println("Sorry, you got the answer wrong, the right answer was: " + sum);
		System.out.println("With your guess off: " + guess);

	}
	System.out.println("Thanks for trying - Give it another go?");

	}
}

Enjoy – hope it helps someone

23 May 2011 ~ 0 Comments

Child Discount – Java Example

Well today I have finally written my first piece of code (From memory) and thought I would share it with you. The code asks for the users age, and then applies a discount if they are under that age. Basic I know, but it does the job.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//ask for users age and then discount if under 14
import java.util.*;
public class ChildDiscountIf {

public static void main(String[] args) {
//Set the price and double - long number
double price = 10.00;
int age;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter your age:- ");
age = keyboard.nextInt(); // ask for input

if (age <= 14) // if 14 or below add the child discount
{
System.out.println("Half price Child Ticket");
price = price/2;
}

System.out.println("Total price: £" + price);
}
}

It’s rather basic, but like I have said – It does the job and at least I’m learning

22 May 2011 ~ 0 Comments

Java Day

Luckily I have been able to get the day off work – So as well as watching the Football (Closest relagation battle since the start of the Premiership!), I’ve decided to do some Java work by going through the “Java in Two Semesters” book. I’ve not got too far yet, however I’ve already started to develop basic classes to execute some basic commands. For example:-

-Swapping two variables

-Work out when someone was born (Useful for what I may be doing for next years Dissertation)

A few hours a day and I should have Java in the bag, well the basics at least. I’ll post some examples of what I’ve been doing when I’ve refined the code and made it a little bit more unique.

Dan

14 May 2011 ~ 2 Comments

C++ and Java

Well today I have finally had the chance to site down and learn some Java, with “Learning Java in Two Semesters” – So far so good, but, I have a long way to go before I can say I am any good at it. The next stage after learning the basics of Java will be to move onto C++ to enable me to widen my programming skills.

I will be using Bjarne Stroustrup’s “Programming: Principles and Practise Using C++” to guide me through the basics and give me the start that I need. It’s going to be one long enjoyable summer!

I will be posting some of the exercises and my own attempts at coding in C++ and Java on the blog so make sure you keep an eye out.

Thanks,

Dan

Tags: , ,
show
 
close
#oGame still play it after so many years, if anyone is #bored give it a go http://t.co/3HKrOZtR