public class Grade
{
private int Math; //instance variable
private int English; //instance variable
private String name; //instance variable
//Constructor
public Grade(String aName, int aMath, int aEnglish)
{
name = aName;
Math = aMath;
English = aEnglish;
}
//Constructor overload
public Grade(String aName)
{
name = aName;
Math = 0;
English = 0;
}
// return the average score of Math and English
public double getAverage()
{
return (Math + English) / 2.0;
}
// get bonus score for Math
public void MathBonus(int M)
{
Math += M;
}
//get bonus score for English
public void EnglishBonus(int E)
{
English += E;
}
//print the information
public void printData()
{
System.out.printf("Name: %s\n", name);
System.out.printf("Math: %d\n", Math);
System.out.printf("English: %d\n", English);
}
}
|
// How to declare an object. // How to call a math. public class Test
{
public static void main(String[] args)
{
Grade obj1 = new Grade("Jennifer", 75, 88);
obj1.printData();
System.out.printf("The average is %.1f\n", obj1.getAverage());
obj1.MathBonus(10);
obj1.printData();
System.out.printf("The average is %.1f\n", obj1.getAverage());
Grade obj2 = new Grade("Robert");
obj2.printData();
System.out.printf("The average is %.1f\n", obj2.getAverage());
} } |
|
You save both files in a folder.
|