Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help with python please. Write out the class definition for CookieJar. It must h

ID: 3579628 • Letter: H

Question

Help with python please.

Write out the class definition for CookieJar. It must have two instance variables: kind (a string), and count (a non-negative integer). The constructor must accept a parameter for each instance variable. The ___str___ method must exist and return a string like "Cookie3ar('snickerdoodle', 12)" implement the take method, accepting integer parameter n. Remove n cookies from the jar's count (or all, when there aren't enough cookies), and return how many cookies were removed. Examples: taking 7 cookies from a jar with 10: rightarrow 7 taking 7 cookies from a jar with 5: rightarrow 5 After/outside of the class, create a variable that refers to a CookieJar object; then, correctly call its take method. Use the back of this sheet if you need more space; if so circle this phrase;

Explanation / Answer

please find below the CookieJar.java

public class CookieJar {
   private String kind;
   private int count;

   public CookieJar(String kind, int count) {
       super();
       this.kind = kind;
       this.count = count;
   }

   public String getKind() {
       return kind;
   }

   public void setKind(String kind) {
       this.kind = kind;
   }

   public int getCount() {
       return count;
   }

   public void setCount(int count) {
       this.count = count;
   }

   public int take(int n) {
       int rd = 0;
       if (count > n) {
           rd = n;
       } else {
           rd = count;
       }
       count = count - rd;
       return rd;
   }

   @Override
   public String toString() {
       return "CookieJar ('" + kind + "', " + count + ")";
   }

   public static void main(String[] args) {
       CookieJar cj = new CookieJar("snickerdoodle", 12);
       int n = cj.take(5);
       System.out.println(n + " cookies are taken");
       System.out.println(cj.toString());
   }

}

-----------------------------------------

output:

5 cookies are taken
CookieJar ('snickerdoodle', 7)

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