3. Create another Java class called StudentMain within the same project Lab7A wh
ID: 3853146 • Letter: 3
Question
3. Create another Java class called StudentMain within the same project Lab7A which includes the main method. a. Include another class called Student in the same Java file. This class has following instance asscStudent in the following instance variables and instance methods. Define all the instance/static variables with private access modifier and constructors, instance/static methods with public access modifier Instance name : String marks double Static variables| numStudents int (Initialized to 0) variables classSum double (Initialized to 0.0) Constructor Get values for name and marks as input parameters and set the name and marks instance variables to them. Increase the numStudents by 1 Add the marks instance variable value to classSum. Instance methods Get and set methods for 2 instance variables. displayStudent Display the values of two instance variables in following format: STUDENT NAME:Explanation / Answer
class Student
{
private String name;
private double marks;
private static int numStudents = 0;
private static double classSum = 0.0;
public Student(String name,double marks)
{
this.name = name;
this.marks = marks;
numStudents++;
classSum = classSum + marks;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setMarks(double marks)
{
this.marks = marks;
}
public double getMarks()
{
return marks;
}
public void displayStudent()
{
System.out.println(" Student Name "+name + " Marks : "+marks);
}
public static void setNumStudents(int n)
{
numStudents = n;
}
public static int getNumStudents()
{
return numStudents;
}
public static void setClassSum(int s)
{
classSum = s;
}
public static double getClassSum()
{
return classSum;
}
public static void findClassAvg()
{
System.out.println(" Class Average : "+classSum/numStudents);
}
}
class StudentMain
{
public static void main (String[] args)
{
Student st1 = new Student("Ann",75);
Student st2 = new Student("Bob",85);
Student st3 = new Student("Emma",95);
st1.displayStudent();
st2.displayStudent();
st3.displayStudent();
Student.findClassAvg();
}
}
Output:
Student Name Ann
Marks : 75.0
Student Name Bob
Marks : 85.0
Student Name Emma
Marks : 95.0
Class Average : 85
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.