package geometry; public class Rectangle { private double length; private double width; public Rectangle(double length, double width){ this.length = length; this.width = width; } public double calculatePerimeter(){ return 2 * (length + width); } public double calculateArea(){ return length * width; } }
package university; public class Student { private String name; private int id; private ListIn this example, the Student class is in the university package. The class has three private attributes, name, id, and list of coursesEnrolled, and a public method, enrollInCourse, that adds course objects to the coursesEnrolled list. The class also contains a private attribute, transcript, of the Transcript class and a public method, viewTranscript, that calls the printTranscript method of the Transcript class. In both examples, the class serves as a blueprint that encapsulates data and functions that operate on that data for creating objects of that class.coursesEnrolled = new ArrayList<>(); private Transcript transcript = new Transcript(); public Student(String name, int id){ this.name = name; this.id = id; } public void enrollInCourse(Course course){ coursesEnrolled.add(course); } public void viewTranscript(){ transcript.printTranscript(); } }