Showing posts with label solutions. Show all posts
Showing posts with label solutions. Show all posts

Wednesday, June 3, 2009

Allan's Pre-test MyProperty

Below is the Property class.



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

namespace BASIC
{
public class MyProperty
{
//fields
private string suburb;
private int bedrooms;
private int price;

//properties
public string Suburb
{
get { return suburb; }
set { suburb = value; }
}

public int Bedrooms
{
get { return bedrooms; }
set { bedrooms = value; }
}

public int Price
{
get { return price; }
set { price = value; }
}

//constructors
public MyProperty()
{
}

public MyProperty(string _suburb, int _bedrooms, int _price)
{
this.suburb = _suburb;
this.bedrooms = _bedrooms;
this.price = _price;
}


//methods
//override ToString() method
public override string ToString()
{
string msg = "";
msg = suburb.ToString() + ", " + bedrooms.ToString() + ", " + price.ToString() + "\n";
return msg;
}
}
}


Below is the form class:



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BASIC
{
public partial class Form1 : Form
{
//fields
private MyProperty[] propertyArray = new MyProperty[10];

//properties
public MyProperty[] PropertyArray
{
get { return propertyArray; }
set { propertyArray = value; }
}

//constructors
public Form1()
{
InitializeComponent();
}

#region methods
//methods

//check for valid fields
public bool checkAllFields()
{
try
{
if ((txtSuburb.Text != "") && (txtPrice.Text != "") && (txtBedrooms.Text != "") ) //check for empty fields
{
int testInt = int.Parse(txtPrice.Text);
testInt = int.Parse(txtBedrooms.Text); //if cannot parse, goto catch section and handle error

return true;
}
else
{
return false; //error occurred
}
}
catch
{
return false; //error occurred
}
}

//adds property
public bool addProperty(MyProperty propObj)
{
for (int i = 0; i < propertyArray.Length; i++)
{
if (propertyArray[i] == null)
{
propertyArray[i] = propObj;
return true;
}
}
return false;
}

//updates property
public bool updateProperty(MyProperty updatedObj)
{
for (int i = 0; i < PropertyArray.Length; i++)
{
if ((propertyArray[i] != null) && (propertyArray[i].Suburb == updatedObj.Suburb) && (propertyArray[i].Bedrooms == updatedObj.Bedrooms))
{
propertyArray[i] = updatedObj;
return true;
}
}

return false; //cannot find object
}

//delete property
public bool deleteProperty(string propName)
{
for (int i = 0; i < propertyArray.Length; i++)
{
if ((propertyArray[i].Suburb == propName) && (propertyArray[i] != null))
{
propertyArray[i] = null;
return true;
}
}
return false; //object not found
}

//get sum
public double getSum()
{
double sum = 0;
for (int i = 0; i < propertyArray.Length; i++)
{
if (propertyArray[i] != null)
{
sum += propertyArray[i].Price;
}
}
return sum;
}

//get average
public double getAverage()
{
double sum = 0;
double propitem = 0;
for (int i = 0; i < propertyArray.Length; i++)
{
if (propertyArray[i] != null)
{
sum += propertyArray[i].Price;
propitem++;
}
}
return (sum/propitem);//average
}

//get max
public double getMax()
{
double dMax = 0;

for (int i = 0; i < propertyArray.Length; i++)
{
if (propertyArray[i] != null)
{
if (propertyArray[i].Price >= dMax)
dMax = propertyArray[i].Price;
}
}
return (dMax);//Max
}

//get min
public double getMin()
{
bool arrayExist = false;
double dMin = 999999999;

for (int i = 0; i < propertyArray.Length; i++)
{
if (propertyArray[i] != null)
{
if (propertyArray[i].Price <= dMin)
dMin = propertyArray[i].Price;
arrayExist = true;
}
}
if (arrayExist == true)
{
return (dMin);//Min
}
else
{
return 0;
}
}

public void clearAllFields()
{
txtBedrooms.Text = "";
txtPrice.Text = "";
txtSuburb.Text = "";
}

#endregion

#region events
private void Form1_Load(object sender, EventArgs e)
{

}

private void addNewPropertyToolStripMenuItem_Click(object sender, EventArgs e)
{
if (checkAllFields() == true)
{
MyProperty propObj = new MyProperty();

propObj.Suburb = txtSuburb.Text;
propObj.Bedrooms = int.Parse(txtBedrooms.Text);
propObj.Price = int.Parse(txtPrice.Text);

bool bool_add = addProperty(propObj);

if (bool_add == true)
{
MessageBox.Show("Property added!");
clearAllFields(); //clears fields
}
else
{
MessageBox.Show("No more property can be stored in the array");
}

}
else
{
MessageBox.Show("Fields are not valid");
}
}

private void updatePropertyToolStripMenuItem_Click(object sender, EventArgs e)
{
if (checkAllFields() == true)
{
MyProperty updateProp = new MyProperty();

updateProp.Suburb = txtSuburb.Text;
updateProp.Price = int.Parse(txtPrice.Text);
updateProp.Bedrooms = int.Parse(txtBedrooms.Text);

if (updateProperty(updateProp)) //true
{
MessageBox.Show("Property updated!");
clearAllFields(); //clears fields
}
else //false
{
MessageBox.Show("Cannot find property for update!");
}

}
else
{
MessageBox.Show("Fields are not valid");
}
}

private void deletePropertyToolStripMenuItem_Click(object sender, EventArgs e)
{
bool deleteProp;
if (txtSuburb.Text != "")
{
deleteProp = deleteProperty(txtSuburb.Text);

if (deleteProp == true)
{
MessageBox.Show("Property deleted!");
clearAllFields(); //clears fields
}
else
{
MessageBox.Show("Cannot find property to delete");
}
}
else
{
MessageBox.Show("Please enter a Suburb name");
txtSuburb.Focus();
}
}

private void displayPropertiesToolStripMenuItem_Click(object sender, EventArgs e)
{
string allPropList = "";
for (int i = 0; i < propertyArray.Length; i++)
{
if (propertyArray[i] != null)
{
allPropList += propertyArray[i].ToString();
}
}

if (allPropList == "")
{
MessageBox.Show("No property found");
}
else
{
MessageBox.Show(allPropList);
}
}

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}

private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Property Application. Pre-test example");
}

private void displayTotalToolStripMenuItem_Click(object sender, EventArgs e)
{
double sum = getSum();
MessageBox.Show("Total Property Price : $ " + sum.ToString());
}

private void displayAverageToolStripMenuItem_Click(object sender, EventArgs e)
{
double average = getAverage();
MessageBox.Show("Total Average Price : $ " + average.ToString());
}

private void displayMaxToolStripMenuItem_Click(object sender, EventArgs e)
{
double max = getMax();
MessageBox.Show("Max Price : $ " + max.ToString());
}

private void displayMinToolStripMenuItem_Click(object sender, EventArgs e)
{
double min = getMin();
MessageBox.Show("Min Price : $ " + min.ToString());
}

#endregion

}
}

Friday, March 27, 2009

Monetary System - Version 2 - Using arrays & for loops - the proper way

Misa devised a brillant way to use arrays, loops and the TableLayoutPanel control to tackle the problem stated in the previous entry (version 1).  


But first, let's understand one important design concept, the TableLayoutPanel control.
We are used to display informations using labels, but we also found out the limitations of this control. With labels, it is very difficult to predict the display of the string or text being output. For example, a label might contain multiple lines of text (we might use '\n' for new lines, or '\t' for tabs).

With TableLayoutPanel, the organisation of any controls including labels, is made easier.
The TableLayoutPanel is just a series of rows and columns, analogous to a table.

One of the main advantage of using the TableLayoutPanel is that you can refer to each control in  a cell, just like an array. An array of controls, found in the Table. We will use that concept in this code.


For example, you might want to contain a list of Coins you have, and the number of coins, each represented on a different row, with separate columns for each category. You can do it with labels only, but the best design solution is to use the TableLayoutPanel with labels.

 

In this scenario, Coins are on the top row, whereas the numbers are below the respective coins, bottom row.  We create the table TableLayoutPanel1.


In design view, the table is set as follows, with 4 normal labels, one in each cell.

Now the nice part. You can actually refer to the labels in an 'array-like' fashion. For example, 

TableLayoutPanel1.Controls[0] will refer to label2.
TableLayoutPanel1.Controls[1] will refer to label3.
TableLayoutPanel1.Controls[2] will refer to label4.
TableLayoutPanel1.Controls[3] will refer to label5.
So this is why we should be using arrays and loops in this question. 
This is the end of the explanation on why we should use the TableLayoutPanel control. 


This is the final code for the home work.

   1:  int[] coins = { 50, 20, 10, 5 };
   2:  int[] numbers = { 0, 0, 0, 0 };
   3:  int iAmount = int.Parse(txtAmount.Text);
   4:  int iCoinsNum = 0;  //stores the number of coins used
   5:   
   6:  iAmount %= 100; //Get the 2 last digits
   7:   
   8:  for (int i = 0; i < coins.Length; i++)
   9:  {
  10:      numbers[i] = iAmount / coins[i];    //gets the number of particular coin in amount
  11:      iAmount = iAmount % coins[i];       //gets the remainder
  12:      tableLayoutPanel1.Controls[i].Text = coins[i] + " cent \n" + numbers[i];
  13:      //display the coins value and amount, in each label per column in the TableLayoutPanel
  14:      iCoinsNum += numbers[i];            //accumulates the amount of coins
  15:  }
  16:   
  17:  if (iCoinsNum == 0) //check if any coin was required
  18:  {
  19:      btnReset_Click(sender, e);
  20:      lblCoinsNum.Text = "No coins required.";
  21:  }
  22:  else
  23:  {
  24:      lblCoinsNum.Text = "" + iCoinsNum + " coins required.";
  25:  }



Note: The order of the Coins array is important, as you want to divide the 50c coins first before moving to the 20c coins, and so on.


Screenshots of design and runtime application

Wednesday, March 25, 2009

Have Your Say!

If you are copying-pasting, the file organisation is as follows:
  1. .myHTML.html
  2. .mysay.css
  3. /images/bg.png
  4. /images/textbg.gif


HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="yoursay.css" />
<title>Have your Say!</title>
</head>
<body>
<div id="wrapper">
<div id="formcontrols">
<form action="" method="post">
Name
<br/>
<input type="text" name="myname" size="30" id="eachcontrol" />
<br/><br/>
Email
<br/>
<input type="text" name="myemail" size="30" id="eachcontrol" />
<br/><br/>

http://
<br/>
<input type="text" name="myhttp" size="30" id="eachcontrol" />
<br/><br/>


Comments
<br/>
<textarea name="myComments" rows="2" cols="23" id="eachcontrol" >
</textarea>
<br/><br/>

<input type="button" name="preview" value="PREVIEW" class=
"previewbtn"
/>
<br/><br/>

Remember details?<br/>
<input type="checkbox" name="chkremember" />
<br/><br/>
</form>
</div>
<div id="myColumn">
<h3>HAVE YOUR SAY</h3>
<div id="content">
<p>Feel free top publish your thoughts but please be judicious
when communicating I am.</p>
<p>Gravatars are enabled. Go get yourself one.</p>
<p>All the cool kids use Textile.</p>
<p>Email address is required but will not be posted or
distributed.</p>
</div>
</div>
</div>
</body>

</html>




CSS

body
{
font-family:Verdana;
font-size:16px;
color:#ffffff;
margin:0 auto;

}

h3{
width:350px;
border-width: 0px 0px 3px 0px;
border-style:dashed;
font-size:30px;
padding:12px;
text-align:center;
background-color:orange;
margin:10px 0px 10px 0px;
padding:2px 2px 5px 2px;
}

#wrapper
{
margin:50px;
float:left;
background-image:url('images/bg.png');
background-repeat:repeat;
width:700px;
}

#formcontrols
{
width:280px;
float:left;
padding:10px;
}

#myColumn{
width:380px;
float:left;

}

#content{
margin-left:30px;
width:300px;
float:left;
}

#eachcontrol
{
background-image:url('images/textbg.gif');
background-repeat:repeat;
}

.previewbtn
{
border:3px solid;
border-color: #ffffff #000000 #000000 #ffffff;
color:#ffffff;
background-image:url('images/bg.png');
background-repeat:repeat;
padding:7px 20px 7px 20px;
}

Tuesday, March 24, 2009

Displaying the TAB escape character in a label

The '\t' character is a bit tricky. When you use the tab character, the compiler translates it to this squared character that we have all seen in class. This squared character is correctly interpreted, and displays perfectly in C# console. The only problem is that the tab character does not display properly in controls at form level.

Solution:
To display the escape character for '\t', I made use of the replace function.

private void button_Click(object sender, EventArgs e)
{
string s = "\tDenis\t";
s = s.Replace("\t", " ");
label1.Text = s.ToString();
}

Thursday, March 19, 2009

Hello World!

Hello there Cert IV Students (IT Programming)!

I've just set up this blog today 19 March 2009 [23 38] so that we can relax in our own comfort at home with a cup of coffee and discuss about anything related to our TAFE course (Cert IV IT Programming), or anything really...

Topics might include
  • Classwork related(C#, HTML, XHTML, CSS, and even how to create a blog or how to use diigo)
  • Homework - Let's discuss the possible solutions to any particular problem and the different algorithms or ways to tackle them (one problem --> many solutions)
  • Websites worth mentioning
  • the weather, good plans, outings etc
and are non-exhaustive.

I will also post weekly solutions to the classes, so check the website regularly, or add it to your CSS feed on diigo, or click FOLLOW ME link on the right hand side of this page.

As from today, I will update the solutions on the blog. If you have any questions, please do ask. Do not be shy. Everyone can give their own views on this blog. You are ENCOURAGED to do so.

Let's make this place an extra knowledge hub that will complement our classes!