Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Having trouble with this Java Assignment. Not exactly sure where to start. Classes included with the problem: public class Main { public static void main(String[]

Having trouble with this Java Assignment. Not exactly sure where to start. image text in transcribedimage text in transcribed

Classes included with the problem:

public class Main { public static void main(String[] args) { Student student1 = new Student("Alice"); student1.addGrade(90); student1.addGrade(100); student1.addGrade(85); int assignment2Grade = student1.getGradeOnAssignment(2); System.out.println(student1.getName() + " got a " + assignment2Grade + " on the second assignment.");

Student student2 = new Student("Bob"); student2.addGrade(82.7); student2.addGrade(64.2); student2.addGrade(91.7); student2.addGrade(70.4); double assignment1Grade = student2.getGradeOnAssignment(1); System.out.println(student2.getName() + " got a " + assignment1Grade + " on the first assignment."); }

import java.util.ArrayList; import java.util.Collections;

public class Student > { public static String name; public ArrayList grades; public Student(String name) { this.name = name; grades = new ArrayList(); } public void addGrade(E grade) { grades.add(grade); } public E getGradeOnAssignment(int i) { return grades.get(i-1); } public String getName() { return name; }

}

Thanks in advance for the assistance.

Head of the Class Purpose To review generics Directions For this lab you will need to download HeadOfTheClass.zip from Pilot. This code contains a Student class that uses generics to store an ArrayList of grades of type E, where is anything that implements the Comparable interface. There are methods to add a new grade and retrieve the student's grade on a particular assignment. The other class contains a main method that exercises the methods in the Student class for two different datatypes: int and double. Your tasks for this lab are: Add a method called getMedianGrade to the Student class that returns the middle of the student's grades when they are placed in ascending order. If the student has an even number of grades, return the higher of the two in the middle. Keep in mind that the method to get the student's grade on a particular assignment should still function correctly after the getMedianGrade method is called. Add the ability to sort students alphabetically based on name or based on their median grade. Note that only students with the same type of grades (e.g. integers or doubles) need to be sortable. In the main method or using JUnit tests, if you prefer) write additional code to test the getMedianGrade and different sort functionality. Hint1: The easiest way to get the median value from a list of values is to sort the list first and then get the item in the middle of the list. However, you don't want to sort the student's ArrayList of grades directly, because that will disturb the functionality of the getGradeOnAssignmentX method. Instead, you should copy the grades into a new ArrayList and sort the copy. Hint2: Because you need to sort the Student objects two different ways, you will need to both implement Comparable and write a Comparator class. Example This example assumes the following students received these grades: Alice: 90, 100, 85 Bob: 82.7, 64.2, 91.7, 70.4 Carol: 72, 57, 71 Aaron: 50,50,50 Note that we do not include Bob in testing the sort functionality, because his grades are of a different datatype (doubles versus integers). Alice's median grade is 90 Alice got a 100 on the second assignment. Bob's median grade is 82.7 Bob got a 82.7 on the first assignment. Ordered by name Aaron 50 Alice 90 Carol 71 Ordered by median grade Alice 90 Carol 71 Aaron 50

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Access Patterns Database Interactions In Object Oriented Applications

Authors: Clifton Nock

1st Edition

0321555627, 978-0321555625

More Books

Students also viewed these Databases questions

Question

What does Processing of an OLAP Cube accomplish?

Answered: 1 week ago