Question

Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

Hello. I need help writing the following Java Program. Thank you

Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course { private String course; private String description; private int credits; private int maxStudents; public Course( ) { } public Course (String course, String desc, int credits, int maxNum ) { } public String getCourse ( ) { } public void setCourse ( String c ) { } public String getDescription ( ) { } public void setDescription ( String desc ) { } public int getCredits ( ) { } public void setCredits ( int cred ) { } public int getStudents ( ) { } public void setStudents ( int students ) { } public String toString ( ) { } public boolean equals ( Object obj ) { } public void finalize ( ) { } } ALL METHODS MUST BE IMPLEMENTED!!! Write a client program(test class) to THOROUGHLY test the class Course. In the client program, create two Course objects. Assign any values to the two objects. Exercise the methods to verify their functionality. Approach to Testing: • Create TWO class objects o One object with the default constructor  Test the set methods initializing the instance variables to the desired values.  Use the get methods to verify that the ‘set’s worked properly. o One object with the overloaded constructor – setting unique values in instance variables • Test the ‘set’ methods with the default object • Test the ‘get’ methods – System.out.println ( “course = “ + obj.getCourse() ). • Test the ‘toString’ method – never EXPLICITLY call the ‘toString’ method. Two separate files: • Course.java • CourseTest.java (client program) ( or other name ) Approach to Testing: •Create an object with the default constructor o Verify and print to insure default values were assigned.  Use the ‘get’ methods coupled with output statements. o Initialize all of the instance variables using the ‘set’ methods. o Verify and print the values of the instance variables using the ‘toString’ method. •Create an object with the overloaded constructor. o Verify and print the values of the instance variables using the ‘toString’ method. •Verify each of the other methods – o Equals method – needs three objects, 2 equal, 2 unequal  Compare equals  Compare unequal’s o ‘toString’ method o Finalize method. o Others.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Following are the code for the respective class files:
Course.java

public class Course{

    private String course;

    private String description;

    private int credits;

    private int maxStudents;

    public Course() {

        this.course = "NIL";

        this.description = "NIL";

        this.credits = 0;

        this.maxStudents = 0;

    }

    public Course(String course, String description, int credits, int maxStudents) {

        this.course = course;

        this.description = description;

        this.credits = credits;

        this.maxStudents = maxStudents;

    }

    public String getCourse() {

        return this.course;

    }

    public void setCourse(String course) {

        this.course = course;

    }

    public String getDescription() {

        return this.description;

    }

    public void setDescription(String description) {

        this.description = description;

    }

    public int getCredits() {

        return this.credits;

    }

    public void setCredits(int credits) {

        this.credits = credits;

    }

    public int getMaxStudents() {

        return this.maxStudents;

    }

    public void setMaxStudents(int maxStudents) {

        this.maxStudents = maxStudents;

    }

    @Override

    public boolean equals(Object o) {

        if (o == this)

            return true;

        if (!(o instanceof Course)) {

            return false;

        }

        Course course = (Course) o;

        return this.course.equals(course.course) && this.description.equals(course.description) && credits == course.credits && maxStudents == course.maxStudents;

    }

    @Override

    public String toString() {

        return "{" +

            " course='" + getCourse() + "'" +

            ", description='" + getDescription() + "'" +

            ", credits='" + getCredits() + "'" +

            ", maxStudents='" + getMaxStudents() + "'" +

            "}";

    }

    @Override

    public void finalize(){

        this.course = null;

        this.description = null;

        this.credits = 0;

        this.maxStudents = 0;

    }

}

Image for indentation reference (Please see through this link: https://imgur.com/a/6OULKb9 because large photos on HomeworkLib become pixelated):

CourseTest.java

public class CourseTest{

    public static void main(String[] args){

        //Object using default constructor

        Course course1 = new Course();

        //Printing default values of course1

        System.out.println("Default values for course1");

        System.out.println("course= " + course1.getCourse());

        System.out.println("description= " + course1.getDescription());

        System.out.println("credits= " + course1.getCredits());

        System.out.println("maxStudents= " + course1.getMaxStudents());

        /* setting values of course 1 */

        course1.setCourse("COSC1337");

        course1.setDescription("Java Course for CS Students");

        course1.setCredits(3);

        course1.setMaxStudents(45);

        /*Verifying values*/

        System.out.println("Course1 after settig values");

        System.out.println("course= " + course1.getCourse());

        System.out.println("description= " + course1.getDescription());

        System.out.println("credits= " + course1.getCredits());

        System.out.println("maxStudents= " + course1.getMaxStudents());

        // Values using toString

        System.out.println("Values of course1 using toString");

        System.out.println(course1);

        //Object using overloaded constructor

        Course course2 = new Course("COSC1227", "Machine learning course" , 4 , 30);

        //Printing of course2 using toString

        System.out.println("Values of course2 using toString");

        System.out.println(course2);

        // creting third object for equal testing:

        Course course3 = new Course("COSC1337", "Java Course for CS Students" , 3 , 45 );

        System.out.println("course1 equals course2: " + course1.equals(course2));

        System.out.println("course1 equals course3: " + course1.equals(course3));

        System.out.println("course2 equals course3: " + course2.equals(course3));

    }

}

Image for indentation reference:

If everything is clear feel free to give a thumbs up otherwise please ask your doubt. :)

Add a comment
Know the answer?
Add Answer to:
Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help me with this code. Thank you Implement the following Java class: Vehicle Class should...

    Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...

  • I need help writing my main method**** Computer Science 111 Introduction to Algorithms and Programming: Java...

    I need help writing my main method**** Computer Science 111 Introduction to Algorithms and Programming: Java Programming Project #4 – Classes and Objects (20 Points) You will create 3 new classes for this project, two will be chosen from the list below and one will be an entirely new class you invent.Here is the list: Shirt Shoe Wine Book Song Bicycle VideoGame Plant Car FootBall Boat Computer WebSite Movie Beer Pants TVShow MotorCycle Design First Create three (3) UML diagrams...

  • You are to create a class Appointment.java that will have the following: 5 Instance variables: private...

    You are to create a class Appointment.java that will have the following: 5 Instance variables: private String month private int day private int year private int hour private int minute 1 default constructor public Appointment() 1 non-default constructor that accepts arguments for all instance variables, your constructor must call the setter methods below to set the values of the instance variables public Appointment(String monthPassed, int dayPassed, int yearPassed, int hourPassed, int minutePassed) 5 setter methods (one for each instance variable)...

  • In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program...

    In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program to create a database of dogs of your own. You will also include File I/O with this program. You will create 3 classes for this assignment. The first class: Create a Class representing the information that is associated with one item (one dog) of your database. Name this class Dog. Using Lab 3, this will include the information in regard to one dog. The...

  • In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following...

    In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following additional attributes: how many people are served every year and the average price per person. code the constructor, accessors, mutators, toString and equals method of the new subclass; also code a method returning the average taxes per year. You also need to include a client class to test your code for both the parent class and the subclass. Code for Store below(Super class aka...

  • Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters...

    Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters long encryptedPassword : String key int Must be between 1 and 10(inclusive) accountId - A unique integer that identifies each account nextIDNum – a static int that starts at 1000 and is used to generate the accountID no other instance variables needed. Default constructor – set all instance variables to a default value. Parameterized constructor Takes in clearPassword, key. Calls encrypt method to create...

  • Java Time Class Class declarations are one of the ways of defining new types in Java....

    Java Time Class Class declarations are one of the ways of defining new types in Java. we will create two classes that both represent time values for a 24 hours period. The valid operations are as follows. int getHours(): returns the number of hours int getMinutes(): returns the number of minutes int getSeconds(): returns the number of seconds String toString(): returns a String representation boolean equals(Time other): returns true if and only if other designates an object that has the...

  • JAVA /** * This class stores information about an instructor. */ public class Instructor { private...

    JAVA /** * This class stores information about an instructor. */ public class Instructor { private String lastName, // Last name firstName, // First name officeNumber; // Office number /** * This constructor accepts arguments for the * last name, first name, and office number. */ public Instructor(String lname, String fname, String office) { lastName = lname; firstName = fname; officeNumber = office; } /** * The set method sets each field. */ public void set(String lname, String fname, String...

  • Here is a sample run of the tester program: Make sure your program follows the Java...

    Here is a sample run of the tester program: Make sure your program follows the Java Coding Guidelines. Consider the following class: /** * A store superclass with a Name and Sales Tax Rate */ public class Store {      public final double SALES_TAX_RATE = 0.06;      private String name;           /**      * Constructor to create a new store      * @param newName      */      public Store(String newName)      {           name = newName;      }     ...

  • Rewrite the Course class in Listing 10.6 to implement the comparable and the cloneable interfaces. The...

    Rewrite the Course class in Listing 10.6 to implement the comparable and the cloneable interfaces. The clone method must allow a deep copy on the students field. In addition, add the equals(Object o) and the toString() methods. Write a test program to invoke the compareTo, clone, equals, and toString methods in a meaningful way. Below is the Listing from the book that needs to be rewritten public class Course {    private String courseName;    private String[] students = new...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT