I need help with a Java Program: Create a Java class and includes the main metho
ID: 3910672 • Letter: I
Question
I need help with a Java Program:
Create a Java class and includes the main method.
a. Include another class called Student in the same Java file. This class has 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.
b. Within the main method of the class StudentMain,
Create three objects of class Student. Say st1, st2 and st3. Initialize the objects using defined constructor by using following values:
For each order st1, st2 and st3 display the details using displayStudent method. ?
Display the class average at the end using the static method findClassAvg.
.
Instance variables name: String marks: double Static variables num Students int (nitalized to 0) class Sum doublelnitialized to 0.0) Constructor Get values for name and marks s input persmeters and set the name and marks instance variables to them Increase the numStudents by 1 Add the marks instance variable value to class Sum. Get and set methods for 2 instance varisbles. nstance methods display Student Display the values of two instance variables in following format Static methods Get and set methods for 2 static variables. findClassAvg Calculate the class aversge using classSum and numStudents static variables and displayExplanation / Answer
public class Main{
public static void main(String[] args) {
/* Create three objects of class Student.
* Initialize the objects using defined constructor */
Student st1=new Student("Ann", 75);
Student st2=new Student("Bob", 85);
Student st3=new Student("Emma", 95);
//display student details using displayStudent function
st1.displayStudent();
st2.displayStudent();
st3.displayStudent();
//Display the class average using the static method findClassAvg
Student.findClassAvg();
}
}
//Student class
class Student{
//declare instance variables name & marks with private access modifiers
private String name;
private double marks;
//declare static variables numStudents & classSum and initialize them to zero
private static int numStudents=0;
private static double classSum=0.0;
//Student constructor that accepts name & marks parameters
public Student(String name, double marks) {
this.name=name; //set name variable
this.marks=marks; //set marks variable
numStudents+=1; //increase numStudents by 1 when new student added
classSum+=marks; //add marks to classSum
}
//displayStudent method to display student details
public void displayStudent() {
System.out.println("STUDENT NAME : "+this.name);
System.out.println("MARKS : "+this.marks);
}
//findClassAvg method to display Class Average
public static void findClassAvg() {
System.out.println("Class Average : "+classSum/numStudents);
}
//below are getters & setters for instance and static variables
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMarks() {
return marks;
}
public void setMarks(double marks) {
this.marks = marks;
}
public static int getNumStudents() {
return numStudents;
}
public static void setNumStudents(int numStudents) {
Student.numStudents = numStudents;
}
public static double getClassSum() {
return classSum;
}
public static void setClassSum(double classSum) {
Student.classSum = classSum;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.