Commit 2f46046d authored by Martin Deinhofer's avatar Martin Deinhofer
Browse files

Initial commit

parents
/.classpath
/.project
/.settings
/bin
package java_exercises3_1_1;
/**
* Alumnis are graduated students, which may also attend courses.
* @author mad
*
*/
public class Alumni implements Attendee {
private String name;
public Alumni(String name) {
super();
this.name = name;
}
/**
* Implementation of getName defined in the Attendee interface.
*/
public String getName() {
// TODO Auto-generated method stub
return this.name;
}
}
package java_exercises3_1_1;
/**
* An Attendee just has a name nothing more.
* @author mad
*
*/
public interface Attendee {
public String getName();
}
package java_exercises3_1_1;
public class CircleFactory {
public static void main(String[] args) {
}
}
package java_exercises3_1_1;
/**
* The class Course represents a university course. There are special courses like Webinar which are
* represented in subclasses.
* @author mad
*
*/
public class Course {
private String name;
private int id;
public Course(String name, int id) {
this.name=name;
this.id=id;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
/**
* Method to book course by an Attendee. As Attendee is only an interface it could actually be a student or an alumni. But this fact is not known by
* the class Course. This is good because this way also other classes could implement interface Attendee without a having to change the code of class Course.
* @param attendee
*/
public void book(Attendee attendee) {
System.out.println("The attendee <"+attendee.getName()+"> is booked for the course <"+getName()+">");
}
/**
* Overrides the toString method of the root superclass Object.
* Returns a concatenated string containing the values of the member variables.
* Is automatically called when a reference variable is used in a System.out.println method.
*/
public String toString() {
return "Course [name=" + name + ", id=" + id + "]";
}
}
package java_exercises3_1_1;
import java.util.Arrays;
/**
* This class represents a special course with laboratories. It inherits some methods from Course.
* @author mad
*
*/
public class LabCourse extends Course {
private String[] exercises=new String[0];
public LabCourse(String name, int id, String[] exercises) {
//Use the ctor of Course to init the vars name and id.
super(name, id);
this.exercises=exercises;
}
/**
* Returns the exercises of the lab.
* @return
*/
public String[] getExercises() {
return exercises;
}
/**
* Override method toString to also add the exercises contents to the string.
*/
public String toString() {
return "LabCourse [exercises=" + (exercises != null ? Arrays.asList(exercises) : null) + ", toString()="
+ super.toString() + "]";
}
//All other methods are inherited.
}
package java_exercises3_1_1;
/**
* A student implements Attendee, because he/she is allowed to attend courses.
* @author mad
*
*/
public class Student implements Attendee {
private String name;
private int id;
private int[] grades;
public Student(String name, int id) {
super();
this.name = name;
this.id = id;
}
/**
* Implementation of getName defined in the Attendee interface.
*/
public String getName() {
return name;
}
public int[] getGrades() {
return grades;
}
public void setGrades(int[] grades) {
this.grades = grades;
}
public int getId() {
return id;
}
}
package java_exercises3_1_1;
import java_exercises3_1_1.*;
/**
* Test the behaviour of the university example.
* @author mad
*
*/
public class TestUni {
public static void main(String[] args) {
//Instantiate student called Martin
Student student=new Student("Martin",1);
University uni=new University();
//Register student
uni.register(student);
//Book Student Martin for the course Java Intro
Course course=new Course("Java Intro",1);
//From here we can't access the member variables of class Course because they are private.
//Let's try to book the course by a Student.
course.book(student);
//Book Alumni Peter for course Java Intro
Alumni alumni=new Alumni("Peter");
//Also alumnis are allowed to book a course.
course.book(alumni);
//But alumnis are not allowed to register. If you uncomment the follwing line you will get a compile error.
//uni.register(alumni);
//Both alumnis and students implement the Attendee interface and hence provide a method called getName.
System.out.println("Name of Student: "+student.getName()+", Name of Alumni: "+alumni.getName());
}
}
package java_exercises3_1_1;
import java.util.Arrays;
/**
* The class university manages students studying there.
* @author mad
*
*/
public class University {
public Student[] students=new Student[0];
public void register(Student student) {
/**students is an array of class Student, so we have to allocate a new size first.
Then we copy previously registered students to the new array instance.
Generally for this task Java collections like {@link Map} or {@link List} would be much better and will
be topic in a later session.
*/
students=(Student[]) Arrays.copyOf(students, students.length+1);
students[students.length-1]=student;
System.out.println("Registerd student: "+student.getId()+": "+student.getName()+", new array size: "+students.length);
}
public void deregister(Student student) {
for(int i=0; i<students.length;i++) {
if(students[i].getId()==student.getId()) {
students[i]=null;
System.out.println("Deregisterd student: "+student.getId()+": "+student.getName()+", new array size: "+students.length);
break;
}
}
}
}
package java_exercises3_1_1;
import java.net.URL;
/**
* This class represents webinar courses.
* @author mad
*
*/
public class Webinar extends Course {
private URL webinarURL;
/**
* ctor of a webinar with the name, id and URL parameters.
* @param name
* @param id
* @param webinarURL
*/
public Webinar(String name, int id, URL webinarURL) {
super(name, id);
this.webinarURL=webinarURL;
}
/**
* Returns the webinar URL.
* @return
*/
public URL getWebinarURL() {
return webinarURL;
}
/**
* Override method toString to also add the webinarURL to the string.
*/
public String toString() {
return "Webinar [webinarURL=" + webinarURL + ", toString()=" + super.toString() + "]";
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment