Python help Make a class called CookieJar with 2 parameters- kind, and count. Cr
ID: 3772974 • Letter: P
Question
Python helpMake a class called CookieJar with 2 parameters- kind, and count. Create three methods- __init__, __str__ that displays for ex: ‘CookieJar(chocolate, 4)’, and a method called taken. In taken, n number of cookies are removed from the count. If n cookies is greater than the count, than just the count is taken out completely. Return the number of cookies taken out from the count/jar. Ex: If you have 10 cookies in the jar (count) and you want to take 12 cookies, you return the 10, since only a max of 10 can be taken out. Afterwards the class is written, you are given a variable q that calls the CookieJar object, and you must call the taken method from class CookieJar Python help
Make a class called CookieJar with 2 parameters- kind, and count. Create three methods- __init__, __str__ that displays for ex: ‘CookieJar(chocolate, 4)’, and a method called taken. In taken, n number of cookies are removed from the count. If n cookies is greater than the count, than just the count is taken out completely. Return the number of cookies taken out from the count/jar. Ex: If you have 10 cookies in the jar (count) and you want to take 12 cookies, you return the 10, since only a max of 10 can be taken out. Afterwards the class is written, you are given a variable q that calls the CookieJar object, and you must call the taken method from class CookieJar Python help
Explanation / Answer
class CookieJar
{
String kind=new String();
int count;
void init(String type,int n)
{
kind=type;
count=n;
}
void displayContent()
{
System.out.println("Jar contents are "+kind+" number of items"+count);
}
int taken(int n)
{
int temp;
if(count>=n)
{
count=count-n;
return n;
}
else
{ temp=count;
count=0;
return temp;
}
}
}
public class ContentDemo
{
public static void main(String[] args)
{
CookieJar jar1=new CookieJar();
jar1.init("chocolate",10);
jar1.displayContent();
System.out.println("Take away 6 from jar1 Number of cookies taken away are:"+jar1.taken(6));//take away 5
jar1.displayContent();
System.out.println("try to take away 5 Number taken from jar1 are:"+jar1.taken(4));
}
}
Generated the following output:
sh-4.3$ javac ContentDemo.java
sh-4.3$ java ContentDemo
Jar contents are chocolate
number of items10
Take away 6 from jar1
Number of cookies taken away are:6
Jar contents are chocolate
number of items4
try to take away 5
Number taken from jar1 are:4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.