Commit 1ab8340e authored by Martin Deinhofer's avatar Martin Deinhofer
Browse files

Initial commit

parents
/.project
/.settings
/bin
/.classpath
package java_exercises2_1_1;
/**
* The class Student represents students that are allowed to study on the University.
*
* @author mad
*
*/
public class Student {
//member variables
public String name;
protected int id;
private int[] grades;
//constructor with name and id to ensure that those fields can't be uninitialized
public Student(String name, int id) {
super();
this.name = name;
this.id = id;
}
/**
* The getter for the name member variable.
* @return
*/
public String getName() {
return name;
}
/**
* Returns the grades of the student.
* @return
*/
public int[] getGrades() {
return grades;
}
/**
* Sets the grades of the students.
* @param grades
*/
public void setGrades(int[] grades) {
this.grades = grades;
}
/**
* Retuns the id of the student.
* @return
*/
public int getId() {
return id;
}
}
package java_exercises2_1_1;
import java_exercises2_1_1.Student;
/**
* 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);
//Create instance of a university.
University uni=new University();
//Register student Martin
uni.register(student);
uni.deregister(student);
//The member variables of Student are not directly accessible due to the private access modifier.
//If you uncomment the following line you will get a compile error.
//student.name;
}
}
package java_exercises2_1_1;
import java.util.Arrays;
import java_exercises2_1_1.Student;
/**
* The class university manages students studying there.
* @author mad
*
*/
public class University {
private 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_exercises2_1_1.other;
import java_exercises2_1_1.Student;
public class TestUni {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student stud=new Student("Martin", 1);
//Student.name is public, hence accessible
System.out.println(stud.name);
//Student.id is protected, hence not accessible
//System.out.println(stud.id);
//Student.grades is private, hence not accessible
//System.out.println(stud.grades);
}
}
package java_exercises2_2_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;
}
/**
* 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_exercises2_2_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_exercises2_2_1;
import java.net.MalformedURLException;
import java.net.URL;
import java_exercises2_1_1.Student;
import java_exercises2_1_1.University;
/**
* Test the behaviour of the university example.
* @author mad
*
*/
public class TestUni {
public static void main(String[] args) throws MalformedURLException {
//Instantiate student called Martin
Student student=new Student("Martin",1);
University uni=new University();
//Register student
uni.register(student);
Course javaEmbedded=new Course("Java for Embedded Systenms",1);
//Due to the overridden toString method we can print the contents of the object with System.out.println
System.out.println("Course javaEmbedded: "+javaEmbedded);
System.out.println("Course: javaEmbedded method calls: id:"+javaEmbedded.getId()+", name: "+javaEmbedded.getName());
//Create Webinar about Web Accessibility
Webinar webinarWebAcc=new Webinar("Make accessible web sites", 2, new URL("http://webinars.com/webaccess"));
System.out.println("Course webinarWebAcc: "+webinarWebAcc);
//But you can also access the member values with the respective getter methods.
System.out.println("Course: id:"+javaEmbedded.getId()+", name: "+javaEmbedded.getName()+", URL: "+webinarWebAcc.getWebinarURL());
}
}
package java_exercises2_2_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