Computer Applications

Define a class called FruitJuice, Constructor based program

Define a class called FruitJuice with the following description:

Instance variables/Data members:

      int product_code         – stores the product code number.

      String flavor                – stores the flavor of the juice. (orange, apple etc.)

      String pack_type         – stores the type of packaging (tetra-pack, bottle etc.)

      int pack_size   – stores package size (200ml, 400ml etc.)

      int product_price        – stores the price of the product.

Member methods:

  • FruitJuice( ) – default constructor to initialize data members to zero and string data members to “ ”.
  • void input( ) – to input and store the product code, flavor, pack type, pack size and product price.
  • void discount( ) – to reduce the product price by 10.
  • void display( ) – to display the product code, flavor, pack type, pack size and product price.

Write a main( ) method where user will create an object of the class and call all the member functions.

import java.io.*;
import java.util.*;

class FruitJuice
{
	int product_code;
	String flavour;
	String pack_type;
	int pack_size;
	int product_price;

	FruitJuice( )                      //constructor
	{
		product_code = 0;
		flavour = " ";
		pack_type = " ";
		pack_size = 0 ;
		product_price = 0 ;
	}

	void input( )
	{
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter Product Code =");
		product_code = sc.nextInt();
		System.out.println("Enter Flavour =");
		flavour = sc.next();
		System.out.println("Enter Pack Type =");
		pack_type = sc.next();
		System.out.println("Enter Pack Size =");
		pack_size = sc.nextInt();
		System.out.println("Enter Product Price =");
		product_price = sc.nextInt();
	}

	void display( )
	{
		System.out.println("Product Code = "+product_code);
		System.out.println("Flavour = "+flavour);
		System.out.println("Pack Type = "+pack_type);
		System.out.println("Pack Size = "+pack_size);
		System.out.println("Product Price = "+product_price);
	}

	void discount( )
	{
		product_price = product_price - 10 ;
	}

	public static void main(String args[ ])
	{
		FruitJuice app = new FruitJuice( );
		app.input( );
		app.discount( );
		app.display( );
	}
}

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

To Top