Friday, 25 November 2016

Program to define a class Tender containing data members cost and company name. Accept data for five objects and display company name for which cost is minimum

This program demonstrates you the array of objects. Array of object is the collection of objects of the same class.The given syntax will define an array of reference therefore assign object to individual reference in the array to declare the array of object. The syntax to define array of object is:
<class-name>  <array-name> [size];

For example, the following statement will define an array of object having size 6 references to the class Box.
Box  b[6];

The above statement just declare array of objects. For creating an array of objects (allocating memory) you have to write down the following statements
for (int i=0; i<6; i++) {
 b[i]  = new  Box( );
 // Assigning object to individual reference in the array.
}

You can access the members of particular object by using membership operator like:
b[i].length = 10;
b[i].width = 20;
b[i].height = 30;

PROGRAM
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Tender {

 String name; // Data members declaration
 int cost;
 Tender()  // Default constructor
 {
  name = null;
  cost = 0;
 }
 
 Tender(String s,int a) // Parametrized constructor
 {
  name = s;
  cost = a;
 }
 
 void disp() {
  
  System.out.println("Company name="+name+" cost="+cost);
 }
 
 public static void main(String args[])throws IOException
 {
  Tender T[] = new Tender[5]; // Creating array of objects
  BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
  String s;
  int c,temp = 0,min=0;
  for(int i=0;i<5;i++)
  {
   System.out.println("Enter company name :: "); // accept Company name
   s = b.readLine();
   System.out.println("Enter the cost :: "); // accept the cost
   c = Integer.parseInt(b.readLine()); // Converting string to int
   T[i] = new Tender(s,c); // assign values to data members by constructor
  }
  for(int i=0;i<5;i++)
  {
   if(T[i].cost<min)
   {
    min = T[i].cost; // store minimum value of cost
    temp = i; // store index value of object having minimum cost
   }
  }
  System.out.println("Company having minimum cost=");
  T[temp].disp();
 }
}
OUTPUT
C:\>javac Tender.java
C:\>java Tender
Enter company name ::
TechInfo
Enter the cost ::
120365
Enter company name ::
ITInfoTech
Enter the cost ::
154231
Enter company name ::
ITSystems
Enter the cost ::
100235
Enter company name ::
CompTech
Enter the cost ::
90452
Enter company name ::
iSoftSys
Enter the cost ::
135421
Company having minimum cost=
Company name=CompTech   cost=90452

1 comment: