The correct way to write javadocs - java

I have been given the exiciting job to add some javadoc to some code.
so here is my Q:
what is the right way to write javadoc for this constructor.
public Match(int MatchID, int MatchRound, int HomeTeamID, int GuestTeamID, boolean IsPlayed) {
this.isPlayed = IsPlayed;
this.matchID = MatchID;
this.matchRound = MatchRound;
this.homeTeamID = HomeTeamID;
this.guestTeamID = GuestTeamID;
}

I wouldn't write any, because it's obvious.
I would rename all the parameters to follow Java conventions, though.

Here is the formatted Javadoc:
/**
* Constructor for creating a new match.
* #param MatchID the id of the match
* #param MatchRound the round for the match
* #param HomeTeamID the id of the home team
* #param GuestTeamID the id of the guest team
* #param IsPlayed whether or not the match is played
*/
public Match(int MatchID, int MatchRound, int HomeTeamID, int GuestTeamID, boolean IsPlayed) {
In Eclipse you can just type /** above the constructor and it will generate the Javadoc template automatically.

Related

How do I get the relative atomic mass onto a different class ? As well as using that to create compounds

I'm trying to figure out how to get the relativeAtomicMass from one class named ChemicalElement onto another called MolarMass in which I try to figure out the atomicmass of an element given it's symbol then find out a compound mass. I'm not sure how to return a double, I tried using a toString to do it since that's what a tutor taught me to do but I'm not sure if it's the correct way that my teacher wants me to do it. I know I need to get the relativeAtomicMass onto MolarMass to finish the assignment but i'm stuck at the moment. Heres the code for my assignment:
package chemical;
public class ChemicalElement {
// Properties (fields/variables): This time we leave give them default visibility, which gives
// other classes in the same package (i.e. the same directory) full access to them, for brevity.
/** The number of protons */
int atomicNumber;
/** 1-3 letter IUPAC symbol */
String symbol;
/** Full name */
String name;
/** Relative atomic mass () */
double relativeAtomicMass;
/** Row in the periodic table */
int period;
/** Column in the periodic table */
int group;
// Actions (methods): For now we provide a constructor to initialize all fields.
/**
* Constructs a new ChemicalElement object with the given properties.
*
* #param atomicNumber the number of protons
* #param symbol the IUPAC symbol
* #param name the full name
* #param relativeAtomicMass the average atomic mass relative to carbon-12
* #param period the row in the periodic table
* #param group the column in the periodic table
*/
public ChemicalElement(int atomicNumber, String symbol, String name, double relativeAtomicMass,
int period, int group) {
this.atomicNumber = atomicNumber;
this.symbol = symbol;
this.name = name;
this.relativeAtomicMass = relativeAtomicMass;
this.period = period;
this.group = group;
}
public String toString()
{
return name;
}
}
package chemical;
import java.net.URL;
import java.util.Scanner;
import java.util.Arrays;
public class MolarMass {
public static void main(String[] compoundTokens) throws Exception {
ChemicalElement[] elements = readElementData();
//System.out.printf("%.6f%n", compoundmass(elements, compoundTokens));
System.out.println(Arrays.toString(elements));
}
/**
* Loads data about all chemical elements into an array.
*
* #return an array containing references to objects describing all 118 chemical elements
* #throws Exception if anything goes awry (we only know how to pass the buck)
*/
private static ChemicalElement[] readElementData() throws Exception {
ChemicalElement[] elements = new ChemicalElement[119];
Scanner dataFile =
new Scanner(new URL("http://jeff.cis.cabrillo.edu/datasets/elements").openStream());
while (dataFile.hasNext()) {
// Example line: 1 H Hydrogen 1.008 1 1
int atomicNumber = dataFile.nextInt();
elements[atomicNumber] = new ChemicalElement(atomicNumber, dataFile.next(), dataFile.next(),
dataFile.nextDouble(), dataFile.nextInt(), dataFile.nextInt());
}
return elements;
}
/**
* Determines the molar mass of a compound.
*
* #param elements an array containing references to objects describing all 118 chemical elements
* #param tokens a compound formula
* #return the molar mass of the given compound
*/
// private static double compoundMass(ChemicalElement[] elements, String[] tokens) {
// TODO
/**
* Determines the relative atomic mass of an element, given its symbol.
*
* #param elements an array containing references to objects describing all 118 chemical elements
* #param symbol the symbol of the element in question
* #return the relative atomic mass of the given element
*/
// private static double relativeAtomicMass(ChemicalElement[] elements, String symbol) {
// TODO
}
Also i'd imagine that once I get the relative atomic mass onto molarmass it would be easy to fill out the code for relativeatomicmass but how would I use that to create compounds?
Thanks any help would be appreciated.

Junit Testing Constructors

So for my programming class, we are supposed to make a Junit test for one of our classes that inherits from an abstract class. I've just been really confused on writing Junit tests since I don't feel like there's enough examples online. My first question is, do we test getters and setters? My professor instructed us to "select all" the methods when setting up our test, but on a website she asked us to read, they advised not to test getters and setters because JVM already has testers for them. My second and most important question is can you test an entire constructor? My code below basically has an undergrad student inheriting from an abstract student class. I'm assuming I should test to make sure that the undergrad student is in fact an undergrad student and not a master student, but I don't know how to exactly test an entire constructor. Any tips would be greatly appreciated!!
public abstract class AbstractStudent {
/**
* First name of a student.
*/
private String myFirstName;
/**
* Last name of a student.
*/
private String myLastName;
/**
* Student 9-digit ID.
*/
private String myID;
/**
* Number of credit hours completed by a student.
*/
private int myCreditHours;
/**
* Current student GPA.
*/
private double myGPA;
/**
* Student gender.
*/
private Gender myGender;
/** Student date of birth.
*
*/
private Date myBirth;
/**
* Private constructor to prohibit default instantiation.
*/
/**
* #param theFirstName Adds the first name into the constructor
* #param theLastName Adds the last name into the constructor
* #param theID Adds the ID into the constructor
* #param theCreditHours Adds the credit hours into the constructor
* #param theGPA Adds the GPA into the constructor
* #param theGender Adds the gender into the constructor
* #param theBirth Adds the birth into the constructor
*/
public AbstractStudent(final String theFirstName, final String theLastName,
final String theID, final int theCreditHours, final double theGPA,
final Gender theGender, final Date theBirth) {
myFirstName = theFirstName;
myLastName = theLastName;
myID = theID;
myCreditHours = theCreditHours;
myGPA = theGPA;
myGender = theGender;
myBirth = new Date(theBirth.getTime());
}
And the UndergradStudent class:
public class UndergradStudent extends AbstractStudent {
/**
* Student status.
*/
private StudentStatus myStatus;
/**
* Parameterized constructor - constructors a Student object.
* #param theFirstName is a string representing the first name of a student, != null
* #param theLastName is a string representing the last name of a student, != null
* #param theID is a string of 9 characters representing student ID
* #param theCreditHours is an integer number >=0 representing number of credit hours
* taken by a student
* #param theGPA is a double number representing GPA, a GPA must be >= 0 and <= 4.0
* #param theStatus is a string representing student status, != null
* #param theGender is a character representing student gender, != null
* #param theBirth is a date representing student birth date; a student cannot be younger
* than 10 years old
* #custom.post Student object constructed; if invalid parameters passed,
* student is in an invalid state.
*/
public UndergradStudent(final String theFirstName, final String theLastName,
final String theID, final int theCreditHours, final double theGPA,
final StudentStatus theStatus, final Gender theGender,
final Date theBirth) {
super(theFirstName, theLastName, theID, theCreditHours, theGPA, theGender, theBirth);
myStatus = theStatus;
}
I can't really comment on the intention of your professor but when writing unit tests you are aiming to test single units of functionality. Most developers consider testing basic POJO behaviour where a get method returns a constructor property as redundant, however if you are writing getter/setter methods which are stateful they are worth testing.
One way you could test the whole constructor of UndergradStudent is to construct an instance either as a private final variable or using JUnit's #BeforeClass annotation and writing a separate #Test for each property getter.

Creating an enum in DELPHI

I have an enum codes in JAVA. I convert all JAVA code to DELPHI.
I almost done, but i stucked in here. I have no idea, how to convert enum to Delphi.
I am wondering, this code can be convert to Delphi ?
/**
* Enum describing the databin class ID's. Methods exist for getting the
* KakaduClassID and the StandardClassID. I have also included the string
* representations of the databins as defined for cache model updates.
*
*
*/
public enum JPIPDatabinClass {
/** Precinct data bin class. */
PRECINCT_DATABIN(KakaduConstants.KDU_PRECINCT_DATABIN, JPIPConstants.PRECINCT_DATA_BIN_CLASS, "P"),
/** Tile Header data bin class. */
TILE_HEADER_DATABIN(KakaduConstants.KDU_TILE_HEADER_DATABIN, JPIPConstants.TILE_HEADER_DATA_BIN_CLASS, "H"),
/** Tile data bin class. */
TILE_DATABIN(KakaduConstants.KDU_TILE_DATABIN, JPIPConstants.TILE_DATA_BIN_CLASS, "T"),
/** Main Header data bin class. */
MAIN_HEADER_DATABIN(KakaduConstants.KDU_MAIN_HEADER_DATABIN, JPIPConstants.MAIN_HEADER_DATA_BIN_CLASS, "Hm"),
/** Meta data bin class. */
META_DATABIN(KakaduConstants.KDU_META_DATABIN, JPIPConstants.META_DATA_BIN_CLASS, "M");
/** The classID as an integer as per the Kakadu library. */
private int kakaduClassID;
/** The classID as an integer as per the JPEG2000 Part-9 standard. */
private int standardClassID;
/**
* The classID as a string as per the JPEG2000 Part-9 standard. Used for
* cache model updates.
*/
private String jpipString;
/**
* Constructor.
*
* #param _kakaduClassID
* #param _standardClassID
* #param _jpipString
*/
JPIPDatabinClass(int _kakaduClassID, int _standardClassID, String _jpipString) {
kakaduClassID = _kakaduClassID;
standardClassID = _standardClassID;
jpipString = _jpipString;
}
/** Returns the classID as an integer as per the Kakadu library. */
public int getKakaduClassID() {
return kakaduClassID;
}
/** Returns the classID as an integer as per the JPEG2000 Part-9 standard. */
public int getStandardClassID() {
return standardClassID;
}
/**
* Returns the classID as a string as per the JPEG2000 Part-9 standard. Used
* for cache model updates.
*/
public String getJpipString() {
return jpipString;
}
};
This Enum can easily be translated to a plain old Delphi class which has a three-argument constructur like the Java Enum, and three read-only public properties.
JPIPDatabinClass = class(TObject)
private
...
public
constructor Create(AKakaduClassID: Integer; AStandardClassID: Integer; AJPIP: string);
property KakaduClassID: Integer; read FKakaduClassID;
property StandardClassID: Integer; read FStandardClassID;
property JPIP: string; read FJPIP;
end;
and 'singleton style' instances:
function PRECINCT_DATABIN: JPIPDatabinClass;
function TILE_HEADER_DATABIN: JPIPDatabinClass;
...
implementation
var
FPRECINCT_DATABIN: JPIPDatabinClass;
FTILE_HEADER_DATABIN: JPIPDatabinClass;
...
FPRECINCT_DATABIN := JPIPDatabinClass.Create( ... );
FTILE_HEADER_DATABIN := JPIPDatabinClass.Create( ... );
...
function PRECINCT_DATABIN: JPIPDatabinClass;
begin
Result := FPRECINCT_DATABIN;
end;
function TILE_HEADER_DATABIN: JPIPDatabinClass;
begin
Result := FTILE_HEADER_DATABIN;
end;
...
Note: the disadvantage of this approach is that does not create real Delphi enum types, it only emulates the Java enum type as immutable Delphi objects.

Mybatis No Getter Property

I'm working with Mybatis 3.2.6 and implementing a custom resulthandler. I've done this before using a simple datatype parameter and have had no problems. This time around I need to pass in several arguments... The signature I'm using is
session.select(statement, parameter, handler);
For parameter I've created a simple POJO to easily send in what I need. It is as follows:
public class DifferenceParam {
private int current;
private int compare;
private String table;
private String comparator;
/**
* Constructor excluding comparator. Will default a value of
* "code" to compare content on, e.g., <br/>
* {#code select * from a minus select * from b where a.code = b.code } <br/>
* #param table
* #param current
* #param compare
*/
public DifferenceParam(String table, int current, int compare) {
this(table, "code", current, compare);
}
/**
* Constructor providing a specific column to compare on, e.g. <br/>
* {#code select * from a minus select * from b where a.[comparator] = b.[comparator] } <br/>
* #param table
* #param comparator
* #param current
* #param compare
*/
public DifferenceParam(String table, String comparator, int current, int compare) {
this.table = table;
this.comparator = comparator;
this.current = current;
this.compare = compare;
}
/** Appropriate setters and getters to follow **/
}
The handler implementation is irrelevant at the moment, because I get an exception well in advance... The query I'm executing is:
<select id="getCodeSetModifications" parameterType="DifferenceParam" resultType="Code">
select *
from
(
select * from ${param.table} where revision_seq = #{param.current}
minus
select * from ${param.table} where revision_seq = #{param.compare}
) a, ${param.table} b
where a.${param.comparator} = b.${param.comparator}
and b.revision_seq = #{param.compare}
</select>
Here is the interface as well
public List<Code> getCodeSetModifications(#Param("param") DifferenceParam param);
The problem I'm having is that execution via a mapper e.g.,
session.getMapper(DifferenceParam.class);
works just fine, but when I invoke through a select on the session I get the following exception.
Error querying database. Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'param' in 'class com.mmm.his.cer.cerval.uidifference.map.param.DifferenceParam'
Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'param' in 'class com.mmm.his.cer.cerval.uidifference.map.param.DifferenceParam'
I've debugged as far as I can go into Mybatis, but am having no luck.
Thanks in advance...
When you use session.getMapper(DifferenceParam.class);, mybatis looks for #Param annotation and uses it's value in query.
When you invoke session.select(statement, parameter, handler);, such mapping doesn't occur.
Try to add public DifferenceParam getParam() { return this; } to DifferenceParam to workaround this.
when the MyBatis query only have one param. Don't need #{param.} reference.
because it default use the only param.
so when u use ${param.table}, it actually using #{DifferenceParam.param.table}.
because it think the ${param.} inside of the #{DifferenceParam.}

Create a live template for Java in Intellij

I would like to create a template to automatically make my Javadoc documentation when I create a method. For example if I create a method:
protected User createUser_1P(String uname, String[] stocks) throws ElementAlreadyExists {
...
return u;
}
I would like the template to generate something like this:
/*
* #param uname user name
* #param stocks list of stocks
* #return User
* #throws ElementAlreadyExists
*/
Any help will be apreciated. Thanks
To generate JavaDoc for your method in IntelliJ all you need to do is to type /** on the line above the method definition and hit Enter.
For your method the following JavaDoc will be generated:
/**
*
* #param uname
* #param stocks
* #return
* #throws ElementAlreadyExists
*/
protected User createUser_1P(String uname, String[] stocks) throws ElementAlreadyExists {
...
return u;
}
Mind you that IntelliJ does not provide with any descriptions, you will have to fill them out.

Categories

Resources