Question

In: Computer Science

Java programmers!!!! Complete the definition of the Tavunu class according to the specifications below and the...

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:

  • setName(name): gives the tavunu a new name. All tavunu names begin with the letters 'T' or 'D'. If the desired new name does not meet this criterion, the method does not change the name and returns false. Otherwise it changes the tavunu's name and returns true.
  • getName(): returns the tavunu's name.
  • spendPava(amount): decreases the amount of pava held by the tavunu. If the amount is zero or less the method does not change the pava and returns false. Otherwise it subtracts the amount from the tavunu's pava and returns true.
  • receivePava(amount): increases the amount of pava held by the tavunu. If the amount is zero or less it does not change the pava and it returns false. Otherwise it adds the amount to the tavunu's pava and returns true.
  • getPava(): returns the amount of pava held by this tavunu.
  • Accessor and mutator for the year of their birth. Negative values are OK --- they correspond to BCE dates.
  • An appropriate toString() method.
  • No-argument (default) and parameterized constructors.
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);
    }

}

Solutions

Expert Solution

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.


Related Solutions

USING JAVA: complete the method below in the BasicBioinformatics class. /** * Class BasicBioinformatics contains static...
USING JAVA: complete the method below in the BasicBioinformatics class. /** * Class BasicBioinformatics contains static methods for performing common DNA-based operations in * bioinformatics. * * */ public class BasicBioinformatics { /** * Calculates and returns the number of times each type of nucleotide occurs in a DNA sequence. * * @param dna a char array representing a DNA sequence of arbitrary length, containing only the * characters A, C, G and T * * @return an int array...
Write a program in Java and run it in BlueJ according to the following specifications: The...
Write a program in Java and run it in BlueJ according to the following specifications: The program reads a text file with student records (first name, last name and grade on each line) and determines their type (excellent or ok). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "all" - prints all student records (first name, last name, grade, type). "excellent" - prints students with grade > 89. "ok"...
Topic: Computer Science | Java Programming Complete the class Packet below. The Packet class contains: Four...
Topic: Computer Science | Java Programming Complete the class Packet below. The Packet class contains: Four private instance variables to store information regarding to the source host, destination host, time stamp and ip packet size. A constructor that takes a string object as a parameter and set up instance variables defined above. public accessor methods: getSourceHost(), getDestinationHost(), getTimeStamp(), and getIpPacketSize(); public mutator methods: setSourceHost(), setDestinationHost(), setTimeStamp(), and setIpPacketSize(); A toString() method that returns a string description of a packet. Test...
Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class...
Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class Fibonacci { // Fib(N): N N = 0 or N = 1 // Fib(N-1) + Fib(N-2) N > 1 // For example, // Fib(0) = 0 // Fib(1) = 1 // Fib(2) = Fib(1) + Fib(0) = 1 + 0 = 1 // Fib(3) = Fib(2) + Fib(1) = Fib(2) + 1 = (Fib(1) + Fib(0)) + 1 = 1 + 0 + 1...
Below is a definition of the class of a simple link list. class Chain; class ChainNode...
Below is a definition of the class of a simple link list. class Chain; class ChainNode { friend class Chain; private: int data; ChainNode *link ; }; class Chain{ public: ... private: ChainNode *first; // The first node points. } Write a member function that inserts a node with an x value just in front of a node with a val value by taking two parameters, x and val. If no node has a val value, insert the node with...
Below is a definition of the class of the circular link list. class CircChain; class ChainNode...
Below is a definition of the class of the circular link list. class CircChain; class ChainNode { friend class CircChain; private: int data; ChainNode *link ; }; class CircChain{ public: ... private: ChainNode *last; // The last node points. } => Create a member function to delete the last node. Returns false if empty list; otherwise, delete last node and return true. 'Bool CircChain::DeleteLastNode()'
using java Define a class Library based on the following specifications: a. A library can have...
using java Define a class Library based on the following specifications: a. A library can have multiple books. Decide on the best possible data structure to store books. b. A library has a name. c. A library has an address. Define a 2-argument constructor for the Library class. Decide on the arguments needed for this constructor. e. Define a method that can add new books to the library. f. Define a method that allows a book to be borrowed (checked...
JAVA PROBLEM Part 1: Create a Car class in accordance with the following specifications. I will...
JAVA PROBLEM Part 1: Create a Car class in accordance with the following specifications. I will provide the CarClassTester class as a test driver to test your Car class for its basic structure. Do not change the CarClassTester class source code. After you get your Car class working correctly using this test driver, proceed to part 2 below. Car Class Specifications: The Car class must be in a separate package from any driver/tester program. The Car class will contain, at...
Build a Date class and a main function to test it Specifications Below is the interface...
Build a Date class and a main function to test it Specifications Below is the interface for the Date class: it is our "contract" with you: you have to implement everything it describes, and show us that it works with a test harness that puts it through its paces. The comments in the interface below should be sufficient for you to understand the project (use these comments in your Date declaration), without the need of any further documentation. class Date...
Using Java, Complete the LinkedListBag down below by using the (LinkedListCollection.java:) package Homework3; public class LinkedListBag...
Using Java, Complete the LinkedListBag down below by using the (LinkedListCollection.java:) package Homework3; public class LinkedListBag extends LinkedListCollection { LinkedListBag() { } public T grab() { T result; Node cursor = head; int rand = (int) (Math.random() * size()); // Some code in here.. result = cursor.getInfo(); remove(result); return result; } } LinkedListCollection.java: package Homework3; public class LinkedListCollection <T> { protected Node<T> head = null; public LinkedListCollection() { } public boolean isEmpty() { return head == null; } public int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT