In: Computer Science
Java programmers!!!!
Complete the definition of the Tavunu class according to the specifications below and the JUnit4 test file given
Background
A tavunu is an imaginary Earth-dwelling being that looks a bit like a Patagonian Mara and lives in a non-gendered but hierarchical society. Most interactions among tavunu are negotiated with pava --- items of status used for bargaining. Pava is always traded whole; you can't trade half a pava.
public class Tavunu {
// See what to do
}
what to do
Develop some code
Complete the definition of the Tavunu class so it represents tavuni (that's plural for tavunu) according to the specifications below and the JUnit4 test file here( see below).
A Tavunu instance should have a name of the tuvunu, the amount of pava the tavunu holds, and their year of birth.
A Tavunu instance should also have the following methods:
import org.junit.Test; import static org.junit.Assert.*; /** * Tests the Tavunu class w/ JUnit 4. * */ public class TavunuTest { /** * Also tests accessors. */ @Test public void testCtorNoArg() { var tv = new Tavunu(); assertEquals(tv.getName(), ""); assertEquals(tv.getPava(), 0); assertEquals(tv.getBirthYear(), Integer.MIN_VALUE); } /** * Also tests accessors. */ @Test public void testCtorParams() { var tv = new Tavunu("Dease", 1944, 42); assertEquals(tv.getName(), "Dease"); assertEquals(tv.getPava(), 42); assertEquals(tv.getBirthYear(), 1944); } @Test public void testToString() { var tv = new Tavunu("Dease", 1944, 42); assertEquals("" + tv, "Dease born in 1944 has 42 pava."); } @Test public void testSetName() { var tv = new Tavunu(); tv.setName(""); assertEquals(tv.getName(), ""); tv.setName("T"); assertEquals(tv.getName(), "T"); tv.setName("D"); assertEquals(tv.getName(), "D"); tv.setName("Trelling"); assertEquals(tv.getName(), "Trelling"); tv.setName("Dint"); assertEquals(tv.getName(), "Dint"); tv.setName("tranque"); assertEquals(tv.getName(), "Dint"); tv.setName("demary"); assertEquals(tv.getName(), "Dint"); tv.setName("Hint"); assertEquals(tv.getName(), "Dint"); } @Test public void testSetYear() { var tv = new Tavunu("Dease", 1944, 42); tv.setBirthYear(-2001); assertEquals(tv.getBirthYear(), -2001); tv.setBirthYear(0); assertEquals(tv.getBirthYear(), 0); tv.setBirthYear(2001); assertEquals(tv.getBirthYear(), 2001); } @Test public void testReceivePava() { var tv = new Tavunu("Dease", 1944, 42); boolean rv = tv.receivePava(-2001); assertEquals(tv.getPava(), 42); assertEquals(rv, false); rv = tv.receivePava(-1); assertEquals(tv.getPava(), 42); assertEquals(rv, false); rv = tv.receivePava(0); assertEquals(tv.getPava(), 42); assertEquals(rv, true); rv = tv.receivePava(1); assertEquals(tv.getPava(), 43); assertEquals(rv, true); rv = tv.receivePava(10); assertEquals(tv.getPava(), 53); assertEquals(rv, true); } @Test public void testSpendPava() { var tv = new Tavunu("Dease", 1944, 42); boolean rv = tv.spendPava(-2001); assertEquals(tv.getPava(), 42); assertEquals(rv, false); rv = tv.spendPava(-1); assertEquals(tv.getPava(), 42); assertEquals(rv, false); rv = tv.spendPava(0); assertEquals(tv.getPava(), 42); assertEquals(rv, true); rv = tv.spendPava(1); assertEquals(tv.getPava(), 41); assertEquals(rv, true); rv = tv.spendPava(10); assertEquals(tv.getPava(), 31); assertEquals(rv, true); } }
Code
public class Tavunu {
private String name;
private int birthYear;
private int pava;
public Tavunu() {
name="";
birthYear=Integer.MIN_VALUE;
pava=0;
}
/**
* @param name
* @param birthYear
* @param pava
*/
public Tavunu(String name, int birthYear, int pava)
{
super();
this.name = name;
this.birthYear = birthYear;
this.pava = pava;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the pava
*/
public int getPava() {
return pava;
}
/**
* @param int the pava to spend
*/
public boolean spendPava(int pava) {
if(pava>=0 &&
pava<=this.pava)
{
this.pava-=pava;
return
true;
}
return false;
}
/**
* @param int the amount to add
*/
public boolean receivePava(int amount)
{
if(pava>=0 &&
pava<=this.pava)
{
this.pava+=pava;
return
true;
}
return false;
}
/**
* @param string the name to set
*/
public boolean setName(String name) {
if(name.charAt(0)=='T' ||
name.charAt(0)=='D')
{
this.name =
name;
return
true;
}
return false;
}
/**
* @return the birthYear
*/
public int getBirthYear() {
return birthYear;
}
/**
* @param birthYear the birthYear to set
*/
public void setBirthYear(int birthYear) {
this.birthYear=birthYear;
}
}
If you have any query regarding the code please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.