ulvis.paste.net

Paste Search Dynamic
Recent pastes
Java arrays
  1. /*
  2. * File: CarLot.java
  3.  
  4. * Author:Dejene Mitiku
  5.  
  6. * Date: Sep 20, 2018
  7.  
  8. * Purpose: This program demonstrates the creation
  9.  
  10. * and use of Java arrays
  11. */
  12.  
  13.  
  14. //constructor
  15.  
  16. class Carlot
  17.  
  18. {
  19.  
  20.     public int lotnum;
  21.  
  22.     public string color;
  23.  
  24.     Carlot(int lotnum, string color)
  25.  
  26.     {
  27.   this.lotnum = lotnum;
  28.  
  29.         this.color = color;
  30.  
  31.     }
  32. }
  33. // Elements of array are objects of a class Carlot.
  34. class Carlotnum
  35. {
  36.  
  37.     public static void main (string[] args)
  38.  
  39.     {
  40.  
  41.          // declares an Array of integers.
  42.  
  43.         Carlot[] arr;
  44.  
  45.  
  46.       // allocating memory for 5 objects of type Student.
  47.  
  48.         arr = new Carlot[5];
  49.  
  50.  
  51.       // initialize the first elements of the array
  52.  
  53.         arr[0] = new Carlot(100,"Brown");
  54.  
  55.  
  56.       // initialize the second elements of the array
  57.  
  58.         arr[1] = new Carlot(200,"Silver");
  59.  
  60.         arr[2] = new Carlot(300,"Black");
  61.  
  62.         arr[3] = new Carlot(400,"Red");
  63.  
  64.          arr[4] = new Carlot(500,"Grey");
  65.  
  66.  
  67.       // accessing the elements of the specified array
  68.  
  69.         for (int i = 0; i < arr.length; i++)
  70.  
  71.             system.out.println("Lot number and car color at " + i + " : " +
  72.  
  73.                         arr[i].lotnum +" "+ arr[i].color);
  74.  
  75.    }
  76. }
Parsed in 0.029 seconds