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...

  • Create a base class and two derived classes. Also, create and call a member function named...

    Create a base class and two derived classes. Also, create and call a member function named communicate() that behaves differently when called by each class. Create a single C++ project (and CPP source file) named animal_communication as follows: Into this source, type the source code for Program 15-16, "Program Output," in Section 15.6, "Polymorphism and Virtual Member Functions," in Ch. 15, "Inheritance, Polymorphism, and Virtual Functions," of Starting Out With C++ From Control Structures Through Objects. Run and debug the...

  • In Java For the following questions, “define a class” means the following: • Create an appropriately-named...

    In Java For the following questions, “define a class” means the following: • Create an appropriately-named file containing the Java class definition, including the following: – A block comment describing the file (and hence the class), as always – Any instance variables, as required by the question – Accessor (getter) and mutator (setter) methods for any instance variables – Constructors as appropriate or as required by the question – A toString method that prints instances of the class informatively –...

  • 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...

  • Create a new class named Fraction that models fractions, such as 2/3 or 45/9. Implement the...

    Create a new class named Fraction that models fractions, such as 2/3 or 45/9. Implement the following API: Constructor: Fraction(int numerator, int denominator) . String toString() so instances of the class can be printed. The format used above is sufficient. Fraction simplify() This method returns a new Fraction that is arithmetically equal to that given, but for which the numerator and denominator have no common factors. Create a testing program, TestFraction, that prompts the user to provide the numerator and...

  • 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...

  • IN JAVA For the following questions, “define a class” means the following: • Create an appropriately-named...

    IN JAVA For the following questions, “define a class” means the following: • Create an appropriately-named file containing the Java class definition, including the following: – A block comment describing the file (and hence the class), as always – Any instance variables, as required by the question – Accessor (getter) and mutator (setter) methods for any instance variables – Constructors as appropriate or as required by the question – A toString method that prints instances of the class informatively –...

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