Write an abstract Student class that stores a students name and all.docx
1. Write an abstract Student class that stores a student's name
and all of their grades for a course. There is no limit on the
number of graded assignments a course may have. Grades for a
course may be characters, integers, doubles, or strings (i.e. a
professor may decide to give grades in terms of A, B, C, etc., as
90, 93, 81, etc., as 81.4, 82.1, 95.4, etc., or as "acceptable",
"excellent", "poor", etc.) Whatever data type is used for the
grades in a course will be the same for all assignments in that
course. In other words, if one grade in a course is a character,
they will all be characters - it's not possible for one assignment
to be given a letter grade and another an integer grade. The
class should provide methods to add a grade, and to retrieve a
specific grade (for example, retrieving the student's score on
the third assignment). Remember that this is an abstract class -
you need to provide the class's data fields and the method
signatures, but not the implementations for the methods. Write
a program that adds one million numbers to the beginning of an
ArrayList and a Linked List. Time how long this takes in
seconds. Which one is faster? Why? (Provide a detailed
explanation of why the faster one is faster as a comment in your
code.) Write a program that adds one million numbers to an
ArrayList and a LinkedList and then remove these numbers one
at a time from the end of the list. Time how long this takes in
seconds. Which one is faster? Why? (Provide a detailed
explanation of why the faster one is faster as a comment in your
code.)
Solution
Problem 2:
2. import java.util.ArrayList;
public abstract class Student<T>{
String name;
ArrayList<T> grades;
public Student() {
grades = new ArrayList<T>();
}
abstract void addGrade(T grade);
abstract T addGrade(int assignment);
}
Problem 3:
import java.util.ArrayList;
import java.util.LinkedList;
public abstract class Student<T>{
public static void main(String args[]) {
ArrayList<Integer> al = new ArrayList<Integer>();
LinkedList<Integer> ll = new LinkedList<Integer>();
long startTime, stopTime, elapsedTime;
startTime = System.currentTimeMillis();
for(int i = 1; i <= 1000000; i++){
al.add(0, i);
}
stopTime = System.currentTimeMillis();
3. elapsedTime = stopTime - startTime;
System.out.println("In case of ArrayList, time: " +
elapsedTime);
startTime = System.currentTimeMillis();
for(int i = 1; i <= 1000000; i++){
ll.add(0, i);
}
stopTime = System.currentTimeMillis();
elapsedTime = stopTime - startTime;
System.out.println("In case of LinkedList, time: " +
elapsedTime);
// LinkedList is faster in adding 1 million elements at the
beginning, because in ArrayList, while adding any element in
the beginning, all the subsequent elements have to be moved 1
place forward
}
}
Problem 4:
import java.util.ArrayList;
import java.util.LinkedList;
public abstract class Student<T>{
4. public static void main(String args[]) {
ArrayList<Integer> al = new ArrayList<Integer>();
LinkedList<Integer> ll = new LinkedList<Integer>();
long startTime, stopTime, elapsedTime;
startTime = System.currentTimeMillis();
for(int i = 1; i <= 1000000; i++){
al.add(i);
}
stopTime = System.currentTimeMillis();
elapsedTime = stopTime - startTime;
System.out.println("In case of ArrayList, time: " +
elapsedTime);
startTime = System.currentTimeMillis();
for(int i = 1; i <= 1000000; i++){
ll.add(i);
}
stopTime = System.currentTimeMillis();
elapsedTime = stopTime - startTime;
System.out.println("In case of LinkedList, time: " +
elapsedTime);
// ArrayList is faster in adding 1 million elements at the
end, because in LinkedList, while adding any element in the
5. end, we have to traverse all the way to the end of the list
}
}