Advertisement
Loogy

Untitled

Mar 14th, 2021
2,426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Exam
  2. {
  3.    private int day;
  4.    private int month;
  5.    private int year;
  6.    private int hour;
  7.    private int minutes;
  8.    private double maxMarks;
  9.  
  10.    private final String type;
  11.    private final int order;
  12.    
  13.    private static int labExamCounter = 0;
  14.    private static int midtermExamCounter = 0;
  15.  
  16.    public static final String LAB_EXAM = "Lab Exam";
  17.    public static final String MIDTERM_EXAM = "Midterm Exam";
  18.    public static final String FINAL_EXAM = "Final Exam";
  19.  
  20.    public Exam(String type)
  21.    {
  22.     this (type, 0, 0, 0, 0, 0, 0);
  23.    }
  24.    
  25.    public Exam(String type, double maxMarks)
  26.    {
  27.     this (type, maxMarks, 0, 0, 0, 0, 0);
  28.    }
  29.  
  30.    public Exam(String type, double maxMarks, int day, int month, int year)
  31.    {
  32.     this (type, maxMarks, day, month, year, 0, 0);
  33.    }
  34.  
  35.    public Exam(Exam other)
  36.    {
  37.     this (other.getType(), other.getMaxMarks(), other.getDay(), other.getMonth(), other.getYear(), other.getHour(), other.getMinutes());
  38.    }
  39.  
  40.    public Exam(String type, double maxMarks, int day, int month, int year, int hour, int minutes)
  41.    {
  42.       switch( type ) {
  43.          case LAB_EXAM:
  44.             this.order = ++labExamCounter;
  45.             this.maxMarks = (maxMarks > 5 && maxMarks <= 10 ? maxMarks : 10 );
  46.             break;
  47.  
  48.          case MIDTERM_EXAM:
  49.             this.order = ++midtermExamCounter;
  50.             this.maxMarks = (maxMarks > 15 && maxMarks <= 20 ? maxMarks : 20 );
  51.             break;
  52.          
  53.          case FINAL_EXAM:
  54.             this.order = 0;
  55.             this.maxMarks = (maxMarks > 30 && maxMarks <= 40 ? maxMarks : 40 );
  56.             break;
  57.  
  58.          default:
  59.             this.order = 0;
  60.       }
  61.  
  62.       if (day >= 1 && day <= 31)
  63.          this.day = day;
  64.  
  65.       if (month >= 1 && month <= 12)
  66.          this.month = month;
  67.      
  68.       if (year > 0)
  69.          this.year = year;
  70.      
  71.       if (hour >= 0 && hour <= 23)
  72.          this.hour = hour;
  73.  
  74.       if (minutes >= 0 && minutes <= 59)
  75.          this.minutes = minutes;
  76.  
  77.       this.type = (type != null ? type : "");
  78.    }
  79.    
  80.    public String getType()
  81.    {
  82.       return type;
  83.    }
  84.    
  85.    public void setDay(int day)
  86.    {
  87.       if (day >= 1 && day <= 31)
  88.          this.day = day;
  89.    }
  90.    
  91.    public int getDay()
  92.    {
  93.       return day;
  94.    }
  95.  
  96.    public void setMonth(int month)
  97.    {
  98.       if (month >= 1 && month <= 12)
  99.          this.month = month;
  100.    }
  101.  
  102.    public int getMonth()
  103.    {
  104.       return month;
  105.    }
  106.  
  107.    public void setYear(int year)
  108.    {
  109.       if (year > 0)
  110.          this.year = year;
  111.    }
  112.  
  113.    public int getYear()
  114.    {
  115.       return year;
  116.    }
  117.  
  118.    public void setDate(int day, int month, int year)
  119.    {
  120.       this.setDay(day);
  121.       this.setMonth(month);
  122.       this.setYear(year);
  123.    }
  124.  
  125.    public void setHour(int hour)
  126.    {
  127.       if (hour >= 0 && hour <= 23)
  128.          this.hour = hour;
  129.    }
  130.  
  131.    public int getHour()
  132.    {
  133.       return hour;
  134.    }
  135.  
  136.    public void setMinutes(int minutes)
  137.    {
  138.       if (minutes >= 0 && minutes <= 59)
  139.          this.minutes = minutes;
  140.    }
  141.  
  142.    public int getMinutes()
  143.    {
  144.       return minutes;
  145.    }
  146.  
  147.    public void setTime(int hour, int minutes)
  148.    {
  149.       this.setHour(hour);
  150.       this.setMinutes(minutes);
  151.    }
  152.  
  153.    public void setMaxMarks(double maxMarks)
  154.    {
  155.        switch( this.type ) {
  156.          case LAB_EXAM:
  157.             this.maxMarks = (maxMarks > 5 && maxMarks <= 10 ? maxMarks : 10 );
  158.             break;
  159.  
  160.          case MIDTERM_EXAM:
  161.             this.maxMarks = (maxMarks > 15 && maxMarks <= 20 ? maxMarks : 20 );
  162.             break;
  163.          
  164.          case FINAL_EXAM:
  165.             this.maxMarks = (maxMarks > 30 && maxMarks <= 40 ? maxMarks : 40 );
  166.             break;
  167.       }
  168.    }
  169.  
  170.    public double getMaxMarks()
  171.    {
  172.       return this.maxMarks;
  173.    }
  174.  
  175.    public String toString()
  176.    {
  177.       return String.format("Exam: %s %s (%04.2f marks)\nDate: %d/%d/%d\nTime: %02d:%02d\n",
  178.                            getType(), (order > 0 ? order : ""), getMaxMarks(), getDay(),
  179.                            getMonth(), getYear(), getHour(), getMinutes());
  180.    }
  181.  
  182.    public static String getExamStats()
  183.    {
  184.       return LAB_EXAM + ": " + labExamCounter + " exam(s)\n" +
  185.              MIDTERM_EXAM + ": " + midtermExamCounter + " exam(s)\n" ;
  186.    }
  187. }
  188.  
  189. import static java.lang.System.out;
  190.  
  191. public class ExamTest
  192. {
  193.    public static void main(String[] args)
  194.    {
  195.        Exam exam1 = new Exam(Exam.FINAL_EXAM);
  196.        exam1.setDate(13, 8, 1439);
  197.        exam1.setTime(8,0);
  198.        exam1.setMaxMarks(50);
  199.      
  200.        Exam exam2 = new Exam(Exam.MIDTERM_EXAM, 10);
  201.        exam2.setDate(3, 7, 1439);
  202.        exam2.setTime(8,30);
  203.  
  204.        Exam exam3 = new Exam(Exam.LAB_EXAM, 12, 18, 6, 1439);
  205.        exam3.setTime(8,30);
  206.  
  207.        Exam exam4 = new Exam("Lab Quiz", 2, 28, 5, 1439, 10, 30);
  208.        
  209.        Exam exam5 = new Exam(exam3);
  210.        exam5.setDate(10, 7, 1439);
  211.  
  212.        out.println( "exam1:\n" + exam1.toString() );      
  213.        out.println( "exam2:\n" + exam2.toString() );      
  214.        out.println( "exam3:\n" + exam3.toString() );      
  215.        out.println( "exam4:\n" + exam4.toString() );      
  216.        out.println( "exam5:\n" + exam5.toString() );
  217.        
  218.        out.println( "Exam.getExamStats():\n" + Exam.getExamStats() );      
  219.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement