[Go to site: main page, start]

0% found this document useful (0 votes)
8 views3 pages

Java Student Class Implementation

The document contains Java starter code for a program that manages student information, including undergraduate and graduate students. It defines classes for UndergraduateStudent and GraduateStudent that extend an abstract Student class, which includes methods for setting and getting student details. The main method initializes an array of students and prints their details, with placeholders for additional functionality for graduate students.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Java Student Class Implementation

The document contains Java starter code for a program that manages student information, including undergraduate and graduate students. It defines classes for UndergraduateStudent and GraduateStudent that extend an abstract Student class, which includes methods for setting and getting student details. The main method initializes an array of students and prints their details, with placeholders for additional functionality for graduate students.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

/*

starter code
*/

public class Lab2


{
public static void main(String[] args)
{
Student students[] = new Student[2];
int i;
students[0] = new UndergraduateStudent(111, "Lambert");
students[1] = new UndergraduateStudent(122, "Lembeck");
[Link]("\n\n\n\nUndergraduate Students:");

for(i = 0; i < [Link]; ++i)


{
[Link]("Student ID: " +
students[i].getId() + ", Name: " +
students[i].getLastName() + ", Tuition: " +
students[i].getTuition() + " per year, " +
"Student Class is: " +
students[i].getClassification());
}
// NOTE: output for first UndergraduateStudent should be:
// "Student ID: 111, Name: Lambert, Tuition: 4000 per year, Student
Class is: Undergraduate"

[Link]("\n\n\nGraduate Students:");

// REUSE the students[] array and create two GraduateStudent objects


// Initialize with ID and lastname values
// Print the two graduates with identical "for loop"
// use very similar code as above...

[Link]("\n\n\nProgrammer is: Dr. Johnson\n\n\n\n");


}
}
//====================================================================

public class UndergraduateStudent extends Student


{
public static final double UNDERGRAD_TUITION = 4000;
public static final String UND_CLASSIFY = "Undergraduate";
public UndergraduateStudent(int pID, String pName)
{
// initialze super class, [Link] for 'pID' and 'Student' values
// initialze the [Link]'s 'tuition' and 'classification' values
}
public void setTuition()
{
tuition = UNDERGRAD_TUITION;
}
public void setClassification()
{
classification = UND_CLASSIFY;
}
}
//====================================================================

public class GraduateStudent extends Student


{
public static final double GRAD_TUITION = 6000;
public static final String GRAD_CLASSIFY = "Graduate";
public GraduateStudent(int pID, String pName)
{
// initialze super class, [Link] for 'pID' and 'Student' values
// initialze the [Link]'s 'tuition' and 'classification' values
}
public void setTuition()
{
tuition = GRAD_TUITION;
}
public void setClassification()
{
classification = GRAD_CLASSIFY;
}
}
//====================================================================

/*
Starter code
*/
public abstract class Student
{
private int ID;
private String lastName;
protected double tuition;
protected String classification;

public Student(int pID, String pName)


{
// set the 'ID' and 'lastname', DO NOT assign values
}
public void setId(int idNum)
{
// write your code here
}
public void setLastName(String pName)
{
// write your code here
}
public int getId()
{
// write your code here
}
public String getLastName()
{
// write your code here
}
public double getTuition()
{
// write your code here
}
public String getClassification()
{
// write your code here
}
// no concrete code for abstract methods
public abstract void setTuition();
public abstract void setClassification();
}

Common questions

Powered by AI

The tuition setting differs between undergraduate and graduate students through the implementation in their respective classes. The UndergraduateStudent class uses a constant UNDERGRAD_TUITION set to 4000, while the GraduateStudent class uses GRAD_TUITION set to 6000. Each class provides an implementation of the setTuition() method that assigns the respective tuition constants to the tuition field, illustrating class-specific behavior .

To reuse the "students" array for GraduateStudent objects, the existing UndergraduateStudent objects must be replaced with new GraduateStudent instances. The same loop can be used to set and print their details due to polymorphism. This design pattern is advantageous as it reduces redundancy by leveraging existing structures for new purposes, making the code more adaptable and modular. Reusing arrays in this manner enhances resource efficiency and maintains a cleaner codebase .

Polymorphism in this code is demonstrated through the use of method overriding. Both GraduateStudent and UndergraduateStudent override the abstract methods setTuition() and setClassification() from the Student class to provide specific implementations. This allows the same method calls to behave differently based on the object's runtime type, demonstrating polymorphism by permitting objects to be treated as instances of their parent class .

To improve readability and maintainability, the Lab2 code could be refactored by separating logic into methods with clear, descriptive names, adhering to the single responsibility principle. For instance, introducing methods like createUndergraduateStudents(), printStudentDetails(), and reuseForGraduateStudents() would encapsulate specific tasks, improving understanding and maintainability. Consistent coding conventions and comments where necessary would enhance clarity and guiding structure .

Using fixed arrays for storing Student objects, like in Lab2, offers simplicity and lower overhead in memory allocation, which is beneficial for a predictable number of elements. However, it lacks flexibility in dynamically managing collection size, leading to potential waste of memory or IndexOutOfBounds exceptions if not properly managed. In contrast, a dynamic data structure like a List allows flexible size, ease of insertion or deletion, which better supports growing or unpredictable data requirements .

An abstract class in Java serves as a blueprint for other classes. It cannot be instantiated and is used to declare common methods that subclasses must implement. The Student class illustrates this concept by declaring abstract methods setTuition() and setClassification(), which must be defined in its subclasses, UndergraduateStudent and GraduateStudent. By doing this, it ensures all student types have these functions, promoting consistency and reusability .

Using an array of Student objects to store instances of UndergraduateStudent and GraduateStudent demonstrates polymorphism, specifically subtype polymorphism. The array facilitates the storage and management of different student types under a common superclass reference, allowing iteration over varied object types with the same interface for method calls like getId() and getLastName(). This pattern is powerful for grouping objects with shared characteristics while maintaining the ability to leverage individual implementations .

Hardcoding values like tuition and classification in subclasses leads to inflexibility, as any change requires modifying the source code, complicating maintenance. It restricts adaptability to new requirements or policies that affect tuition rates or classifications. A more flexible approach would involve retrieving such values from configuration files or databases, allowing changes without code modification, thus facilitating easier and more robust updates .

The Lab2 program's design facilitates reuse and extendability through its hierarchical class structure and polymorphic principles. By using an abstract Student class, it ensures essential methods are defined across all student types. The ability to add new student subclasses without altering existing logic, as they conform to the same interface, aids extendability. This structure promotes reusability by allowing existing functionality to serve multiple subclasses .

Encapsulation in the Student class is achieved by using private fields like ID and lastName, accessible only through public methods such as getId() and getLastName(). This restricts direct access and modification from outside the class, ensuring data integrity and security. Encapsulation allows changes in the implementation to occur without affecting external code, improving maintainability and flexibility .

You might also like