import java.util.ArrayList;
public class AverageGrade {
private ArrayList<Double> grades = new ArrayList<Double>();
public void addGrade(double grade) {
grades.add(grade);
}
public double getTotal() {
double sum = 0;
for (double grade : grades)
sum += grade;
return sum;
}
public double getAverage() {
return getTotal() / grades.size();
}
public static void main(String[] args) {
AverageGrade ag = new AverageGrade();
ag.addGrade(90); // Korean
ag.addGrade(85); // English
ag.addGrade(78); // Mathematics
ag.addGrade(65); // Biology
ag.addGrade(83); // Chemistry
System.out.println("Total: " + ag.getTotal());
System.out.println("Average: " + ag.getAverage());
}
}
Hopefully that gets you on the right path!
We don't give away solution in this forum and you went and did the poster's entire homework. Not only that, but it is clearly that rhoxart faye don't even know how to declare variables and you wrote a code with ArrayList, object instantiation and confusing for loops. You think that offered any significant help? What you think will happen when rhoxart faye tries to deliver your code to the teacher.
A simple solution would be:
class Example1
{
public static void main(String[] args)
{
// declare variables
double total = 0;
double average = 0;
..... // declare the rest of the grades
// add the grades
average= total/5;
System.out.println("total grade :" + total);
System.out.println("average :" + average);
}
Hi, this is not against the rules of these forums. See Member Rules for more information.
I wonder which is easier....reading API (as you suggest) or reading examples of code? Hrmm.....my guess is the later.
You gave a code that the poster cannot understand. From the mistakes that were made from the original code, you think that the poster can understand how to create object, instantiate them and also use ArrayLists ?
And I never said that the poster should read the API. I said that the poster should study how to declare variables, because that was clearly the problem. Don't know how to declare variables: double total grade = int Korean(90) + English(85) + Mathematics(78) + Biology(65) + Chemistry(83); When someone is having difficulty with the above code I am sure that your code makes perfect sense and if the poster submits that to his teacher, the teacher will have no idea that he copied it from someone else.
You gave a code that the poster cannot understand. From the mistakes that were made from the original code, you think that the poster can understand how to create object, instantiate them and also use ArrayLists ?
And I never said that the poster should read the API. I said that the poster should study how to declare variables, because that was clearly the problem. Don't know how to declare variables: double total grade = int Korean(90) + English(85) + Mathematics(78) + Biology(65) + Chemistry(83); When someone is having difficulty with the above code I am sure that your code makes perfect sense and if the poster submits that to his teacher, the teacher will have no idea that he copied it from someone else.
And whose code is more similar to the poster's ?
So this post where I expressed my opinion with valid arguments has been responded with a negative vote when someone has run out of arguments. The idea of negative votes according to the rules is to mark bad posts, not when you don't like someone's opinion.