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: 3685872 • 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;
}
}

Please give me an example

Explanation / Answer

Hello user! I remind you that in case of a question with multiple parts, you must post each part individually, with the full statement, and specifying which question you need to be answered. That said, I will gladly answer the first part of your question :D

In the code I added a main function for testing the class, you can delete it anytime! :D

Here is the output i got:

And here is the functional code:

Term.java:

/*
* Term represents terms in a chemical formula
* Only elements denoted by a single letter are supported
* Examples: N, H2, C20, H147
*
* @author Marvin
* @version 1.00 2016/4/5
*/

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
       element=s.charAt(0);
       atoms=Integer.parseInt(s.substring(1));
   }
  
   // turns the Term into a String
   // e.g. element = 'C', atoms = 4 would give "C4"
   public String display()   {
       // TODO
       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;
   }
  
   public static void main(String[] args){
       Term term1=new Term('C',8);
       Term term2=new Term("H2456");
       System.out.println(term1.display());
       System.out.println(term2.display());
   }
}

If you have any doubts, you can contact me by this way. Have a nice day! :D

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