Using the Course class as defined in Coursejava construct an

Using the Course class, as defined in Course.java, construct and assign new instances to each of the corresponding variables for the following courses. Use the course data here to construct your objects.//Data to construct your objects with//Dept Num Hours COMP 401 4 COMP 410 3 ECON 410 3 Change only the lines with null on them. Do not change the println statements. The expected output is: COMP401 COMP410 ECON410 Refer to the CourseComparer class for Problem 8. Problem0789.java package comp110; public class Problem0789 {public static void main(String[] args) {//PROBLEM 7. System.out.println(\"PROBLEM 7\");//Dept Num Hours//COMP 401 4//COMP 410 3//ECON 410 3 Course comp401, comp410, econ410; comp401 = null;//T0D0: Construct a Course instance System.out.println(comp401); comp410 = null;//T0D0: Construct a Course instance System.out.println(comp410); econ410 = null;//T0D0: Construct a Course instance System.out.println(econ410);//PROBLEM 8 System.out.println(\"PROBLEM 8\"); CourseComparer c = new CourseComparer();//T0D0: Perform method calls on CourseComparer c//that would result in the following Expected Output: Course.java public class Course {private String _department; private int _number; private int _hours; public Course(int h, String d, int n) _department = d; _number = n; _hours = h;} public String getDepartment () {return _department;} public int getNum() {return _number;} public int getHours() {return _hours;} public String toString() {return _department + _number;}}

Solution

Please follow the code and comments for description :

CODE :

a) Course.java :

public class Course { // parent class

private String _department; // instance variables
private int _number;
private int _hours;

public Course(int h, String d, int n) { // constructor
this._department = d;
this._number = n;
this._hours = h;
}

public String getDepartment() { // getter methods
return _department;
}

public int getNumber() {
return _number;
}

public int getHours() {
return _hours;
}

@Override
public String toString() { // to string method to return the data
return _department + _number;
}
}


b) Problem0789.java :

public class Problem0789 { // class to run the code

public static void main(String[] args) { // driver method
System.out.println(\"PROBLEM 7\"); // message

Course comp401, comp410, econ410; // object creation for the course class

comp401 = null; // initialise the data
comp401 = new Course(4, \"COMP\", 401); // pass the data to the constructor
System.out.println(comp401);// print the data to console

comp410 = null; // initialise the data
comp410 = new Course(3, \"COMP\", 410); // pass the data to the constructor
System.out.println(comp410); // print the data to console

econ410 = null; // initialise the data
econ410 = new Course(3, \"ECON\", 410); // pass the data to the constructor
System.out.println(econ410); // print the data to console
}
}

OUTPUT :


PROBLEM 7
COMP401
COMP410
ECON410


Hope this is helpful.

 Using the Course class, as defined in Course.java, construct and assign new instances to each of the corresponding variables for the following courses. Use the
 Using the Course class, as defined in Course.java, construct and assign new instances to each of the corresponding variables for the following courses. Use the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site