Java Programming I am trying to make a program that will be for a student to kee
ID: 3708248 • Letter: J
Question
Java Programming
I am trying to make a program that will be for a student to keep track of each course and how much time they spend on each activity (i.e., exams, assignments, projects, classroom), so by the time the course is finished they can see how much time in hours was spent on each activity and an overall total of the hours devoted to each class. After I get this accomplished I will them be putting this in a GUI for now I just need a program that is basic. I have two of the classes created but I don’t have a class for the actual time tracking. I am posting what code I have so far.
public class Student {
//Instance Fields
private String firstName;
private String lastName;
private String address;
private String city;
private String state;
private String zip;
private String email;
private String phone;
private String degree;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getDegree() {
return degree;
}
public void setDegree(String degree) {
this.degree = degree;
}
}
public class Course {
private String courseTitle;
private int creditHours;
public String getCourseTitle() {
return courseTitle;
}
public void setCourseTitle(String courseTitle) {
this.courseTitle = courseTitle;
}
public int getCreditHours() {
return creditHours;
}
public void setCreditHours(int creditHours) {
this.creditHours = creditHours;
}
}
Explanation / Answer
Please find the code below with detailed inline comments.
CODE
=======================
Student.java
*************************
import java.util.ArrayList;
class Student {
//Instance Fields
private String firstName;
private String lastName;
private String address;
private String city;
private String state;
private String zip;
private String email;
private String phone;
private String degree;
private ArrayList<Course> courses;
public Student(String firstName, String lastName, String address, String city, String state, String zip,
String email, String phone, String degree, ArrayList<Course> courses) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.city = city;
this.state = state;
this.zip = zip;
this.email = email;
this.phone = phone;
this.degree = degree;
this.courses = courses;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return the address
*/
public String getAddress() {
return address;
}
/**
* @param address the address to set
*/
public void setAddress(String address) {
this.address = address;
}
/**
* @return the city
*/
public String getCity() {
return city;
}
/**
* @param city the city to set
*/
public void setCity(String city) {
this.city = city;
}
/**
* @return the state
*/
public String getState() {
return state;
}
/**
* @param state the state to set
*/
public void setState(String state) {
this.state = state;
}
/**
* @return the zip
*/
public String getZip() {
return zip;
}
/**
* @param zip the zip to set
*/
public void setZip(String zip) {
this.zip = zip;
}
/**
* @return the email
*/
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
/**
* @return the phone
*/
public String getPhone() {
return phone;
}
/**
* @param phone the phone to set
*/
public void setPhone(String phone) {
this.phone = phone;
}
/**
* @return the degree
*/
public String getDegree() {
return degree;
}
/**
* @param degree the degree to set
*/
public void setDegree(String degree) {
this.degree = degree;
}
/**
* @return the courses
*/
public ArrayList<Course> getCourses() {
return courses;
}
/**
* @param courses the courses to set
*/
public void setCourses(ArrayList<Course> courses) {
this.courses = courses;
}
public int getTotalTimeSpent() {
int time = 0;
for(Course c : courses) {
time += c.getCreditHours();
}
return time;
}
}
Course.java
*************************
class Course {
private String courseTitle;
private int creditHours;
public Course(String courseTitle, int creditHours) {
super();
this.courseTitle = courseTitle;
this.creditHours = creditHours;
}
/**
* @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;
}
}
tester.java
*************************
import java.util.ArrayList;
import java.util.Arrays;
public class tester {
public static void main(String args[]) {
Course c1 = new Course("MAT101", 50);
Course c2 = new Course("PHY101", 48);
Course c3 = new Course("CSE101", 55);
ArrayList<Course> courses = new ArrayList<>(Arrays.asList(c1, c2, c3));
Student s = new Student("John", "Doe", "101 Street", "City", "State", "Zip", "Email", "phone", "MS", courses);
System.out.println("Total hours spent on all the courses: " + s.getTotalTimeSpent());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.