0% found this document useful (0 votes)
6 views6 pages

Expt 5a

The document describes a Java program that simulates stock trading, allowing users to input stock prices and perform transactions to maximize profit. It includes a Stock class for initializing stock prices and a Transaction subclass for calculating profits from buying and selling on specified days. The program uses a greedy approach to identify optimal buying and selling days, providing total profit and transaction details to the user.

Uploaded by

nachiket.kale24
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)
6 views6 pages

Expt 5a

The document describes a Java program that simulates stock trading, allowing users to input stock prices and perform transactions to maximize profit. It includes a Stock class for initializing stock prices and a Transaction subclass for calculating profits from buying and selling on specified days. The program uses a greedy approach to identify optimal buying and selling days, providing total profit and transaction details to the user.

Uploaded by

nachiket.kale24
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/ 6

Name: Nachiket Kale

UID: 2024800052

Experiment No. 5A

AIM:

Program 1

PROBLEM The cost of stock on each day is given in an array A[] of size N.
STATEMENT :
Day 1 price in first location, day 2 price in second location etc. Find all
the days on which you buy and sell the stock any number of time so that
in between those days your profit is maximum.A new transaction can
only start after previous transaction is complete. Person can hold only
one share at a time.

Create class Stock that has name of stock and array of prices. Also it
has input method that initialises the predicted price of the stock in an
array of length N.

Create class Transaction that is sub class of Stock class. It has method
findMaximumProfit method.

PROGRAM: import java.util.Scanner;


class Stock
{
Scanner sc=new Scanner(System.in);
String stock_name;
double arr[]=new double[7];
Stock()
{
System.out.println("Welcome to StockTrading.");
}
void input()
{
System.out.println("Enter the prices of stock :");
for(int i=0;i<7;i++)
{
System.out.println("Enter the price of Stock on
day"+(i+1)+":");
arr[i]=sc.nextDouble();
}
}
}
class Transaction extends Stock
{
double profit;
void max_profit(int d1,int d2)
{
double temp=arr[d2-1]-arr[d1-1];
System.out.println("Profit or loss made in this
transaction:"+temp);
profit += temp;
}
}
class Stock_Transaction
{
public static void main(String[] args)
{
Scanner sc =new Scanner(System.in);
Transaction tr=new Transaction();
tr.input();
while(true)
{
int c;
System.out.println("Choose from the following");
System.out.println("1.To start a transaction");
System.out.println("2.View total profit");
System.out.println("3.Display the prices");
System.out.println("4.Exit Trading session");
System.out.print("Enter the choice");
c=sc.nextInt();
switch(c)
{
case 1:
int d1,d2;
System.out.println("Enter the day to buy");
d1=sc.nextInt();
System.out.println("Enter the day to sell");
d2=sc.nextInt();
tr.max_profit(d1,d2);
break;
case 2:
System.out.println("Total profit or
loss:RS"+tr.profit);
break;
case 3:
System.out.println("Displaying the prices....");
for(int i=0;i<7;i++)
{
System.out.println("Day"+(i+1)+":RS"+tr.arr[i]);
}
break;
case 4:
System.out.println("Exitting the Trading
session.....");
return;
}
}
}
}

RESULT:
CONCLUSION:
The problem is solved using a greedy approach, where we buy at local
minima and sell at local maxima to maximize profit. Multiple transactions
are allowed, ensuring we capture every price rise. The output provides total
profit along with specific buy and sell days.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy