Ask a Teacher



Java program,Define a class named FruitJuice with the following description: Instance variables/data members: int product_code- stores the product code number. String flavour- stores the flavour of the juice(eg:orange,apple) String pac_type- stores the type of packaging(eg:terapack) int pack_size- stores package size(eg:200ml,400ml) int product_price- stores the price of the product. Member methods: 1.FruitJuice()-Default constructor to initialize integer data members to 0 and string data members to "". 2.void input()-To input and store the product code,flavour,pack type,pack size and product price. 3.void discount()-To reduce the product price by 10. 4.void display()-To display the product code,flavour,pack type,pack size and prduct price.

import java.util.*;
class FruitJuice
{
    // instance variables / data members
    int product_code;
    String flavour;
    String pack_type;
    int pack_size, product_price;

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

    // input and store the member details
    public void input()
    {
        product_code = askInt("Enter the product code: ");
        flavour = askString("Enter the flavour: ");
        pack_type = askString("Enter the pack type: ");
        pack_size = askInt("Enter the pack size: ");
        product_price = askInt("Enter the product price: ");
    }

    // compute the discount
    public void discount()
    {
        product_price -= 10;
    }

    // display the object details
    public 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 + " ml.");
        System.out.println("Product price: " + product_price + " Rs.");
        System.out.println();
    }

    // utility method to ask user to enter an int
    // and return the int value entered
    public int askInt(String prompt)
    {
        Scanner sc = new Scanner( System.in );        
        System.out.print(prompt);
        int n = sc.nextInt();
        String temp = sc.nextLine(); // read the end of line
        return n;
    }

    // utility method to ask user to enter a String
    // and return the String value entered
    public String askString(String prompt)
    {
        Scanner sc = new Scanner( System.in );        
        System.out.print(prompt);
        String s = sc.nextLine();
        return s;
    }

    // main method to execute the class
    public static void main(String[] args)
    {
        FruitJuice obj = new FruitJuice();
        obj.input();    // (1) ask user data for one juice
        obj.display(); // (2) display
        obj.discount(); // (3) compute discount
        obj.display();  // (4) display discounted price
    }
}


comments powered by Disqus