Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Questions: Q1. The original Example Animal to study is listed below. Please compile and run it. Please compile and run it. Briefly describe how

Java Questions:

Q1.

The original Example Animal to study is listed below. Please compile and run it. Please compile and run it. Briefly describe how it works in regards to abstract class, and polymorphism.

import java.util.Random; class Animal { public Animal(String aType) { type = new String(aType); } public String toString() { return "This is a " + type; } public void sound() { System.out.println("Sound for an animal"); } private String type; } class Dog extends Animal { public Dog(String aType) { super(aType); } public void sound() { System.out.println("Woof Woof"); } } class Cat extends Animal { public Cat(String aName) { super("Cat"); name = aName; breed = "Unknown"; } public Cat(String aName, String aBreed) { super("Cat"); name = aName; breed = aBreed; } public String toString() { return super.toString() + " It's " + name + " the " + breed; } public void sound() { System.out.println("Miiaooww"); } private String name; private String breed; } class Duck extends Animal { public Duck(String aName) { super("Duck"); name = aName; breed = "Unknown"; } public Duck(String aName, String aBreed) { super("Duck"); name = aName; breed = aBreed; } public String toString() { return super.toString() + " It's " + name + " the " + breed; } public void sound() { System.out.println("Quack quackquack"); } private String name; private String breed; } public class MainClass { public static void main(String[] args) { Animal[] theAnimals = { new Dog("A"), new Cat("C", "D"), new Duck("E", "F") }; Animal petChoice; Random select = new Random(); for (int i = 0; i < 5; i++) { petChoice = theAnimals[select.nextInt(theAnimals.length)]; System.out.println(" Your choice: " + petChoice); petChoice.sound(); } } }

Q2.

The Card2 example is listed below. Please compile and run it. Briefly describe how it works using what you just learned about abstract class and polymorphism.

public class Card2 { private int rank; private int suit; public final static int DIAMONDS = 1; public final static int CLUBS = 2; public final static int HEARTS = 3; public final static int SPADES = 4; public final static int ACE = 1; public final static int DEUCE = 2; public final static int THREE = 3; public final static int FOUR = 4; public final static int FIVE = 5; public final static int SIX = 6; public final static int SEVEN = 7; public final static int EIGHT = 8; public final static int NINE = 9; public final static int TEN = 10; public final static int JACK = 11; public final static int QUEEN = 12; public final static int KING = 13; public Card2(int rank, int suit) { this.rank = rank; this.suit = suit; } public int getSuit() { return suit; } public int getRank() { return rank; } public static boolean isValidRank(int rank) { return ACE <= rank && rank <= KING; } public static boolean isValidSuit(int suit) { return DIAMONDS <= suit && suit <= SPADES; } public boolean equals(Object obj) { if (obj instanceof Card2) { if (((Card2)obj).getRank() == this.rank && ((Card2)obj).getSuit() == this.suit) { return true; } else { return false; } } else { return false; } } public int hashCode() { return ((suit-1)*13)+rank; } public String toString() { return rankToString(this.rank) + " of " + suitToString(this.suit); } public static String rankToString(int rank) { switch (rank) { case ACE: return "Ace"; case DEUCE: return "Deuce"; case THREE: return "Three"; case FOUR: return "Four"; case FIVE: return "Five"; case SIX: return "Six"; case SEVEN: return "Seven"; case EIGHT: return "Eight"; case NINE: return "Nine"; case TEN: return "Ten"; case JACK: return "Jack"; case QUEEN: return "Queen"; case KING: return "King"; default: //Handle an illegal argument. There are generally two ways //to handle invalid arguments, throwing an exception (see //the section on Handling Exceptions): throw new IllegalArgumentException("Invalid rank " + rank); //or //return null;

} } public static String suitToString(int suit) { String result = ""; switch (suit) { case DIAMONDS: return "Diamonds"; case CLUBS: return "Clubs"; case HEARTS: return "Hearts"; case SPADES: return "Spades"; default: throw new IllegalArgumentException("Invalid suit " + suit); } } public static void main(String... args) { new Card2(ACE, DIAMONDS); new Card2(KING, SPADES); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Administrator Limited Edition

Authors: Martif Way

1st Edition

B0CGG89N8Z

More Books

Students also viewed these Databases questions

Question

Use a three-step process to develop effective business messages.

Answered: 1 week ago