Hopefully this is easy. Right now everything is setup just how it should be. The
ID: 3684721 • 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
Right now the Score is butted up right against their name, but it should be in a nice line like name. Sort of like a table layout, 2 even columns.
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
Answer -
import java.util.ArrayList;
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(Score s : list)
{
s.toStr();
if(s.getScore() instanceof Double)
{
sum = sum + Double.parseDouble(s.getScore().toString());
count++;
}
}
System.out.println(" Average of numerical scores: " + (sum / count));
}
}
static 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 void toStr()
{
System.out.printf("%-8s%-12s%-8s%-12s ","Name: ",name,"Score: ",score);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.