Design a class named Course . The class contains: Private data fields, including
ID: 3926826 • Letter: D
Question
Design a class named Course. The class contains:
Private data fields, including courseNumber, courseTitle, and creditHours
Getter and setter methods for all data fields
A default constructor (assign initial values of your choice)
A constructor that creates a Course object with courseNumber courseTitle, and creditHours.
Design a class named Student. The class contains:
Private data fields, including student name, and a course array that contains all courses the student is currently taking.
Getter and setter methods for all data fields
A default constructor (assign initial values of your choice)
A constructor that creates a student with name. Initialize the course list with the size of 4 course array-assuming each student is taking 4 courses. (hint: this constructor takes a name as parameter, it signature looks like: Student(String aName))
A method named getTotalCreditHours that calculates and returns the total credit hours of a student.
A method named print that print student information in the following format:
Steve Davis 13 (hrs)
Java Programming
Multimedia
Advanced Database Management
WebPage Design
Tester-Write a tester program named CourseTaking.java to perform the following tasks:
Read a data file and construct students and courses from the data file
Print student and course list information
The file dataTest2.txt will contain a set of student information as follows:
Example Input: (the first line of the input files contains the number of students in the record; a line starting with # is a student name, followed by a set of courses (4 courses) the student is currently taking.)
2
#Steve Davis
CSC201 Java Programming 4
ITE170 Multimedia 3
ITD256 Advanced Database Management 3
ITD110 Web Page Design 3
#John Kanet
CSC201 Java Programming 4
ITP100 Software Design 3
ITN100 Intro to Telecommunications 3
ITN260 Network Security Basics 3
Program Output
Output of the program:
Steve Davis 13 (hrs)
Java Programming
Multimedia
Advanced Database Management
Web Page Design
John Kanet 13 (hrs)
Java Programming
Software Design
Intro to Telecommunications
Network Security Basics
Explanation / Answer
public class Course {
String courseNumber;
String courseTitle;
int creditHours;
/**
* @param courseNumber
* @param courseTitle
* @param creditHours
*/
public Course(String courseNumber, String courseTitle, int creditHours) {
this.courseNumber = courseNumber;
this.courseTitle = courseTitle;
this.creditHours = creditHours;
}
/**
* @return the courseNumber
*/
public String getCourseNumber() {
return courseNumber;
}
/**
* @param courseNumber
* the courseNumber to set
*/
public void setCourseNumber(String courseNumber) {
this.courseNumber = courseNumber;
}
/**
* @return the courseTitle
*/
public String getCourseTitle() {
return courseTitle;
}
/**
* @param courseTitle
* the courseTitle to set
*/
public void setCourseTitle(String courseTitle) {
this.courseTitle = courseTitle;
}
/**
* @return the creditHours
*/
public int getCreditHours() {
return creditHours;
}
/**
* @param creditHours
* the creditHours to set
*/
public void setCreditHours(int creditHours) {
this.creditHours = creditHours;
}
}
public class Student {
String name;
Course[] courses;
public Student() {
// TODO Auto-generated constructor stub
this.name = "No name";
this.courses = new Course[4];
}
/**
* @param name
* @param courses
*/
public Student(String name) {
this.name = name;
this.courses = new Course[4];
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the courses
*/
public Course[] getCourses() {
return courses;
}
/**
* @param courses
* the courses to set
*/
public void setCourses(Course[] courses) {
this.courses = courses;
}
public int getTotalCreditHours() {
int creditHours = 0;
for (int i = 0; i < courses.length; i++) {
creditHours += courses[i].getCreditHours();
}
return creditHours;
}
public void print() {
System.out.println(this.getName() + " " + this.getTotalCreditHours());
for (int i = 0; i < courses.length; i++) {
System.out.println(courses[i].getCourseTitle());
}
}
}
import java.io.File;
import java.util.Scanner;
public class CourseTaking {
public static void main(String[] args) {
Scanner scanner = null;
try {
scanner = new Scanner(new File("input.txt"));
int noOfStudents = Integer.parseInt(scanner.nextLine());
Student[] students = new Student[noOfStudents];
for (int i = 0; i < noOfStudents; i++) {
Student student = new Student();
String name = scanner.nextLine();
student.setName(name.substring(1, name.length()));
students[i] = student;
Course[] courses = new Course[4];
int counter = 0;
while (counter < 4) {
String line = scanner.nextLine();
Course course = new Course(line.substring(0,
line.indexOf(' ')), line.substring(
line.indexOf(' '), line.lastIndexOf(' ')),
Integer.parseInt(line.substring(line.length() - 1,
line.length())));
courses[counter++] = course;
}
students[i].setCourses(courses);
students[i].print();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
input.txt
2
#Steve Davis
CSC201 Java Programming 4
ITE170 Multimedia 3
ITD256 Advanced Database Management 3
ITD110 Web Page Design 3
#John Kanet
CSC201 Java Programming 4
ITP100 Software Design 3
ITN100 Intro to Telecommunications 3
ITN260 Network Security Basics 3
OUTPUT:
Steve Davis 13
Java Programming
Multimedia
Advanced Database Management
Web Page Design
John Kanet 13
Java Programming
Software Design
Intro to Telecommunications
Network Security Basics
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.