Suppose we have a preexisting Rational class with methods add, subtract, multipl
ID: 3726491 • Letter: S
Question
Suppose we have a preexisting Rational class with methods add, subtract, multiply and divide. Now we want to build a Complex class with instance variables b and c of type Rational. We can think of this a representing a complex number b + c i, where i is the square root of -1. b is called the real part of the complex number and the value c is called the imaginary part.
What is the OOP term for the association between Rational and Complex when we build a Complex object using Rational objects in this manner? ______________composition_________________________ (11 letters begins with ‘c’ )
To multiply two complex numbers we multiply individual parts as follows:
That’s to say, b1b2 – c1c2 is the real part of the result and b1c2 + b2c1 is the imaginary part. Your job is to complete the Complex times method below. That method returns a new Complex object equal to product of this and other. To compute the result , call Rational methods plus, minus and times (e.g. call b1.times(b2) to get b1 times b2.) You have access to a Complex constructor that takes two parameters for initializing b and c, respectively. That’s all you need; you won’t be using *, + or – anywhere in your code. (It’s possible, though not required, to complete this question by adding a single return statement to code below)
Complex times(Complex other){
Rational b1 = this.b, c1 = this.c, b2 = other.b, c2 = other.c;
}
An n-by-n TicTacToeBoard class uses a two-dimensional array of char called squares to store the current board. That means squares is an array of n rows, where each row is a one-dimensional array of char having length = n. The instance variable int numMoves will store the current number of squares that contain an 'X' or an 'O'. The board starts with all elements of squares initialized to '-'. Each move changes one of those to an 'X' or an 'O', depending on who’s turn it is. It’s X’s turn if moveNumber is even and O’s turn otherwise. Your job is to complete the TicTacToeBoard methods below.
public class TicTacToeBoard {
private int n; // The size of the board e.g. n=3 means traditional 3 by 3 tictactoe.
private char [][] squares; // Allocated in the constructor with size n by n.
int numMoves; // Initial value = zero, incremented with each move.
// Initialize this.n to n, allocate squares and set each element to '-'.
// If n < 3, throw an IllegalArgumentException with message "__ by __ board is too small"
// e.g. "2 by 2 board is too small"
public TicTacToeBoard(int n)
{
If(n < 3)
Throw IllegalArgumentException(“__ by __ board is too small”);
Else
if
Square equals
For(I = 0; I < -1, i++)
{
}
.length() – 1;
}
// If square[row][column] is a legal position and free (i.e. still a '-')
// then set it to 'X' or 'O' depending on whose turn it is, update numMoves and
// return true. If the position is illegal or the square is taken then return false.
public boolean moveTo(int row, int column)
{
If(row < n && column < n)
Return true;
If(row > n && column > n)
Return true;
Else
Return false
}
//
// Print the board to the console. For example: x x o
O x
X - o
//
public void printBoard()
{
}
Explanation / Answer
I have completed the code as mentioned and explained in the comments.
Complex times(Complex other){
Rational b1 = this.b, c1 = this.c, b2 = other.b, c2 = other.c;
Rational temp1 = b1.times(b2).minus(c1.times(c2));
Rational temp2 = b1.times(c2).plus(b2.times(c1));
//since it is mentioned that there is a constructor taking two rational numbers
return complex(temp1,temp2);
}
public class TicTacToeBoard {
private int n; // The size of the board e.g. n=3 means traditional 3 by 3 tictactoe.
private char [][] squares; // Allocated in the constructor with size n by n.
int numMoves; // Initial value = zero, incremented with each move.
public TicTacToeBoard(int n)
{
// If n < 3, throw an IllegalArgumentException with message "__ by __ board is too small"
// e.g. "2 by 2 board is too small"
if(n < 3)
Throw IllegalArgumentException( "n" + " by " + "n "+ "board is too small”);
// Initialize this.n to n, allocate squares and set each element to '-'.
this.n = n;
squares = new char[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
squares[i][j] = '-';
}
}
numMoves =0;
}
// If square[row][column] is a legal position and free (i.e. still a '-')
// then set it to 'X' or 'O' depending on whose turn it is, update numMoves and
// return true. If the position is illegal or the square is taken then return false.
public boolean moveTo(int row, int column)
{
if(row > n && column > n)Return false;
if(row < n && column < n){
if(squares[row][column] != '-'){
if(numMoves%2 == 0) squares[row][column]='x';
else squares[row][column]='o';
numMoves++;
Return true;
}
}
Return false;
}
// Print the board to the console. For example: x x o
O x
X - o
//
public void printBoard(){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
System.out.print(squares[i][j]+" ");
}
System.out.println()
}
}