Question: I have shared with a starter code In the last labs we implemented a class. Here we will explore this class in a bit more

I have shared with a starter code  I have shared with a starter code In the last labs
we implemented a class. Here we will explore this class in a
bit more detail. Previously, all our methods and attributes were simply made
public, but in general this does not provide much assurance that other
users will misuse our application. Change of Hands Your student class has

In the last labs we implemented a class. Here we will explore this class in a bit more detail. Previously, all our methods and attributes were simply made public, but in general this does not provide much assurance that other users will misuse our application. Change of Hands Your student class has a which allows the lecturer to change the grade of the student. In the following year the lecturer has left the university and the codebase has been passed to an administrator who is a much worse coder, but is still keen to use software to save time. Instead of using the method, the new administrator has decided to set the attribute directly for each student. olice-arade =72 bob. arade - 9800; charlie.grode = 20009 ; They also have an old keyboard where the 0 key often gets stuck. This is causing some angry emails. When designing software we cannot trust the end user. If software can be accidentally misused, it will be misused eventually, it is our responsibility as engineers to prevent this from happening. Luckily we cantorce people to use our software correctly with access modifiers. Privacy is Important There are four access modifiers in Java: They also have an old keyboard where the 0 key often gets stuck. This is causing some angry emails. When designing software we cannot trust the end user. If software can be accidentally misused, it will be misused eventually. It is our responsibility as engineers to prevent this from happening. Luckily we can force people to use our software correctly with access modifiers. Privacy is important There are four access modifiers in Java: - no modifier For now let us look at and variables, as you have seen now, are publicly accessible to change in any part of the code, which enables other people to make a an exception (which is a good thing). Are variables useless thn? No, because these can be changed within the class body. We can assume that the end user won't be able to modify our class, and therefore we can build error correction into our program from the start. This gives us full control over how these variables can be interacted with. Your turn: Encapsulation to the rescue! You've been called in to streamline the system. Going forward we want there to be two attributes related to coursework in our student class: These should not be changeable directly. Having learned from the past mistakes, once we have created a student, there should only be a single method we can call to change the grade of the student. This method will take a grade as an argument and update the appropriate entry in the attribute and mark the assignment as submitted in the attribute. To make sure that only valid grade values are inputted, the method should check if the grade is between 0 and 100 and complain otherwise. This should be done by printing an error as below to the terminal and by not further setting attribute values. public void updateGrade(int newbrade) f Sy ensure a val id grade has been entered. Otherwise... System.out.printinc" Enter a grade from 0100.77 : return; 1/ othenwise set grade ond has submitted. Whilst it can be useful to keep attributes which are only used by internal methods, here the students would like to see their grades eventually. For this purpose we will implemented a "getter" method This should method the value. Do not just print these to the console. This method will take a grade as an argument and update the appropriate entry in the grode attribute and mark the assignment as submitted in the attribute. To make sure that only valid grade values are inputted, the method should check if the grade is between 0 and 100 and complain otherwise. This should be done by printing an error as below to the terminal and by not further setting attribute values, public void updateGrade(int neaGrade) f II ensure a valid grade has been entered, Othenwise,.. System. out.println(" Enter a grade from 0100.7 ); return; 1/ otherwise set grade and hassubwitted. Whilst it can be useful to keep attributes which are only used by internal methods, here the students would like to see their grades eventually. For this purpose we will implemented a "getter" method This should method the value. Do not just print these to the console. Once done, we have a nicely controlled way to update and read the attribute in the Student class. To complete the exercise, make all other attributes as well and add "getter" methods for each. Make sure that the return type for these match the respective attribute type. Now, users (code outside the class context) can read the attribute values indirectly by calling getters, but they cannot modify these values at all, as they were set only in the constructor method and do not have "setters" like the method. pubUe class student \{ public boolean hasSubmitted = Hilit; pubtuc votd subaitCoursework() i hasSubmitted =

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!