Question

2-1. Define and implement a class named cart. A cart object represents a horse drawn cart that can seat up to four meerkats, after that meerkats have to walk. The meerkats must be represented by meerkat objects. The cart class has the following constructors and behaviours: cort)i bool addleerkat(meerkat cat); vold enptyCort() void printMeerkats0 // create an erpty cart object // dd, ?eerkat to the cart, returns false if full // renove all reerkats from the core print the nane, space,the age, then a new line for eech seerkat in the order they were added Your main program should create a cart object and 5 meerkat objects. The cart object is initially empty. Your main program must then add all 5 meerkat objects to the cart object. An appropriate error message must be printed by your main program when the cart is full
0 0
Add a comment Improve this question Transcribed image text
Answer #1
//meerkart class
public class meerkart
{
string name; //I just add name for example.if you wish you add other properties and functions
meerkart(string name)
{
this.name=name;
}
}
// cart class
public class cart{
static int count=0; //a static object to add only four meerkart
String[] m=new String[4];//I use array as I take only name in meerkart class, you can use 
// list or collection or other datatype for detailed program
cart() //cart constructor
{} 
boolean addMeerkat(meerkat cat) // adding a meerkart to the cart

{

if (count>5) //check the count

{

m[count+1]=cat.name;

return 1; ///return boolean value

}

else return 0;

}

void emptyCart() //check weather the cart is empty or not

{

if(count==0)

  System.out.println("Cart is empty");

else

  System.out.println("Cart is not empty");

}

    
void printMeerkats() // print meerkart names added to the cart
{
for(int i=0;i<=count;i++)
  System.out.println(m[i]+",  ");
}
   public static void main(String[] args)  //main funtion
   {
      System.out.println("WELCOME");
      cart c1=new cart(); //cart object
      cart c2=new cart();
      meerkart m1=new meerkart("cat"); //meerkart objects
      meerkart m2=new meerkart("dog");
      meerkart m3=new meerkart("pig");
      meerkart m4=new meerkart("cow");
      meerkart m5=new meerkart("duck");
int i =c1.addMeerkat(meerkat m1);

if (i==0)

System.out.println(" Meerkat added successfully to the cart 1");

else

System.out.println(" Sorry, Something went wrong.");

i =c1.addMeerkat(meerkat m2);

if (i==0)

System.out.println(" Meerkat added successfully to the cart 1");

else

System.out.println(" Sorry, Something went wrong.");

i =c1.addMeerkat(meerkat m3);

if (i==0)

System.out.println(" Meerkat added successfully to the cart 1");

else

System.out.println(" Sorry, Something went wrong.");

i =c1.addMeerkat(meerkat m4);

if (i==0)

System.out.println(" Meerkat added successfully to the cart 1");

else

System.out.println(" Sorry, Something went wrong.");

i =c1.addMeerkat(meerkat m5);

if (i==0)

System.out.println(" Meerkat added successfully to the cart");

else

System.out.println(" Sorry, Something went wrong.");

c1.emptyCart();
System.out.println("Meerkats in cart 1 - ");
c1.printMeerkats();
   }
}

OUTPUT:

WELCOME

Meerkat added successfully to the cart 1

Meerkat added successfully to the cart 1

Meerkat added successfully to the cart 1

Meerkat added successfully to the cart 1

Sorry, Something went wrong.

Cart is not empty
Meerkats in cart 1 - cat, dog, pig, cow,
Add a comment
Know the answer?
Add Answer to:
2-1. Define and implement a class named cart. A cart object represents a horse drawn cart...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1) Using the O-O Paradigm create a class in C++ for the object Dog 2) Implement...

    1) Using the O-O Paradigm create a class in C++ for the object Dog 2) Implement ALL the behaviors  (member functions/methods- including any needed constructors, and helper methods ) of the class. 3) Write the necessary Client/Application/User program The Client program should do the following: a) create at least 4 objects. b) show all the behaviors

  • JAVA i need write java program Object Classes: designing Shopping Cart 1.Shopping Cart , build a...

    JAVA i need write java program Object Classes: designing Shopping Cart 1.Shopping Cart , build a checkout system for a shop which sells items (i.e., products say Bread, Milk, and Bananas). A shopping cart that can have multiples. Costs of the products are : Bread - $1, Milk - $0.60 and Banana - $0.40. A system should displays the order total. 2.The heart of a shopping cart can be represented in three classes: a cart class an order class, and...

  • Write a python program using Object Oriented and do the following: 1. create class "cat" with...

    Write a python program using Object Oriented and do the following: 1. create class "cat" with the following properties: name, age (create constructor method: def __init__) 2. create class "adopter" with the following properties: name, phone 3. create class "transaction" with these properties: adopter, cat (above objects) cat1 = "puffy, 2" adopter1 = "Joe, 123" Test your program: Joe adopts puffy. Print: "Per Transaction 1 <joe> has adopted <puffy>" this can only be done with object oriented programming, no way...

  • c++ Part 1 Consider using the following Card class as a base class to implement a...

    c++ Part 1 Consider using the following Card class as a base class to implement a hierarchy of related classes: class Card { public: Card(); Card (string n) ; virtual bool is_expired() const; virtual void print () const; private: string name; Card:: Card() name = ""; Card: :Card (string n) { name = n; Card::is_expired() return false; } Write definitions for each of the following derived classes. Derived Class Data IDcard ID number CallingCard Card number, PIN Driverlicense Expiration date...

  • Please use C++,thank you! Write an AddressBook class that manages a collection of Person objects. Use the Person class developed in question 2. An AddressBook will allow a person to add, delete, o...

    Please use C++,thank you! Write an AddressBook class that manages a collection of Person objects. Use the Person class developed in question 2. An AddressBook will allow a person to add, delete, or search for a Perso n object in the address book The add method should add a person object to the address book. The delete method should remove the specified person object from the address book 0 .The search method that searches the address book for a specified...

  • For C++ This is the information about the meal class 2-1. (24 marks) Define and implement...

    For C++ This is the information about the meal class 2-1. (24 marks) Define and implement a class named Dessert, which represents a special kind of Meal. It is to be defined by inheriting from the Meal class. The Dessert class has the following constructor: Dessert (string n, int co) // creates a meal with name n, whose type // is "dessert" and cost is co The class must have a private static attribute static int nextID ; which is...

  • Question 2 (3 mark): Write a program to implement the class Coffee according to the following...

    Question 2 (3 mark): Write a program to implement the class Coffee according to the following UML diagram and description so that it can display as the output example. Coffee -energy: double -protein: double -fat: double +Coffee +Coffee _energy: double, _protein: double. _fat: double) Coffee(sourceCup: Coffee) -printNutrition Information(): void +main(String[] args): void REQUIREMENTS The program has three constructors: the first one is a default constructor, it initializes the 3 data fields (50.0, 1.5, 1.7) at default; the second initializes the...

  • Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the c...

    C++ Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the class point that should have the following Private data members x and y (of type int), with default values of 0 A constant ID of type int A private static integer data member named numOfPoints This data member should be o Incremented whenever a new point object is created. o Decremented whenever a point object is destructed. A default constructor An...

  • C++ Programming Assignment S Mammal Lab This lab's goal is to give you some practice using inheritance, virtual functions, pointers, dynamic memory allocation, random numbers, and polym...

    C++ Programming Assignment S Mammal Lab This lab's goal is to give you some practice using inheritance, virtual functions, pointers, dynamic memory allocation, random numbers, and polymorphism. To complete the lab implement the following steps: Create a class called Mammal. All mammals have a weight and a name, so its data should be the mammal's weight and name. Provide a default constructor that sets the mammal's weight to 0 and name to null, and another constructor that allows the weight...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT