DEV Community

Pavithra Saravanan
Pavithra Saravanan

Posted on

2 1

Task 2(28/12)

Task 2:

  1. Create a class called 'Shop'
  2. Create below static variables: static int doorNo = 5; static int discount = 10; 2.1. Create below non-static variables int price, weight;
  3. Create main method.
  4. Inside main method, create instance as below Shop product1 = new Shop(); Shop product2 = new Shop();
  5. Using product1, assign price and weight.
  6. Using product2, assign price and weight.
  7. Call a non-static method as below. product1.bill(); product2.bill();
  8. Save, Compile and fix the error.
  9. Inside bill() method, print price and weight.
  10. Inside main method and inside bill() method, print doorNo and discount
public class Shop
{
static int doorNo = 5;
static int discount = 10;
int price,weight;
public static void main(String[] args)
{
Shop product1 = new Shop();
Shop product2 = new Shop();
product1.price = 550;
product1.weight = 1000;
product2.price = 150;
product2.weight = 50;
System.out.println("Doorno:"+doorNo+ " Discount:"+discount);
product1.bill();
product2.bill();
}
public void bill()
{

System.out.println("Price:"+price+ " Weight:"+weight);
}
}
Enter fullscreen mode Exit fullscreen mode

Output:

Image description

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay