help me..how to print a sum and average of grades using Java?

Recommended Answers

All 11 Replies

help me..how to print a sum and average of grades using Java?

Start a new thread with your question and your code. Don't expect anyone to do your homework.
You should use a for loop

help me..
Why it is having an error?
please help me to fix this my assignment..

class Example1 
{
	public static void main(String[] args) 
	{

	
 		double total grade = int Korean(90) + English(85) + Mathematics(78) + Biology(65) + Chemistry(83);
		
		average= total/5;
		{
			
		System.out.println("total grade :" + total);
		System.out.println("average :" + average);
	}
}
}

help me..
Why it is having an error?
please help me to fix this my assignment..

class Example1 
{
    public static void main(String[] args) 
    {


        double total grade = int Korean(90) + English(85) + Mathematics(78) + Biology(65) + Chemistry(83);

        average= total/5;
        {

        System.out.println("total grade :" + total);
        System.out.println("average :" + average);
    }
}
}

end quote.

Start a new thread. And this is not java: Korean(90)
Start a new thread do some studying before posting again.

commented: Why be nasty? You asked to see their code, they showed...you told them to go study. +0
commented: This does not deserve neg-rep +16
Member Avatar for ztini

Try something like this:

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!

commented: Don't give away complete homework solutions. We're here to help people understand, not to make them pass their class without effort -3
commented: Are you having fun doing people's homework for them? +0

Try something like this:

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);

}
Member Avatar for ztini

We don't give away solution in this forum and you went and did the poster's entire homework.

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.

nice code you wrote, but i do agree that it is a little to advanced for him since he didnt know how to declare a variable ( from above poster).

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.

And whose code is more similar to the poster's ?

commented: Equalizer +16

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.

Not a rule but recommendation - We only give homework help to those who show effort as explained there...
Besides as you can see rhoxart faye haven't got time to come back and reply

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.

commented: This does not contribute constructively to the thread. +0

@ztini you better stop abusing reputation system before you get into trouble

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.