Hopefully this is easy. Right now everything is setup just how it should be. The
ID: 3684717 • Letter: H
Question
Hopefully this is easy. Right now everything is setup just how it should be. The only issue is the output format looks horrible. Can someone help me align the output for scores please. It should look like this:
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
JAVA
public class Score<String, T>
{
private String name;
private T score = null;
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;
}
public Score(String name, T score)
{
this.name = name;
this.score = score;
}
public java.lang.String toString()
{
// System.out.printf("Name: %d Score: %d", name, score);
return ("Name: " + name + "Score: " + score);
}
}
----------------------------------------------------------------------------------------------
import java.util.ArrayList;
public class P9
{
public static void main(String[] args)
{
double sum = 0;
int count = 0;
int add_count = 0;
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);
//for(int i=0; i<list.size(); i++)
for(Score s : list)
{
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));
}
}
Explanation / Answer
I have integrate both the classes
P9.java
import java.util.ArrayList;
class Score<String, T>
{
String name;
private T score = null;
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;
}
public Score(String name, T score)
{
this.name = name;
this.score = score;
}
public java.lang.String toString()
{
return ("Name: "+ name+" Score: " + score);
}
}
public class P9
{
public static void main(String[] args)
{
double sum = 0;
int count = 0;
int add_count = 0;
ArrayList<Score> list = new ArrayList<Score>(30);
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);
//for(int i=0; i<list.size(); i++)
for(Score s : list)
{
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
run:
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
BUILD SUCCESSFUL (total time: 0 seconds)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.