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
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
Get Answers For Free
Most questions answered within 1 hours.