Helping me with my Coursework!!!

Aimi Haraguni

Watching Anime 24/7
Dec 10, 2009
324
7
0
Would anyone with more advanced programming knowledge then myself be able to help me out on a few questions?

This is the code.
Spoiler for Code:
public interface IAccount
{
bool PayInFunds ( decimal amount );
bool WithdrawFunds ( decimal amount );
decimal GetBalance ();
string RudeLetterString();
string GetName ();
bool SetName ( string inName);
}
public abstract class Account : IAccount
{
private decimal balance = 0;
public abstract string RudeLetterString();
public virtual bool WithdrawFunds ( decimal amount )
{
if ( balance < amount )
{
return false ;
}
this.balance = this.balance - amount ;
return true;
}
public virtual decimal GetBalance ()
{
return this.balance;
}
public bool PayInFunds ( decimal amount )
{
if ( (balance + amount) < balance )
{
return false;
}
this.balance = this.balance + amount ;
return true;
}
private string name;
public string GetName()
{
return this.name;
}
public static string ValidateName ( string name )
{
string trimmedName = name.Trim();
if ( trimmedName.Length == 0 )
{
return "No text in the name";
}
return "";
}
public bool SetName ( string inName )
09/07/2015 5
{
string reply ;
reply = ValidateName(inName);
if ( reply.Length > 0 )
{
return false;
}
this.name = inName.Trim();
return true;
}
public Account ( string inName, decimal inBalance )
{
if ( !SetName(inName) )
{
throw new Exception("Invalid name");
}
this.balance = inBalance;
}
}
public class CustomerAccount : Account
{
public override string RudeLetterString()
{
return "You are overdrawn" ;
}
public CustomerAccount ( string inName, decimal inBalance ) :
base (inName, inBalance)
{
}
}
public class BabyAccount : Account
{
public override bool WithdrawFunds ( decimal amount )
{
if (amount > 10)
{
return false ;
}
return base.WithdrawFunds(amount);
}
public override string RudeLetterString()
{
return "Tell daddy you are overdrawn";
}
public BabyAccount ( string inName, decimal inBalance ) :
base (inName, inBalance)
{
}
}
public class Test
{
public static void Main ()
{
CustomerAccount test = new CustomerAccount ( "Fred", 25);
Console.WriteLine ( test.GetName() );
}
}


Questions


1. The system should not allow the creation of any acounts with names which are empty strings. Describe how the
programmer has accomplished this in the code above. Add code to prevent the creation of accounts with an
initial balance value which is less than 0 or greater than 10000.
Give the amended C# code.
[10 marks]
2. The ValidateName method in the Account class has been made static. Describe the effect of this.what
it means for users of the Account class and why it is a good idea to do this.
[10 marks]
3. There is a need for an additional constructor which would allow instances of the Account class to be created
without providing an initial balance value. In this situation the balance should be set to 0.
Write the C# code for this constructor.
[10 marks]
4. Describe how you could use the ToString method in the classes above to allow the class content to be
printed out. Create code to implement a ToString behaviour for the BabyAccount class. Ensure that it
uses behaviour in the parent Account class as appropriate.
Write the C# code for the ToString method.
[10 marks]
5. There is a need for a method called TestBank that wil serves as a test harness for the bank. It will create
instances of the account classes and perform tests on the management of the account balance that they provide.
The tests should be used to make sure that when a user pays in and attempts to withdraw funds the system
always behaves correctly. The following tests have been suggested:
 Create an account and make sure the initial balance is zero
 Create an account with an empty name string and make sure it is rejected
 Create an account with an initial balance value of -50 and make sure it is rejected
 Create an account with a balance of zero. Pay in 50 pounds and make sure that the balance is now 50
pounds.
Write the C# code for the TestBank method.
[10 marks]
6. At the moment a BabyAccount holder cannot withdraw more than 10 pounds at once. The bank has asked
you to change the program so that the amount that can be withdrawn from a BabyAccount can be set for
each account between 1 and 15 pounds. Add a data member maxBabyWithDraw to the BabyAccount and
create a method called SetMaxBabyWithdraw which will provide this behaviour. Create two tests for the
new method which make sure that the maximum withdrawal amount cannot be set above 15 or below 1 using
your code.
Give the complete C# code for each of the methods that you have modified.
 
1. There is a section for homework help

2. Use code tags next time

3. We can't help you unless you let us know what you need help with and if you are looking to be spoon fed, you've come to the wrong place

Edit: I guess you need help debugging this code. If you can't debug on your own you should change majors
 
1. There is a section for homework help

2. Use code tags next time

3. We can't help you unless you let us know what you need help with and if you are looking to be spoon fed, you've come to the wrong place

Yeah I really was looking to be spoon fed. I'm sorry but I have no idea how to approach these questions. I'll try the homework section. Thanks for taking the time to reply though
 
Yeah I really was looking to be spoon fed. I'm sorry but I have no idea how to approach these questions. I'll try the homework section. Thanks for taking the time to reply though

At least you were honest.

You learn nothing by being spoon fed, especially when it comes to programming. You should review the language and then attempt to problem solve the issues, because that's what programming is
 
  • Like
Reactions: thim slug
At least you were honest.

You learn nothing by being spoon fed, especially when it comes to programming. You should review the language and then attempt to problem solve the issues, because that's what programming is

Haha it was obvious, no point denying it. Not that I would anyways. It's some great advice and I would honestly do as you suggest if it wasn't for the fact it was due in 9 hours. I'm planning on switching courses which lead me to neglect this piece of work but I really wanted to if I can achieve some points by doing this, or as I was trying to do asking someone else too haha.
 
Haha it was obvious, no point denying it. Not that I would anyways. It's some great advice and I would honestly do as you suggest if it wasn't for the fact it was due in 9 hours. I'm planning on switching courses which lead me to neglect this piece of work but I really wanted to if I can achieve some points by doing this, or as I was trying to do asking someone else too haha.

Don't procrastinate
 
Moved to Homework.

I'd help you with this, but like Trixstar said/you admitted, you procrastinated and expected to be spoon fed at the last second. This is your coursework man.. I'm assuming it's for something college/university related. You gotta start taking it seriously and not pissing around with it.

Becoming a programmer isn't about just "knowing things", it's about understanding things and being able to apply them to all kinds of situations. Yeah I can spend 10-15 minutes of my time helping you and give you all the answers, but then you'll just "know the answers", you won't understand the answers. I'm not sure if you actually don't know/understand how to do this kind of work, or if you're just lazy, but if it's the former you need to start putting more time into your work to help yourself. Programming relies a lot on expanding your knowledge in various ways, not just sitting in class for 20 hours a week. Put effort into learning, and understanding what you're doing rather than just knowing.
 
Code:
public static string ValidateName ( string name )
 {
 string trimmedName = name.Trim();
 if ( trimmedName.Length == 0 )
 {
 return "No text in the name";
 }
 return "";
 }

1. The code above removes extra white space (This removes the possibility of having a length greater than 0 without actual characters). Then checks to see if the length is equal to 0. If so, sends a reply to a boolean method to be processed. If greater than 0 sends an empty reply.

Code:
public Account ( string inName, decimal inBalance )
 {
 if ( !SetName(inName) )
 {
   throw new Exception("Invalid name");
 }
if(inBalance < 0 || > 10000)
throw new Exception("Invalid Balance");
else
 this.balance = inBalance;
 }


2. A call to a static method generates a call instruction in Microsoft intermediate language (MSIL). whereas a call to an instance method generates a callvirt instruction, which also checks for a null object references. However, most of the time the performance difference between the two is not significant. So it's slightly faster than non-static because it does less.

3.
Code:
public Account ( string inName)
 {
 if ( !SetName(inName) )
 {
 throw new Exception("Invalid name");
 }
 this.balance = 0;
 }

4. the ToString method returns whatever value as a string.
Code:
    protected string ToString(object obj)
    {
        return obj.ToString();
    }

5.
Code:
public void TestBank()
{
 
Account acc = new Account("Kiryn");
Account acc1 = new Account("", 1337);
Account acc2 = new Account("Kiryn", -50);
Account acc3 = new Account("Kiryn");
acc3.payInFunds(50);
acc3.getBalance();
}


6.
Code:
private decimal limit;
Code:
public override bool WithdrawFunds ( decimal amount )
 {
 if (amount > limit)
 {
 return false ;
 }
 return base.WithdrawFunds(amount);
 }
Code:
    public decimal Limit { 
        get {
            return limit;
        } 
        set {
            limit = value;
        } 
    }
}

Edit: this took me like 20min bro. In the time you spent posting on the forums you could've had it done.
 
Code:
public static string ValidateName ( string name )
 {
 string trimmedName = name.Trim();
 if ( trimmedName.Length == 0 )
 {
 return "No text in the name";
 }
 return "";
 }

1. The code above removes extra white space (This removes the possibility of having a length greater than 0 without actual characters). Then checks to see if the length is equal to 0. If so, sends a reply to a boolean method to be processed. If greater than 0 sends an empty reply.

Code:
public Account ( string inName, decimal inBalance )
 {
 if ( !SetName(inName) )
 {
   throw new Exception("Invalid name");
 }
if(inBalance < 0 || > 10000)
throw new Exception("Invalid Balance");
else
 this.balance = inBalance;
 }


2. A call to a static method generates a call instruction in Microsoft intermediate language (MSIL). whereas a call to an instance method generates a callvirt instruction, which also checks for a null object references. However, most of the time the performance difference between the two is not significant. So it's slightly faster than non-static because it does less.

3.
Code:
public Account ( string inName)
 {
 if ( !SetName(inName) )
 {
 throw new Exception("Invalid name");
 }
 this.balance = 0;
 }

4. the ToString method returns whatever value as a string.
Code:
    protected string ToString(object obj)
    {
        return obj.ToString();
    }

5.
Code:
public void TestBank()
{
 
Account acc = new Account("Kiryn");
Account acc1 = new Account("", 1337);
Account acc2 = new Account("Kiryn", -50);
Account acc3 = new Account("Kiryn");
acc3.payInFunds(50);
acc3.getBalance();
}


6.
Code:
private decimal limit;
Code:
public override bool WithdrawFunds ( decimal amount )
 {
 if (amount > limit)
 {
 return false ;
 }
 return base.WithdrawFunds(amount);
 }
Code:
    public decimal Limit {
        get {
            return limit;
        }
        set {
            limit = value;
        }
    }
}

Edit: this took me like 20min bro. In the time you spent posting on the forums you could've had it done.
Thanks man. Going to submit it a tad late, but I'm sure my lecturer will understand.
 

Users who are viewing this thread (total: 1, members: 0, guests: 1)