/**
* Author: Tan Jun Qi
* Student ID: 33872846
* Unit: ICT 167 Principles of computer Science
* Lecturer: Aaron Yeo
*
*/
public class Postgraduate extends Student {
int assignment;
int presentation;
int exam;
double overallmark;
int assignment, int presentation, int exam) {
super(type,title, firstname, lastname, StuID, day, month, year);
this.assignment = assignment;
this.presentation = presentation;
this.exam = exam;
Overallmark();
}
public void Overallmark() // overide from super class
{
double totala = assignment*0.3; // times 30%
double present = presentation*0.2; //time 20%
double texam = exam*0.5; // times 50%
double total = totala+present+texam; // add together
overallmark = total;
}
public double getOverallmark()
{
return overallmark; // overide from super clasee
}
public void print()
{
super.print();
super.grade(overallmark);
System.
out.
println("The assignment mark is "+assignment
);
System.
out.
println("The presentation is "+presentation
);
System.
out.
println("The exam mark is "+exam
);
System.
out.
printf("The overallmark is %.2f\ngS",overallmark
);
System.
out.
println("The grade is "+grade
);
}
public int getAssignment() {
return assignment;
}
public void setAssignment(int assignment) {
this.assignment = assignment;
}
public int getPresentation() {
return presentation;
}
public void setPresentation(int presentation) {
this.presentation = presentation;
}
public int getExam() {
return exam;
}
public void setExam(int exam) {
this.exam = exam;
}
}
/**
*Author: Tan Jun Qi
* Student ID: 33872846
* Unit: ICT 167 Principles of computer Science
* Lecturer: Aaron Yeo
*
*/
public class UndergradeStudent extends Student{
int assignment1;
int assignment2;
int assignment3;
int practical;
int exam;
double overallmark;
int year,int assignment1, int assignment2, int assignment3,int practical, int exam) {
super(type, title, firstname,lastname,stuID, day, month, year);
this.assignment1 = assignment1;
this.assignment2 = assignment2;
this.assignment3 = assignment3;
this.practical = practical;
this.exam = exam;
Overallmark(); // calculate marks when object created
}
public UndergradeStudent()
{
}
public void Overallmark()
{ // overide from super class
double ass1 = assignment1*0.15; // 15%
double ass2 = assignment2*0.15;//15%
double ass3 = assignment3*0.15;//15%
double exam1 = exam*0.45;//45*
double prac = practical*0.1;//10%
double overallmark = ass1+ass2+ass3+exam1+prac; // adding them up
this.overallmark = overallmark; // assigned to instance variable
}
public double getOverallmark() {
//overide from super class
return overallmark;
}
public void print()
{
super.print();
super.grade(overallmark);
System.
out.
println("The mark for assignment 1 is "+assignment1
);
System.
out.
println("The mark for assignment 2 is "+assignment2
);
System.
out.
println("The mark for assignment 3 is "+assignment3
);
System.
out.
println("The mark for practical is "+practical
);
System.
out.
println("The mark for exam is "+exam
);
System.
out.
printf("The overallmark is %.2f\n",overallmark
);
System.
out.
println("The grade is "+grade
);
}
public int getAssignemnt1() {
return assignment1;
}
public void setAssignemnt1(int assignemnt1) {
this.assignment1 = assignemnt1;
}
public int getAssignment2() {
return assignment2;
}
public void setAssignment2(int assignment2) {
this.assignment2 = assignment2;
}
public int getAssignment3() {
return assignment3;
}
public void setAssignment3(int assignment3) {
this.assignment3 = assignment3;
}
public int getPractical() {
return practical;
}
public void setPractical(int practical) {
this.practical = practical;
}
public int getExam() {
return exam;
}
public void setExam(int exam) {
this.exam = exam;
}
}
import java.util.*;
/**
* Author: Tan Jun Qi
* Student ID: 33872846
* Unit: ICT 167 Principles of computer Science
* Lecturer: Aaron Yeo
*/
public class Student {
long stuID;
int year;
int month;
int day;
{ // Constructor used to create student object
this.title = title;
this.firstname = firstname;
this.lastname = lastname;
this.stuID = stuID;
this.year = year;
this.month=month;
this.day=day;
this.type = type;
}
public Student()
{
}
return type;
}
public void Overallmark() // it would polymorph into either Undergrad or student if its empty
{
}
public void grade(double overallmark)
{ // assignning grade t0 parameter overallmark
if(overallmark>=80)
{
grade = "HD";
}
else if(overallmark>=70)
{
grade = "D";
}
else if(overallmark>=60)
{
grade = "C";
}
else if (overallmark>=50)
{
grade = "P";
}
else if (overallmark<50)
{
grade = "N";
}
}
public boolean equals(Student s1, Student s2)
{
boolean equals = false;
if(s1.getStuID()==s2.getStuID())
{
equals = true;
}
return equals;
}
public void sort(ArrayList<Student> stu)
{
boolean swap = true;
while(swap)
{
swap = false;
for(int i =1;i<stu.size();i++)
{
Student second = stu.get(i); // assigning index 1 student into student variable second
Student first = stu.get(i-1); // assigning index 0 student into student variable first
if(second.getStuID()<first.getStuID())
{
stu.set(i,first); // swap index 1 with index 0
stu.set(i-1, second); // swap index 0 to index 1
swap = true;
}
}
}
}
public void print()
{
System.
out.
println("The Salutation is "+title
);
System.
out.
println("The first name is "+firstname
);
System.
out.
println("the last name is "+lastname
);
System.
out.
println("The student ID is "+stuID
);
System.
out.
println("The day of birth is "+day
);
System.
out.
println("The month of birth is "+month
);
System.
out.
println("The year of birth is "+year
);
}
return title;
}
public void setTitle
(String title
) {
this.title = title;
}
public String getFirstname
() {
return firstname;
}
public void setFirstname
(String firstname
) {
this.firstname = firstname;
}
return lastname;
}
public void setLastname
(String lastname
) {
this.lastname = lastname;
}
public long getStuID() {
return stuID;
}
public void setStuID(int stuID) {
this.stuID = stuID;
}
public double getOverallmark()
{
return 0; // it would polymorph to under grade or post graduate
}
return grade;
}
public void setGrade
(String grade
) {
this.grade = grade;
}
}
/*
* Author: Tan Jun Qi
* Student ID: 33872846
* Unit: ICT 167 Principles of computer Science
* Lecturer: Aaron Yeo
*/
/**
*
* @author mega-
*/
public class StudentTest {
public static void main
(String [] args
)
{
Scanner read
= new Scanner
(System.
in);
int undercount = 0;
int postcount = 0;
int addchoice = 0;
double Uavg=0;
double Pavg=0;
System.
out.
println("Type 1 for UnderGrad and 2 for PostGrad");
int choice = read.nextInt(); // reading if it 1 or 2
read.nextLine();
while(choice!=1 && choice!=2) // if choice not 1 and not 2
{
System.
out.
println("Invalid choice, please re-enter your choice again");
choice = read.nextInt(); // prompt again
}
ArrayList<Student> Ustu = new ArrayList<Student>(); // create arraylist of student object
// Reading the file to the arraylist
try{
String fileName
= "student.txt";
File file
= new File(fileName
); // create a new file
Scanner inputstream = new Scanner(file);// ready to read from the new created file
while(inputstream.hasNextLine())//whille there is next line of records
{
String line
= inputstream.
nextLine(); // read the line of record
String [] linearray
= line.
split(","); // Store the individual data from file seperated by commas.
String status
= linearray
[0]; // Determining if the student is recorded as post or under.
String firstname
= linearray
[2];
String lastname
= linearray
[3];
long Stuid
= Long.
parseLong(linearray
[4]);
int day
= Integer.
parseInt(linearray
[5]);
int month
= Integer.
parseInt(linearray
[6]);
int year
= Integer.
parseInt(linearray
[7]);
if(status.equals("Under"))
{
int ass1
= Integer.
parseInt(linearray
[8]);
int ass2
= Integer.
parseInt(linearray
[9]);
int ass3
= Integer.
parseInt(linearray
[10]);
int prac
= Integer.
parseInt(linearray
[11]);
int exam
= Integer.
parseInt(linearray
[12]);
Ustu.add (new UndergradeStudent(status,title,firstname,lastname,Stuid,day,month,
year,ass1,ass2,ass3,prac,exam)); // creating object undergradestudent with the constructor and store in arraylist
undercount++;
}
else if(status.equals("Graduate"))
{
int assignment
= Integer.
parseInt(linearray
[8]);
int presentation
= Integer.
parseInt(linearray
[9]);
int exam
= Integer.
parseInt(linearray
[10]);
Ustu.add(new Postgraduate(status,title,firstname,lastname,Stuid,day,month
,year,assignment,presentation,exam)); // creating object postgraduate and store in arraylist
postcount++;
}
}
}
{
System.
out.
println("File not found");
}
while(addchoice!=1)
{
Menu(); // show the menu from the method
addchoice=read.nextInt(); // prompt for the method choice
read.nextLine(); // cleaning the scanner
switch(addchoice)
{
case 1:
{
System.
out.
println("-----------Exit system--------/");
break;
}
case 2:
{
Addstudent(addchoice,choice,Ustu); // call from method to add student manually or from file
break;
}
case 3:
{
for(Student s: Ustu)
{ // loop all student in arraylist
s.print(); // print all object in arraylist
}
break;
}
case 4:
{
double sum=0;
double sum1=0;
double avg=0;
double avg1=0;
int ucount=0;
int gcount=0;
for(int i = 0;i<Ustu.size();i++)
{
if(Ustu.get(i) instanceof UndergradeStudent)
{ // if arraylist index is an object of undergrad student
sum += Ustu.get(i).getOverallmark(); // get the overallmark of the student
ucount++; // count how many undergrad student
}
else if(Ustu.get(i) instanceof Postgraduate)
{
sum1+=Ustu.get(i).getOverallmark();
gcount++;
}
}
// total overallmark of post/undergrad divide by total number of undergrade student or poststudent
avg = sum/ucount;
avg1 = sum1/gcount;
System.
out.
println("The overallmark for undergrad student is "
+avg+" and post graduate is "+avg1);
Uavg = avg;
Pavg = avg1;
break;
}
case 5:
{
int Ucount=0;
int Pcount=0;
double track=0;
for (int i = 0;i<Ustu.size();i++)
{
if(Ustu.get(i) instanceof UndergradeStudent)
{// instance of = Ustu.get(i).gettype.equals("Under);
track = Ustu.get(i).getOverallmark();
// getting overallmark of student
if(track>=Uavg)
{ // if less than total overall marks
Ucount++; // count student
}
}
else if(Ustu.get(i) instanceof Postgraduate)
{
track = Ustu.get(i).getOverallmark();
if(track>=Pavg)
{
Pcount++;
}
}
}
System.
out.
println("The number of undergraduate student that has more than the overallmark is "
+Ucount+ "and Postgraduate is "+Pcount);
break;
}
case 6:
{
boolean found = false;
System.
out.
println("Please enter the student ID to search");
long search = read.nextLong();
for(int i = 0;i<Ustu.size();i++)
{
if(search==Ustu.get(i).stuID)
{ // search for student ID
Ustu.get(i).print(); // print if search id found student id
found=true;
}
}
if(found == false)
{
System.
out.
println("No student found with the Student ID");
}
break;
}
case 7:
{
boolean found = false;
System.
out.
println("Please enter the first name ");
String fname
= read.
nextLine();
System.
out.
println("please enter the last name");
String lname
= read.
nextLine();
for(int i = 0;i<Ustu.size();i++)
{
if(fname.equalsIgnoreCase(Ustu.get(i).firstname) && lname.equalsIgnoreCase(Ustu.get(i).lastname))
{ // if user input firstname and last name match with arraylist index
Ustu.get(i).print(); // print arraylist index
found=true;
}
}
if(found == false)
{
System.
out.
println("Student data not found");
}
break;
}
case 8:
{
Student s = Ustu.get(0); // assigning arraylist index 0 to student type s
s.sort(Ustu); // sorting the array list
for(Student c:Ustu)
{
c.print(); // print out the sorted array list
}
break;
}
case 9:
{
String file
= "Sorted.CSV"; // create csv file name
try{
OutputStream.
println("StudentID"+","+"First name"+","+"Last name"+","+"day of birth"+","
+ "Month of birth"+","+"Year of birth"+","+"Grade"+","+"Overallmark"); // set the title for the csv record
for(Student s:Ustu)
{
OutputStream.
println(s.
stuID+","+s.
firstname+","+s.
lastname+","+s.
day+","+s.
month+","+s.
year+","
+s.grade+","+s.getOverallmark()); // print out all of the details of the array list
}
System.
out.
println("data recorded to sorted.CSV file"); // print if all successfully converted
}
{
System.
out.
println("File not found ");
}
}
}
}
}
public static void Menu()
{
System.
out.
println("Type in the numbers to commence the action");
System.
out.
println("1. Quit");
System.
out.
println("3. Output");
System.
out.
println("4. Compute");
System.
out.
println("5. Determine and display");
System.
out.
println("6. Student ID");
System.
out.
println("7. Student name");
System.
out.
println("8. Sort ");
System.
out.
println("9. Print");
}
public static void Addstudent( int choice, int type, ArrayList<Student> stu)
{
Scanner read
= new Scanner
(System.
in);
System.
out.
println("Do you want to read from another file (yes or no)");
String addchoice
= read.
nextLine();
if(addchoice.equalsIgnoreCase("yes"))
{
System.
out.
println("please enter the file name to enter");
String filename
= read.
nextLine();
try{
Scanner inputstream = new Scanner(file1);
while(inputstream.hasNextLine())
{
String line
= inputstream.
nextLine(); // read the line of record
String [] linearray
= line.
split(","); // Store the individual data from file seperated by commas.
String status
= linearray
[0]; // Determining if the student is recorded as post or under.
String firstname
= linearray
[2];
String lastname
= linearray
[3];
long Stuid
= Long.
parseLong(linearray
[4]);
int day
= Integer.
parseInt(linearray
[5]);
int month
= Integer.
parseInt(linearray
[6]);
int year
= Integer.
parseInt(linearray
[7]);
if(status.equals("Under"))
{
int ass1
= Integer.
parseInt(linearray
[8]);
int ass2
= Integer.
parseInt(linearray
[9]);
int ass3
= Integer.
parseInt(linearray
[10]);
int prac
= Integer.
parseInt(linearray
[11]);
int exam
= Integer.
parseInt(linearray
[12]);
stu.add (new UndergradeStudent(status,title,firstname,lastname,Stuid,day,month,
year,ass1,ass2,ass3,prac,exam));
}
else if(status.equals("Graduate"))
{
int assignment
= Integer.
parseInt(linearray
[8]);
int presentation
= Integer.
parseInt(linearray
[9]);
int exam
= Integer.
parseInt(linearray
[10]);
stu.add(new Postgraduate(status,title,firstname,lastname,Stuid,day,month
,year,assignment,presentation,exam));
}
}
System.
out.
println("Record added successfully");
}
{
System.
out.
println("file not found");
}
}
System.
out.
println("Do you want to manually add student? (yes or no)");
String mchoice
= read.
nextLine();
if(mchoice.equalsIgnoreCase("yes")&&choice==2 && type ==1)
{
System.
out.
println("Enter how many undergrad students do you want to add?");
int num = read.nextInt();
int count = 1;
read.nextLine();
while(count<=num)
{
System.
out.
println("Please enter the Salutation for the new undergrad student");
String title
= read.
nextLine();
System.
out.
println("Please enter the firstname of the undergrad student");
String first
= read.
nextLine();
System.
out.
println("Please enter the lastname of the undergrad student");
String last
= read.
nextLine();
System.
out.
println("Please enter the student ID");
long ID = read.nextLong();
read.nextLine();
System.
out.
println("Please enter the day of birth");
int day = read.nextInt();
read.nextLine();
System.
out.
println("Please enter the month of birth");
int month = read.nextInt();
read.nextLine();
System.
out.
println("Please enter the year of birth");
int year = read.nextInt();
read.nextLine();
System.
out.
println("Please enter the mark for assignment1");
int ass1 = read.nextInt();
read.nextLine();
System.
out.
println("please enter the mark for assignment2");
int ass2 = read.nextInt();
read.nextLine();
System.
out.
println("Please enter the mark for assignment2");
int ass3 = read.nextInt();
read.nextLine();
System.
out.
println("please enter the mark for practical");
int prac = read.nextInt();
read.nextLine();
System.
out.
println("Please enter the mark for exam");
int exam = read.nextInt();
read.nextLine();
stu.add(new UndergradeStudent(status,title,first,last,ID,day,month,year,ass1,ass2,ass3,prac,exam));
count++;
}
}
else if (mchoice.equalsIgnoreCase("yes")&&choice==2 && type ==2)
{
System.
out.
println("How many post graduate students do you want to enter?");
int num2 = read.nextInt();
int count2 = 1;
while(count2<=num2)
{
System.
out.
println("Please enter the Salutation for the new postgraduate student");
String title
= read.
nextLine();
System.
out.
println("Please enter the firstname of the postgraduate student");
String first
= read.
nextLine();
System.
out.
println("Please enter the lastname of the postgraduate student");
String last
= read.
nextLine();
System.
out.
println("Please enter the student ID");
long ID = read.nextLong();
read.nextLine();
System.
out.
println("Please enter the day of birth");
int day = read.nextInt();
read.nextLine();
System.
out.
println("Please enter the month of birth");
int month = read.nextInt();
read.nextLine();
System.
out.
println("Please enter the year of birth");
int year = read.nextInt();
read.nextLine();
System.
out.
println("Please enter the mark for group assignment");
int groupass = read.nextInt();
read.nextLine();
System.
out.
println("please enter the mark for group presentation");
int grouppre = read.nextInt();
read.nextLine();
System.
out.
println("Please enter the mark for exam");
int exam = read.nextInt();
stu.add(new Postgraduate(status,title,first,last,ID,day,month,year,groupass,grouppre,exam));
count2++;
}
}
}
}