In a project named 'DogApplication', create a class called 'Dog'
1. declare two instance variables in 'Dog' :
name (String type)
age (int type)
2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables
3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with any values of your choice for name and age.
4. go back to 'Dog' class and implement the 'toString' method and then call this method from 'Driver' to print the dogs' info.
this is what I have so far:
for Dog
public class Dog
{
//instance variables
private String name;
private int age;
//constructor, takes the instance variable and binds
to constructor
public Dog(String dogName, int dogAge) //has the same
name as the class
{
name = dogName;
//assigns
the instance variables which has no value to the constructor
parameters
age = dogAge;
}
public String toString()
{
String Dog = "";
Dog += this.name + "\t";
Dog += this.age;
return Dog;
}
}
and for Driver:
public class Driver
{
public static void main (String [] args)
{
Dog myDog = new Dog ("Gino",
6);
Dog yourDog = new Dog
("Amie",7);
System.out.println(Dog.name);
System.out.println(Dog.age);
}
}
I'm not sure if I did this right. Can you explain?
//File1 //Dog.java public class Dog { private String name; private String breed; private int age; public Dog() { } public Dog(String name, int age) { this.name = name; this.age = age; } // Accessors and Mutators public String getName() { return name; } public void setName(String name) { this.name = name; } public String getBreed() { return breed; } public void setBreed(String breed) { this.breed = breed; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } // create these for each of the three instance variables /* your code goes here */ public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Dog)) return false; Dog dog = (Dog) o; if (age != dog.age) return false; if (name != null ? !name.equals(dog.name) : dog.name != null) return false; return breed != null ? breed.equals(dog.breed) : dog.breed == null; } public void writeOutput() { // print out the Dog's characteristics as shown above System.out.println("Name: "+getName()+" Breed: "+getBreed()+" Age in calendar years: "+getAge()+" Age in human years: "+getAgeInHumanYears()); /* your code goes here */ } public int getAgeInHumanYears() { // implement this method return getAge()*2; /* your code goes here */ } public String toString() { return "Dog{" + "name='" + name + ''' + ", breed='" + breed + ''' + ", age=" + age + '}'; } }
=======================================================
//File2 //Driver.java public class Driver{ public static void main(String[] args) { Dog myDog = new Dog("Balto",8); myDog.setBreed("Siberian Husky"); System.out.println(myDog); Dog yourDog = new Dog("Scooby",42); yourDog.setBreed("Great Dane"); System.out.println(yourDog); } }
=======================================================
Dog{name='Balto', breed='Siberian Husky', age=8} Dog{name='Scooby', breed='Great Dane', age=42}
In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in...
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...
Using your Dog class from earlier this week, complete the following: Create a new class called DogKennel with the following: Instance field(s) - array of Dogs + any others you may want Contructor - default (no parameters) - will make a DogKennel with no dogs in it Methods: public void addDog(Dog d) - which adds a dog to the array public int currentNumDogs() - returns number of dogs currently in kennel public double averageAge() - which returns the average age...
Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters long encryptedPassword : String key int Must be between 1 and 10(inclusive) accountId - A unique integer that identifies each account nextIDNum – a static int that starts at 1000 and is used to generate the accountID no other instance variables needed. Default constructor – set all instance variables to a default value. Parameterized constructor Takes in clearPassword, key. Calls encrypt method to create...
C# programming 50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (String) edition ( int) published year (int) Constructor that takes all of the above variables as input parameters. set/get methods ToString method// that return sting representation of Book object. 2-Create the sub class New_Book that is derived from the base class Book and has the following instance variables, constructor, and methods: title...
Java Create four classes: 1. An Animal class that acts as a superclass for Dog and Bird 2. A Bird class that is a descendant of Animal 3. A Dog class that is a descendant of Animal 4. A “driver” class named driver that instantiates an Animal, a Dog, and a Bird object. The Animal class has: · one instance variable, a private String variable named name · a single constructor that takes one argument, a String, used to set...
Start a new project in NetBeans that defines a class Person with the following: Instance variables firstName (type String), middleInitial (type char) and lastName (type String) (1) A no-parameter constructor with a call to the this constructor; and (2) a constructor with String, char, String parameters (assign the parameters directly to the instance variables in this constructor--see info regarding the elimination of set methods below) Accessor (get) methods for all three instance variables (no set methods are required which makes...
In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program to create a database of dogs of your own. You will also include File I/O with this program. You will create 3 classes for this assignment. The first class: Create a Class representing the information that is associated with one item (one dog) of your database. Name this class Dog. Using Lab 3, this will include the information in regard to one dog. The...
Answer the following: 1. Create a Class Car with the following instance variables: model (String), Brand (String), maxspeed (double) Note: use encapsulation (all variables are private). (3 Marks) 2. Create a non-default constructor for Class Car which takes 3 parameters. (2 Marks) 3. Create a get and set methods for instance variable model variable only. (2 Marks) 4. Create PrintInfo() method which prints all three instance variables. (2 Marks) 5. Create a subclass GasCar with instance variable TankSize (double).. (2...
You are to create a class Appointment.java that will have the following: 5 Instance variables: private String month private int day private int year private int hour private int minute 1 default constructor public Appointment() 1 non-default constructor that accepts arguments for all instance variables, your constructor must call the setter methods below to set the values of the instance variables public Appointment(String monthPassed, int dayPassed, int yearPassed, int hourPassed, int minutePassed) 5 setter methods (one for each instance variable)...
Create a NetBeans project called "Question 1". Then create a public class inside question1 package called Author according to the following information (Make sure to check the UML diagram) Author -name:String -email:String -gender:char +Author(name:String, email:String, gender:char) +getName():String +getEmail):String +setEmail (email:String):void +getGender():char +tostring ):String . Three private instance variables: name (String), email (String), and gender (char of either 'm' or 'f'): One constructor to initialize the name, email and gender with the given values . Getters and setters: get Name (), getEmail() and getGender (). There are no setters for name and...