diff --git a/.idea/misc.xml b/.idea/misc.xml index e208459..53f706d 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/src/uoft/csc207/week2/Main.java b/src/uoft/csc207/week2/Main.java index 648f0d3..dc8a4af 100644 --- a/src/uoft/csc207/week2/Main.java +++ b/src/uoft/csc207/week2/Main.java @@ -3,8 +3,15 @@ public class Main { public static void main(String[] args) { - String[] name = {"First", "Middle", "Last"}; - Person p = new Person(name, "moogah"); - System.out.println(p); + String[] name = {"First", "Middle", "Last"}; // string array object; array of strings + // array is like a list, but it doesn't grow or shrink + Person p = new Person(name, "moogah"); // Person class + Person s = new Student(name, "froogal", "132141546"); + // because we declared it as a person, it knows it is a superclass + System.out.println(p); // out's type is print stream + // toString annotation + // Ctrl + Click for finding where the variable is defined + // null is like None but its not even an object + // ternary operator... } } diff --git a/src/uoft/csc207/week2/Person.java b/src/uoft/csc207/week2/Person.java index 24a57e3..483ecf0 100644 --- a/src/uoft/csc207/week2/Person.java +++ b/src/uoft/csc207/week2/Person.java @@ -1,12 +1,19 @@ -package uoft.csc207.week2; +package uoft.csc207.week2; // collection of related classes /** * A person at the UofT. */ class Person { - /** The person's name (family name last). */ - String[] name; /** The person's UTORid */ String utorid; + /** + * The person's name (family name last). + */ + String[] name; // specifically declare type and name, like __init__ + /** + * The person's UTORid + */ + String utorid; + /** * Initialize this Person named name with UTORid utorid. * @@ -14,29 +21,35 @@ class Person { * @param utorid the person's UTORid */ Person - (String[] name, String utorid){this.name=name;this.utorid=utorid;} + (String[] name, String utorid) { + this.name = name; + this.utorid = utorid; + } /** * Return a string representation of this person with this format: * 'last name, other names: utorid' + * * @return a string representation of this person */ - public String toString(){ - return this.formatName()+": "+this.utorid; + public String toString() { + return this.formatName() + ": " + this.utorid; } /** * Return the name formatted as a str. The last name is first, then a * comma, then the rest of the names. + * * @return the name formatted as a str. */ String formatName() { String formattedName = this.name[name.length - 1] + ","; - int i = 0; - while (i != this.name.length - 1) { formattedName = - formattedName+" "+this.name[i]; + int i = 0; + while (i != this.name.length - 1) { + formattedName = formattedName + " " + this.name[i]; i += 1; - } + } // Java doesn't care about spaces and indents! + // don't need to worry about style errors as much return formattedName; } diff --git a/src/uoft/csc207/week2/Student.java b/src/uoft/csc207/week2/Student.java new file mode 100644 index 0000000..48b86ca --- /dev/null +++ b/src/uoft/csc207/week2/Student.java @@ -0,0 +1,13 @@ +package uoft.csc207.week2; +// "don't type code, type stuff and make IntelliJ do everything!" +public class Student extends Person { + private final String studentid; // wow! create field + // need a type and variable name, but can add optional modifiers + // "final says you can only assign to it once, it's its final value" + // private is accessibility modifier - specifies where the name can be used in the program + // only in the curly brackets of the class + public Student(String[] name, String utorid, String studentid) { + super(name, utorid); // calls the parent's constructor + this.studentid = studentid; + } +}