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: 3684758 • 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);
   atoms = Integer.parseInt(s.substring(1).trim()); // getting integer part
}
  
// turns the Term into a String
// e.g. element = 'C', atoms = 4 would give "C4"
public String display()
{
return (""+element)+atoms;
}
  
// 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;
import java.util.HashMap;
import java.util.Map;
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)
{
int atoms = 1;
char elements;
Term term;
for(int i=0; i<s.length(); ){
   elements = s.charAt(i); // getting element
   i++;
   if(i<s.length() && Character.isDigit(s.charAt(i))){ // if next character is digit
       atoms = (int)s.charAt(i+1);
       i++;
   }
   else // else atom number = 1
       atoms = 1;
  
   term = new Term(elements, atoms); // creating new term
   terms.add(term); // adding current term into list
}
}
  
// 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)
{
int index = -1;
for(int i=s.length()-1; i>=0; i--){
   if(Character.isLetter(s.charAt(i))){
       index = i;
       break;
   }
}
return index;
}
  
// turns the Formula into a String
// e.g. terms = {Term('C', 1),Term('O',2)} would give "CO2"
public String display()
{
String formula = "";
for(Term term : terms){
   formula = formula+term.getElement(); // adding element into formula
   if(term.getAtoms() > 1) // if number of atoms is > 1, them add into string
       formula = formula + term.getAtoms();
}
return formula;
}
  
// 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()
{
   // adding elements and atoms into map
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
  
ArrayList<Term> newTerm = new ArrayList<Term>();
for(Term term : terms){
   if(map.containsKey(term.getElement())){ // if element is already in map
       // add current element atoms into existing and again add into map with updated value
       map.put(term.getElement(), map.get(term.getElement()) + term.getAtoms());
   }else// new element
       map.put(term.getElement(), term.getAtoms());
}
  
// iterate through map and create newTerm arrayList
for(Map.Entry<Character, Integer> entry : map.entrySet()){
   newTerm.add(new Term(entry.getKey(), entry.getValue()));
}
  
terms = newTerm;
}
  
// 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)
{
   if(f == null)
       return false;
  
   if(terms.size() != f.getTerms().size())
       return false;
  
   ArrayList<Term> fTerms = f.getTerms();
for(int i=0; i<terms.size(); i++){
   Term t1 = terms.get(i);
   Term t2 = fTerms.get(i);
  
   if(t1.getElement() != t2.getElement() || t1.getAtoms()!= t2.getAtoms())
       return false;
}
return true;
}
  
// 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)
{
// creating molecular map for terms
   HashMap<Character, Integer> termsMap = new HashMap<Character, Integer>();
   //creating molecular map for f
   HashMap<Character, Integer> fMap = new HashMap<Character, Integer>();
  
   for(Term term : terms){
   if(termsMap.containsKey(term.getElement())){ // if element is already in map
       // add current element atoms into existing and again add into map with updated value
       termsMap.put(term.getElement(), termsMap.get(term.getElement()) + term.getAtoms());
   }else// new element
       termsMap.put(term.getElement(), term.getAtoms());
}
  
   for(Term term : f.getTerms()){
   if(fMap.containsKey(term.getElement())){ // if element is already in map
       // add current element atoms into existing and again add into map with updated value
       fMap.put(term.getElement(), fMap.get(term.getElement()) + term.getAtoms());
   }else// new element
       fMap.put(term.getElement(), term.getAtoms());
}
  
   // now comparing number of atoms for each element in termsMap and fMap
   for(Map.Entry<Character, Integer> entry : termsMap.entrySet()){
         
    char ele = entry.getKey();
    int atoms = entry.getValue();
   
    if(!fMap.containsKey(ele)) // if element is not present in fMAp
        return false;
   
    else if(fMap.get(ele) != atoms) // if number of atoms are not equal
        return false;
}
     
return true; // reach here means isomeric
}
}

// Equation.java : I will update soon.

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