4️⃣

15.4 Practice

Practice

Abstract Classes: University

Create an abstract class called University. It should have a constructor that takes two strings, one for name and one for location and assigns them. Then, create a child class that extend University, called College, with one more constructor argument than University (i.e. 3)
 

Abstract Classes: Restaurants

 
Create an abstract class Restaurant, Then create four classes of your favorite restaurants and it should print the food it makes.

Abstract Methods: University

 
Using the abstract class University that we made last time, make an abstract method for University. Then, in College class, override the abstract method to make it print out the class’ attributes.
 

Interfaces: Triangle

 
Create class Square and class Triangle that implement the interface Shape. In the sides method, explain how many sides each shape has. Ex. “I have ___ sides!”
 

Interface: Comparable

 
A commonly used interface is the Comparable interface. In order to implement it, we use the keyword implement followed by Comparable<class>. The Comparable interface only has one method, the compareTo method: public int compareTo(Object obj){}
 
The compareTo method allows us to compare a current object to a specified object. The method should return certain values:
  • If the current object is greater than the specified object we return 1
  • If the current object is less than the specified object we return -1
  • If the two are equal we return 0
 
Write the compareTo method to compare the current rectangle to the specified rectangle and return the appropriate values. Source: Introduction to Java Programming Comprehensive 10th Edition, Source: Introduction to Java Programming Comprehensive 10th Edition
 

Interface: Comparable - Measurable

 
Create a Measurable interface that is used to “measure” anything. Add necessary methods. Required Methods: double average(Measurable[] items), largest(Measurable[] items) or smallest(Measurable[] items). Create a Student class that implements Measurable and it’s methods(you can compare whatever you measure, i.e. GPA).
Note: for the largest/smallest methods don’t worry about the case if they are equal. Credit: Problem only adapted from Java SE 9 Textbook
 

Previous Section

3️⃣
15.3 Interfaces

Next Section