package Learning; public class Number { private int num; public Number(int num)
ID: 3585736 • Letter: P
Question
package Learning;
public class Number
{
private int num;
public Number(int num)
{
this.num = num;
}
public static Number addNumbers(Number n1, Number n2)
{
return new Number(n1.num + n2.num);
}
public Number addNumbers(Number n)
{
return new Number(this.num + n.num);
}
public static void main(String []args)
{
Number num1 = new Number(10);
Number num2 = new Number(20);
//ans1 should be 30
Number ans1 = Number.addNumbers(num1, num2);
// ans2 should be 30
Number ans2 = num1.addNumbers(num2);
}
}
class NumberAlternate
{
private int num;
public NumberAlternate(int num)
{
this.num = num;
}
public static Number addNumbers(NumberAlternate n1, NumberAlternate n2)
{
return new Number(n1.num + n2.num);
}
public void addNumbers(NumberAlternate n)
{
this.num = this.num + n.num;
}
public static void main(String []args)
{
NumberAlternate num1 = new NumberAlternate(10);
NumberAlternate num2 = new NumberAlternate(20);
// num1 should be 30
num1.addNumbers(num2);
}
}
Explanation / Answer
Note : Could you please check the output .If you required any changes Just intimate.I will modify it.Thank You.
_________________
Rational.java
public class Rational {
private int numerator, denominator;
// -----------------------------------------------------------------
// Sets up the rational number by ensuring a nonzero denominator
// and making only the numerator signed.
// -----------------------------------------------------------------
public Rational(int numer, int denom) {
if (denom == 0)
denom = 1;
// Make the numerator "store" the sign
if (denom < 0) {
numer = numer * -1;
denom = denom * -1;
}
if (numer < 0 && denom < 0) {
numer = numer * -1;
denom = denom * -1;
}
numerator = numer;
denominator = denom;
reduce();
}
// -----------------------------------------------------------------
// Returns the numerator of this rational number.
// -----------------------------------------------------------------
public int getNumerator() {
return numerator;
}
// -----------------------------------------------------------------
// Returns the denominator of this rational number.
// -----------------------------------------------------------------
public int getDenominator() {
return denominator;
}
// -----------------------------------------------------------------
// Adds this rational number to the one passed as a parameter.
// A common denominator is found by multiplying the individual
// denominators.
// -----------------------------------------------------------------
public static Rational add(Rational op1, Rational op2) {
int num1 = op1.getNumerator();
int denom1 = op1.getDenominator();
int num2 = op2.getNumerator();
int denom2 = op2.getDenominator();
int num3 = (num1 * denom2) + (num2 * denom1);
int denom3 = denom1 * denom2;
Rational r1 = new Rational(num3, denom3);
return r1;
}
// -----------------------------------------------------------------
// Subtracts the rational number passed as a parameter from this
// rational number.
// -----------------------------------------------------------------
public static Rational subtract(Rational op1, Rational op2) {
int commonDenominator = op1.getDenominator() * op2.getDenominator();
int numerator1 = op1.getNumerator() * op2.getDenominator();
int numerator2 = op2.getNumerator() * op1.getDenominator();
int difference = numerator1 - numerator2;
return new Rational(difference, commonDenominator);
}
// -----------------------------------------------------------------
// Multiplies this rational number by the one passed as a
// parameter.
// -----------------------------------------------------------------
public static Rational multiply(Rational op1, Rational op2) {
int numer = op1.getNumerator() * op2.getNumerator();
int denom = op1.getDenominator() * op2.getDenominator();
return new Rational(numer, denom);
}
// -----------------------------------------------------------------
// Divides this rational number by the one passed as a parameter
// by multiplying by the reciprocal of the second rational.
// -----------------------------------------------------------------
public static Rational divide(Rational op1, Rational op2) {
int numer = op2.getDenominator();
int denom = op2.getNumerator();
return multiply(op1, new Rational(numer, denom));
}
// This method checks Whether the two Rational Numbers are equal or not
public static boolean equals(Rational op1, Rational op2) {
if (op1.getNumerator() * op2.getDenominator() == op1.getDenominator() * op2.getNumerator())
return true;
else
return false;
}
// -----------------------------------------------------------------
// Reduces this rational number by dividing both the numerator
// and the denominator by their greatest common divisor.
// -----------------------------------------------------------------
public void reduce() {
if (numerator != 0) {
int common = gcd(Math.abs(numerator), denominator);
numerator = numerator / common;
denominator = denominator / common;
}
}
// -----------------------------------------------------------------
// Computes and returns the greatest common divisor of the two
// positive parameters. Uses Euclid's algorithm.
// -----------------------------------------------------------------
private int gcd(int num1, int num2) {
// % is modulus which is the remainder of a division
// base case
if ((num1 % num2) == 0) {
return num2;
}
// recursive case
else {
return gcd(num2, num1 % num2);
}
}
// -----------------------------------------------------------------
// Returns this rational number as a string.
// -----------------------------------------------------------------
public String toString() {
String result;
if (numerator == 0)
result = "0";
else if (denominator == 1)
result = numerator + "";
else
result = numerator + "/" + denominator;
return result;
}
}
__________________
TestClass.java
import java.util.Scanner;
public class TestClass {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
//Getting the First Rational number From the user
System.out.println(":: Enter Rational No 1 ::");
System.out.print("Enter Numerator =");
int num1 = sc.nextInt();
System.out.print("Enter Denominator =");
int denom1 = sc.nextInt();
//Getting the second Rational number From the user
System.out.println(":: Enter Rational No 2 ::");
System.out.print("Enter Numerator =");
int num2 = sc.nextInt();
System.out.print("Enter Denominator =");
int denom2 = sc.nextInt();
//Creating the objects to Rational Class
Rational r1 = new Rational(num1, denom1);
Rational r2 = new Rational(num2, denom2);
System.out.println(" ");
// Reducing the Rational Numbers
System.out.println("** Rational Reduced Forms **");
r1.reduce();
System.out.println("(" + num1 + "/" + denom1 + ") reduced to :" + r1.toString());
r2.reduce();
System.out.println("(" + num2 + "/" + denom2 + ") reduced to :" + r2.toString());
System.out.println(" ** Adding two Rational Numbers **");
// Addition of two Rational Numbers
Rational r11 = Rational.add(r1, r2);
System.out.println("(" + r1.toString() + ") + " + "(" + r2.toString() + ") = " + r11.toString());
System.out.println(" ** Subtracting two Rational Numbers **");
Rational r12 = Rational.subtract(r1, r2);
System.out.println("(" + r1.toString() + ") - " + "(" + r2.toString() + ") = " + r12.toString());
System.out.println(" ** Multiplying two Rational Numbers **");
Rational r13 = Rational.multiply(r1, r2);
System.out.println("(" + r1.toString() + ") * " + "(" + r2.toString() + ") = " + r13.toString());
System.out.println(" ** Dividing two Rational Numbers **");
Rational r14 = Rational.divide(r1, r2);
System.out.println("(" + r1.toString() + ") / " + "(" + r2.toString() + ") = " + r14.toString());
System.out.println(" ** Comparing two Rational Numbers **");
// Comparing Two Rational Numbers
boolean bool = Rational.equals(r1, r2);
if (bool)
System.out.println("(" + r1.toString() + ") equal to (" + r2.toString() + ")");
else
System.out.println("(" + r1.toString() + ") not equal to (" + r2.toString() + ")");
System.out.print("Do you want to continue(Y/N):");
char c = sc.next(".").charAt(0);
if (c == 'Y' || c == 'y')
continue;
else {
System.out.println(":: Program Exit ::");
break;
}
}
}
}
_____________________
Output:
:: Enter Rational No 1 ::
Enter Numerator =1
Enter Denominator =2
:: Enter Rational No 2 ::
Enter Numerator =4
Enter Denominator =7
** Rational Reduced Forms **
(1/2) reduced to :1/2
(4/7) reduced to :4/7
** Adding two Rational Numbers **
(1/2) +(4/7) = 15/14
** Subtracting two Rational Numbers **
(1/2) -(4/7) = -1/14
** Multiplying two Rational Numbers **
(1/2) *(4/7) = 2/7
** Dividing two Rational Numbers **
(1/2) /(4/7) = 7/8
** Comparing two Rational Numbers **
(1/2) not equal to (4/7)
Do you want to continue(Y/N):n
:: Program Exit ::
____________________
Part Two:
Rational.java
public class Rational {
private int numerator, denominator;
// -----------------------------------------------------------------
// Sets up the rational number by ensuring a nonzero denominator
// and making only the numerator signed.
// -----------------------------------------------------------------
public Rational(int numer, int denom) {
if (denom == 0)
denom = 1;
// Make the numerator "store" the sign
if (denom < 0) {
numer = numer * -1;
denom = denom * -1;
}
numerator = numer;
denominator = denom;
reduce();
}
// -----------------------------------------------------------------
// Returns the numerator of this rational number.
// -----------------------------------------------------------------
public int getNumerator() {
return numerator;
}
// -----------------------------------------------------------------
// Returns the denominator of this rational number.
// -----------------------------------------------------------------
public int getDenominator() {
return denominator;
}
// -----------------------------------------------------------------
// Adds this rational number to the one passed as a parameter.
// A common denominator is found by multiplying the individual
// denominators.
// -----------------------------------------------------------------
public Rational add(Rational op2) {
int num1 = numerator;
int denom1 = denominator;
int num2 = op2.getNumerator();
int denom2 = op2.getDenominator();
int num3 = (num1 * denom2) + (num2 * denom1);
int denom3 = denom1 * denom2;
Rational r1 = new Rational(num3, denom3);
return r1;
}
// -----------------------------------------------------------------
// Subtracts the rational number passed as a parameter from this
// rational number.
// -----------------------------------------------------------------
public Rational subtract(Rational op2) {
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int difference = numerator1 - numerator2;
return new Rational(difference, commonDenominator);
}
// -----------------------------------------------------------------
// Multiplies this rational number by the one passed as a
// parameter.
// -----------------------------------------------------------------
public Rational multiply(Rational op2) {
int numer = numerator * op2.getNumerator();
int denom = denominator * op2.getDenominator();
return new Rational(numer, denom);
}
// -----------------------------------------------------------------
// Divides this rational number by the one passed as a parameter
// by multiplying by the reciprocal of the second rational.
// -----------------------------------------------------------------
public Rational divide(Rational op2) {
int numer = op2.getDenominator();
int denom = op2.getNumerator();
return multiply(new Rational(numer, denom));
}
// -----------------------------------------------------------------
// Determines if this rational number is equal to the one passed
// as a parameter. Assumes they are both reduced.
// -----------------------------------------------------------------
// This method checks Whether the two Rational Numbers are equal or not
public boolean equals(Rational op2) {
if (this.getNumerator() * op2.getDenominator() == this.getDenominator() * op2.getNumerator())
return true;
else
return false;
}
// -----------------------------------------------------------------
// Returns this rational number as a string.
// -----------------------------------------------------------------
public String toString() {
String result;
if (numerator == 0)
result = "0";
else if (denominator == 1)
result = numerator + "";
else
result = numerator + "/" + denominator;
return result;
}
// -----------------------------------------------------------------
// Reduces this rational number by dividing both the numerator
// and the denominator by their greatest common divisor.
// -----------------------------------------------------------------
public void reduce() {
if (numerator != 0) {
int common = gcd(Math.abs(numerator), denominator);
numerator = numerator / common;
denominator = denominator / common;
}
}
// -----------------------------------------------------------------
// Computes and returns the greatest common divisor of the two
// positive parameters. Uses Euclid's algorithm.
// -----------------------------------------------------------------
private int gcd(int num1, int num2) {
// % is modulus which is the remainder of a division
// base case
if ((num1 % num2) == 0) {
return num2;
}
// recursive case
else {
return gcd(num2, num1 % num2);
}
}
}
______________________
TestClass.java
import java.util.Scanner;
public class TestClass {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
//Getting the First Rational number From the user
System.out.println(":: Enter Rational No 1 ::");
System.out.print("Enter Numerator =");
int num1 = sc.nextInt();
System.out.print("Enter Denominator =");
int denom1 = sc.nextInt();
//Getting the second Rational number From the user
System.out.println(":: Enter Rational No 2 ::");
System.out.print("Enter Numerator =");
int num2 = sc.nextInt();
System.out.print("Enter Denominator =");
int denom2 = sc.nextInt();
//Creating the objects to Rational Class
Rational r1 = new Rational(num1, denom1);
Rational r2 = new Rational(num2, denom2);
System.out.println(" ");
// Reducing the Rational Numbers
System.out.println("** Rational Reduced Forms **");
r1.reduce();
System.out.println("(" + num1 + "/" + denom1 + ") reduced to :" + r1.toString());
r2.reduce();
System.out.println("(" + num2 + "/" + denom2 + ") reduced to :" + r2.toString());
System.out.println(" ** Adding two Rational Numbers **");
// Addition of two Rational Numbers
Rational r11 = r1.add(r2);
System.out.println("(" + r1.toString() + ") +" + "(" + r2.toString() + ") = " + r11.toString());
System.out.println(" ** Subtracting two Rational Numbers **");
Rational r12 = r1.subtract(r2);
System.out.println("(" + r1.toString() + ") -" + "(" + r2.toString() + ") = " + r12.toString());
System.out.println(" ** Multiplying two Rational Numbers **");
Rational r13 = r1.multiply(r2);
System.out.println("(" + r1.toString() + ") *" + "(" + r2.toString() + ") = " + r13.toString());
System.out.println(" ** Dividing two Rational Numbers **");
Rational r14 = r1.divide(r2);
System.out.println("(" + r1.toString() + ") /" + "(" + r2.toString() + ") = " + r14.toString());
System.out.println(" ** Comparing two Rational Numbers **");
// Comparing Two Rational Numbers
boolean bool = r1.equals(r2);
if (bool)
System.out.println("(" + r1.toString() + ") equal to (" + r2.toString() + ")");
else
System.out.println("(" + r1.toString() + ") not equal to (" + r2.toString() + ")");
System.out.print("Do you want to continue(Y/N):");
char c = sc.next(".").charAt(0);
if (c == 'Y' || c == 'y')
continue;
else {
System.out.println(":: Program Exit ::");
break;
}
}
}
}
___________________
Output:
:: Enter Rational No 1 ::
Enter Numerator =3
Enter Denominator =4
:: Enter Rational No 2 ::
Enter Numerator =5
Enter Denominator =6
** Rational Reduced Forms **
(3/4) reduced to :3/4
(5/6) reduced to :5/6
** Adding two Rational Numbers **
(3/4) +(5/6) = 19/12
** Subtracting two Rational Numbers **
(3/4) -(5/6) = -1/12
** Multiplying two Rational Numbers **
(3/4) *(5/6) = 5/8
** Dividing two Rational Numbers **
(3/4) /(5/6) = 9/10
** Comparing two Rational Numbers **
(3/4) not equal to (5/6)
Do you want to continue(Y/N):N
:: Program Exit ::
_____________Thank YOu
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.