Simple Java Project Implement a generic class named “Score”. This class will hav
ID: 3684677 • Letter: S
Question
Simple Java Project
Implement a generic class named “Score”. This class will have two instance variables. A String for the name of the student, and a generic type for the score variable. Thus, this generic class will have one type variable. For the sake of simplicity, assume that the score can be either String or Double. Nothing else. Implement getter and setter methods for the two fields; name and score. Implement toString method to display the scores formatted.
Write a P9 class with main method. Insert 5 Score objects into an ArrayList. When inserting, use the parameterized constructor of Score class. Use the values below; ("Donatello", 3.0)); ("April", "Five")); ("Raphael", 3.5)); ("Shredder", "One")); ("Splinter", "Seven"));
Using a loop (enhanced for loop is recommended), display each student’s score. Also calculate and display the average of numerical scores. Hint; use instanceof . For instance; if( someObject instanceof Double)
Your program must work with any input correctly. Do not assume 5 inputs only. When taking screenshot, use the values given above. Your screenshots must look the same as the screenshot below
Name: Donatello Score: 3.0
Name: April Score: Five
Name: Raphael Score: 3.5
Name: Shredder Score: One
Name: Splinter Score: Seven
Average of numerical scores: 3.25
Explanation / Answer
Score.java
public class Score<String, T> {
public String getName() {
return name;
}
public void setName(String name, T score) {
this.name = name;
}
public T getScore() {
return score;
}
public void setScore(T score) {
this.score = score;
}
private String name;
//declaration of object type T
private T score = null;
public Score(String name, T score){
this.name = name;
this.score = score;
}
@Override
public java.lang.String toString(){
return "Name : "+name+" Score : "+score;
}
}
ScoreTest.java
import java.util.ArrayList;
public class ScoreTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Score> list = new ArrayList<Score>();
Score<String, Double> s1 = new Score<String, Double>("Donatello", 3.0);
Score<String, String> s2 = new Score<String, String>("April", "Five");
Score<String, Double> s3 = new Score<String, Double>("Raphael", 3.5);
Score<String, String> s4 = new Score<String, String>("Shredder", "One");
Score<String, String> s5 = new Score<String, String>("Splinter", "Seven");
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
double sum = 0;
int count = 0;
for(int i=0; i<list.size(); i++){
Score s = list.get(i);
String str = s.toString();
System.out.println(str);
if(s.getScore() instanceof Double){
sum = sum + Double.parseDouble(s.getScore().toString());
count++;
}
}
System.out.println("Average of numerical scores: "+(sum/count));
}
}
Output:
Name : Donatello Score : 3.0
Name : April Score : Five
Name : Raphael Score : 3.5
Name : Shredder Score : One
Name : Splinter Score : Seven
Average of numerical scores: 3.25
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.