JAVA Add a method to the lecturer object that changes the student’s marks. This
ID: 3875669 • Letter: J
Question
JAVA
Add a method to the lecturer object that changes the student’s marks. This method should be defined to take a student object and a number as arguments.
Write a program that creates one student and lecturer object. The program should invoke the lecturer object’s method to change the student’s marks. The program should also display the details of the student object, including the student’s new marks.
Person.java
public class Person {
private String ID;
private String name;
private String gender;
public Person(String ID, String name, String gender) {
this.ID = ID;
this.name = name;
this.gender = gender;
}
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Person [ID=" + ID + ", name=" + name + ", gender=" + gender + ", getID()=" + getID() + ", getName()="
+ getName() + ", getGender()=" + getGender() + "]";
}
}
Student.java
public class Student extends Person{
private int marks;
public Student(String ID, String name, String gender, int marks) {
super(ID, name, gender);
this.marks = marks;
}
public int getMarks() {
return marks;
}
public void setMarks(int marks) {
this.marks = marks;
}
}
Lecturer.java
public class Lecturer extends Person{
public Lecturer(String ID, String name, String gender){
super(ID, name, gender);
}
}
Explanation / Answer
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Surya
*/
class Person {
private String ID;
private String name;
private String gender;
public Person(String ID, String name, String gender) {
this.ID = ID;
this.name = name;
this.gender = gender;
}
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Person [ID=" + ID + ", name=" + name + ", gender=" + gender + ", getID()=" + getID() + ", getName()="
+ getName() + ", getGender()=" + getGender() + "]";
}
}
class Student extends Person{
private int marks;
public Student(String ID, String name, String gender, int marks) {
super(ID, name, gender);
this.marks = marks;
}
public int getMarks() {
return marks;
}
public void setMarks(int marks) {
this.marks = marks;
}
}
class Lecturer extends Person{
public Lecturer(String ID, String name, String gender){
super(ID, name, gender);
}
//a method to the lecturer object that changes the student’s marks.
//This method should be defined to take a student object and a number as arguments.
void set_student_marks(Student s, int m)
{
s.setMarks(m);//setting marks
}
}
public class tester {
// a program that creates one student and lecturer object.
//The program should invoke the lecturer object’s method to change the student’s marks.
//The program should also display the details of the student object, including the student’s new marks.
public static void main(String argv[])
{
Student s = new Student("N9182","Surya","Male",500);//creating student object
Lecturer l = new Lecturer("N9834","Satya","Male");//creatin lecturer object
l.set_student_marks(s, 400);//changing marks of student object
//displaying student details..
System.out.println(s.toString()+" getMarks = "+s.getMarks());
}
}
output:
run:
Person [ID=N9182, name=Surya, gender=Male, getID()=N9182, getName()=Surya, getGender()=Male] getMarks = 400
BUILD SUCCESSFUL (total time: 0 seconds)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.