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

Java questions Tasks You are required to write three Java classes: Term.java For

ID: 3685382 • Letter: J

Question

Java questions

Tasks

You are required to write three Java classes:

Term.java
Formula.java
Equation.java
Each link above gives you a skeleton of the required class, including instance variables, a constructor, and several methods. You are required to complete the constructors and methods whose bodies are marked with the comment TODO. Note that where a TODO method has a non-void return-type, it has a dummy return statement to allow the partially-written class to compile: you should delete that return statement when you write that method. Make sure you understand the connections between the various classes.

Term.Java

/**
* Term represents terms in a chemical formula
* Only elements denoted by a single letter are supported
*
* Examples: N, H2, C20, H147
*
* @author
* @version
*/
public class Term
{
private char element;
private int atoms;

// creates a Term with the provided values
public Term(char element, int atoms)
{
this.element = element;
this.atoms = atoms;
}

// creates a Term by parsing s
// e.g. "H20" would give element = 'H', atoms = 20
public Term(String s)
{
// TODO
}
  
// turns the Term into a String
// e.g. element = 'C', atoms = 4 would give "C4"
public String display()
{
// TODO
return "";
}
  
// returns the current value of element
public char getElement()
{
return element;
}

// returns the current value of atoms
public int getAtoms()
{
return atoms;
}
}

Formula.java

/**
* Formula represents chemical formulae
* e.g. Hexane:
Structural formula CH3CH2CH2CH2CH2CH3
Molecular formula C6H14
*
* In a molecular formula, elements are listed only once, and they are listed alphabetically
*
* @author
* @version
import java.util.ArrayList;

public class Formula
{
private ArrayList<Term> terms;

// creates a Formula with the provided value
public Formula(ArrayList<Term> terms)
{
this.terms = terms;
}

// creates a Formula by parsing s
// e.g. "CH2O" would give terms = {Term('C',1),Term('H',2),Term('O',1)}
public Formula(String s)
{
// TODO
}
  
// returns the index of the rightmost upper-case letter in s, or -1 if none found
// e.g. "COHOHH2" would give 5
public static int lastElement(String s)
{
// TODO
return -2;
}
  
// turns the Formula into a String
// e.g. terms = {Term('C', 1),Term('O',2)} would give "CO2"
public String display()
{
// TODO
return "";
}
  
// returns the current value of terms
public ArrayList<Term> getTerms()
{
return terms;
}
  
// changes terms into molecular form
// e.g. terms = {Term('H',3),Term('C',1),Term('H',3),Term('C',1)} would become {Term('C',2),Term('H',6)}
public void makeMolecular()
{
// TODO
}
  
// returns the next element in terms, alphabetically
// e.g. terms = {Term('H',4),Term('C',2),Term('H',4),Term('C',1)} would return Term('C',2)
public Term nextElement()
{
// TODO
return null;
}
  
// returns true iff f is identical to this Formula
// e.g. terms = {Term('C',2),Term('H',6)} and f = {Term('C',2),Term('H',6)} would return true
// but terms = {Term('C',2),Term('H',6)} and f = {Term('H',6),Term('C',2)} would return false
public boolean identical(Formula f)
{
// TODO
return false;
}
  
// returns true iff f is an isomer of this Formula
// e.g. terms = {Term('C',2),Term('H',6)} and f = {Term('C',1),Term('H',3),Term('C',1),Term('H',3)}
// would return true
public boolean isomer(Formula f)
{
// TODO
return false;
}
}

Equation.java

/**
* Equation represents chemical equations
* It allows only single molecules atm, i.e. "O+H2=H2O" is fine,
* but "O+2H=H2O" is illegal, and would have to be written "O+H+H=H20"
*
* @author
* @version
*/
import java.util.ArrayList;

public class Equation
{
private ArrayList<Formula> lhs, rhs;

// creates an Equation with the provided values
public Equation(ArrayList<Formula> lhs, ArrayList<Formula> rhs)
{
this.lhs = lhs;
this.rhs = rhs;
}
  
// creates an Equation by parsing s
// e.g. "O+H2=H2O" would give
// lhs = {Formula({Term('O',1)}), Formula({Term('H',2)})},
// rhs = {Formula({Term('H',2), Term('O',1)})}
public Equation(String s)
{
// TODO
}
  
// returns a parsed form of one side of an equation
// e.g. "O+H2" would return {Formula({Term('O',1)}), Formula({Term('H',2)})}
public static ArrayList<Formula> parseSide(String s)
{
// TODO
return null;
}
  
// turns the Equation into a String
// e.g. lhs = {Formula({Term('O',1)}), Formula({Term('H',2)})},
// rhs = {Formula({Term('H',2), Term('O',1)})}
// would return "O + H2 = H2O"
public String display()
{
// TODO
return "";
}
  
// returns true iff this Equation specifies the same atom-counts on each side
// e.g. lhs = {Formula({Term('O',1)}), Formula({Term('H',2)})},
// rhs = {Formula({Term('H',2), Term('O',1)})}
// would return true
public boolean balanced()
{
// TODO
return false;
}
  
// returns the current value of lhs
public ArrayList<Formula> getLHS()
{
return lhs;
}
  
// returns the current value of rhs
public ArrayList<Formula> getRHS()
{
return rhs;
}
}

Explanation / Answer

Term.java:

public class Term

{

    private char element;

    private int atoms;

    // creates a Term with the provided values

    public Term(char element, int atoms)

    {

        this.element = element;

        this.atoms = atoms;

    }

    // creates a Term by parsing s

    // e.g. "H20" would give element = 'H', atoms = 20

    public Term(String s)

    {

        element = s.charAt(0);

        if (s.length() == 1)

          atoms = 1;

        else

          atoms = Integer.parseInt(s.substring(1));

    }   

    // turns the Term into a String

    // e.g. element = 'C', atoms = 4 would give "C4"

    public String display()

    {

        String s = "" + element;

        if (atoms > 1) s += atoms;

        return s;

    }   

    // returns the current value of element

    public char getElement()

    {

        return element;

    }

    // returns the current value of atoms

    public int getAtoms()

    {

        return atoms;

    }

}

Formula.java:

import java.util.ArrayList;

public class Formula

{

    private ArrayList<Term> terms;

    // creates a Formula with the provided value

    public Formula(ArrayList<Term> terms)

    {

        this.terms = terms;

    }

    // creates a Formula by parsing s

    // e.g. "CH2O" would give terms = {Term('C',1),Term('H',2),Term('O',1)}

    public Formula(String s)

    {

        terms = new ArrayList<> ();

        int k = lastElement(s);

        terms.add(new Term(s.substring(k)));

        while (k > 0)

        {

            int j = lastElement(s.substring(0, k));

            terms.add(0, new Term(s.substring(j, k)));

            k = j;

        }

    }   

    // returns the index of the rightmost upper-case letter in s, or -1 if none found

    // e.g. "COHOHH2" would give 5

    public static int lastElement(String s)

    {

        for (int k = s.length() - 1; k >= 0; k--)

            if (Character.isUpperCase(s.charAt(k)))

               return k;

        return -1;

    }  

    // turns the Formula into a String

    // e.g. terms = {Term('C', 1),Term('O',2)} would give "CO2"

    public String display()

    {

        String s = "";

        for (Term t : terms) s += t.display();

        return s;

    }   

    // returns the current value of terms

    public ArrayList<Term> getTerms()

    {

        return terms;

    }   

    // changes terms into molecular form

    // e.g. terms = {Term('H',3),Term('C',1),Term('H',3),Term('C',1)} would become {Term('C',2),Term('H',6)}

    public void makeMolecular()

    {

        ArrayList<Term> z = new ArrayList<>();

        Term t, u;

        while (terms.size() > 0)

        {

            t = nextElement();

            terms.remove(t);

            if (z.isEmpty() || t.getElement() != z.get(z.size() - 1).getElement())

                z.add(t);

            else

            {

                u = new Term(t.getElement(), t.getAtoms() + z.get(z.size() - 1).getAtoms());

                z.remove(z.size() - 1);

                z.add(u);

            }

        }

        terms = z;

    }   

    // returns the next element in terms, alphabetically

    // e.g. terms = {Term('H',4),Term('C',2),Term('H',4),Term('C',1)} would return Term('C',2)

    public Term nextElement()

    {

        Term z = terms.get(0);

        for (Term x : terms)

            if (x.getElement() < z.getElement())

               z = x;

        return z;

    }   

    // returns true iff f is identical to this Formula

    // e.g. terms = {Term('C',2),Term('H',6)} and f = {Term('C',2),Term('H',6)} would return true

    // but terms = {Term('C',2),Term('H',6)} and f = {Term('H',6),Term('C',2)} would return false

    public boolean identical(Formula f)

    {

        if (this.getTerms().size() != f.getTerms().size())

            return false;

        for (int k = 0; k < this.getTerms().size(); k++)

            if (this.getTerms().get(k).getElement() != f.getTerms().get(k).getElement() ||

                this.getTerms().get(k).getAtoms()   != f.getTerms().get(k).getAtoms())

               return false;

        return true;

        // Alternatively, the following would do it:

        // return this.display().equals(f.display());

    }   

    // returns true iff f is an isomer of this Formula

    // e.g. terms = {Term('C',2),Term('H',6)} and f = {Term('C',1),Term('H',3),Term('C',1),Term('H',3)}

    // would return true

    public boolean isomer(Formula f)

    {

        this.makeMolecular();

        f.makeMolecular();

        return this.identical(f);

    }

}

Equation.java:

import java.util.ArrayList;

public class Equation

{

    private ArrayList<Formula> lhs, rhs;

    // creates an Equation with the provided values

    public Equation(ArrayList<Formula> lhs, ArrayList<Formula> rhs)

    {

        this.lhs = lhs;

        this.rhs = rhs;

    }   

    // creates an Equation by parsing s

    // e.g. "O+H2=H2O" would give

    // lhs = {Formula({Term('O',1)}), Formula({Term('H',2)})},

    // rhs = {Formula({Term('H',2), Term('O',1)})}

    public Equation(String s)

    {

        int eq = s.indexOf('=');

        lhs = parseSide(s.substring(0, eq));

        rhs = parseSide(s.substring(eq + 1));

    }   

    // returns a parsed form of one side of an equation

    // e.g. "O+H2" would return {Formula({Term('O',1)}), Formula({Term('H',2)})}

    public static ArrayList<Formula> parseSide(String s)

    {

        ArrayList<Formula> side = new ArrayList<>();

        int plus = s.indexOf('+');

        while (plus >= 0)

        {

            side.add(new Formula(s.substring(0, plus)));

            s = s.substring(plus + 1);

            plus = s.indexOf('+');

        }

        side.add(new Formula(s));

        return side;

    }   

    // turns the Equation into a String

    // e.g. lhs = {Formula({Term('O',1)}), Formula({Term('H',2)})},

    // rhs = {Formula({Term('H',2), Term('O',1)})}

    // would return "O + H2 = H2O"

    public String display()

    {

        String sl = "";

        for (Formula f : lhs)

            sl += " + " + f.display();

        String sr = "";

        for (Formula f : rhs)

            sr += " + " + f.display();

        return sl.substring(3) + " = " + sr.substring(3);

    }   

    // returns true iff this Equation specifies the same atom-counts on each side

    // e.g. lhs = {Formula({Term('O',1)}), Formula({Term('H',2)})},

    // rhs = {Formula({Term('H',2), Term('O',1)})}

    // would return true

    public boolean balanced()

    {

        ArrayList<Term> l = new ArrayList<>();

        for (Formula f : lhs)

            l.addAll(f.getTerms());

        ArrayList<Term> r = new ArrayList<>();

        for (Formula f : rhs)

            r.addAll(f.getTerms());

        return (new Formula(l)).isomer(new Formula(r));

    }   

    // returns the current value of lhs

    public ArrayList<Formula> getLHS()

    {

        return lhs;

    }   

    // returns the current value of rhs

    public ArrayList<Formula> getRHS()

    {

        return rhs;

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote