Commit c7059415 authored by cosmina.dunca's avatar cosmina.dunca

Commit

parents
File added
## 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.
## Folder Structure
The workspace contains two folders by default, where:
- `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies
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.
## 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).
File added
## 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() - 5 marks
* public Complex add(Complex complexNum) - 10 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
File added
File added
public class App {
public static void main(String[] args) throws Exception {
// First test
/* Complex a = new Complex(4.2, 5.7);
a.imag();
a.real();
System.out.println(a.imag);
System.out.println(a.real);
System.out.println(a.real());
System.out.println(a.imag()); -> correct */
// Second test
/* Complex a = new Complex(9.6, 11.5);
System.out.println(a.magnitude()); -> correct */
// Third test
/* Complex a = new Complex(9.6, 11.5);
System.out.println(a.argument()); -> correct */
// Fourth test
/* Complex a = new Complex(1.2, 3.4);
System.out.println(a.toString());
Complex b = new Complex(1.2, 0.0);
System.out.println(b.toString());
Complex c = new Complex(0.0, 9.3);
System.out.println(c.toString());
Complex d = new Complex(1.2, -3.4);
System.out.println(d.toString()); -> correct */
// Fifth test
Complex a = new Complex(6.6, 12.9);
Complex b = new Complex(13.1, 13.5);
Complex c = a.add(b);
System.out.println(c.real());
System.out.println(c.imag());
}
}
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 double d;
public Complex()
{
real = 0;
imag = 0;
}
public Complex(double r, double i)
{
this.real = r;
this.imag = i;
this.d = Math.atan2(i,r);
}
public double imag()
{
return this.imag;
}
public double real()
{
return this.real;
}
public double magnitude()
{
return Math.sqrt(Math.pow(this.real, 2) + Math.pow(this.imag, 2));
}
public double argument()
{
return d;
}
public String toString()
{
if(this.imag < 0)
{
if(this.real != 0) {
return this.real + " - j" + this.imag * (-1);
}
else {
return "- j" + this.imag * (-1);
}
} else if (this.imag > 0)
{
if (this.real != 0) {
return this.real + " + j" + this.imag;
}
else {
return "j" + this.imag;
}
} else {
return this.real + "";
}
}
public Complex add(Complex complexNum)
{
Complex answer = new Complex(0.0,0.0);
answer.real = complexNum.real + this.real;
answer.imag = complexNum.imag + this.imag;
return answer;
}
}
\ 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() - 5 marks
* public Complex add(Complex complexNum) - 10 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
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