Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Exam
- {
- private int day;
- private int month;
- private int year;
- private int hour;
- private int minutes;
- private double maxMarks;
- private final String type;
- private final int order;
- private static int labExamCounter = 0;
- private static int midtermExamCounter = 0;
- public static final String LAB_EXAM = "Lab Exam";
- public static final String MIDTERM_EXAM = "Midterm Exam";
- public static final String FINAL_EXAM = "Final Exam";
- public Exam(String type)
- {
- this (type, 0, 0, 0, 0, 0, 0);
- }
- public Exam(String type, double maxMarks)
- {
- this (type, maxMarks, 0, 0, 0, 0, 0);
- }
- public Exam(String type, double maxMarks, int day, int month, int year)
- {
- this (type, maxMarks, day, month, year, 0, 0);
- }
- public Exam(Exam other)
- {
- this (other.getType(), other.getMaxMarks(), other.getDay(), other.getMonth(), other.getYear(), other.getHour(), other.getMinutes());
- }
- public Exam(String type, double maxMarks, int day, int month, int year, int hour, int minutes)
- {
- switch( type ) {
- case LAB_EXAM:
- this.order = ++labExamCounter;
- this.maxMarks = (maxMarks > 5 && maxMarks <= 10 ? maxMarks : 10 );
- break;
- case MIDTERM_EXAM:
- this.order = ++midtermExamCounter;
- this.maxMarks = (maxMarks > 15 && maxMarks <= 20 ? maxMarks : 20 );
- break;
- case FINAL_EXAM:
- this.order = 0;
- this.maxMarks = (maxMarks > 30 && maxMarks <= 40 ? maxMarks : 40 );
- break;
- default:
- this.order = 0;
- }
- if (day >= 1 && day <= 31)
- this.day = day;
- if (month >= 1 && month <= 12)
- this.month = month;
- if (year > 0)
- this.year = year;
- if (hour >= 0 && hour <= 23)
- this.hour = hour;
- if (minutes >= 0 && minutes <= 59)
- this.minutes = minutes;
- this.type = (type != null ? type : "");
- }
- public String getType()
- {
- return type;
- }
- public void setDay(int day)
- {
- if (day >= 1 && day <= 31)
- this.day = day;
- }
- public int getDay()
- {
- return day;
- }
- public void setMonth(int month)
- {
- if (month >= 1 && month <= 12)
- this.month = month;
- }
- public int getMonth()
- {
- return month;
- }
- public void setYear(int year)
- {
- if (year > 0)
- this.year = year;
- }
- public int getYear()
- {
- return year;
- }
- public void setDate(int day, int month, int year)
- {
- this.setDay(day);
- this.setMonth(month);
- this.setYear(year);
- }
- public void setHour(int hour)
- {
- if (hour >= 0 && hour <= 23)
- this.hour = hour;
- }
- public int getHour()
- {
- return hour;
- }
- public void setMinutes(int minutes)
- {
- if (minutes >= 0 && minutes <= 59)
- this.minutes = minutes;
- }
- public int getMinutes()
- {
- return minutes;
- }
- public void setTime(int hour, int minutes)
- {
- this.setHour(hour);
- this.setMinutes(minutes);
- }
- public void setMaxMarks(double maxMarks)
- {
- switch( this.type ) {
- case LAB_EXAM:
- this.maxMarks = (maxMarks > 5 && maxMarks <= 10 ? maxMarks : 10 );
- break;
- case MIDTERM_EXAM:
- this.maxMarks = (maxMarks > 15 && maxMarks <= 20 ? maxMarks : 20 );
- break;
- case FINAL_EXAM:
- this.maxMarks = (maxMarks > 30 && maxMarks <= 40 ? maxMarks : 40 );
- break;
- }
- }
- public double getMaxMarks()
- {
- return this.maxMarks;
- }
- public String toString()
- {
- return String.format("Exam: %s %s (%04.2f marks)\nDate: %d/%d/%d\nTime: %02d:%02d\n",
- getType(), (order > 0 ? order : ""), getMaxMarks(), getDay(),
- getMonth(), getYear(), getHour(), getMinutes());
- }
- public static String getExamStats()
- {
- return LAB_EXAM + ": " + labExamCounter + " exam(s)\n" +
- MIDTERM_EXAM + ": " + midtermExamCounter + " exam(s)\n" ;
- }
- }
- import static java.lang.System.out;
- public class ExamTest
- {
- public static void main(String[] args)
- {
- Exam exam1 = new Exam(Exam.FINAL_EXAM);
- exam1.setDate(13, 8, 1439);
- exam1.setTime(8,0);
- exam1.setMaxMarks(50);
- Exam exam2 = new Exam(Exam.MIDTERM_EXAM, 10);
- exam2.setDate(3, 7, 1439);
- exam2.setTime(8,30);
- Exam exam3 = new Exam(Exam.LAB_EXAM, 12, 18, 6, 1439);
- exam3.setTime(8,30);
- Exam exam4 = new Exam("Lab Quiz", 2, 28, 5, 1439, 10, 30);
- Exam exam5 = new Exam(exam3);
- exam5.setDate(10, 7, 1439);
- out.println( "exam1:\n" + exam1.toString() );
- out.println( "exam2:\n" + exam2.toString() );
- out.println( "exam3:\n" + exam3.toString() );
- out.println( "exam4:\n" + exam4.toString() );
- out.println( "exam5:\n" + exam5.toString() );
- out.println( "Exam.getExamStats():\n" + Exam.getExamStats() );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement