Question

****Please read the following requirements and use java language w/ comments**** ****Can make this a multi...

****Please read the following requirements and use java language w/ comments****

****Can make this a multi part question if needed****

Project requires a base class, a derived subclass, and an interactive driver (class with a main) that allows the user to utilize and manipulate the classes created. You may select one super class from the following:

              Superclass                                    Example subclasses

  1. Ship class                                a) battleship, tugboat, icebreaker
  2. Person class                           b) manager, salaryworker, hourlyworker
  3. Aircraft class                         c) Learjet, cargoplane, fighter
  4. Vehicle class                          d) car, truck, tank
  5. Animal class                          e) dog, cat, bird
  1. Create the “internals” for the selected superclass. This class should hold the minimum following data items:
  • Appropriate properties (have at least 10 – try to select those with differing types).
  • Constructors (have at least 4, ensure at least 2 have some error checking)
  • Gets/Sets (build at least 6 of each)
  • Utilities (A print at a minimum, you may add 1 or 2 more if desired – think this one through)
  • [optional] You may create no more than 3 static methods, and 3 variables - if desired
  1. Extend your base class with an appropriate subclass of your choice. Similar to your homework, add appropriate functionality.
  • Add at least 2 additional data properties
  • Add at least 2 additional constructors, 2 gets, and 2 sets
  • Override the print utility to support your subclass specific items. You may (optionally) adjust, override, or overload any other appropriate methods.
  1. In a separate class with your “main” (the driver), create a program that accomplishes the following:
  • Your program should allow the creation and manipulation of an object of your subclass type.
  • The program should contain a menu allowing the user to load/change (constructors/sets), delete, interrogate (retrieve data from [gets]), and print your subclass object. Obviously, you would use the subclass member methods (constructors/sets/gets/utilities) to accomplish the above requirements. Don’t have more than 10 options total
  • Your menu should also include a “quit” selection – i.e. the program should loop to allow for multiple iterations. Place the program menu in a method (NON-member method, i.e. it will be in the driver class, NOT the super/sub class you created).
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.IOException;

import java.lang.NullPointerException;

import java.util.Scanner;

class Animal {

// class properties

private int age;

private double height;

private double weight;

private String color;

private boolean isCarnivorous;

private String sex;

private int ageExpectency;

private boolean isPawed;

private boolean isDomestic;

private double speed;

// default constructor

public Animal() {

// System.out.println("Animal class constructor");

}

// parameterized constructors

// two of which are checking for valid ages

public Animal(int age) {

// "Constructing animal object with given age if greater than 0"

if(age>0) {

this.age = age;

} else {

throw new Error("Age cannot be less than zero");

}

}

public Animal(int age, double height, double weight) {

if(age>0) {

this.age = age;

this.height = height;

this.weight = weight;

} else {

throw new Error("Age cannot be less than zero");

}

}

public Animal(int age, double height, double weight, String color) {

this.age = age;

this.height = height;

this.weight = weight;

this.color = color;

}

// Getters and setters for private member variables

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public double getHeight() {

return height;

}

public void setHeight(double height) {

this.height = height;

}

public double getWeight() {

return weight;

}

public void setWeight(double weight) {

this.weight = weight;

}

public String getColor() {

return color;

}

public void setColor(String color) {

this.color = color;

}

public boolean isCarnivorous() {

return isCarnivorous;

}

public void setCarnivorous(boolean isCarnivorous) {

this.isCarnivorous = isCarnivorous;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

public int getAgeExpectency() {

return ageExpectency;

}

public void setAgeExpectency(int ageExpectency) {

this.ageExpectency = ageExpectency;

}

public boolean isPawed() {

return isPawed;

}

public void setPawed(boolean isPawed) {

this.isPawed = isPawed;

}

public boolean isDomestic() {

return isDomestic;

}

public void setDomestic(boolean isDomestic) {

this.isDomestic = isDomestic;

}

public double getSpeed() {

return speed;

}

public void setSpeed(double speed) {

this.speed = speed;

}

// class utilities

public void eat() {

System.out.println("Eating..");

}

public void sleep() {

System.out.println("Sleeping..");

}

public void print() {

System.out.println("Height: " + this.getHeight());

System.out.println("Weight: " + this.getWeight());

System.out.println("Sex: " + this.getSex());

}

// static method

public static boolean isSame(Animal a, Animal b) {

return a.equals(b);

}

}

class Dog extends Animal {

private String species;

private String petName;

// default constructor

Dog() {

// System.out.println("Dog class constructor");

}

// parameterized constructors

Dog(int age) {

super(age); // super keyword invokes super class constructor

}

Dog(int age, String species) {

super(age);

this.species = species;

}

// getter and setters for sub class properties

public String getSpecies() {

return species;

}

public void setSpecies(String species) {

this.species = species;

}

public String getPetName() {

return petName;

}

public void setPetName(String petName) {

this.petName = petName;

}

// utility methods of sub class

public void bark() {

System.out.println("Barking..");

}

// over riding super class method

@Override

public void print() {

System.out.println("Details about Dog");

System.out.println("Age: " + this.getAge());

System.out.println("Species: " + this.getSpecies());

System.out.println("Pet name: " + this.getPetName());

super.print(); // invoke super class print() method

}

}

public class Main {

public static void main(String[] args) throws IOException, NullPointerException {

Scanner in = new Scanner(System.in);

// an object of type sub class - Dog

Dog puppy = new Dog();

while(true) {

System.out.print("\nChoose an option:\n1. Create a Dog\n2. Set pet name\n3. Delete the object"

+ "\n4. Set basic details\n5. Print the object\n6. Exit\n>>>");

int option = in.nextInt();

switch(option) {

case 1:

puppy = new Dog(12, "Labrador");

continue;

case 2:

// this block will throw NullPointerException if invoked immediately after deleting dog object

// in other words, you need a dog first to set it's details

try {

in.nextLine();

System.out.print("Enter the pet name for your dog: ");

String name= in.nextLine(); //reads name

puppy.setPetName(name);

continue;

} catch(Exception e) {

System.out.println(e);

System.out.println("Create an object first");

continue; // we want the program to continue even after exception

}

case 3:

puppy = null;

continue;

case 4:

// this block will throw NullPointerException if invoked immediately after deleting dog object

// in other words, you need a dog first to set it's details

try {

System.out.print("Enter age: ");

puppy.setAge(in.nextInt());

System.out.print("Enter height: ");

puppy.setHeight(in.nextDouble());

System.out.print("Enter weight: ");

puppy.setWeight(in.nextDouble());

in.nextLine();

System.out.print("Enter Sex: ");

puppy.setSex(in.nextLine());

continue;

} catch(Exception e) {

System.out.println(e);

System.out.println("Create an object first");

continue; // we want the program to continue even after exception

}

case 5:

puppy.print();

continue;

case 6:

in.close(); // close the stream reader

System.out.println("Programs exits successfully");

return;

default:

System.out.println("Invalid input. Choose from menu");

}

}

}

}

Add a comment
Know the answer?
Add Answer to:
****Please read the following requirements and use java language w/ comments**** ****Can make this a multi...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java Which of the following statements declares Salaried as a subclass of payType? Public class...

    In Java Which of the following statements declares Salaried as a subclass of payType? Public class Salaried implements PayType Public class Salaried derivedFrom(payType) Public class PayType derives Salaried Public class Salaried extends PayType If a method in a subclass has the same signature as a method in the superclass, the subclass method overrides the superclass method. False True When a subclass overloads a superclass method………. Only the subclass method may be called with a subclass object Only the superclass method...

  • write java program and for the following (you can add more classes as you see necessary)...

    write java program and for the following (you can add more classes as you see necessary) as follows: a class called Coursewith courseCode, courseName,and creditHours; a superclass called FacultyMemberwith FacultyID, firstName, lastName, academicRank, and academicSpecialization; a course Convener subclass inherits from the FacultyMember class with specific member variables representing the coursesand members (Lecturers andTAs) whom he/she is responsible of; Lecturers andTAsare also subclasses that inherit from FacultyMemberclass with some specific member variables (i.e., maximumNumberOfCourses, quotaOfCreditHoursthey can take, and assignedCourses). Create...

  • JAVA Problem:  Coffee do not use ArrayList or Case Design and implement a program that manages coffees....

    JAVA Problem:  Coffee do not use ArrayList or Case Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type, price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public void prepare ();...

  • ***Please give the java code for the below and add comments for different areas for a...

    ***Please give the java code for the below and add comments for different areas for a dummy to understand*** This java program will combine the techniques of handling arrays, decision control statements, and loops. Your program should accomplish the following: Build an array that holds characters (size 35) Load the array with random capital letters (use random generator) Your program should present a menu to the user allowing for the following “actions”. To “search” for a target letter of the...

  • Java file Name Dog Classes and Methods Create a constructor that incorporates the type, breed, and...

    Java file Name Dog Classes and Methods Create a constructor that incorporates the type, breed, and name variables (do not include topTrick). Note: The type refers to what the breed typically does; for example, a corgi would be a “cattle herding dog.” A Shiba Inu would be a “hunting dog.” Create the setTopTrick() mutator method Dog is parent class Corgi and Driver are subclasses Complete the Corgi class: Using the UML Class diagram, declare the instance variables. Create the two...

  • Java Program Please help me with this. It should be pretty basic and easy but I...

    Java Program Please help me with this. It should be pretty basic and easy but I am struggling with it. Thank you Create a superclass called VacationInstance Variables destination - String budget - double Constructors - default and parameterized to set all instance variables Access and mutator methods budgetBalance method - returns the amount the vacation is under or over budget. Under budget is a positive number and over budget is a negative number. This method will be overwritten in...

  • Can someone please help with this in JAVA? Write one application program by using the following...

    Can someone please help with this in JAVA? Write one application program by using the following requirements: 1) Exception handling 2) Inheritance a) At least one superclass or abstract superclass: this class needs to have data members, accessor, mutator, and toString methods b) At least one subclass: this class also needs to have data members, accessor, mutator, and toString methods c) At least one interface: this interface needs to have at least two abstract methods d) At least one method...

  • JAVA Problem:  Coffee    Design and implement a program that manages coffees. First, implement an abstract class...

    JAVA Problem:  Coffee    Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type, price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public void prepare (); } The prepare method will...

  • Problem: Coffee(Java) Design and implement a program that manages coffees. First, implement an abstract class named...

    Problem: Coffee(Java) Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type(does not mention the data type of the variable coffeeType), price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public...

  • Must be in Java. Please show step by step process with proper explanation and comments. Please...

    Must be in Java. Please show step by step process with proper explanation and comments. Please maintain proper indentation. CODE PROVIDED: Doctor.java HospitalDoctor.java Q1Runner.java import java.util.Scanner; public class Q1Runner { public static void main(String[] args) { Scanner kb = new Scanner(System.in);               // Do your work here        } } You are asked to create the starting point of a system to represent several types of medical doctors. You decided to implement two classes: a superclass...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT