Skip to content

Instantly share code, notes, and snippets.

@rjlutz rjlutz/AppleTester.java
Last active Sep 7, 2017

Embed
What would you like to do?
Inheritance and Polymorphism (Chapter 11) -- examples from Liang Intro to Java Comprehensive 10e
public class AppleTester {
public static void main(String[] args) {
Apple apple = new Apple();
System.out.println(apple.toString());
}
}
public class Apple extends Fruit {
}
class Fruit {
// needed!
// public Fruit() {
// }
public Fruit(String name) {
System.out.println("Fruit's constructor is invoked");
}
}
public class CastingDemo {
/** Main method */
public static void main(String[] args) {
// Create and initialize two objects
Object object1 = new CircleFromSimpleGeometricObject(1);
Object object2 = new RectangleFromSimpleGeometricObject(1, 1);
// Display circle and rectangle
displayObject(object1);
displayObject(object2);
}
/** A method for displaying an object */
public static void displayObject(Object object) {
if (object instanceof CircleFromSimpleGeometricObject) {
System.out.println("The circle area is " +
((CircleFromSimpleGeometricObject)object).getArea());
System.out.println("The circle diameter is " +
((CircleFromSimpleGeometricObject)object).getDiameter());
}
else if (object instanceof
RectangleFromSimpleGeometricObject) {
System.out.println("The rectangle area is " +
((RectangleFromSimpleGeometricObject)object).getArea());
}
}
}
public class CircleFromSimpleGeometricObject
extends SimpleGeometricObject {
private double radius;
public CircleFromSimpleGeometricObject() {
}
public CircleFromSimpleGeometricObject(double radius) {
this.radius = radius;
}
public CircleFromSimpleGeometricObject(double radius,
String color, boolean filled) {
this.radius = radius;
setColor(color);
setFilled(filled);
}
/** Return radius */
public double getRadius() {
return radius;
}
/** Set a new radius */
public void setRadius(double radius) {
this.radius = radius;
}
/** Return area */
public double getArea() {
return radius * radius * Math.PI;
}
/** Return diameter */
public double getDiameter() {
return 2 * radius;
}
/** Return perimeter */
public double getPerimeter() {
return 2 * radius * Math.PI;
}
/* Print the circle info */
public void printCircle() {
System.out.println("The circle is created " + getDateCreated() +
" and the radius is " + radius);
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class DistinctNumbers {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
Scanner input = new Scanner(System.in);
System.out.print("Enter integers (input ends with 0): ");
int value;
do {
value = input.nextInt(); // Read a value from the input
if (!list.contains(value) && value != 0)
list.add(value); // Add the value if it is not in the list
} while (value != 0);
// Display the distinct numbers
for (int i = 0; i < list.size(); i++)
System.out.print(list.get(i) + " ");
}
}
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}
public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}
class Employee extends Person {
public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");
}
public Employee(String s) {
System.out.println(s);
}
}
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
import java.util.ArrayList;
public class TestArrayList {
public static void main(String[] args) {
// Create a list to store cities
ArrayList<String> cityList = new ArrayList<>();
// Add some cities in the list
cityList.add("London");
// cityList now contains [London]
cityList.add("Denver");
// cityList now contains [London, Denver]
cityList.add("Paris");
// cityList now contains [London, Denver, Paris]
cityList.add("Miami");
// cityList now contains [London, Denver, Paris, Miami]
cityList.add("Seoul");
// contains [London, Denver, Paris, Miami, Seoul]
cityList.add("Tokyo");
// contains [London, Denver, Paris, Miami, Seoul, Tokyo]
System.out.println("List size? " + cityList.size());
System.out.println("Is Miami in the list? " +
cityList.contains("Miami"));
System.out.println("The location of Denver in the list? "
+ cityList.indexOf("Denver"));
System.out.println("Is the list empty? " +
cityList.isEmpty()); // Print false
// Insert a new city at index 2
cityList.add(2, "Xian");
// contains [London, Denver, Xian, Paris, Miami, Seoul, Tokyo]
// Remove a city from the list
cityList.remove("Miami");
// contains [London, Denver, Xian, Paris, Seoul, Tokyo]
// Remove a city at index 1
cityList.remove(1);
// contains [London, Xian, Paris, Seoul, Tokyo]
// Display the contents in the list
System.out.println(cityList.toString());
// Display the contents in the list in reverse order
for (int i = cityList.size() - 1; i >= 0; i--)
System.out.print(cityList.get(i) + " ");
System.out.println();
// Create a list to store two circles
java.util.ArrayList<CircleFromSimpleGeometricObject> list
= new java.util.ArrayList<>();
// Add two circles
list.add(new CircleFromSimpleGeometricObject(2));
list.add(new CircleFromSimpleGeometricObject(3));
// Display the area of the first circle in the list
System.out.println("The area of the circle? " +
list.get(0).getArea());
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class DistinctNumbers {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
Scanner input = new Scanner(System.in);
System.out.print("Enter integers (input ends with 0): ");
int value;
do {
value = input.nextInt(); // Read a value from the input
if (!list.contains(value) && value != 0)
list.add(value); // Add the value if it is not in the list
} while (value != 0);
// Display the distinct numbers
for (int i = 0; i < list.size(); i++)
System.out.print(list.get(i) + " ");
}
}
import java.util.ArrayList;
public class MyStack {
private ArrayList<Object> list = new ArrayList<Object>();
public boolean isEmpty() {
return list.isEmpty();
}
public int getSize() {
return list.size();
}
public Object peek() {
return list.get(getSize() - 1);
}
public Object pop() {
Object o = list.get(getSize() - 1);
list.remove(getSize() - 1);
return o;
}
public void push(Object o) {
list.add(o);
}
@Override /** Override the toString in the Object class */
public String toString() {
return "stack: " + list.toString();
}
}
import java.util.ArrayList;
public class MyStack {
private ArrayList<Object> list = new ArrayList<Object>();
public boolean isEmpty() {
return list.isEmpty();
}
public int getSize() {
return list.size();
}
public Object peek() {
return list.get(getSize() - 1);
}
public Object pop() {
Object o = list.get(getSize() - 1);
list.remove(getSize() - 1);
return o;
}
public void push(Object o) {
list.add(o);
}
@Override /** Override the toString in the Object class */
public String toString() {
return "stack: " + list.toString();
}
}
public class PolymorphismDemo {
/** Main method */
public static void main(String[] args) {
// Display circle and rectangle properties
displayObject(new CircleFromSimpleGeometricObject
(1, "red", false));
displayObject(new RectangleFromSimpleGeometricObject
(1, 1, "black", true));
}
/** Display geometric object properties */
public static void displayObject(SimpleGeometricObject object) {
System.out.println("Created on " + object.getDateCreated() +
". Color is " + object.getColor());
}
}
public class RectangleFromSimpleGeometricObject
extends SimpleGeometricObject {
private double width;
private double height;
public RectangleFromSimpleGeometricObject() {
}
public RectangleFromSimpleGeometricObject(
double width, double height) {
this.width = width;
this.height = height;
}
public RectangleFromSimpleGeometricObject(
double width, double height, String color, boolean filled) {
this.width = width;
this.height = height;
setColor(color);
setFilled(filled);
}
/** Return width */
public double getWidth() {
return width;
}
/** Set a new width */
public void setWidth(double width) {
this.width = width;
}
/** Return height */
public double getHeight() {
return height;
}
/** Set a new height */
public void setHeight(double height) {
this.height = height;
}
/** Return area */
public double getArea() {
return width * height;
}
/** Return perimeter */
public double getPerimeter() {
return 2 * (width + height);
}
}
public class SimpleGeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/** Construct a default geometric object */
public SimpleGeometricObject() {
dateCreated = new java.util.Date();
}
/** Construct a geometric object with the specified color
* and filled value */
public SimpleGeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
/** Return color */
public String getColor() {
return color;
}
/** Set a new color */
public void setColor(String color) {
this.color = color;
}
/** Return filled. Since filled is boolean,
its get method is named isFilled */
public boolean isFilled() {
return filled;
}
/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}
/** Get dateCreated */
public java.util.Date getDateCreated() {
return dateCreated;
}
/** Return a string representation of this object */
public String toString() {
return "created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
}
}
import java.util.ArrayList;
public class TestArrayList {
public static void main(String[] args) {
// Create a list to store cities
ArrayList<String> cityList = new ArrayList<>();
// Add some cities in the list
cityList.add("London");
// cityList now contains [London]
cityList.add("Denver");
// cityList now contains [London, Denver]
cityList.add("Paris");
// cityList now contains [London, Denver, Paris]
cityList.add("Miami");
// cityList now contains [London, Denver, Paris, Miami]
cityList.add("Seoul");
// contains [London, Denver, Paris, Miami, Seoul]
cityList.add("Tokyo");
// contains [London, Denver, Paris, Miami, Seoul, Tokyo]
System.out.println("List size? " + cityList.size());
System.out.println("Is Miami in the list? " +
cityList.contains("Miami"));
System.out.println("The location of Denver in the list? "
+ cityList.indexOf("Denver"));
System.out.println("Is the list empty? " +
cityList.isEmpty()); // Print false
// Insert a new city at index 2
cityList.add(2, "Xian");
// contains [London, Denver, Xian, Paris, Miami, Seoul, Tokyo]
// Remove a city from the list
cityList.remove("Miami");
// contains [London, Denver, Xian, Paris, Seoul, Tokyo]
// Remove a city at index 1
cityList.remove(1);
// contains [London, Xian, Paris, Seoul, Tokyo]
// Display the contents in the list
System.out.println(cityList.toString());
// Display the contents in the list in reverse order
for (int i = cityList.size() - 1; i >= 0; i--)
System.out.print(cityList.get(i) + " ");
System.out.println();
// Create a list to store two circles
java.util.ArrayList<CircleFromSimpleGeometricObject> list
= new java.util.ArrayList<>();
// Add two circles
list.add(new CircleFromSimpleGeometricObject(2));
list.add(new CircleFromSimpleGeometricObject(3));
// Display the area of the first circle in the list
System.out.println("The area of the circle? " +
list.get(0).getArea());
}
}
public class TestCircleRectangle {
public static void main(String[] args) {
CircleFromSimpleGeometricObject circle =
new CircleFromSimpleGeometricObject(1);
System.out.println("A circle " + circle.toString());
System.out.println("The color is " + circle.getColor());
System.out.println("The radius is " + circle.getRadius());
System.out.println("The area is " + circle.getArea());
System.out.println("The diameter is " + circle.getDiameter());
RectangleFromSimpleGeometricObject rectangle =
new RectangleFromSimpleGeometricObject(2, 4);
System.out.println("\nA rectangle " + rectangle.toString());
System.out.println("The area is " + rectangle.getArea());
System.out.println("The perimeter is " +
rectangle.getPerimeter());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.