Commit fb5043b0 authored by jinny.wilkin's avatar jinny.wilkin

assessment upload

parent de95c2bd
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}
## Getting Started ## Getting Started
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
## Folder Structure ## Folder Structure
The workspace contains two folders by default, where: The workspace contains two folders by default, where:
- `src`: the folder to maintain sources - `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies - `lib`: the folder to maintain dependencies
Meanwhile, the compiled output files will be generated in the `bin` folder by default. Meanwhile, the compiled output files will be generated in the `bin` folder by default.
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. > If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
## Dependency Management ## Dependency Management
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).
## Complex Numbers Assessment Exercise 1 ## Complex Numbers Assessment Exercise 1
## 1 Skills ## 1 Skills
This exercise should help you with: This exercise should help you with:
1. Member variables and member methods. 1. Member variables and member methods.
2. Unit testing. 2. Unit testing.
3. Creation of a class to a specification without necessarily understanding the details. 3. Creation of a class to a specification without necessarily understanding the details.
## 2 Background ## 2 Background
A complex number is one that has two parts: **real** and **imaginary**. They are written (by engineers at least) like this 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 * 4.1 + j9.9
* 0.5 + j3.2 * 0.5 + j3.2
Or in general Or in general
* a + *j*b * 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 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) * **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) * **argument** = tan−1(b/a) (using `Math.atan2(b,a)` in Java)
**Addition** **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. 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 CN1 = a +*j*b
CN2 = c +*j*d CN2 = c +*j*d
CN1 + CN2 = (a+c) + *j*(b+d) CN1 + CN2 = (a+c) + *j*(b+d)
## 3 Unit Testing ## 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. 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. 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() * testConstructor()
* testMagnitude() * testMagnitude()
* testArgument() * testArgument()
* testAdd() * testAdd()
* testToString() * 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`. 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 ## 4 Task
You need to complete the following methods. You need to complete the following methods.
* public double magnitude() * public double magnitude()
* public double argument() * public double argument()
* public String toString() * public String toString()
* public Complex add(Complex complexNum) * 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. 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. `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| |Real Part|Imaginary Part|String|
|---|---|---| |---|---|---|
|`2.6`|`5.2`|`2.6 + j5.6`| |`2.6`|`5.2`|`2.6 + j5.6`|
|`2.6`|`0`|`2.6`| |`2.6`|`0`|`2.6`|
|`0`|`5.2`|`j5.2`| |`0`|`5.2`|`j5.2`|
|`2.6`|`-9.8`|`2.6 - j9.8`| |`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 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. 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 ## 5 Marking
25 marks are available for this exercise. This breaks down as follows. 25 marks are available for this exercise. This breaks down as follows.
* public double magnitude() - 5 marks * public double magnitude() - 5 marks
* public double argument() - 5 marks * public double argument() - 5 marks
* public String toString() - 10 marks * public String toString() - 10 marks
* public Complex add(Complex complexNum) - 5 marks * public Complex add(Complex complexNum) - 5 marks
## 6 Submission ## 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. 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 class App {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
System.out.println("Hello, World!"); System.out.println("Hello, World!");
} }
} }
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import org.junit.Test; import org.junit.Test;
public class AppTest { public class AppTest {
@Test @Test
public void testConstructor() { public void testConstructor() {
/* Constructor should put the first parameter as the real part and second as the imaginary part. /* Constructor should put the first parameter as the real part and second as the imaginary part.
* real() should return real part, imag() imaginary part. * real() should return real part, imag() imaginary part.
*/ */
Complex a = new Complex(4.2, 5.7); Complex a = new Complex(4.2, 5.7);
assertEquals(4.2, a.real(), 1e-6); assertEquals(4.2, a.real(), 1e-6);
assertEquals(5.7, a.imag(), 1e-6); assertEquals(5.7, a.imag(), 1e-6);
Complex b = new Complex(); Complex b = new Complex();
assertEquals(0, b.real(), 1e-6); assertEquals(0, b.real(), 1e-6);
assertEquals(0, b.imag(), 1e-6); assertEquals(0, b.imag(), 1e-6);
} }
@Test @Test
public void testMagnitude() { public void testMagnitude() {
/* magnitude() should return the magnitude of the complex number */ /* magnitude() should return the magnitude of the complex number */
Complex a = new Complex(9.6, 11.5); Complex a = new Complex(9.6, 11.5);
assertEquals(14.98032042, a.magnitude(), 1e-6); assertEquals(14.98032042, a.magnitude(), 1e-6);
} }
@Test @Test
public void testArgument() { public void testArgument() {
/* argument() should return the argument of the complex number */ /* argument() should return the argument of the complex number */
Complex a = new Complex(9.6, 11.5); Complex a = new Complex(9.6, 11.5);
assertEquals(0.87520335, a.argument(), 1e-6); assertEquals(0.87520335, a.argument(), 1e-6);
} }
@Test @Test
public void testToString() { public void testToString() {
/* toString() should return a string like 1.2 + j3.4 where 1.2 is the real part /* toString() should return a string like 1.2 + j3.4 where 1.2 is the real part
* and 3.4 the imaginary part. * and 3.4 the imaginary part.
*/ */
Complex a = new Complex(1.2, 3.4); Complex a = new Complex(1.2, 3.4);
assertEquals("1.2 + j3.4", a.toString()); assertEquals("1.2 + j3.4", a.toString());
Complex b = new Complex(1.2, 0.0); Complex b = new Complex(1.2, 0.0);
assertEquals("1.2", b.toString()); assertEquals("1.2", b.toString());
Complex c = new Complex(0.0, 9.3); Complex c = new Complex(0.0, 9.3);
assertEquals("j9.3", c.toString()); assertEquals("j9.3", c.toString());
Complex d = new Complex(1.2, -3.4); Complex d = new Complex(1.2, -3.4);
assertEquals("1.2 - j3.4", d.toString()); assertEquals("1.2 - j3.4", d.toString());
} }
@Test @Test
public void testAdd() { public void testAdd() {
/* add() should return a new complex number which is the sum of this and the parameter */ /* 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 a = new Complex(6.6, 12.9);
Complex b = new Complex(13.1, 13.5); Complex b = new Complex(13.1, 13.5);
Complex c = a.add(b); Complex c = a.add(b);
assertEquals(19.7, c.real(), 1e-6); assertEquals(19.7, c.real(), 1e-6);
assertEquals(26.4, c.imag(), 1e-6); assertEquals(26.4, c.imag(), 1e-6);
} }
} }
public class Complex { public class Complex {
public double real; public double real;
public double imag; public double imag;
public Complex() public Complex()
{ {
real = 0; real = 0;
imag = 0; imag = 0;
} }
public Complex(double r, double i) public Complex(double r, double i)
{ {
real = r; real = r;
imag = i; imag = i;
double d = Math.atan2(i,r); //double d = Math.atan2(i,r);
} }
public double imag() public double imag()
{ {
return imag; return imag;
} }
public double real() public double real()
{ {
return real; return real;
} }
public double magnitude() public double magnitude()
{ {
return 1.0; return Math.sqrt((Math.pow(real, 2))+(Math.pow(imag, 2)));
} }
public double argument() public double argument()
{ {
return 1.0; return Math.atan2(imag, real);
} }
public String toString() public String toString()
{ {
return ""; String result;
} String realStr = "";
String imagStr = "";
public Complex add(Complex complexNum) if(real != 0){
{ realStr = Double.toString(real);
Complex answer = new Complex(0.0,0.0); }
if(imag != 0){
return answer; imagStr = imag>0?" + j"+imag:" - j"+(-imag);
} }
if((real != 0) && (imag == 0)){
result = realStr;
}else if ((real == 0)&& (imag != 0)){
result = imagStr.substring(3);
}else{
result = realStr + imagStr;
}
return result;
}
public Complex add(Complex complexNum)
{
double realAdd = real + complexNum.real;
double imagAdd = imag + complexNum.imag;
Complex answer = new Complex(0.0,0.0);
answer.real = realAdd;
answer.imag = imagAdd;
return answer;
}
} }
\ No newline at end of file
## Complex Numbers Assessment Exercise 1 ## Complex Numbers Assessment Exercise 1
## 1 Skills ## 1 Skills
This exercise should help you with: This exercise should help you with:
1. Member variables and member methods. 1. Member variables and member methods.
2. Unit testing. 2. Unit testing.
3. Creation of a class to a specification without necessarily understanding the details. 3. Creation of a class to a specification without necessarily understanding the details.
## 2 Background ## 2 Background
A complex number is one that has two parts: **real** and **imaginary**. They are written (by engineers at least) like this 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 * 4.1 + j9.9
* 0.5 + j3.2 * 0.5 + j3.2
Or in general Or in general
* a + *j*b * 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 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) * **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) * **argument** = tan−1(b/a) (using `Math.atan2(b,a)` in Java)
**Addition** **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. 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 CN1 = a +*j*b
CN2 = c +*j*d CN2 = c +*j*d
CN1 + CN2 = (a+c) + *j*(b+d) CN1 + CN2 = (a+c) + *j*(b+d)
## 3 Unit Testing ## 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. 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. 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() * testConstructor()
* testMagnitude() * testMagnitude()
* testArgument() * testArgument()
* testAdd() * testAdd()
* testToString() * 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`. 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 ## 4 Task
You need to complete the following methods. You need to complete the following methods.
* public double magnitude() * public double magnitude()
* public double argument() * public double argument()
* public String toString() * public String toString()
* public Complex add(Complex complexNum) * 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. 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. `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| |Real Part|Imaginary Part|String|
|---|---|---| |---|---|---|
|`2.6`|`5.2`|`2.6 + j5.6`| |`2.6`|`5.2`|`2.6 + j5.6`|
|`2.6`|`0`|`2.6`| |`2.6`|`0`|`2.6`|
|`0`|`5.2`|`j5.2`| |`0`|`5.2`|`j5.2`|
|`2.6`|`-9.8`|`2.6 - j9.8`| |`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 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. 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 ## 5 Marking
25 marks are available for this exercise. This breaks down as follows. 25 marks are available for this exercise. This breaks down as follows.
* public double magnitude() - 5 marks * public double magnitude() - 5 marks
* public double argument() - 5 marks * public double argument() - 5 marks
* public String toString() - 10 marks * public String toString() - 10 marks
* public Complex add(Complex complexNum) - 5 marks * public Complex add(Complex complexNum) - 5 marks
## 6 Submission ## 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. 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
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