In: Computer Science
Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks.
Code:
DemoNantucket.java
import java.util.Scanner;
public class DemoNantucket {
static void print(String msg){
System.out.println(msg);
}
public static void main(String[] args) {
// TODO Auto-generated method
stub
Scanner input=new
Scanner(System.in);
int totalCourses;
String department;
int courseNumber;
int courseCredits;
double totalCost=0;
print("Please give total number of
course.");
totalCourses=Integer.parseInt(input.nextLine());
Course[] courseArray = new
Course[totalCourses];
for(int
i=0;i<totalCourses;i++){
print("Please give department :");
department=input.nextLine();
print("Please give course Number (100-499)
:");
courseNumber=Integer.parseInt(input.nextLine());
print("Please give course credits (1,2 or 3)
:");
courseCredits=Integer.parseInt(input.nextLine());
if(department.equals("PHY") ||
department.equals("CSC")|| department.equals("SCI")){
LabCourse temp=new
LabCourse(department,courseNumber,courseCredits);
courseArray[i]=temp;
}else{
Course temp=new
Course(department,courseNumber,courseCredits);
courseArray[i]=temp;
}
totalCost+=courseArray[i].getCourseCost();
}
for(int
i=0;i<totalCourses;i++){
print(courseArray[i].toString());
}
print("Total Bill is
:"+totalCost);
input.close();
}
}
---------------------------------------
Course.java
public class Course {
private String department;
private int courseNumber;
private int courseCredits;
private double courseCost;
public Course() {
this.department="0";
this.courseNumber=0;
this.courseCost=0;
}
public Course(String department,int courseNumber,int
courseCredits){
setDepartment(department);
setCourseNumber(courseNumber);
setCourseCredits(courseCredits);
setCourseCost(courseCredits*120);
}
public double getCourseCost() {
return courseCost;
}
public void setCourseCost(double courseCost) {
this.courseCost = courseCost;
}
public int getCourseCredits() {
return courseCredits;
}
public void setCourseCredits(int courseCredits)
{
if(courseCredits == 1 ||
courseCredits == 2 ||courseCredits == 3){
this.courseCredits =
courseCredits;
}else{
throw new
IllegalArgumentException("Course Credits can be 1, 2 or 3
only.");
}
}
public int getCourseNumber() {
return courseNumber;
}
public void setCourseNumber(int courseNumber) {
if(courseNumber>=100 &&
courseNumber<=499){
this.courseNumber =
courseNumber;
}else{
throw new
IllegalArgumentException("CourseNumbers must be 100 - 499
(inclusive)");
}
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
if(department.equals("ENG") ||
department.equals("MTH") || department.equals("CSC") ||
department.equals("HST") || department.equals("HUM") ||
department.equals("SCI") || department.equals("LAN") ||
department.equals("PHY"))
this.department = department;
else
throw new
IllegalArgumentException("department must be ENG, MTH, CSC, HST,
HUM, SCI, LAN, PHY");
}
@Override
public String toString(){
String courseDetails;
courseDetails = "Department
:"+getDepartment()+"\n";
courseDetails += "Course Number
:"+getCourseNumber()+"\n";
courseDetails += "Course Cost
:"+getCourseCost()+"\n";
courseDetails += "Course credits
:"+getCourseCredits()+"\n";
return courseDetails;
}
public boolean equals(Course obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() !=
obj.getClass())
return false;
if(obj.getDepartment().equals(getDepartment())
&& obj.getCourseCredits()== getCourseCredits() &&
obj.getCourseNumber() == getCourseNumber()){
return true;
}else{
return false;
}
}
}
---------------------------------------------
LabCourse.java
public class LabCourse extends Course {
public LabCourse(){
super();
}
public LabCourse(String department,int
courseNumber,int courseCredits){
super(department,courseNumber,courseCredits);
if(department.equals("PHY") ||
department.equals("CSC")|| department.equals("SCI")){
setCourseCost(getCourseCost()+50);
}else{
throw new
IllegalArgumentException("A course is a lab course if the
department is CSC, SCI or PHY.");
}
}
@Override
public String toString(){
String courseDetails;
courseDetails ="Its a lab course
:\n";
courseDetails += "Department
:"+getDepartment()+"\n";
courseDetails += "Course Number
:"+getCourseNumber()+"\n";
courseDetails += "Course Cost
:"+getCourseCost()+"\n";
courseDetails += "Course credits
:"+getCourseCredits()+"\n";
return courseDetails;
}
public boolean equals(LabCourse obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() !=
obj.getClass())
return false;
if(obj.getDepartment().equals(getDepartment())
&& obj.getCourseCredits()== getCourseCredits() &&
obj.getCourseNumber() == getCourseNumber()){
return true;
}else{
return false;
}
}
}
------------------
Output: