Commit e729182b authored by jithushan.umaipalan's avatar jithushan.umaipalan

Initial commit

parents
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}
## Complex Numbers Assessment Exercise 1
## 1 Skills
This exercise should help you with:
1. Member variables and member methods.
2. Unit testing.
3. Creation of a class to a specification without necessarily understanding the details.
## 2 Background
A complex number is one that has two parts: **real** and **imaginary**. They are written (by engineers at least) like this
* 4.1 + j9.9
* 0.5 + j3.2
Or in general
* a + *j*b
There are a few operations defined for complex numbers. If a complex number has a real part called **a** and an imaginary part called **b** we have
* **magnitude** = square root of (**a** squared + **b** squared) (using `Math.sqrt()` in java)
* **argument** = tan−1(b/a) (using `Math.atan2(b,a)` in Java)
**Addition**
To add two complex numbers we add the real parts to give the new real part and the imaginary parts to give the new imaginary part.
CN1 = a +*j*b
CN2 = c +*j*d
CN1 + CN2 = (a+c) + *j*(b+d)
## 3 Unit Testing
This project is set up with unit testing enabled. Unit testing is a development technique where tests can be created in advance and used to help ensure the code is corrent when it is written.
Click on the `Testing` icon in the left hand tool bar. It looks like a flask. Open up `javaAssessEx1`, then `<Default Package>`, then `AppTest`. You should now see a list of tests.
* testConstructor()
* testMagnitude()
* testArgument()
* testAdd()
* testToString()
You can right click on `AppTest` and choose `Run Test` to run all the tests or you can run each test individually by right clicking on each one and choosing `Run Test`.
## 4 Task
You need to complete the following methods.
* public double magnitude()
* public double argument()
* public String toString()
* public Complex add(Complex complexNum)
Looking at the tests in AppTest.java might help you see how these methods will be used. Section 2 above will show you how to calculate the magnitude and argument and how to add two complex numbers together.
`toString()` should return a string in the format `a + jb` where `a` is the real component and `b` is the imaginary component. Note the rules below should be followed.
|Real Part|Imaginary Part|String|
|---|---|---|
|`2.6`|`5.2`|`2.6 + j5.6`|
|`2.6`|`0`|`2.6`|
|`0`|`5.2`|`j5.2`|
|`2.6`|`-9.8`|`2.6 - j9.8`|
If the real or imaginary part is 0 it should not be shown. If the imaginary part is negative the negative symbol should be before the j not the number (i.e. `4.3 - j2.1` not `4.3 + j-2.1`).
If you have completed the methods correctly all the unit tests will pass. Please note that hardcoding the methods to return the correct answers for the tests carried out in the unit tests you will get zero marks.
## 5 Marking
25 marks are available for this exercise. This breaks down as follows.
* public double magnitude() - 5 marks
* public double argument() - 5 marks
* public String toString() - 10 marks
* public Complex add(Complex complexNum) - 5 marks
## 6 Submission
Your project should be uploaded to your gitlab account and you should submit a link to the gitlab project, alongside the rest of the assessment, through moodle, by 12 noon 16/05/22.
\ No newline at end of file
public class App {
public static void main(String[] args) throws Exception {
System.out.println("Hello, World!");
AppTest test = new AppTest();
test.testAdd();
}
}
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class AppTest {
@Test
public void testConstructor() {
/* Constructor should put the first parameter as the real part and second as the imaginary part.
* real() should return real part, imag() imaginary part.
*/
Complex a = new Complex(4.2, 5.7);
assertEquals(4.2, a.real(), 1e-6);
assertEquals(5.7, a.imag(), 1e-6);
Complex b = new Complex();
assertEquals(0, b.real(), 1e-6);
assertEquals(0, b.imag(), 1e-6);
}
@Test
public void testMagnitude() {
/* magnitude() should return the magnitude of the complex number */
Complex a = new Complex(9.6, 11.5);
assertEquals(14.98032042, a.magnitude(), 1e-6);
}
@Test
public void testArgument() {
/* argument() should return the argument of the complex number */
Complex a = new Complex(9.6, 11.5);
assertEquals(0.87520335, a.argument(), 1e-6);
}
@Test
public void testToString() {
/* toString() should return a string like 1.2 + j3.4 where 1.2 is the real part
* and 3.4 the imaginary part.
*/
Complex a = new Complex(1.2, 3.4);
assertEquals("1.2 + j3.4", a.toString());
Complex b = new Complex(1.2, 0.0);
assertEquals("1.2", b.toString());
Complex c = new Complex(0.0, 9.3);
assertEquals("j9.3", c.toString());
Complex d = new Complex(1.2, -3.4);
assertEquals("1.2 - j3.4", d.toString());
}
@Test
public void testAdd() {
/* add() should return a new complex number which is the sum of this and the parameter */
Complex a = new Complex(6.6, 12.9);
Complex b = new Complex(13.1, 13.5);
Complex c = a.add(b);
assertEquals(19.7, c.real(), 1e-6);
assertEquals(26.4, c.imag(), 1e-6);
}
}
public class Complex {
public double real;
public double imag;
public Complex() {
real = 0;
imag = 0;
}
public Complex(double r, double i) {
real = r;
imag = i;
}
public double imag() {
return imag;
}
public double real() {
return real;
}
public double magnitude() {
return Math.sqrt(real * real + imag * imag);
}
public double argument() {
return Math.atan2(imag, real);
}
public String toString() {
if (imag == 0) {
return real + "";
} else if (real == 0) {
return "j" + imag;
} else if (imag < 0) {
return real + " - j" + (-imag);
} else {
return real + " + j" + imag;
}
}
public Complex add(Complex complexNum) {
Complex sum = new Complex();
sum.real = this.real + complexNum.real;
sum.imag = this.imag + complexNum.imag;
return sum;
}
}
\ No newline at end of file
## Complex Numbers Assessment Exercise 1
## 1 Skills
This exercise should help you with:
1. Member variables and member methods.
2. Unit testing.
3. Creation of a class to a specification without necessarily understanding the details.
## 2 Background
A complex number is one that has two parts: **real** and **imaginary**. They are written (by engineers at least) like this
* 4.1 + j9.9
* 0.5 + j3.2
Or in general
* a + *j*b
There are a few operations defined for complex numbers. If a complex number has a real part called **a** and an imaginary part called **b** we have
* **magnitude** = square root of (**a** squared + **b** squared) (using `Math.sqrt()` in java)
* **argument** = tan−1(b/a) (using `Math.atan2(b,a)` in Java)
**Addition**
To add two complex numbers we add the real parts to give the new real part and the imaginary parts to give the new imaginary part.
CN1 = a +*j*b
CN2 = c +*j*d
CN1 + CN2 = (a+c) + *j*(b+d)
## 3 Unit Testing
This project is set up with unit testing enabled. Unit testing is a development technique where tests can be created in advance and used to help ensure the code is corrent when it is written.
Click on the `Testing` icon in the left hand tool bar. It looks like a flask. Open up `javaAssessEx1`, then `<Default Package>`, then `AppTest`. You should now see a list of tests.
* testConstructor()
* testMagnitude()
* testArgument()
* testAdd()
* testToString()
You can right click on `AppTest` and choose `Run Test` to run all the tests or you can run each test individually by right clicking on each one and choosing `Run Test`.
## 4 Task
You need to complete the following methods.
* public double magnitude()
* public double argument()
* public String toString()
* public Complex add(Complex complexNum)
Looking at the tests in AppTest.java might help you see how these methods will be used. Section 2 above will show you how to calculate the magnitude and argument and how to add two complex numbers together.
`toString()` should return a string in the format `a + jb` where `a` is the real component and `b` is the imaginary component. Note the rules below should be followed.
|Real Part|Imaginary Part|String|
|---|---|---|
|`2.6`|`5.2`|`2.6 + j5.6`|
|`2.6`|`0`|`2.6`|
|`0`|`5.2`|`j5.2`|
|`2.6`|`-9.8`|`2.6 - j9.8`|
If the real or imaginary part is 0 it should not be shown. If the imaginary part is negative the negative symbol should be before the j not the number (i.e. `4.3 - j2.1` not `4.3 + j-2.1`).
If you have completed the methods correctly all the unit tests will pass. Please note that hardcoding the methods to return the correct answers for the tests carried out in the unit tests you will get zero marks.
## 5 Marking
25 marks are available for this exercise. This breaks down as follows.
* public double magnitude() - 5 marks
* public double argument() - 5 marks
* public String toString() - 10 marks
* public Complex add(Complex complexNum) - 5 marks
## 6 Submission
Your project should be uploaded to your gitlab account and you should submit a link to the gitlab project, alongside the rest of the assessment, through moodle, by 12 noon 16/05/22.
\ No newline at end of file
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}
## Assessment Exercise 2 - Shapes
## 1 Skills
This exercise should help you with:
1. Member variables and member methods.
2. Unit testing.
3. Inheritance
4. Encapsulation
5. Abstraction
## 2 Shapes
The code in this project contains an abstract class `Shape` used to define the methods any shape class must possess (see the Inheritance section of the module website).
I've created a `Triangle` class as an example shape for you.
## 3 Unit Testing
This project is set up with unit testing enabled. Unit testing is a development technique where tests can be created in advance and used to help ensure the code is corrent when it is written. See Assessment Exercise 1 for more details on Unit Testing.
## 4 Task
You need to create to classes which inherit (`extend`) from `Shape`. These are
* `Square`
* `Rectangle`
### `Square`
The `Square` class should store the information and methods related to a square shape.
1. `size` should be used to represent the width and height of the square.
2. `Square` should have two constructors. The first should have no parameters and set the size equal to 3. The second should take a single integer parameter and use it to set the size.
3. You should be able to use the `getSize()` and `setSize()` methods properly for the `Square`.
4. You should correctly implement the `area()` and `circumference()` methods for `Square`.
5. You should implement a `draw()` method that will output an ascii square of the correct size.
6. All the Square unit tests must be passed.
### `Rectangle`
The `Rectangle` class should store the information and methods related to a Rectangle shape.
1. Instead of using `size` you should use `width` and `hieght` to represent the width and height of the rectangle.
2. `Rectangle` should have two constructors. The first should have no parameters and set the `width` equal to 3 and the `height` equal to 4. The second should take a two integer parameter and uses them to set the `width` and `height` (in that order).
3. You should be able to use the `getWidth()` and `setWidth()` methods properly for the `Rectangle`.
4. You should be able to use the `getHeight()` and `setHeight()` methods properly for the `Rectangle`.
5. You should correctly implement the `area()` and `circumference()` methods for `Rectangle`.
6. You should implement a `draw()` method that will output an ascii rectangle of the correct size.
7. All the Rectangle unit tests must be passed.
## 5 Marking
25 marks are available for this exercise. This breaks down as follows.
### `Square` (10 Marks)
* constructors - 2 marks
* setSize(), getSize() - 2 marks
* area(), circumference() - 2 marks
* draw() - 2 marks
* Pass all Square Unit Tests - 2 marks
### `Rectangle` (15 Marks)
* constructors - 3 marks
* setWidth(), getWidth(), setHeight(), getHeight() - 3 marks
* area(), circumference() - 3 marks
* draw() - 3 marks
* Pass all Square Unit Tests - 3 marks
## 6 Submission
Your project should be uploaded to your gitlab account and you should submit a link to the gitlab project, alongside the rest of the assessment, through moodle, by 12noon 16/05/22.
\ No newline at end of file
import shapes.*;
public class App {
public static void main(String[] args) throws Exception {
// Shape shape = new Rectangle();
// shape.setHeight(4);
// shape.setWidth(10);
// System.out.println("Width: " + shape.getWidth());
// System.out.println("Height: " + shape.getHeight());
// System.out.println("Area: " + shape.area());
// System.out.println("Circumference: " + shape.circumference());
// shape.draw();
}
}
## Assessment Exercise 2 - Shapes
## 1 Skills
This exercise should help you with:
1. Member variables and member methods.
2. Unit testing.
3. Inheritance
4. Encapsulation
5. Abstraction
## 2 Shapes
The code in this project contains an abstract class `Shape` used to define the methods any shape class must possess (see the Inheritance section of the module website).
I've created a `Triangle` class as an example shape for you.
## 3 Unit Testing
This project is set up with unit testing enabled. Unit testing is a development technique where tests can be created in advance and used to help ensure the code is corrent when it is written. See Assessment Exercise 1 for more details on Unit Testing.
## 4 Task
You need to create to classes which inherit (`extend`) from `Shape`. These are
* `Square`
* `Rectangle`
### `Square`
The `Square` class should store the information and methods related to a square shape.
1. `size` should be used to represent the width and height of the square.
2. `Square` should have two constructors. The first should have no parameters and set the size equal to 3. The second should take a single integer parameter and use it to set the size.
3. You should be able to use the `getSize()` and `setSize()` methods properly for the `Square`.
4. You should correctly implement the `area()` and `circumference()` methods for `Square`.
5. You should implement a `draw()` method that will output an ascii square of the correct size.
6. All the Square unit tests must be passed.
### `Rectangle`
The `Rectangle` class should store the information and methods related to a Rectangle shape.
1. Instead of using `size` you should use `width` and `hieght` to represent the width and height of the rectangle.
2. `Rectangle` should have two constructors. The first should have no parameters and set the `width` equal to 3 and the `height` equal to 4. The second should take a two integer parameter and uses them to set the `width` and `height` (in that order).
3. You should be able to use the `getWidth()` and `setWidth()` methods properly for the `Rectangle`.
4. You should be able to use the `getHeight()` and `setHeight()` methods properly for the `Rectangle`.
5. You should correctly implement the `area()` and `circumference()` methods for `Rectangle`.
6. You should implement a `draw()` method that will output an ascii rectangle of the correct size.
7. All the Rectangle unit tests must be passed.
## 5 Marking
25 marks are available for this exercise. This breaks down as follows.
### `Square` (10 Marks)
* constructors - 2 marks
* setSize(), getSize() - 2 marks
* area(), circumference() - 2 marks
* draw() - 2 marks
* Pass all Square Unit Tests - 2 marks
### `Rectangle` (15 Marks)
* constructors - 3 marks
* setWidth(), getWidth(), setHeight(), getHeight() - 3 marks
* area(), circumference() - 3 marks
* draw() - 3 marks
* Pass all Square Unit Tests - 3 marks
## 6 Submission
Your project should be uploaded to your gitlab account and you should submit a link to the gitlab project, alongside the rest of the assessment, through moodle, by 12noon 16/05/22.
\ No newline at end of file
package shapes;
public class Rectangle extends Shape {
// A rectangle
protected int width;
protected int height;
public Rectangle() {
super(3, 4);
this.width = 3;
this.height = 4;
}
public Rectangle(int width, int height) {
super(width, height);
this.width = width;
this.height = height;
}
@Override
public void setHeight(int height) {
super.setHeight(height);
this.height = height;
}
@Override
public void setWidth(int width) {
super.setWidth(width);
this.width = width;
}
// @Override
// public int getHeight() {
// return this.height;
// }
// @Override
// public int getWidth() {
// return this.width;
// }
@Override
public void draw() {
// draw a rectangle
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
System.out.print("*");
}
System.out.println("");
}
}
@Override
public float area() {
// area of a rectangle
return width * height;
}
@Override
public float circumference() {
// circumference of a rectangle
return 2 * (width + height);
}
}
package shapes;
abstract public class Shape {
protected int size;
protected int width;
protected int height;
abstract public void draw();
abstract public float area();
abstract public float circumference();
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
// getters and settters for width and height
public int getWidth() {
return this.width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return this.height;
}
public void setHeight(int height) {
this.height = height;
}
public Shape() {
this.size = 3;
}
public Shape(int size) {
this.size = size;
}
public Shape(int width, int height) {
this.width = width;
this.height = height;
}
}
package shapes;
public class Square extends Shape {
protected int size;
public Square() {
super();
this.size = 3;
}
public Square(int size) {
super(size);
this.size = size;
}
@Override
public void setSize(int size) {
this.size = size;
}
@Override
public int getSize() {
return this.size;
}
@Override
public void draw() {
// draw a square
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
System.out.print("*");
}
System.out.println("");
}
}
@Override
public float area() {
// area of a square
return size * size;
}
@Override
public float circumference() {
// circumference of a square
return size * 4;
}
}
package shapes;
public class Triangle extends Shape {
// An equilateral triangle
public Triangle() {
super();
}
public Triangle(int i) {
super(i);
}
@Override
public void draw() {
for (int loop1 = 0; loop1 < size; loop1++) {
for (int loop2 = loop1; loop2 < (size - 1); loop2++) {
System.out.print(" ");
}
System.out.print("/");
for (int loop2 = 0; loop2 < loop1; loop2++) {
System.out.print(" ");
}
System.out.println("\\");
}
for (int loop1 = 0; loop1 < size; loop1++) {
System.out.print("--");
}
System.out.println("");
}
@Override
public float area() {
float fSize = size;
float area = 0.5f * fSize * fSize;
return area;
}
@Override
public float circumference() {
float fSize = size;
float circ = fSize + fSize + fSize;
return circ;
}
}
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import shapes.*;
public class unitTest {
// *********************************
// Triangle Tests
// *********************************
@Test
public void testTriangleConstructor() {
// test default constructor
Shape testShape = new Triangle();
assertEquals(3, testShape.getSize());
// test constructor with size parameter
testShape = new Triangle(5);
assertEquals(5, testShape.getSize());
}
@Test
public void testTriangleSize() {
// test setSize()
Shape testShape = new Triangle(5);
testShape.setSize(6);
assertEquals(6, testShape.getSize());
}
@Test
public void testTriangleCircumference() {
// test circumference()
Shape testShape = new Triangle(6);
assertEquals(18.0, testShape.circumference(), 1e-6);
}
@Test
public void testTriangleArea() {
// test area()
Shape testShape = new Triangle(6);
assertEquals(18.0, testShape.area(), 1e-6);
}
// *********************************
// Square Tests
// *********************************
@Test
public void testSquareConstructor() {
// test default constructor
Shape testShape = new Square();
assertEquals(3, testShape.getSize());
// test constructor with size parameter
testShape = new Square(5);
assertEquals(5, testShape.getSize());
}
@Test
public void testSquareSize() {
// test setSize()
Shape testShape = new Square(5);
testShape.setSize(6);
assertEquals(6, testShape.getSize());
}
@Test
public void testSquareCircumference() {
// test circumference()
Shape testShape = new Square(6);
assertEquals(24.0, testShape.circumference(), 1e-6);
}
@Test
public void testSquareArea() {
// test area()
Shape testShape = new Square(5);
assertEquals(25.0, testShape.area(), 1e-6);
}
// *********************************
// Square Tests
// *********************************
@Test
public void testRectangleConstructor() {
// test default constructor
Shape testShape = new Rectangle();
assertEquals(3, testShape.getWidth());
assertEquals(4, testShape.getHeight());
// test constructor with size parameter
testShape = new Rectangle(5, 6);
assertEquals(5, testShape.getWidth());
assertEquals(6, testShape.getHeight());
}
@Test
public void testRectangleWidth() {
// test setSize()
Shape testShape = new Rectangle(5, 4);
testShape.setWidth(6);
assertEquals(6, testShape.getWidth());
}
@Test
public void testRectangleHeight() {
// test setSize()
Shape testShape = new Rectangle(78);
testShape.setHeight(3);
assertEquals(3, testShape.getHeight());
}
@Test
public void testRectangleCircumference() {
// test circumference()
Shape testShape = new Rectangle(6, 3);
assertEquals(18.0, testShape.circumference(), 1e-6);
}
@Test
public void testRectangleArea() {
// test area()
Shape testShape = new Rectangle(5, 7);
assertEquals(35.0, testShape.area(), 1e-6);
}
}
import accounts.*;
public class App {
public static void main(String[] args) {
// SavingsAccount savingsAccounts = new SavingsAccount(1, 100, "John");
// savingsAccounts.deposit(100);
// savingsAccounts.withdraw(101);
// savingsAccounts.deposit(110);
// savingsAccounts.display();
// System.out.println("-----------------------------------------------------");
// OverdraftAccount overdraftAccounts = new OverdraftAccount(2, 100, "John");
// overdraftAccounts.addOverdraft(100);
// overdraftAccounts.withdraw(150);
// overdraftAccounts.withdraw(50);
// overdraftAccounts.withdraw(50);
// overdraftAccounts.display();
// System.out.println("-----------------------------------------------------");
// BankAccount bankAccounts = new BankAccount(3, 100, "John");
// bankAccounts.deposit(100);
// bankAccounts.withdraw(50);
// bankAccounts.withdraw(50);
// bankAccounts.display();
}
}
package accounts;
public class BankAccount {
protected int accountNumber;
protected double balance;
protected String customerName;
public BankAccount(int accountNumber, double balance, String customerName) {
this.accountNumber = accountNumber;
this.balance = balance;
this.customerName = customerName;
}
public void deposit(double amount) {
this.balance += amount;
bankFees();
}
public void withdraw(double amount) {
if (amount > this.balance) {
System.out.println("Insufficient funds");
} else {
this.balance -= amount;
}
}
public void bankFees() {
// apply bank fees with a 5% of the balance
this.balance -= this.balance * 0.05;
}
public void display() {
// display account information
System.out.println("Account number: " + this.accountNumber);
System.out.println("Balance: " + this.balance);
System.out.println("Customer name: " + this.customerName);
System.out.println("Bank fees: 5%");
}
}
\ No newline at end of file
package accounts;
// overdraft accounts
public class OverdraftAccount extends BankAccount {
protected double overdraftLimit = 0;
public OverdraftAccount(int accountNumber, double balance, String customerName) {
super(accountNumber, balance, customerName);
}
public void addOverdraft(double amount) {
this.overdraftLimit += amount;
}
@Override
public void withdraw(double amount) {
if (amount > this.balance + this.overdraftLimit) {
System.out.println("You cannot withdraw more than the overdraft limit.");
} else {
super.withdraw(amount);
}
}
@Override
public void display() {
// display account information
System.out.println("Account number: " + this.accountNumber);
System.out.println("Balance: " + this.balance);
System.out.println("Customer name: " + this.customerName);
System.out.println("Overdraft limit: " + this.overdraftLimit);
}
}
package accounts;
public class SavingsAccount extends BankAccount {
protected double withdrawalLimit = 100;
public SavingsAccount(int accountNumber, double balance, String customerName) {
super(accountNumber, balance, customerName);
accrueInterest();
}
@Override
public void withdraw(double amount) {
if (amount > this.withdrawalLimit) {
System.out.println("You cannot withdraw more than the withdrawal limit.");
} else {
super.withdraw(amount);
}
}
@Override
public void deposit(double amount) {
this.balance += amount;
accrueInterest();
}
public void accrueInterest() {
// accrue interest with a 5% of the balance
this.balance += this.balance * 0.05;
}
@Override
public void display() {
// display account information
System.out.println("Account number: " + this.accountNumber);
System.out.println("Balance: " + this.balance);
System.out.println("Customer name: " + this.customerName);
System.out.println("Withdrawal limit: " + this.withdrawalLimit);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment