Exercises
Classes and Objects
Work on these exercises in the classes-part-one directory in java-web-dev-projects.
If you have not already cloned the java-web-dev-projects project repository you can find it here: Java Web Dev Projects Repository
- Open up the file,
Student.java, and add all of the necessary getters and setters. Think about the access level each field and method should have, and try reducing the access level of at least one setter to less than public.
- Instantiate the
Studentclass within the StudentPractice class using yourself as the student. For thenumberOfCreditsgive yourself1for this class and a GPA of4.0because you are a Java superstar!
- Within the same package, create a class
Coursewith at least three fields. Before diving into IntelliJ, try using pen and paper to work through what these might be. At least one of your fields should be anArrayListorHashMap, and you should use yourStudentclass.
Within the same package, create a class
Teacherthat has four fields:firstName,lastName,subject, andyearsTeaching. Add getters and setters to the class and add the access level to each field and method in the class. When adding your getters and setters, think about what we read about in the section on Encapsulation.What access modifier restricts access the most for what we need?
If it is a field, could we restrict the access to
privateand use getters and setters?If we do set fields to
private, what access level do we have to give our getters and setters?