All pastes #475681 Raw Edit

Someone

public text v1 · immutable
·published 2007-05-07 16:47 UTC
rendered paste body
Write code which creates an array of 5 exam objects, uses a JOptionPane to populate this 
array with 5 exams, and the displays those 5 exams back to the user sorted by date (using a 
JOptionPane to display each exam’s details). 


---------------



import javax.swing.JOptionPane;


// if the class is in its own file, it has to be declared public
class Exam {

   private String examName;
   private String examLocation;
   private int examDate;
   private int examMonth;
   private int examYear;

   public Exam(String n, String l, int d, int m, int y){
     examName = n;
     examLocation = l;
     examDate = d;
     examMonth = m;
     examYear = y;
}   

   public String toString(){
     return examName +": " +examLocation+", " +examDate+"/"+examMonth+"/"+examYear+". ";
   }

	public String getName(){
		return examName;
	}
	public String getLoc(){
		return examLocation;
	}
	public int getDate(){
		return examDate;
	}
	public int getMonth(){
		return examMonth;
	}
	public int getYear(){
		return examYear;
	}


}
public class SortExams{
	// there's no point in doing else if's, because
	// the code can't be reached unless
	// the previous statement returns false
		 public boolean compareTwo(Exam ex, Exam this1) {
			 if(ex.getYear() > this1.getYear()) {
				 return false;
			 }

			 if(ex.getMonth() > this1.getMonth()) {
				 return false;
			 }

			 if(ex.getDate() >= this1.getDate()) {
				 return false;
			 }

			 return true;
		 }
	
   public void main(String[] args){
	Exam[] examArray = new Exam[5];
	for(int i=0; i<examArray.length; i++){
		int  count = i + 1;
		String n=JOptionPane.showInputDialog(null,"Enter Exam "+count+"'s name:");
        String l=JOptionPane.showInputDialog(null, "Enter Exam "+count+"'s location:");
		String ds=JOptionPane.showInputDialog(null,"Enter Exam "+count+"'s date in month:");
        String ms=JOptionPane.showInputDialog(null, "Enter Exam "+count+"'s month:");
        String ys=JOptionPane.showInputDialog(null, "Enter Exam "+count+"'s year:");
        int d=Integer.parseInt(ds);
        int m=Integer.parseInt(ms);
        int y=Integer.parseInt(ys);
        Exam e=new Exam(n,l,d,m,y);
        examArray[i]=e;
    }
        for(int i=examArray.length; i>0; i--){
               for(int a=0; a<i; a++){
                       Exam ex=examArray[i];
                       Exam this1=examArray[a];
                       if(SortExams.compareTwo(ex, this1)){
                               Exam temp=examArray[i];
                               examArray[i]=examArray[a];
                               examArray[a]=temp;
                       }
               }
    }
               for(int b=0;b<5;b++){
                       JOptionPane.showMessageDialog(null, "Box "+b+" of the date-sorted array contains: "+examArray[b].toString());
               }

       System.exit(0);
	}
}