Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Array output

$
0
0
I am trying to get the output to display the element from each array. Program will find and display the highest empNo but I have tried all different ways in the "return" from getMax() in the derived class to display all. This is a past assignment so I am just looking for an answer for my own knowledge.

current output is only: 75
Want to display: 75 S T U

Thanks

Main Class
public class HighArrayObject2a
{
   protected long[] empNo;
   protected String[] ssno;
   protected String[] lastName;
   protected String[] firstName;
   protected int nElems;
   
   public HighArrayObject2a(int max)         // constructor
   {
      empNo = new long[max];                 // create the array
      ssno = new String[max];				 // a string is an object
      lastName = new String[max];			 // this only creates references
      firstName = new String[max];
      nElems = 0;                     		 // default value is 0
   }
   
   public boolean find(long searchKey)
   {                             
      int j;
      for(j=0; j<nElems; j++)    
         if(empNo[j] == searchKey)           // == ok since empNo is a primative
            break;                           // exit loop before end
      if(j == nElems)                        // gone to end?
         return false;                       
      else
         return true;                        // no, found it
   }  // end find()
   
   public void insert(long eNo, String sNo, String lName, String fName)    
   {
	   empNo[nElems] = eNo;
	   ssno[nElems] = sNo;
	   lastName[nElems] = lName;
	   firstName[nElems] = fName;
	   nElems++;
   }

   public boolean delete(long value)
   {
      int j;
      for(j=0; j<nElems; j++)        // look for it
         if( value == empNo[j] )
            break;
      if(j==nElems)                  // can't find it
         return false;
      else                           // found it
      {
         for(int k=j; k<nElems; k++) // move higher ones down
         {
            empNo[k] = empNo[k+1];	 // can we do this with employee objects?
            ssno[k] = ssno[k+1];
            lastName[k] = lastName[k+1];
            firstName[k] = firstName[k+1];
         }	
         nElems--;                   // decrement size
         return true;
         }
      }  // end delete()
   
   public void display()             // displays array contents
   {
      for(int j=0; j<nElems; j++)      
         System.out.print(empNo[j] + " " + ssno[j] + " " + lastName[j] + " " + firstName[j]+ "\n"); 	
   }
	  
}   // end class HighArrayObject2a




Here is my derived class.
public class HighArrayObject2b extends HighArrayObject2a
{
			
	public HighArrayObject2b(int max)
	{
		super(max);
	}

	int j = 0;
	int maxIndex = 0;
   public long getMax()
   {
	   if(nElems == 0)
	   {
		   return -1;   
	   }
	   
	   long max = empNo[0];
	   
	   	for(int j = 0; j < nElems; j++) 
	   	{
		   if(max < empNo[j])
		   {
			   max = empNo[j]; 
				
		   }
	   }
		return max;
   }// end getMax()
 }



Here is the App.
import java.io.*;  //needed for file work

public class HighArrayObject2aApp
{
	
	public static void main(String[] args)throws IOException
	{
		int maxSize = 100;
		HighArrayObject2b arr;
		arr = new HighArrayObject2b(maxSize);
				
		arr.insert(10, "A", "B", "C");
		arr.insert(05, "D", "E", "F");
		arr.insert(25, "G", "H", "I");
		arr.insert(35, "J", "K", "L");
		arr.insert(12, "M", "N", "O");
		arr.insert(06, "P", "Q", "R");
		arr.insert(75, "S", "T", "U");
		arr.insert(45, "V", "W", "X");
		arr.insert(41, "Y", "Z", "AA");
		arr.insert(69, "BB", "CC", "DD");
		
			
		// set up printer output file
		PrintWriter pw = new PrintWriter(new BufferedWriter
			(new FileWriter("bs_outputfile.dat")));
		
		System.out.println("Highest key number is.." + arr.getMax());
		
		pw.println("Highest key number is.." + arr.getMax());	// send output to printer file
		
		pw.close(); 
		
		
		} // end main()
}// end class HighArrayObject2a



Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>