MrGamma195 MrGamma195 - 1 month ago 16
Java Question

Averaging grades using objects in java

I'm trying to average 6 grades for two different people for school, I have all the grades for each student in different classes I was wondering

How can I import the numbers that I enter for each students object that I create?

//first class
public class GradesA {
int art;
int math;
int science;

int AddGrades(int a, int b, int c){
art = a;
math = b;
science = c;
return a+b+c;

}}
//second class
public class GradesB {
int english;
int carpentry;
int geography;

int AddGradesB(int a,int b,int c){
english = a;
carpentry = b;
geography = c;
return a+b+c;

}}

//final class
public class Classes {
public static void main(String[]args){
GradesA objGrades = new GradesA();
System.out.println(objGrades.AddGrades(100,85,95));
GradesB objGradesB = new GradesB();
System.out.println (objGradesB.AddGradesB(95,85,75));
}}

Answer

Hope I understood what you are looking for

Since

int addGrades(int a, int b, int c){

return and integer

why do not you just divide return number from this function by 3 and get your average that you are looking for.

If you want to have access to data fields art, math, and science values

you need getters and setters like follwing example for art data filed

setter function is

public void setArt(int art){
   this.art = art;
}

getter function is

public int getArt(){
   return this.art;
}

Read More About Setter and Getter