0% found this document useful (0 votes)
5 views11 pages

5 Bii

The document outlines two programming experiments focused on inheritance in Java. The first experiment involves creating classes for stock transactions to calculate maximum profit from stock prices, while the second experiment involves creating a production system for plays and musicals, tracking performances and box office collections. The conclusion highlights the learning outcomes related to inheritance, constructors, and method overriding.
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)
5 views11 pages

5 Bii

The document outlines two programming experiments focused on inheritance in Java. The first experiment involves creating classes for stock transactions to calculate maximum profit from stock prices, while the second experiment involves creating a production system for plays and musicals, tracking performances and box office collections. The conclusion highlights the learning outcomes related to inheritance, constructors, and method overriding.
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/ 11

Name: Ridhima Srivastava

UID: 2024200126

Experiment No. 5b

AIM: Experiment on inheritance

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.
Sample Input
Enter stock name: abc
Enter number of days: 4
Enter predicted prices for stock abc:
10
20
5
4
Output
Buy on day 1 and sell on day 2
Maximum Profit: 10
--------------------------------------------------------------------------------------
Sample Input
Enter stock name: xyz
Enter number of days: 4
Enter predicted prices for stock xyz:
10
9
8
7
Output
Maximum Profit: 0

PROGRAM: import java.util.Scanner;

class Stock {
String stockName;
int[] prices;

// Constructor to initialize stock name and prices array


public Stock(String stockName, int[] prices) {
this.stockName = stockName;
this.prices = prices;
}
}

class Transaction extends Stock {

// Constructor to initialize stock name and prices


public Transaction(String stockName, int[] prices) {
super(stockName, prices);
}

// Method to find the maximum profit


public void findMaximumProfit() {
int n = prices.length;
int totalProfit = 0;
StringBuilder transactions = new StringBuilder();
// Loop through the prices and find profitable transactions
for (int i = 1; i < n; i++)
{
if (prices[i] > prices[i - 1])
{
// Buy at previous day and sell at current day
transactions.append("Buy on day ").append(i).append(" and sell
on day ").append(i + 1).append("\n");
totalProfit += prices[i] - prices[i - 1];
}
}

if (totalProfit > 0)
{
System.out.print(transactions.toString());
}
System.out.println("Maximum Profit: " + totalProfit);
}
}

public class main


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

System.out.print("Enter stock name: ");// Input stock name


String stockName = scanner.nextLine();

System.out.print("Enter number of days: "); // Input number of days


int n = scanner.nextInt();

int[] prices = new int[n];


System.out.println("Enter predicted prices for stock " + stockName +
":");
for (int i = 0; i < n; i++) {
prices[i] = scanner.nextInt();// Input predicted prices
}

Transaction t = new Transaction(stockName, prices);// Create an


instance of Transaction class

// Call method to find and print the maximum profit


t.findMaximumProfit();
}
}

RESULT:

Program 2

PROBLEM Define class Production that has attributes String title, String
STATEMENT : director, String writer. Production class has 3 argument
constructor that sets the values. It also has getter and setter
methods and Overridden toString() of object class to display
details of class.

class Play is a sub class of Production with getter and setter


methods and has an attribute int performances that is
incremented every time a play happens.

Add Overridden toString() of object class to display details of the


class.

class Musical is a Play with songs. Musical object has all


attributes of Play as well as String composer and String lyricist
along with getter and setter methods. Override toString display
all attributes of Musical object

In main create 3 objects of Play and 2 objects of Musical. Every


time an object of Play or Musical is created, performances get
incremented. Also add the number of seats booked for each play
or musical.

Find the total box office collection, provided cost of 1 seat for
Play is Rs 500(can be variable) and cost of 1 seat for Musical is
Rs 800(can be variable)

Display total No. of performances as 5 and display the box


office collection.

Input

Enter seats booked for ABC: 2

Enter seats booked for DEF: 3

Enter seats booked for GHI: 4

Enter seats booked for JKL: 5

Enter seats booked for MNO: 6

OutPut

Total No. of Performances: 5

Total Box Office Collection: Rs 13300

PROGRAM: import java.util.Scanner;

class Production
{
private String title;
private String director;
private String writer;

// Constructor
public Production(String title, String director, String writer)
{
this.title = title;
this.director = director;
this.writer = writer;
}

// Getter and Setter methods


public String getTitle()
{
return title;
}

public void setTitle(String title)


{
this.title = title;
}

public String getDirector()


{
return director;
}

public void setDirector(String director)


{
this.director = director;
}

public String getWriter()


{
return writer;
}

public void setWriter(String writer)


{
this.writer = writer;
}

// Overridden toString() method


@Override
public String toString()
{
return "Title: " + title + ", Director: " + director + ", Writer: " + writer;
}
}

class Play extends Production


{
private int performances;
private int seatsBooked;

// Constructor
public Play(String title, String director, String writer)
{
super(title, director, writer);
this.performances = 0; // Initializing with 0 performances
}

// Getter and Setter methods for performances and seatsBooked


public int getPerformances()
{
return performances;
}

public void incrementPerformances()


{
performances++;
}

public int getSeatsBooked()


{
return seatsBooked;
}

public void setSeatsBooked(int seatsBooked)


{
this.seatsBooked = seatsBooked;
}

// Overridden toString() method for Play


@Override
public String toString() {
return super.toString() + ", Performances: " + performances;
}
}

class Musical extends Play


{
private String composer;
private String lyricist;

// Constructor
public Musical(String title, String director, String writer, String
composer, String lyricist)
{
super(title, director, writer);
this.composer = composer;
this.lyricist = lyricist;
}

// Getter and Setter methods for composer and lyricist


public String getComposer()
{
return composer;
}

public void setComposer(String composer)


{
this.composer = composer;
}

public String getLyricist()


{
return lyricist;
}

public void setLyricist(String lyricist)


{
this.lyricist = lyricist;
}
// Overridden toString() method for Musical
@Override
public String toString()
{
return super.toString() + ", Composer: " + composer + ", Lyricist: " +
lyricist;
}
}

public class main1


{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int totalPerformances = 0;
int totalBoxOfficeCollection = 0;

// Create Play objects


Play play1 = new Play("ABC", "Director1", "Writer1");
play1.incrementPerformances();
System.out.print("Enter seats booked for ABC: ");
play1.setSeatsBooked(scanner.nextInt());

Play play2 = new Play("DEF", "Director2", "Writer2");


play2.incrementPerformances();
System.out.print("Enter seats booked for DEF: ");
play2.setSeatsBooked(scanner.nextInt());

Play play3 = new Play("GHI", "Director3", "Writer3");


play3.incrementPerformances();
System.out.print("Enter seats booked for GHI: ");
play3.setSeatsBooked(scanner.nextInt());

// Create Musical objects


Musical musical1 = new Musical("JKL", "Director4", "Writer4",
"Composer1", "Lyricist1");
musical1.incrementPerformances();
System.out.print("Enter seats booked for JKL: ");
musical1.setSeatsBooked(scanner.nextInt());
Musical musical2 = new Musical("MNO", "Director5", "Writer5",
"Composer2", "Lyricist2");
musical2.incrementPerformances();
System.out.print("Enter seats booked for MNO: ");
musical2.setSeatsBooked(scanner.nextInt());

// Calculate total performances and box office collection


totalPerformances = play1.getPerformances() +
play2.getPerformances() + play3.getPerformances() +
musical1.getPerformances() +
musical2.getPerformances();

totalBoxOfficeCollection = (play1.getSeatsBooked() +
play2.getSeatsBooked() + play3.getSeatsBooked()) * 500
+ (musical1.getSeatsBooked() +
musical2.getSeatsBooked()) * 800;

// Display details
System.out.println("\nTotal No. of Performances: " +
totalPerformances);
System.out.println("Total Box Office Collection: Rs " +
totalBoxOfficeCollection);
}
}
RESULT:

CONCLUSION: Through the above codes i learnt about the concepts of inheritance,keyword
extends,multiple inheritance,override function .Revision of concepts of
constructor ,methods.

You might also like