MATLAB- You are to complete the Student class definition given. Your Student cla
ID: 3814320 • Letter: M
Question
MATLAB- You are to complete the Student class definition given. Your Student class should have the following properties, in the format shown: FirstName, LastName, Year, LectureSection, LabSection, and DiscussionSection. Your Student class will also have two methods: one constructor, and one ordinary method.
The student constructor will have one input, which will be a first name as a string. The constructor will set that name to the FirstName property of the student. The ordinary method will be called Greet, and will take one Student object as input. This method will print a greeting to the student, including their name. When called in the Command Window, the output should be exactly as follows:
This is what I have so far:
classdef Student
properties
FirstName
LastName
Year
LectureSection
LabSection
DiscussionSection
end
% Fill in more properties here (do not erase any!)
end
methods
function student = Student(name)
% Finish the constructor here
if nargin==1
obj.FirstName=name;
end
% Fill in the Greet() method, using the given template below:
function Greet ()
fprintf('Hello,%s!',obj.Firstname)
end
end
end
What am I doing wrong?
Explanation / Answer
Here is your fixed code.
You were missing "end" at multiple places, method should receive obj that was not being passed to Greet, property name was wrong in Greet(), in constructor your were assigning property to obj but return was student.
Save this in file named Student.m
Student.m
classdef Student
properties
FirstName
LastName
Year
LectureSection
LabSection
DiscussionSection
end
methods
function student = Student(name)
% Finish the constructor here
if nargin==1
student.FirstName=name;
end
end
% Fill in the Greet() method, using the given template below:
function Greet(obj)
fprintf('Hello,%s! ',obj.FirstName)
end
end
end
// testing this
file demo.m
student = Student("FirstName");
student.Greet()
Output
Hello,FirstName!
Please rate positively if this answered your query.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.