Create a class called Student.
Create another class called CourseSection (20 points extra credit)
Create a client that tests the CourseSection class. Test the accessors, mutators, equals, to toString method. Ultimately, this will also be testing the Student class because instances of Student are in the Course Section class.
Submit the .java files for each of the 2 classes as well as the file containing the client.
//TestStudent.java
public class TestStudent {
public static void main(String[] args) {
//creating 2 objects
Student s1 = new Student("Uday", "Hyd", "12323",3.5);
Student s2 = new Student("Uday", "Hyd", "12323",3.5);
// printing 2 objects. toString wil be called
System.out.println(s1);
System.out.println(s2);
//checking equal method
System.out.println("Equal? "+(s1.equals(s2)));
}
}
//Student.java
class Student{
private String name;
private String address;
private String phone;
private double gpa;
/**
* Constructor taking parameters
* @param aName
* @param aAddress
* @param aPhone
* @param aGpa
*/
public Student(String aName, String aAddress, String aPhone, double aGpa) {
super();
name = aName;
address = aAddress;
phone = aPhone;
gpa = aGpa;
}
//getters and setters
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getPhone() {
return phone;
}
public double getGpa() {
return gpa;
}
public void setName(String aName) {
name = aName;
}
public void setAddress(String aAddress) {
address = aAddress;
}
public void setPhone(String aPhone) {
phone = aPhone;
}
public void setGpa(double aGpa) {
gpa = aGpa;
}
//tostring
@Override
public String toString() {
return "Student [name=" + name + ", address=" + address + ", phone=" + phone + ", gpa=" + gpa + "]";
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (Double.doubleToLongBits(gpa) != Double.doubleToLongBits(other.gpa))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (phone == null) {
if (other.phone != null)
return false;
} else if (!phone.equals(other.phone))
return false;
return true;
}
}
//CourseSection.java
class CourseSection{
private String courseName;
private int days;
private String courseMeets;
private String description;
private Student a;
private Student b;
private Student c;
//constructor to initialize the variables
public CourseSection(String aCourseName, int aDays, String aCourseMeets, String aDescription, Student aA,
Student aB, Student aC) {
super();
courseName = aCourseName;
days = aDays;
courseMeets = aCourseMeets;
description = aDescription;
a = aA;
b = aB;
c = aC;
}
//setters and getters
public String getCourseName() {
return courseName;
}
public int getDays() {
return days;
}
public String getCourseMeets() {
return courseMeets;
}
public String getDescription() {
return description;
}
public Student getA() {
return a;
}
public Student getB() {
return b;
}
public Student getC() {
return c;
}
public void setCourseName(String aCourseName) {
courseName = aCourseName;
}
public void setDays(int aDays) {
days = aDays;
}
public void setCourseMeets(String aCourseMeets) {
courseMeets = aCourseMeets;
}
public void setDescription(String aDescription) {
description = aDescription;
}
public void setA(Student aA) {
a = aA;
}
public void setB(Student aB) {
b = aB;
}
public void setC(Student aC) {
c = aC;
}
//toString
@Override
public String toString() {
return "CourseSection [courseName=" + courseName + ", days=" + days + ", courseMeets=" + courseMeets
+ ", description=" + description + ", a=" + a + ", b=" + b + ", c=" + c + "]";
}
//equls method
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CourseSection other = (CourseSection) obj;
if (a == null) {
if (other.a != null)
return false;
} else if (!a.equals(other.a))
return false;
if (b == null) {
if (other.b != null)
return false;
} else if (!b.equals(other.b))
return false;
if (c == null) {
if (other.c != null)
return false;
} else if (!c.equals(other.c))
return false;
if (courseMeets == null) {
if (other.courseMeets != null)
return false;
} else if (!courseMeets.equals(other.courseMeets))
return false;
if (courseName == null) {
if (other.courseName != null)
return false;
} else if (!courseName.equals(other.courseName))
return false;
if (days != other.days)
return false;
if (description == null) {
if (other.description != null)
return false;
} else if (!description.equals(other.description))
return false;
return true;
}
}
//TestCourseSection.java
public class TestCourseSection {
public static void main(String[] args) {
//creating 3 student objects
Student a = new Student("Uday", "Hyd", "12323",3.5);
Student b = new Student("Virat", "BLR", "12312",3.3);
Student c = new Student("Keerthi", "CSK", "12343",3.9);
//creaeting 2 CouseSection Objects
CourseSection cs1 = new CourseSection("Java", 35, "Java programs", "Core Java", a, b, c);
CourseSection cs2 = new CourseSection("Java", 35, "Java programs", "Core Java", a, b, c);
//calling toString
System.out.println(cs1);
System.out.println(cs2);
//calling equal
System.out.println("Equal? "+(cs1.equals(cs2)));
}
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
Please Like and Support me as it helps me a lot
Get Answers For Free
Most questions answered within 1 hours.