Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The code he already gave us is and must be used: package testing; import java.ut

ID: 3667199 • Letter: T

Question

The code he already gave us is and must be used:

package testing;

import java.util.Vector;
import assignment1.Integer;
import assignment1.Rational;
import assignment1.Complex;

public class Test {

       static Vector<String> passed = new Vector<>();
       static Vector<String> failed = new Vector<>();

       public static void main(String args[])
       {
           Integer i1 = new Integer(5);
           Integer i2 = new Integer(6);

           Integer clone1 = i1.clone();

           Test.test(i1 == i1,            "Testing Integer identity");
           Test.test( i1 != clone1,           "Testing Integer inequality");
           Test.test(i1.equals(clone1),     "Testing Integer equality");

           Test.test( i1.add(5).getInt() == 10,         "Testing Integer add");
           Test.test( i1.subtract(3).getInt() == 2,     "Testing Integer subtract");
           Test.test(i1.multiply(4).getInt() == 20,    "Testing Integer multiply");
           Test.test(i1.divide(2).getInt() == 2,        "Testing Integer divide");
           Test.test(i1.toString().equals("5"),        "Testing Integer toString");
           i1.setInt(33);
           Test.test(i1.getInt() == 33,    "Testing Integer Set");

           Rational r1 = new Rational();
           Rational r2 = new Rational(3, 4);   //r2 = 3 / 4
           Rational r3 = new Rational(6);       // r3 = 6 / 1

           Test.test(r1 == r1,                "Testing Rational identity");
           Test.test( r1!= r1.clone(),           "Testing Rational inequality2");

           r1.setInt(3);
           r1.setDenominator(4);
           Test.test(r1.equals(r2),                "Testing Rational equality");
           Test.test( !r1.equals(i1),                "Testing Rational equality2");
           Test.test(r1.add(r2).getInt() == 24,        "Testing Rational add");
           Test.test(r1.add(r2).getDenominator() == 16,"Testing Rational add2");

           r1.setDenominator(2);   //R1 is now 3/2
           r2.setInt(2);       //R2 is now 2/4

           Test.test(r1.subtract(r2).getInt() == 8,            "Testing Rational add3"); //(3*4) - (2*2)
           Test.test(r1.subtract(r2).getDenominator() == 8,   "Testing Rational add4");
           Test.test(r1.multiply(r2).getInt() == 6,            "Testing Rational multiply");
           Test.test(r1.multiply(r2).getDenominator() == 8,    "Testing Rational multiply2");
           Test.test(r1.divide(r2).getInt() == 12,               "Testing Rational divide");
           Test.test(r1.divide(r2).getDenominator()== 4,        "Testing Rational divide2");
           Test.test(r1.toString().equals("3/2"),                "Testing Rational toString");
           Test.test(r2.toString().equals("2/4"),               "Testing Rational toString2");

           Complex c1 = new Complex();
           Complex c2 = new Complex(2, 4);       //Real part is 2, Imaginary is 4
           Complex result = null;

           c1.setReal( new Rational(2) );
           c1.setImaginary( new Rational(4) );

           Test.test( c1.clone() != c1,        "Testing Complex identity");
           Test.test( c1 == c1,               "Testing Complex identity2");
           Test.test( c1.equals(c2),           "Testing Complex equality");

           result = c1.add(c2);
           Test.test( result.getReal().getInt() == 4,            "Testing Complex add");
           Test.test( result.getReal().getDenominator() == 1,    "Testing Complex add2");

           Test.test( result.getImaginary().getInt() == 8,    "Testing Complex add3");
           Test.test( result.getReal().getDenominator() == 1,    "Testing Complex add4");

           result = c1.subtract(new Complex( 3, 3));
           Test.test(result.getReal().getInt() == -1,            "Testing Complex subtract");
           Test.test(result.getImaginary().getInt() == 1,       "Testing Complex subtract2");

           c2.setReal( new Rational(3) );
           c2.setImaginary( new Rational(3) );       //c2 is now (3 + i3)
      
           result = c1.multiply(c2);
           Test.test(result.getReal().getInt() == -6,            "Testing Complex multiply");
           Test.test(result.getImaginary().getInt() == 18,    "Testing Complex multiply2");

           result = c1.divide(c2);
           Test.test(result.getReal().getInt() == 18,               "Testing Complex divide");
           Test.test(result.getReal().getDenominator() == 18,       "Testing Complex divide2");
           Test.test(result.getImaginary().getInt() == 6,           "Testing Complex divide3");
           Test.test(result.getImaginary().getDenominator() == 18,   "Testing Complex divide4");

           Test.test(c1.toString().equals("2/1 + i4/1"),   "Testing Complex toString");
           Test.test(c2.toString().equals("3/1 + i3/1"),   "Testing Complex toString2");

           c1.setImaginary( new Rational(-4)); // c1 is now "2/1 - i4/1"
           String str = c1.toString();
           Test.test(str.equals("2/1 - i4/1"),   "Testing Complex toString3");

           Test.printTestResults();
       }

       public static void test(boolean result, String message)
       {
           if(passed.isEmpty() && failed.isEmpty())
           {
               System.out.println("Beginning tests:");
           }

           System.out.println(message + ":" + result);
           if(result)
               passed.add(message);
           else
               failed.add(message);
       }

       public static void printTestResults()
       {
           System.out.println("TEST RESULTS - Passed:");
           for(String test: passed)
           {
               System.out.println(" " + test);
           }
           if(passed.isEmpty())   System.out.println("None");


           System.out.println("TEST RESULTS - Failed:");
           for(String test: failed)
           {
               System.out.println(" " + test);
           }
           if(failed.isEmpty())   System.out.println("None");

           System.out.println("Summary:" + passed.size() + " passed, " + failed.size() + " failed, " + (passed.size() + failed.size()) + " total");
       }
   }

package testing;

import java.util.Vector;
import assignment1.Integer;
import assignment1.Rational;
import assignment1.Complex;

public class Test {

       static Vector<String> passed = new Vector<>();
       static Vector<String> failed = new Vector<>();

       public static void main(String args[])
       {
           Integer i1 = new Integer(5);
           Integer i2 = new Integer(6);

           Integer clone1 = i1.clone();

           Test.test(i1 == i1,            "Testing Integer identity");
           Test.test( i1 != clone1,           "Testing Integer inequality");
           Test.test(i1.equals(clone1),     "Testing Integer equality");

           Test.test( i1.add(5).getInt() == 10,         "Testing Integer add");
           Test.test( i1.subtract(3).getInt() == 2,     "Testing Integer subtract");
           Test.test(i1.multiply(4).getInt() == 20,    "Testing Integer multiply");
           Test.test(i1.divide(2).getInt() == 2,        "Testing Integer divide");
           Test.test(i1.toString().equals("5"),        "Testing Integer toString");
           i1.setInt(33);
           Test.test(i1.getInt() == 33,    "Testing Integer Set");

           Rational r1 = new Rational();
           Rational r2 = new Rational(3, 4);   //r2 = 3 / 4
           Rational r3 = new Rational(6);       // r3 = 6 / 1

           Test.test(r1 == r1,                "Testing Rational identity");
           Test.test( r1!= r1.clone(),           "Testing Rational inequality2");

           r1.setInt(3);
           r1.setDenominator(4);
           Test.test(r1.equals(r2),                "Testing Rational equality");
           Test.test( !r1.equals(i1),                "Testing Rational equality2");
           Test.test(r1.add(r2).getInt() == 24,        "Testing Rational add");
           Test.test(r1.add(r2).getDenominator() == 16,"Testing Rational add2");

           r1.setDenominator(2);   //R1 is now 3/2
           r2.setInt(2);       //R2 is now 2/4

           Test.test(r1.subtract(r2).getInt() == 8,            "Testing Rational add3"); //(3*4) - (2*2)
           Test.test(r1.subtract(r2).getDenominator() == 8,   "Testing Rational add4");
           Test.test(r1.multiply(r2).getInt() == 6,            "Testing Rational multiply");
           Test.test(r1.multiply(r2).getDenominator() == 8,    "Testing Rational multiply2");
           Test.test(r1.divide(r2).getInt() == 12,               "Testing Rational divide");
           Test.test(r1.divide(r2).getDenominator()== 4,        "Testing Rational divide2");
           Test.test(r1.toString().equals("3/2"),                "Testing Rational toString");
           Test.test(r2.toString().equals("2/4"),               "Testing Rational toString2");

           Complex c1 = new Complex();
           Complex c2 = new Complex(2, 4);       //Real part is 2, Imaginary is 4
           Complex result = null;

           c1.setReal( new Rational(2) );
           c1.setImaginary( new Rational(4) );

           Test.test( c1.clone() != c1,        "Testing Complex identity");
           Test.test( c1 == c1,               "Testing Complex identity2");
           Test.test( c1.equals(c2),           "Testing Complex equality");

           result = c1.add(c2);
           Test.test( result.getReal().getInt() == 4,            "Testing Complex add");
           Test.test( result.getReal().getDenominator() == 1,    "Testing Complex add2");

           Test.test( result.getImaginary().getInt() == 8,    "Testing Complex add3");
           Test.test( result.getReal().getDenominator() == 1,    "Testing Complex add4");

           result = c1.subtract(new Complex( 3, 3));
           Test.test(result.getReal().getInt() == -1,            "Testing Complex subtract");
           Test.test(result.getImaginary().getInt() == 1,       "Testing Complex subtract2");

           c2.setReal( new Rational(3) );
           c2.setImaginary( new Rational(3) );       //c2 is now (3 + i3)
      
           result = c1.multiply(c2);
           Test.test(result.getReal().getInt() == -6,            "Testing Complex multiply");
           Test.test(result.getImaginary().getInt() == 18,    "Testing Complex multiply2");

           result = c1.divide(c2);
           Test.test(result.getReal().getInt() == 18,               "Testing Complex divide");
           Test.test(result.getReal().getDenominator() == 18,       "Testing Complex divide2");
           Test.test(result.getImaginary().getInt() == 6,           "Testing Complex divide3");
           Test.test(result.getImaginary().getDenominator() == 18,   "Testing Complex divide4");

           Test.test(c1.toString().equals("2/1 + i4/1"),   "Testing Complex toString");
           Test.test(c2.toString().equals("3/1 + i3/1"),   "Testing Complex toString2");

           c1.setImaginary( new Rational(-4)); // c1 is now "2/1 - i4/1"
           String str = c1.toString();
           Test.test(str.equals("2/1 - i4/1"),   "Testing Complex toString3");

           Test.printTestResults();
       }

       public static void test(boolean result, String message)
       {
           if(passed.isEmpty() && failed.isEmpty())
           {
               System.out.println("Beginning tests:");
           }

           System.out.println(message + ":" + result);
           if(result)
               passed.add(message);
           else
               failed.add(message);
       }

       public static void printTestResults()
       {
           System.out.println("TEST RESULTS - Passed:");
           for(String test: passed)
           {
               System.out.println(" " + test);
           }
           if(passed.isEmpty())   System.out.println("None");


           System.out.println("TEST RESULTS - Failed:");
           for(String test: failed)
           {
               System.out.println(" " + test);
           }
           if(failed.isEmpty())   System.out.println("None");

           System.out.println("Summary:" + passed.size() + " passed, " + failed.size() + " failed, " + (passed.size() + failed.size()) + " total");
       }
   }

CST 8284 Assignment 1 (due Feb 12, total of 65 marks) The goal of this lab is to create a sub-class in Java, and use inheritance, super, and override. I am giving you some code to start the assignment. You must create each of the following objects: Integer, Rational, and Complex, which must be added to the package assignment1, which already exists I have also created a class called Test, which is in the testing package. This class has a main method which has many lines of code which test the various functions that you will implement Your task is to write the Integer, Rational, and Complex classes so that the tests in the main method all pass. You should also write the JavaDoc comments for the three classes, and create the UML diagrams 1) Integer should be a class that has a single int variable. This class must inherit from Object, and have its own variable, you can not use the Integer class that is already included in the Java language 2) Rational should be a class that inherits from Integer, but has a second integer variable to represent the denominator. Rational numbers are a special kind of number that can be represented as a fraction, in the form a/ b. For example, 2.75 can be written as (275/ 100) or (11/4). The number pi 3.1415926535 is an irrational number, meaning that it can not be written as a fraction a b 3) A complex number is a number that contains both real and imaginary Rational components. The imaginary component, shown by letter i, represents V-1. For this assignment, Complex should extend Rational, and the inherited variables should represent the real component of the number. Complex should then have a new variable, of type Rational, to represent the imaginary component For all three classes, implement the following methods * Empty constructor, Default constructor, Copy constructor, equals clone toString add subtract multiply divide

Explanation / Answer

Answer:

Note: code has been implemented as per user requirements. In 2 hrs I could complete only 2 classes Integer, Rational.

import java.io.*;

import java.util.*;

import java.lang.*;

//Integer Class

class Integer extends Object

{

int intVar;

public Integer()

{

}

public Integer(int v)

{

intVar=v;

}

public Integer(Integer c)

{

this.intVar=c.intVar;

}

public boolean equals(Object c)

{

if(c==this)

     return true;

if(!(c instanceof Integer))

     return false;

     Integer a=(Integer)c;

if(this.intVar==a.intVar)

     return true;

     return false;

}

public Integer clone()

{

    

     try

     {

          return (Integer)super.clone();

     }

     catch(CloneNotSupportedException p)

     {

        

     }

     return null;

    

    

}

public String toString()

{

     return String.valueOf(intVar);

}

public Integer add(int c)

{

intVar=intVar+c;

return this;

}

public Integer subtract(int c)

{

intVar=intVar-c;

return this;

}

public Integer multiply(int c)

{

intVar=intVar*c;

return this;

}

public Integer divide(int c)

{

intVar=intVar/c;

return this;

}

public int getInt()

{

return intVar;

}

public void setInt(int c)

{

intVar=c;

}

}

//Rational Class

class Rational extends Integer

{

Integer denVar;

public Rational()

{

super();

denVar=new Integer(0);

}

public Rational(int n1)

{

super(n1);

denVar=new Integer(1);

}

public Rational(int n1,int d1)

{

super(n1);

denVar=new Integer(d1);

}

public Rational(Rational c)

{

super(c.getInt());

denVar=c.denVar;

}

public boolean equals(Object c)

{

if(c==this)

     return true;

if(!(c instanceof Rational))

     return false;

     Rational r=(Rational)c;

if((super.getInt()==r.getInt())&&(this.denVar.getInt()==r.denVar.getInt()))

     return true;

return false;

}

public Rational clone()

{   

          return (Rational)super.clone();       

}

public String toString()

{

     return super.toString()+"/"+denVar.toString();

}

public Rational add(Rational c)

{

int n=super.getInt()*c.denVar.getInt()+denVar.getInt()*c.getInt();

int d=c.denVar.getInt()*denVar.getInt();

super.setInt(n);

denVar.setInt(d);

return this;

}

public Rational subtract(Rational c)

{

int n=super.getInt()*c.denVar.getInt()-denVar.getInt()*c.getInt();

int d=c.denVar.getInt()*denVar.getInt();

super.setInt(n);

denVar.setInt(d);

return this;

}

public Rational multiply(Rational c)

{

int n=super.getInt()*c.getInt();

int d=c.denVar.getInt()*denVar.getInt();

super.setInt(n);

denVar.setInt(d);

return this;

}

public Rational divide(Rational c)

{

int n=super.getInt()*c.denVar.getInt();

int d=c.getInt()*denVar.getInt();

super.setInt(n);

denVar.setInt(d);

return this;

}

public int getDenominator()

{

return denVar.getInt();

}

public void setDenominator(int c)

{

denVar.setInt(c);

}

}