Problems :
1.
Class PracticeArithmetics (attached below) is intended
as a class for elementary school students for practicing basic arithmetic
operations on one digit positive whole numbers.
The class has three data fields :
private int op1;
first operand (i.e. 9).
private int op2;
second operand (i.e. 3).
private char operation;
arithmetic operation to be used on operands (i.e. '*').
Its member methods have the following meaning :
private int oneDigitInt()
produces and returns a random int whose value is between
2
and 9 (including the boundaries).
private char getOperation()
prompts the user for the arithmetic operation ('+', '-', '*', or
'/') and returns the provided value in the form of an char.
private void prepareQuestion()
prepares the question for the user. It does so by getting the random ints
between 2 and 9 into the operands op1 and op2,
then, getting the operation (+, -, *, or /) from the user and storing it
into the data field operation. It also makes sure
that op1 is greater than or equal to op2 when
the operation requested is '-' or '/'. If op1
is smaller than op2 and the operation
is '-' or '/', op1 and op2 are
swapped.
public void poseQuestionandGetAnswers()
prepares the question for the user (by using the services of the prepareQuestion()
method),
and reads the user's response. In addition, it computes the correct response
and stores the two responses into the corresponding data fields (respectively
answer,
and correctAnswer).
public void displayOutputResults()
displays either a congratulatory message if the user gave a correct answer
or a message indicating that the answer was incorrect. In both cases, it
echoes the question, the user's and the correct answer.
Add missing Java source code into the class PracticeArithmetics' methods (where requested by the presence of the comments // YOUR CODE) to get the methods to behave as follows :
(i) Design private int oneDigitInt()
method so that it produces and returns a random int whose
value is between
2 and 9 (including the boundaries),
(ii) Write the missing code for
the section (ii) of the method private void prepareQuestion() so
that this code will be getting the random ints between 2 and 9 into
the operands op1 and op2 (using the services
of the method from (i)).
(iii) Write the missing code for the
section (iii) of the method private void prepareQuestion() so that
this code will be making sure that op1 is greater
than or equal to op2 when the operation used
is '-' or '/'. If op1 is smaller than op2
and
the operation used is '-' or '/',
op1
and
op2 will get swapped.
(iv) Write the missing code for
the section (iv) of the method private void poseQuestionandGetAnswers()
so
that this code will be getting the correct answer into the
correctAnswer
data
field (based on the current values in data fields op1,
op2 and operation.
(v) Write the missing code for
the section (v) of the method public void displayOutputResults() so
that this code will congratulate the user (saying a sentence like "Your
answer 25 is correct.") or inform him/her that the answer was wrong
(by saying sentences in the following format : "Your answer 25 is wrong.
The correct answer is 24.").
(vi) Create an application class
"MT" that :
(a) creates an instance of the "PracticeArithmetics"
class,
(b) poses an arithmetic question to the user, records
his/her answer, and computes the correct answer (by calling its appropriate
member method), and finally,
(c) displays the results of its processing (by calling
its appropriate member method).
Attachment :
import SimpleIO.*;
public class PracticeArithmetics extends SimpleGui{
private int op1, op2;
private char operation;
private int oneDigitInt()
{
//
(i) YOUR CODE
return (int) (Math.rand() * 8.0) + 2.0);
}
private char getOperation() {
return getChar ("Please, enter one of the following operations : '+' '-' '*' or '/' ");
}
private void prepareQuestion() {
// (ii)
YOUR CODE
// The code getting the random ints (between 2
and 9)
// into the operands op1 and op2
op1 = oneDigitInt(); op2 = oneDigitInt();
operation = getOperation();
// (iii)
YOUR CODE
// The code making sure that op1 is greater than
// or equal to op2 if the operation is '-' or
'/'
// (otherwise op1 and op2 get swapped)
if ((operation == '-') || (operation == '/'))
if (op2 > op1) {
// swap op1 and op2
int temp = op1;
op1 = op2; op2 = temp;
}
/*
if (operation == '-'))
if (op2 > op1) {
// swap op1 and op2
int temp = op1;
op1 = op2; op2 = temp;
}
if (operation == '/')
if (op2 > op1) {
// swap op1 and op2
int temp = op1;
op1 = op2; op2 = temp;
}
*/
/*
if (((operation == '-') || (operation == '/')) && (op2 > op1))
{
// swap op1 and op2
int temp = op1;
op1 = op2; op2 = temp;
}
*/
}
public void poseQuestionandGetAnswers() {
prepareQuestion();
// Get the user's answer
answer = getInt(op1 + operand + op2 + " = ");
// (iv)
YOUR CODE
// The code getting the correct answer
// into the correctAnswer data field
if (operation == '+')
correctAnswer = op1 + op2;
else if (operation == '-')
correctAnswer = op1 - op2;
else if (operation == '*')
correctAnswer = op1 * op2;
else if (operation == '/')
correctAnswer = op1 / op2;
else
correctAnswer = 0;
/*
switch (operation) {
case '+' : correctAnswer = op1 + op2;
break;
case '-' : correctAnswer = op1 - op2;
break;
case '*' : correctAnswer = op1 * op2;
break;
case '/' : correctAnswer = op1 / op2;
break;
default : correctAnswer = 0;
break;
}
*/
}
public void displayOutputResults() {
displayResult("The problem was :");
displayResult(op1 + operation + op2);
// (v) YOUR CODE
// Congratulate the user saying a sentence like
// Your answer 25 is correct.
// or inform him/her of that the answer was wrong
// by saying a sentence like in the following format
// Your answer 25 is wrong. The correct answer is 24.
if (answer == correctAnswer)
displayResult ("Your answer "+answer+" is correct.");
else
displayResult ("Your answer "+answer +" is wrong. The correct answer
is "+correctAnswer+".");
}
}
========================================================================
// (vi) YOUR CODE
// MT.java
import SimpleIO.*;
class MT extends SimpleGui {
public static void main
(String[] args) {
// (a)
PracticeArithmetics ar = new PracticeArithmetics();
// (b)
ar.poseQuestionandGetAnswers();
// (c)
ar.displayOutputResults();
}
}
2.
Find all syntax errors in
the following JAVA method and correct them. Then, explain what it does.
Please be brief and up to the point.
public question ( float X, Y);
{
if (X = Y)
if (X = Z)
return 0
return 1
public int question ( float X, float Y, float Z )
{
if (X == Y)
if (X == Z)
return 0;
return 1;
}
This method compares its arguments and if all three are equal, it returns
0. Otherwise, it returns 1.
3.
Explain briefly :
(a) Difference between static
and non-static member methods of a class,
Static member methods and data members are one per class and shared by all objects of the class. Non-static member methods and data members are one per each object. Static member methods could be called by using the class name only (no need to create an object and use it to access the method). I.e. Integer.parseInt ("-234");. For non-static methods, an object has to be created first, and then used to access the method. I.e. PracticeArithmetics ar = new PracticeArithmetics(); ar.poseQuestionandGetAnswers(); .
(b) Difference between public and private member methods of a class,
Public could be accessed from within the same class, as well as from outside of it. Private can be referred to only from within the same class.
(c) Difference between a class and an object (instance).
A class is a blueprint description of a new data type with its member methods and data members. An object is an instance of the class with its own data members (non-static ones) and member methods.