2.9 KiB
2.9 KiB
Lecture 1 (11:00)
- Class is a template
- Object is the product of a class
- Classes have attributes / fields, and methods, which can have parameters
- Attributes are shared between all instances of the class
- Methods are executable code, which change depending on the parameters given
- Extra information = argument
Lecture 2 (13:00)
Types
- System.out.println()- print line function
void changeColor(String newColor)
- newColor is a parameter, of type String
- A parameter receives the argument
void withdraw(int amount)
- amount is a parameter with type integer, holding a whole number
- withdraw is a method
void withdraw()
int getBalance()
- void type means a method does not return a value / returns a null value
- int type means a method returns an integer type
public int getBalance()
{
return balance;
}
- Empty () tells the compiler that
getBalance
is a method and not an integer variable return balance;
passes the integer value of the variablebalance
as the result of the method. ifbalance
is 200, x.getBalance() would result in 200 as an integer.- Return type must always be included in method header.
Bank Account Class
public class Account
{
// Account Balance
private int balance;
private int overdraftLimit;
/**
* Constructor for objects of class Account
*/
public Account()
{
// initialise account balance
balance = 10;
overdraftLimit = -100;
cash = 0;
}
/**
* @param no parameter needed
* @return the balance
*/
public int getBalance()
{
// put your code here
return balance;
}
/**
* Display current overdraft limit
*/
public int getOverdraft()
{
return overdraftLimit;
}
/**
* Set a new overdraft limit, overwriting with @newOverdraftLimit
*/
public void modifyOverdraft( int newOverdraftLimit )
{
overdraftLimit = newOverdraftLimit;
System.out.println( "Your overdraft limit is now £" + newOverdraftLimit );
}
/**
* Deposit @amount into the account
*/
public void doDeposit( int amount )
{
balance = balance + amount;
System.out.println( "Your account has been credited with £" + amount + ". Your balance is now £" + balance );
}
/**
* If withdraw amount is less than total usable balance, allow withdraw
* Else reject and show balance
*/
public void withdraw( int amount )
{
private int cash;
if( overdraftLimit < ( balance - amount ))
{
balance = balance - amount;
cash = amount;
System.out.println( "You have withdrawn £" + cash );
}
else
{
System.out.println( "Not enough balance to withdraw. Your balance is: £" + balance );
}
}
}