CPT105/2023/24 S1 COURSEWORK3 INTRODUCTION TO PROGRAMMING IN JAVAJava

Java Python CPT105 

1st SEMESTER 2023/24 COURSEWORK 3 Resit

Undergraduate – Year 1

INTRODUCTION TO PROGRAMMING IN JAVA

SUBMISSION DUE: 4.August 2024, 23:59 BEIJING TIME

Complex Operations (100 marks)

A complex number is a number that can be expressed in the form. a + bi, where a and b are real numbers, and i is a solution of the equation x2 = −1. Because no real number satisfies this equation, i is called an imaginary number. For the complex number a + bi, a is called the real part, and b is called the imaginary part. To add or subtract two complex numbers, just add or subtract the corresponding real and imaginary parts. For instance, the sum of 5 + 3i and 4 + 2i is 9 + 5i. For another, the sum of 3 + i and –1 + 2i is 2 + 3i.

Write a class with the name ComplexNumber (10 marks)The class needs two fields (instance variables) (10 marks) with name real  (5 marks) and imaginary  (5 marks) of type double. It represents the Complex Number.

The class needs to have one constructor  (10 marks). The constructor has parameters real and imaginary of type double and it needs to initialize the fields.

Write the following methods (instance methods):

l Method named getReal without any parameters, it needs to return the value of real field. (5 marks)

l Method named getImaginary without any parameters, it needs to return the value of imaginary field. (5 marks)

l Method named add with two parameters real and imaginary of type double, CPT105/2023/24 S1 COURSEWORK3 INTRODUCTION TO PROGRAMMING IN JAVAJava it needs to add parameters to fields. In other words, it needs to do a complex number add operation as described above. (10 marks)

l Method named add with one parameter of type ComplexNumber. It needs to add the ComplexNumber parameter to the corresponding instance variables. (15 marks)

l Method named subtract with two parameters real and imaginary of type double, it needs to subtract parameters from fields, in other words, it needs to do a complex number subtract operation as described above. (10 marks)

l Method named subtract with one parameter other of type ComplexNumber. It needs to subtract the other parameter from this complex number. (15 marks)

TEST EXAMPLE

→ TEST CODE:

1. ComplexNumber one = new ComplexNumber(1.0, 1.0);  

2. ComplexNumber number = new ComplexNumber(2.5, -1.5);  

3.    one.add(1,1);  

4. System.out.println("one.real= " + one.getReal());  

5. System.out.println("one.imaginary= " + one.getImaginary());  

6.    one.subtract(number);  

7. System.out.println("one.real= " + one.getReal());  

8. System.out.println("one.imaginary= " + one.getImaginary());  

9.      number.subtract(one);  

10. System.out.println("number.real= " + number.getReal());  

11. System.out.println("number.imaginary= " + number.getImaginary());  

→ OUTPUT

one.real= 2.0

one.imaginary= 2.0

one.real= -0.5

one.imaginary= 3.5

number.real= 3.0

number.imaginary= -5.0

NOTE: Try to avoid duplicated code.

NOTE: All methods should be defined as public NOT public static.

NOTE: In total, you have to write 6 methods.

NOTE: Do not add a main method to the solution code         

  • 17
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值