In: Computer Science
Hello,
Please write this program in java and include a lot of comments and please try to make it as simple/descriptive as possible since I am also learning how to code. The instructions the professor gave was:
Create your own class
Your own class can be anything you want
Must have 3 instance variables
1 constructor variable → set the instance variables
1 method that does something useful in relation to the class
Create a driver class that creates an object ( the object is referred to the method above)
Call the method that I created using the object
An example she gave was using like a student class because it has many variables such as grades, classification, major, etc.
Implementation In JAVA:
import java.util.ArrayList;
import java.util.Scanner;
//Driver class
public class Student_Driver {
// main method or driver method
public static void main(String[] args) {
// list consists of Students
ArrayList<Students> list= new
ArrayList<Students>();
// create instance of Scanner class for taking inputs
from users
Scanner s= new
Scanner(System.in);
System.out.println("Enter the
Details of 5 Students of class");
System.out.println();
// take details of 5 students so
looping untill 5
for(int i=0;i<5;i++) {
System.out.println("Enter the details of "+(i+1)+"th Student :
");
// take details
from user as input
System.out.print("Enter the Name of Student : ");
String
name=s.next();
System.out.print("Enter the Roll no of Student : ");
int
roll=s.nextInt();
System.out.print("Enter the Grade of Student : ");
double
grade=s.nextDouble();
// create
instance of Students class and pass arguments
// in Constructor
of Students class
Students st= new
Students(name,roll,grade);
// add instance
of Students class in List for future refrence
list.add(st);
System.out.println();
}
// showing details of Students
System.out.println("Student of
whole class with details : ");
// will print details of
Students
for(int i=0;i<list.size();i++)
{
// Store
instance/object of Student class and
Students st =
list.get(i);
// and call the
function in Students class for printing details
st.to_String();
System.out.println();
}
System.out.println();
// will call the function average_grades_of_whole_class from driver
class
System.out.println("The Average grades of whole class is
"+average_grades_of_whole_class(list));
}
// made static because directly access into main
public static double
average_grades_of_whole_class(ArrayList<Students> list)
{
// will store sum of all
grades
double sum=0;
// loop untill list.size
for(int i=0;i<list.size();i++)
{
Students st =
list.get(i);
// call getgrade
for every student and store in sum
sum+=st.getgrade();
}
// getting average
double avg=sum/list.size();
// return avg.
return avg;
}
}
// class Student
class Students{
// Instance variables declared private so
// no one from outside access them
private String name;
private int roll_no;
private double grade;
// 3 argument constructor
public Students(String name,int roll_no,double grade)
{
// initialize variables
this.name=name;
this.roll_no=roll_no;
this.grade=grade;
}
// now below are getters and setters through which we
can access those variables
// will return name
public String getname() {
return name;
}
// will set name
public void setname(String name) {
this.name= name;
}
// will return roll.no
public int getroll() {
return roll_no;
}
// will set roll no.
public void setroll(int roll_no) {
this.roll_no=roll_no;
}
// will return grade
public double getgrade() {
return grade;
}
// will set grades
public void setgrades(double grade) {
this.grade=grade;
}
// method that does something useful in relation to the
class
// this will print the details of Student
public void to_String() {
System.out.println("Name : "+name
);
System.out.println("Roll no. :
"+roll_no );
System.out.println("Grade : "+grade
);
}
}
SAMPLE OUTPUT:
If you have any doubt regarding this question please ask mein comments
// THANK YOU:-)