0% found this document useful (0 votes)
8 views

Aman Java Exp 2.1

This document outlines an experiment for a Java program that calculates the profit for electronic gadgets based on user-provided revenue and cost. It describes the structure of the program, which includes a Business class for user input and a Profit class for profit calculation using the formula Profit = Revenue - Cost. The program displays the calculated profit formatted to two decimal places for each product entered by the user.

Uploaded by

Only Menes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Aman Java Exp 2.1

This document outlines an experiment for a Java program that calculates the profit for electronic gadgets based on user-provided revenue and cost. It describes the structure of the program, which includes a Business class for user input and a Profit class for profit calculation using the formula Profit = Revenue - Cost. The program displays the calculated profit formatted to two decimal places for each product entered by the user.

Uploaded by

Only Menes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 2.1
Student Name: Aman Kumar UID: 23BCS12527
Branch: BE-CSE Section/Group: 608-B
Semester: 4th Date: 18/02/2025
Subject Name: OOPs using Java Subject Code: 23CSP-202

1. Aim:
The business sells electronic gadgets, and the owner needs to calculate the profit for
each product based on its revenue and cost. The program consists of two classes: The
Business Class, which handles user input for multiple products, prompts the user for
revenue and cost, and uses the Profit class to calculate and display the profit for each
product. The Profit Class calculates the profit using the formula.

Formula:
Profit = Revenue - Cost

With a constructor to initialize the values and a method to compute the profit. The
profit for each product should be displayed formatted to two decimal places.

2. Objective:

The objective of this project is to develop a Java program that calculates and
displays the profit for each electronic gadget using user-provided revenue and cost.
It utilizes two classes—Business for handling input and display, and Profit for
computing profit using the formula: Profit = Revenue - Cost, formatted to two
decimal places.
3. Java Code:

import java.util.Scanner;

public class Business {

// Inner Profit class


static class Profit {
private double revenue;
private double cost;

// Constructor to initialize revenue and cost


public Profit(double revenue, double cost) {
this.revenue = revenue;
this.cost = cost;
}

// Method to calculate profit


public double calculateProfit() {
return revenue - cost;
}
}

// Main method
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of products: ");


int numberOfProducts = scanner.nextInt();

for (int i = 1; i <= numberOfProducts; i++) {


System.out.println("\nProduct " + i + ":");

System.out.print("Enter revenue: ");


double revenue = scanner.nextDouble();

System.out.print("Enter cost: ");


double cost = scanner.nextDouble();

// Create a Profit object and calculate profit


Profit profit = new Profit(revenue, cost);
double result = profit.calculateProfit();

// Display profit formatted to two decimal places


System.out.printf("Profit for Product %d: %.2f\n", i, result);
}

scanner.close();
}
}
4. Output:

You might also like