In: Computer Science
writing a well documented program with multiple classes and arrays.
Include a pledge and the IDE used in the descriptive header of the driver class
The following is the code for the above question. It has met all the requirements stated in the question.
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
Student[] students=new Student[3]; //declare array of Student objects
int i;
String fname,lname;
int s1,s2,s3;
double average;
char grade;
for(i=0;i<3;i++)
{ //input all the values for 3 students
fname=in.nextLine();
lname=in.nextLine();
s1=in.nextInt();
s2=in.nextInt();
s3=in.nextInt();
in.nextLine(); //this is used when int is captured and next line to be captured to overcome errors
students[i]=new Student(fname,lname,s1,s2,s3); //store them in Student array
}
for(i=0;i<3;i++)
{
students[i].cal_average(); //calculate average
students[i].cal_grade(); //calculate grade
students[i].tostring(); //call tostring() method (Note: toString() should return String, but in question it is asked to printf(). Hence here we cant override toString() otherwise it will raise error. Try once for your understanding Thats why tostring() is used here)
System.out.println();
}
}
}
class Student
{
private String fname,lname;
private int s1,s2,s3;
private double average;
private char grade;
Student(String fname,String lname,int s1,int s2,int s3) //constrcutor to initialize values. You can also use set methods
{
this.fname=fname;
this.lname=lname;
this.s1=s1;
this.s2=s2;
this.s3=s3;
}
//set and get methods for all instance variables
public void setfname(String fname)
{
this.fname=fname;
}
public String getfname()
{
return this.fname;
}
public void setlname(String lname)
{
this.lname=lname;
}
public String getlname()
{
return this.lname;
}
public void sets1(int s1)
{
this.s1=s1;
}
public int gets1()
{
return this.s1;
}
public void sets2(int s2)
{
this.s2=s2;
}
public int gets2()
{
return this.s2;
}
public void sets3(int s3)
{
this.s3=s3;
}
public int gets3()
{
return this.s3;
}
public double getaverage()
{
return this.average;
}
public char getgrade()
{
return this.grade;
}
public void cal_average() //calculate average
{
this.average=(double)((double)(this.s1+this.s2+this.s3)/(double)(3)); //type conversion
}
public void cal_grade() //calculate grade
{
double avg=this.average;
char grd;
if(avg>=90 && avg<=100)
{
grd='A';
}
else if(avg>=80 && avg<90)
{
grd='B';
}
else if(avg>=70 && avg<80)
{
grd='C';
}
else
{
grd='F';
}
this.grade=grd;
}
public void tostring() //tostring() print the information
{
System.out.printf("Fname:%s, lname:%s, Score 1:%d, Score 2:%d, Score 3:%d, average: %.2f, grade: %c",getfname(),getlname(),gets1(),gets2(),gets3(),getaverage(),getgrade());
}
}
I am also attaching the output and code screenshot for your reference.
Output and code screenshot:
#Please don't forget to upvote if you find the solution
helpful. Feel free to ask doubts if any, in the comments section.
Thank you.