Question

Define a class named Shopping with data members items_bought, cost_of_item, shopping_type (online shopping / mall shopping),...

Define a class named Shopping with data members items_bought, cost_of_item,
shopping_type (online shopping / mall shopping), payment_type (card/cash). The
member functions of this class are calculate_bill and display_bill. Create objects
and illustrate the concept of
i. Constructor overloading
ii. Static method

need in java programming

Homework Answers

Answer #1

import java.util.*;
public class Shopping
{
static int n;
static float[] cost_of_item=new float[20];
static float amount;
String[] items_bought = new String[20];
String shopping_type, payment_type;

//constructor with one arguement
Shopping(int a)
{
n=a;
items_bought=new String[]{"Soap","Hand Wash","Choclates","Tooth Paste"};
cost_of_item=new float[]{33,3,4,5};
shopping_type="online shopping";
payment_type="card";

}

//constructor without arguement hence constructor overloading is acheived
Shopping()
{
Scanner f =new Scanner(System.in);
Scanner s =new Scanner(System.in);
Scanner p =new Scanner(System.in);
System.out.println("Enter the number of items bought ");
n=f.nextInt();
System.out.println("Enter the item name and cost one by one");
for(int i=0;i<n;i++)
{
items_bought[i]=s.nextLine();
cost_of_item[i]=p.nextFloat();
}
Scanner t =new Scanner(System.in);
Scanner q =new Scanner(System.in);
System.out.println("Enter the shopping type (online shopping / mall shopping)");
shopping_type=t.nextLine();
System.out.println("Enter the payment type (cash / card)");
payment_type=q.nextLine();

}

//static member function
static void calculate_bill()
{
amount=0;
for(int i=0;i<n;i++)
{
amount=amount+cost_of_item[i];
}
}

//member function to display
void display_bill()
{
System.out.println("==========BILL==========");
for(int i=0;i<n;i++)
{
System.out.println(items_bought[i]+"\t"+cost_of_item[i]);
}
System.out.println("Total : "+amount);
System.out.println("Shopping Type : "+shopping_type+"\nPayment Type : "+payment_type);
}
   public static void main(String[] args)
   {
   Shopping m1 = new Shopping();
   Shopping.calculate_bill();//accessing static member function without object
   m1.display_bill();//accessing member function using objects
   Shopping m2 =new Shopping(4);
   Shopping.calculate_bill();//accessing static member function without object
   m2.display_bill();//accessing member function using objects
   }
}

OUTPUT

Enter the number of items bought
3
Enter the item name and cost one by one
Rice
15
Apple
10
Orange
12
Enter the shopping type (online shopping / mall shopping)
Mall
Enter the payment type (cash / card)
Cash
==========BILL==========
Rice   15.0
Apple   10.0
Orange   12.0
Total : 37.0
Shopping Type : Mall
Payment Type : Cash
==========BILL==========
Soap   33.0
Hand Wash   3.0
Choclates   4.0
Tooth Paste   5.0
Total : 45.0
Shopping Type : online shopping
Payment Type : card


Static Functions are those functions which can be accesed by the class itself directly without the help of instace ie, objects.Here it is acheived by using the static function calculate_bill() and calls it with just using the class itself.

Constructors are those functions with same name that of the class.Unlike member functions,they don't have return type.Constructor overloading is acheived by calling multiple constructors but with different arguement list or without arguements.Here Shopping () and Shopping(int a) are the constructors used for acheiving constructor overloading

Know the answer?
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
Similar Questions
Write the class definition for a class named Employee. The class should include data members for...
Write the class definition for a class named Employee. The class should include data members for an employee object's name and salary (the salary will be an integer). The class should contain two member functions: the constructor and a function that allows a program to assign values to the data members. Add two member functions to the employee class. One member function should allow any program using an employee object to view the contents of the salary data member. The...
C++ : Create a class called Student. The class includes four data items: studentId, studenName, courseNo...
C++ : Create a class called Student. The class includes four data items: studentId, studenName, courseNo and enrolmentstatus. Define a constructor to get the values of the data members and a member function named void display() to display the values of the data members. Write a class driver to test your class.
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
Design a class named NumberClass that has a vector of any data type (use Class Template)....
Design a class named NumberClass that has a vector of any data type (use Class Template). You should create a parameterized constructor that accept the initialization value for the vector. There should also be member functions to perform the following operations: - Print (cout) each element stored in the vector. - #include <iostream> #include <vector> using namespace std; //create code here int main() { NumberClass<int> myNum({99,34,12,44,77,2,37,4,54,68, 44, 102, 5, 42}); cout << "Print the elements in the vector" << endl;...
1. Define a class named Book that contains:  An int data field named pages that...
1. Define a class named Book that contains:  An int data field named pages that stores the number of pages in the book.  A String data field named title that represents the title of the book.  A constructor with parameters for initializing pages and title.  The getter and setter methods for all data fields.  A toString method that returns book information, including the book title and pages.  The equals method that returns true if...
Create a class hierarchy to be used in a university setting. The classes are as follows:...
Create a class hierarchy to be used in a university setting. The classes are as follows:  The base class is Person. The class should have 3 data members: personID (integer), firstName(string), and lastName(string). The class should also have a static data member called nextID which is used to assign an ID number to each object created (personID). All data members must be private. Create the following member functions: o Accessor functions to allow access to first name and last...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A string data field named symbol1 for the stock’s symbol. • A string data field named name for the stock’s name. • A double data field named previousClosingPrice that stores the stock price for the previous day. • A double data field named currentPrice that stores the stock price for the current time. • A constructor that creates a stock with the specified symbol and...
java Create a program that defines a class called circle. Circle should have a member variable...
java Create a program that defines a class called circle. Circle should have a member variable called radius that is used to store the radius of the circle. Circle should also have a member method called calcArea that calculates the area of the circle using the formula area = pi*r^2. Area should NOT be stored in a member variable of circle to avoid stale data. Use the value 3.14 for PI. For now, make radius public and access it directly...
#ifndef BAG_H #define BAG_H #include <cstdlib> // Provides size_t using namespace std; class bag { public:...
#ifndef BAG_H #define BAG_H #include <cstdlib> // Provides size_t using namespace std; class bag { public: // TYPEDEFS and MEMBER CONSTANTS typedef int value_type; typedef std::size_t size_type; static const size_type CAPACITY = 30; // CONSTRUCTOR bag() {used = 0;} // MODIFICATION MEMBER FUNCTIONS size_type erase(const value_type& target); bool erase_one(const value_type& target); void insert(const value_type& entry); void operator +=(const bag& addend); void sort(const bag& b); //Sort the array in the bag object // CONSTANT MEMBER FUNCTIONS size_type size( ) const {...
in C++ Define a class bank account with proper constructors, accessors, mutators, and other member functions....
in C++ Define a class bank account with proper constructors, accessors, mutators, and other member functions. The class will be store money expressed in dollars and cents. The class should have following data members (fname, lname, accountNum, balance, dollars, cents).Write A function to print out the object bank account info.The class should implement two member functions for deposit and withdrawal. Overload the + operator, so that adding two objects of this class works. please respond quickly and clearly, thank you.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT