In Java,
If you use \'string\' instead of \'\'string\'\'
(single quotes instead of double)
What kind of error is that: lexical or syntactic?
Solution
Its a LEXICAL Error in JAVA as double quotes are used when we enter any string and single
quotes are only used for single characters like \'a\', \'J\' etc. Lexical errors are the errors thrown
by your lexer when unable to continue..
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
In this project you will define some interfaces, abstract classes, a.pdf
1. In this project you will define some interfaces, abstract classes, and concrete classes, all placed in
a specific package. You will also use the instanceofoperator. Note: if you do not use interfaces,
abstract classes, and/or instanceof, you will not receive full credit.
Project description:
1. Define an interface called Nameable with the following methods:
2. Define an interface called Runner with the following method:
3. Define an interface called Flyer with the following method:
4. Define an abstract class called Animal which must implement the Nameable interface. Add the
representation of:
Animal's name,
Animal's number of legs,
Animal's age, and
Keep count of the instances of the Animal class by using a static variable.
If you believe you should define a variable as static, or final, or even both, do so and briefly
mention the reason as a comment on that variable in javadoc format (see below).
Use suitable qualifiers for all fields in the class.
5. Define a class called Dog, where a Dog is-a subclass of Animal. Also, a Dog can run (i.e. this
class should implement the Runner interface).
6. Define a class called Cat, where a Cat is-a subclass of Animal. Also, a Cat can run (i.e. this
class should implement the Runner interface).
7. Define a class called FlyCat, where a FlyCat is-a (imaginary) subclass of Animal that can fly
(i.e. this class should implement the Flyerinterface).
8. Define a class called Dragon, where a Dragon is-a (imaginary) subclass of Animal that can
both run and fly (i.e. this class should implement both the Runner and Flyer interfaces).
9. Each of the Animal subclasses has one or two capabilities: it can fly, or run, or both. When
one calls fly on an animal that can fly, fly should print [name of the animal] + " is flying".
Similarly run should print [name of the animal] + " is running".
10. Be creative and find more Animal kinds (at least two more). One of these new animal types
(can be imaginary) should be able to both fly and run, and should be a direct subclass of Animal.
11. Create three animals of each type (3 dogs, 3 cats, etc), with different given names and
suitable attribute values. The names can be as simple as Joe1, Joe2, etc.
12. Store the Animal objects (above) in an array of type Animal.
13. Display a menu to the user on the screen (before implementing this part, read the notes
below).
Press 1 to see how many animals are in the system.
2. Press 2 to see the name and kind of each animal.
Press 3 to see which animals can fly.
Press 4 to see which animals can run.
Press 5 to see which animals can run AND fly.
Press 6 to see a description of each animal.
Press q to terminate the program.
14. Important notes about the menu:
If the user enters anything other than {1, 2, 3, 4, 5, 6, q}, the program should print a proper error
message, and ask the user to enter a number between {1, 2, 3, 4, 5, 6} or 'q' (to quit). In other
words, your program should not continue (or even worse, crash) if the user enters a wrong
choice.
In option 1, the result should be the total number of instances of Animal class created so far (use
the static variable).
For option 2, use instanceof to get kind.
For option 3, use instanceof. The result should contain both name (Joe1, Joe2, etc) and type
(Dog, Cat, etc) of the animal.
For option 4, use instanceof.The result should contain both name and type (Dog, Cat, etc) of the
animal.
For option 5, use instanceof. The result should contain both name and type (Dog, Cat, etc) of the
animal.
Option 6 above could display something like this:
Things to note:
Your program must be in package zoo.
You must provide setters and getters for all instance variables in classes.
Follow good coding style (proper variable names, indentation, etc).
Your design should be reasonably efficient, and in accordance with object oriented design
principles (encapsulation, information hiding, inheritance, etc.).
If you are defining a variable as public or protected, briefly mention the reason for that.
If you are defining a variable as static or final, briefly mention the reason for that, as well.
Write your important comments in a way that is usable by javadoc. You should use appropriate
tags in your comments. Important comments include:
Explanation of what a specific variable is. Such a comment will be used by javadoc only if it is
for a public or protected variable. So if you have provided a comment for a private variable and
it does not show up in the HTML files, that is ok.
Explanation of what a function does, its parameters, its return types, the exceptions it may throw.
3. Solution
Java Code:
package zoo;
public interface Nameable {
String getName();
void setName(String name);
}
package zoo;
public interface Flyer {
void fly();
}
package zoo;
public interface Runner {
void run();
}
package zoo;
public abstract class Animal implements Nameable {
private String name;
private int numberOfLegs;
private int age;
/**
* Shared variable across all
*/
protected static int ANIMAL_COUNT=0;
/**
* @param name
* @param numberOfLegs
* @param age
*/
public Animal(String name, int numberOfLegs, int age) {
this.name = name;
4. this.numberOfLegs = numberOfLegs;
this.age = age;
ANIMAL_COUNT++;
}
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name=name;
}
/**
* @return the numberOfLegs
*/
public int getNumberOfLegs() {
return numberOfLegs;
}
/**
* @param numberOfLegs the numberOfLegs to set
*/
public void setNumberOfLegs(int numberOfLegs) {
this.numberOfLegs = numberOfLegs;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
5. }
/**
*
*/
package zoo;
/**
* @author ramesh
*
*/
public class Cat extends Animal implements Runner {
public Cat(String name, int numberOfLegs, int age) {
super(name, numberOfLegs, age);
}
/* (non-Javadoc)
* @see zoo.Runner#run()
*/
@Override
public void run() {
System.out.println(this.getName()+ "is running");
}
}
package zoo;
public class Dog extends Animal implements Runner {
public Dog(String name, int numberOfLegs, int age) {
super(name, numberOfLegs, age);
}
@Override
public void run() {
System.out.println(this.getName()+ "is running");
}
}
/**
*
*/
package zoo;
6. /**
* @author ramesh
*
*/
public class Dragon extends Animal implements Flyer, Runner {
public Dragon(String name, int numberOfLegs, int age) {
super(name, numberOfLegs, age);
}
/*
* (non-Javadoc)
*
* @see zoo.Runner#run()
*/
@Override
public void run() {
System.out.println(this.getName()+ "is running");
}
/*
* (non-Javadoc)
*
* @see zoo.Flyer#fly()
*/
@Override
public void fly() {
System.out.println(this.getName()+ "is flying");
}
}
/**
*
*/
package zoo;
/**
* @author ramesh
*
*/
public class FlyCat extends Animal implements Flyer {
7. public FlyCat(String name, int numberOfLegs, int age) {
super(name, numberOfLegs, age);
}
/* (non-Javadoc)
* @see zoo.Flyer#fly()
*/
@Override
public void fly() {
System.out.println(this.getName()+ "is flying");
}
}
/**
*
*/
package zoo;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* @author ramesh
*
*/
public class ZooDriver {
// AVL Tree test driver
public static void main(String[] args) {
try (Scanner scan = new Scanner(System.in)) {
List animals = new ArrayList();
Animal dog1 = new Dog("Joe1", 4, 8);
Animal dog2 = new Dog("Joe2", 4, 7);
Animal dog3 = new Dog("Joe3", 4, 5);
animals.add(dog1);
animals.add(dog2);
animals.add(dog3);
Animal cat1 = new Cat("cat1", 4, 6);
Animal cat2 = new Cat("cat2", 4, 3);
Animal cat3 = new Cat("cat3", 4, 2);
8. animals.add(cat1);
animals.add(cat2);
animals.add(cat3);
// Display MANU to user
System.out.println(showManu());
while (true) {
// Get user choice from system console
System.out.println("Enter a number between {1, 2, 3, 4, 5, 6} or 'q' (to quit)");
String choice = scan.next();
if (choice.equals("q"))
break;
switch (choice) {
case "1":
System.out.println(
"The total number of instances of Animal class created so far:" +
Animal.ANIMAL_COUNT);
break;
case "2":
for (Animal animal : animals) {
System.out.print("Name of Animal: " + animal.getName()+", ");
if (animal instanceof Dog) {
System.out.println("Kind of animal: Dog");
} else if (animal instanceof Cat) {
System.out.println("Kind of animal: Cat");
} else if (animal instanceof FlyCat) {
System.out.println("Kind of animal: FlyCat");
} else if (animal instanceof Dragon) {
System.out.println("Kind of animal: Dragon");
}
}
break;
case "3":
for (Animal animal : animals) {
if (animal instanceof FlyCat) {
9. System.out.println("Name of animal:" + animal.getName() + ", Kind of
animal: FlyCat");
} else if (animal instanceof Dragon) {
System.out.println("Name of animal:" + animal.getName() + ", Kind of
animal: Dragon");
}
}
break;
case "4":
for (Animal animal : animals) {
if (animal instanceof Dog) {
System.out.println("Name of animal:" + animal.getName() + ", Kind of
animal: Dog");
} else if (animal instanceof Cat) {
System.out.println("Name of animal:" + animal.getName() + ", Kind of
animal: Cat");
}
}
break;
case "5":
for (Animal animal : animals) {
if (animal instanceof Dragon) {
System.out.println("Name of animal:" + animal.getName() + ", Kind of
animal: Dragon");
}
}
break;
case "6":
for (Animal animal : animals) {
if (animal instanceof Dog) {
System.out.println(
"I am a " + animal.getAge() + "years old Dog and my name is " +
animal.getName()
+ ". I have " + animal.getNumberOfLegs() + " legs and I can
run.");
} else if (animal instanceof Cat) {
10. System.out.println(
"I am a " + animal.getAge() + "years old Cat and my name is " +
animal.getName()
+ ". I have " + animal.getNumberOfLegs() + " legs and I can
run.");
} else if (animal instanceof FlyCat) {
System.out.println(
"I am a " + animal.getAge() + "years old FlyCat and my name is " +
animal.getName()
+ ". I have " + animal.getNumberOfLegs() + " legs and I can
fly/run.");
} else if (animal instanceof Dragon) {
System.out.println(
"I am a " + animal.getAge() + "years old Dragon and my name is " +
animal.getName()
+ ". I have " + animal.getNumberOfLegs() + " legs and I can
fly/run.");
}
}
break;
default:
System.out.println("Wrong Entry ");
break;
}
}
}
}
private static String showManu() {
return "Press 1 to see how many animals are in the system. "
+ "Press 2 to see the name and kind of each animal. " + "Press 3 to see which
animals can fly. "
+ "Press 4 to see which animals can run. " + "Press 5 to see which animals can run
AND fly. "
+ "Press 6 to see a description of each animal. " + "Press q to terminate the
program. ";
}