Blog
Blog about me
Trystan
Java Takeaways
The Code below goes over the different lessons we were taught.
- Classes/Objects are shown in the class Book, with the getter, setter, and other methods.
- Inheritance and the Object Superclass is shown when I override the implicitly inheriteed method toString()
- Array Lists and iteration are shown near the bottom when I create an ArrayList of Books.
What I think was most important from our learning
- I think the most important things I learned about were overloading constructors and arraylists,
- Overloading constuctors allws me to create objects with a default, and then with peramaters depending on the situtation that is needed.
- ArrayLists are helpful to learn about becuase it is a class I am likely to use a lot, the ability to change around arrays, in a similar way to lists is great.
import java.util.ArrayList;
public class Book{
protected String title;
protected int pages;
protected int words;
//default constuctor
Book(){
this.title = "{null}";
this.pages = 0;
this.words = 0;
}
// constuctor with only a title
Book(String title){
this.title = title;
this.pages = 0;
this.words = 0;
}
//constuctor with all arguments
Book(String title, int pages, int words){
this.title = title;
this.pages = pages;
this.words = words;
}
//getters (final because I don't want any children to change them)
public final int getPageCount(){
return this.pages;
}
public final int getWordCount(){
return this.words;
}
public final String getTitle(){
return this.title;
}
//setters (not finalized, so children can override them if needed)
public void setPageCount(int pages){
this.pages = pages;
}
public void setWordCount(int words){
this.words = words;
}
public void setTitle(String title){
this.title = title;
}
//override toString to return relavent information about the instance
@Override
public String toString(){
return this.title + ", pages: " + this.pages + ", words: " + this.words;
}
}
//array list with type book, so that I can add any number of books
ArrayList<Book> books = new ArrayList();
//add some books, and yes I did look up the acutal word/page counts
books.add(new Book("The Lion, The Witch, and The Wardrobe", 172, 38421));
//I gave the play 5 pages for its 5 parts, but it may have been better to create a subclass specically for this one
books.add(new Book("Romeo and Juliet", 5, 24545));
books.add(new Book("Harry Potter and the Socerer's Stone", 233, 76944));
if(books.get(1).getWordCount() < books.get(2).getWordCount()){
System.out.println(books.get(1).getTitle() + " is shorter than " + books.get(2).getTitle());
}
//I think this is a Bubble Sort, it sorts the books in a given array by the number of words
ArrayList<Book> SortByWords(ArrayList<Book> array){
boolean complete = false;
while (!complete){
for(int i = 1; i < array.size(); i++){
if(array.get(i-1).getWordCount() > array.get(i).getWordCount()){
Book temp = array.get(i-1);
array.remove(i-1);
array.add(i,temp);
break;
}
else if (i == array.size() - 1){
complete = true;
}
}
}
return array;
};
books = SortByWords(books);
// Fun way to print out an arraylist, first converts it to an array, then uses the Arrays.toString we learned about
System.out.println(Arrays.toString(books.toArray()));
Romeo and Juliet is shorter than Harry Potter and the Socerer's Stone
[Romeo and Juliet, pages: 5, words: 24545, The Lion, The Witch, and The Wardrobe, pages: 172, words: 38421, Harry Potter and the Socerer's Stone, pages: 233, words: 76944]
my lesson
My Lesson was on Inheritance, and I specifically taught Polymorphism and the Object superclass. To demonstrate my knowelge very simply, I created a subclass to the previous book class, which I would use for polymorphism.
I also wanted to disply some basic knowelege of static variables, so I had the constuctor increment the book count each time a new instance is created.
class Textbook extends Book{
static public int bookCount = 0;
Textbook(){
this.pages = Integer.MAX_VALUE; //it should be as large as possible for an integer
this.words = Integer.MAX_VALUE; //same for the words
this.title = "The Endless Texts";
Textbook.bookCount +=1;
}
@Override
public String toString(){ //this will be my polymorphic method
return "The Endless Archive Arises...";
}
}
Book myTextile = new Textbook(); //polymorphism :)
Object myTextileObject = new Textbook(); //Object Superclass Polymorphism (Super Morphism :D)
System.out.println(myTextile.toString());// executes the textbook toString() method at runtime
System.out.println(myTextileObject.toString());// executes the textbook toString() method at runtime
System.out.println(Textbook.bookCount);
The Endless Archive Arises...
The Endless Archive Arises...
2
More stuff about my lesson
While I was creating the lesson, I made some images to replace explaing as much. But there was an image that I actually didn’t include in the final lesson becuase I didn’t think I would nor needed to have time for it.
This image was created to show what static types could use which classes as references, but I didn’t think was required for the lesson to go smoothly, so to aviod having more clutter on a difficult lesson I didn’t use it.
The Solid lines in the lesson show which static type the constuctor is based on. The dashed lines show which constuctors you could use polymorphically for the static type. FInally the crossed out lines show examples of where you couldn’t use the constucotrfor the typing.