Question

In: Computer Science

Learning Objectives Writing classes that represent objects Define object's state (Instance variables) Define objects constructors Write...

Learning Objectives

  • Writing classes that represent objects
  • Define object's state (Instance variables)
  • Define objects constructors
  • Write getter (accessor) and setter (mutator) methods.

As in previous labs, please write the Java code for each of the following exercises in BlueJ. For each exercise, write a comment before the code stating the exercise number.

Make sure you are using the appropriate indentation and styling conventions

Exercise 1 (15 Points)

  • In BlueJ, create a new project called Lab5
  • In the project create a class called DogDemo with a static method called makeDogs
  • Create a second class in the same project called Dog that will represent a single dog (just a class header and an empty body for now).
  • In the makeDogs method, create a Dog object called myDog. Note that the Dog and DogDemo classes are now connected by a dotted arrow in the project window to show that DogDemo depends on (uses) the Dog class.

Exercise 2 (20 Points)

  • In the Dog class, create a variable at the class level (also called instance data or a field) that will represent the dog's name (a String).
  • Declare this variable as private. This puts the Dog class in charge of how the name gets updated. This variable is not DIRECTLY accessible from outside the Dog class.
  • In the declaration of the variable, initialize it to "Lassie".
  • Create a toString method that returns the dog's name.
  • In the makeDogs method of the DogDemo class, print the Dog object you created. Run the makeDogs method.

Exercise 3 (15 Points)

  • This would be OK if all dogs were named Lassie!! Each instance of Dog (each Dog object) will have its own name variable, which contain different values. So create a constructor in the Dog class that accepts a parameter which is used to set the name variable.
  • In the makeDogs method, create a new dog and pass in the name "Lassie" to its constructor.
  • Create a second Dog object with a different name, "Rover" for example.
  • Print the two dog objects.

Exercise 4 (15 Points)

  • Add another field to the Dog class representing the dog's breed (a String) and one representing the dog's age (an int). Both fields should be private.
  • Modify the Dog constructor to set these values as well, based on parameters.
  • Define a new Dog objects in makeDogs and give them a name, breed and age.
  • Modify the toString method to return a string in the following form:

Fido is a 3-year-old German Sheppard

  • Test the updated makeDogs method. Think through what is happening at each step.

Exercise 5 (10 Points)

  • Add standard "getter" methods for all three fields. Remember that getter methods just returns the value of a private instance variable (field). This will make the value of a private variable accessible ONLY through this method.
  • In the makeDogs method, get and print just the breed of one of your created objects.
  • Then get and print just the name of another one of the created objects.

Exercise 6 (10 Points)

  • In Dog, add a "setter" method for the age field. Don't change the age if the parameter is less than 1.
  • In the makeDogs method, try to set the age of yourDog to 0, then print the Dog object to make sure the age did not change.
  • Then set the age of yourDog to 4 and print it again.

Exercise 7 (15 Points)

  • You may have heard the expression "That's 21 in dog years" or something similar. The idea is to come up with a number to make it easy to compare a dog's age to that of a human in terms of overall life span. The calculation used is real age multiplied by 7. So a dog that's 3 years old is "21 in dog years".
  • Add a method in Dog called getDogYears that returns the dog's age in dog-years. Do NOT create another field for this. Just return the dog's current age multiplied by 7.
  • In the makeDogs method, print each dog's age in dog-years:

Rover is 28 in dog-years.

Solutions

Expert Solution

Here is the code for the given exercises, as each exercise is an enhancement of existing exercise, I mentioned the exercise number for each statement. Code separate by each exercise are pasted at the end

DogDemo.java


//Exercise-1 creating class DogDemo
public class DogDemo
{
    /**
     * Exercise-1 Static method called makeDogs
     */
    public static void makeDogs()
    {
        /*
        //Exercise-1 create a Dog object called myDog
        Dog myDog = new Dog();
        //Exercise-2 print the Dog object you created.
        System.out.println(myDog.toString());
        //Exercise-3 create a new dog and pass in the name "Lassie" to its constructor.
        Dog myDog1 = new Dog("Lassie");
        //Exercise-3 Create a second Dog object with a different name, "Rover" for example.
        Dog myDog2 = new Dog("Rover");
        //Exercise-3 Print the two dog objects.
        System.out.println(myDog1.toString());
        System.out.println(myDog2.toString());
        */
        //Exercise-4 Define a new Dog objects in makeDogs and give them a name, breed and age.
        Dog myDog3 = new Dog("Fido", "German Sheppard", 3);
        //Exercise-4 print the dog object
        System.out.println(myDog3.toString());
        //Exercise-5 get and print just the breed of one of your created objects.
        System.out.println(myDog3.getBreed());
        //Exercise-6 try to set the age of yourDog to 0
        myDog3.setAge(0);
        //Exercise-6 print the Dog object to make sure the age did not change.
        System.out.println(myDog3.getAge());
        //Exercise-6 set the age of yourDog to 4 and print it again.
        myDog3.setAge(4);
        System.out.println(myDog3.getAge());
        //Exercise-7 print each dog's age in dog-years:Rover is 28 in dog-years.
        System.out.println(myDog3.getName() + " is " + myDog3.getDogYears() + " in dog-years." );
    }
}

Dog.java


// Exercise-1 creating class Dog
public class Dog
{
    //Exercise-2 create a variable at the class level that will represent the dog's name (a String). Initialize it to Lassie
    private String name = "Lassie";
    //Exercise-4 Add another field to the Dog class representing the dog's breed (a String)
    private String breed;
    //Exercise-4 add another field representing the dog's age (an int).
    private int age;
  
    /**Exercise-2
     * toString method that returns the dog's name.
     */
    public String toString()
    {
        //return this.name;
        //Exercise4 Modify the toString method to return a string
        return this.name + " is a " + this.age + "-year-old " + this.breed;
    }
  
    /**Exercise-3
     * create a constructor in the Dog class that accepts a parameters which is used to set the variables.
     */
    public Dog(String dogName, String dogBreed, int dogAge)
    {
        this.name = dogName;
        //Exercise-4 Modify the Dog constructor to set these values as well, based on parameters.
        this.breed = dogBreed;
        this.age = dogAge;
    }
  
    /**Exercise-5
     * Create getter methods for all the three variables
     */
    public String getName()
    {
        return this.name;
    }
  
    public String getBreed()
    {
        return this.breed;
    }
  
    public int getAge()
    {
        return this.age;
    }
  
    /**Exercise-6
     * Add a "setter" method for the age field
     */
    public void setAge(int dogAge)
    {
        //Exercise-6 Don't change the age if the parameter is less than 1.
        if(dogAge >= 1)
            this.age = dogAge;
    }
  
    /**Exercise-7
     * Add a method in Dog called getDogYears that returns the dog's age in dog-years.
     */
    public int getDogYears()
    {
        return this.getAge() * 7;
    }
}

Output (Each line is the output generated for each exercise)

Below are the code for each exercise

Exercise-1
DogDemo.java
//Exercise-1 creating class DogDemo
public class DogDemo
{
    /**
     * Exercise-1 Static method called makeDogs
     */
    public static void makeDogs()
    {
        //Exercise-1 create a Dog object called myDog
        Dog myDog = new Dog();
    }
}

Dog.java
// Exercise-1 creating class Dog
public class Dog
{

}

Exercise-2
DogDemo.java
//Exercise-1 creating class DogDemo
public class DogDemo
{
    /**
     * Exercise-1 Static method called makeDogs
     */
    public static void makeDogs()
    {
        //Exercise-1 create a Dog object called myDog
        Dog myDog = new Dog();
        //Exercise-2 print the Dog object you created.
        System.out.println(myDog.toString());
    }
}

Dog.java
// Exercise-1 creating class Dog
public class Dog
{
    //Exercise-2 create a variable at the class level that will represent the dog's name (a String). Initialize it to Lassie
    private String name = "Lassie";
  
    /**Exercise-2
     * toString method that returns the dog's name.
     */
    public String toString()
    {
        return this.name;
    }
}

Exercise-3
DogDemo.java

//Exercise-1 creating class DogDemo
public class DogDemo
{
    /**
     * Exercise-1 Static method called makeDogs
     */
    public static void makeDogs()
    {
        //Exercise-1 create a Dog object called myDog
        Dog myDog = new Dog();
        //Exercise-2 print the Dog object you created.
        System.out.println(myDog.toString());
        //Exercise-3 create a new dog and pass in the name "Lassie" to its constructor.
        Dog myDog1 = new Dog("Lassie");
        //Exercise-3 Create a second Dog object with a different name, "Rover" for example.
        Dog myDog2 = new Dog("Rover");
        //Exercise-3 Print the two dog objects.
        System.out.println(myDog1.toString());
        System.out.println(myDog2.toString());
    }
}

Dog.java
// Exercise-1 creating class Dog
public class Dog
{
    //Exercise-2 create a variable at the class level that will represent the dog's name (a String). Initialize it to Lassie
    private String name = "Lassie";
  
    /**Exercise-2
     * toString method that returns the dog's name.
     */
    public String toString()
    {
        return this.name;
    }
  
    /**Exercise-3
     * create a constructor in the Dog class that accepts a parameters which is used to set the variables.
     */
    public Dog(String dogName)
    {
        this.name = dogName;
     }
}

Exercise-4
DogDemo.java
//Exercise-1 creating class DogDemo
public class DogDemo
{
    /**
     * Exercise-1 Static method called makeDogs
     */
    public static void makeDogs()
    {
        /*
        //Exercise-1 create a Dog object called myDog
        Dog myDog = new Dog();
        //Exercise-2 print the Dog object you created.
        System.out.println(myDog.toString());
        //Exercise-3 create a new dog and pass in the name "Lassie" to its constructor.
        Dog myDog1 = new Dog("Lassie");
        //Exercise-3 Create a second Dog object with a different name, "Rover" for example.
        Dog myDog2 = new Dog("Rover");
        //Exercise-3 Print the two dog objects.
        System.out.println(myDog1.toString());
        System.out.println(myDog2.toString());
        */
        //Exercise-4 Define a new Dog objects in makeDogs and give them a name, breed and age.
        Dog myDog3 = new Dog("Fido", "German Sheppard", 3);
        //Exercise-4 print the dog object
        System.out.println(myDog3.toString());
    }
}

Dog.java
// Exercise-1 creating class Dog
public class Dog
{
    //Exercise-2 create a variable at the class level that will represent the dog's name (a String). Initialize it to Lassie
    private String name = "Lassie";
    //Exercise-4 Add another field to the Dog class representing the dog's breed (a String)
    private String breed;
    //Exercise-4 add another field representing the dog's age (an int).
    private int age;
  
    /**Exercise-2
     * toString method that returns the dog's name.
     */
    public String toString()
    {
        //return this.name;
        //Exercise4 Modify the toString method to return a string
        return this.name + " is a " + this.age + "-year-old " + this.breed;
    }
  
    /**Exercise-3
     * create a constructor in the Dog class that accepts a parameters which is used to set the variables.
     */
    public Dog(String dogName, String dogBreed, int dogAge)
    {
        this.name = dogName;
        //Exercise-4 Modify the Dog constructor to set these values as well, based on parameters.
        this.breed = dogBreed;
        this.age = dogAge;
    }
}

Exercise-5
DogDemo.java

//Exercise-1 creating class DogDemo
public class DogDemo
{
    /**
     * Exercise-1 Static method called makeDogs
     */
    public static void makeDogs()
    {
        /*
        //Exercise-1 create a Dog object called myDog
        Dog myDog = new Dog();
        //Exercise-2 print the Dog object you created.
        System.out.println(myDog.toString());
        //Exercise-3 create a new dog and pass in the name "Lassie" to its constructor.
        Dog myDog1 = new Dog("Lassie");
        //Exercise-3 Create a second Dog object with a different name, "Rover" for example.
        Dog myDog2 = new Dog("Rover");
        //Exercise-3 Print the two dog objects.
        System.out.println(myDog1.toString());
        System.out.println(myDog2.toString());
        */
        //Exercise-4 Define a new Dog objects in makeDogs and give them a name, breed and age.
        Dog myDog3 = new Dog("Fido", "German Sheppard", 3);
        //Exercise-4 print the dog object
        System.out.println(myDog3.toString());
        //Exercise-5 get and print just the breed of one of your created objects.
        System.out.println(myDog3.getBreed());
    }
}

Dog.java
// Exercise-1 creating class Dog
public class Dog
{
    //Exercise-2 create a variable at the class level that will represent the dog's name (a String). Initialize it to Lassie
    private String name = "Lassie";
    //Exercise-4 Add another field to the Dog class representing the dog's breed (a String)
    private String breed;
    //Exercise-4 add another field representing the dog's age (an int).
    private int age;
  
    /**Exercise-2
     * toString method that returns the dog's name.
     */
    public String toString()
    {
        //return this.name;
        //Exercise4 Modify the toString method to return a string
        return this.name + " is a " + this.age + "-year-old " + this.breed;
    }
  
    /**Exercise-3
     * create a constructor in the Dog class that accepts a parameters which is used to set the variables.
     */
    public Dog(String dogName, String dogBreed, int dogAge)
    {
        this.name = dogName;
        //Exercise-4 Modify the Dog constructor to set these values as well, based on parameters.
        this.breed = dogBreed;
        this.age = dogAge;
    }
  
    /**Exercise-5
     * Create getter methods for all the three variables
     */
    public String getName()
    {
        return this.name;
    }
  
    public String getBreed()
    {
        return this.breed;
    }
  
    public int getAge()
    {
        return this.age;
    }
}

Exercise-6
DogDemo.java

//Exercise-1 creating class DogDemo
public class DogDemo
{
    /**
     * Exercise-1 Static method called makeDogs
     */
    public static void makeDogs()
    {
        /*
        //Exercise-1 create a Dog object called myDog
        Dog myDog = new Dog();
        //Exercise-2 print the Dog object you created.
        System.out.println(myDog.toString());
        //Exercise-3 create a new dog and pass in the name "Lassie" to its constructor.
        Dog myDog1 = new Dog("Lassie");
        //Exercise-3 Create a second Dog object with a different name, "Rover" for example.
        Dog myDog2 = new Dog("Rover");
        //Exercise-3 Print the two dog objects.
        System.out.println(myDog1.toString());
        System.out.println(myDog2.toString());
        */
        //Exercise-4 Define a new Dog objects in makeDogs and give them a name, breed and age.
        Dog myDog3 = new Dog("Fido", "German Sheppard", 3);
        //Exercise-4 print the dog object
        System.out.println(myDog3.toString());
        //Exercise-5 get and print just the breed of one of your created objects.
        System.out.println(myDog3.getBreed());
        //Exercise-6 try to set the age of yourDog to 0
        myDog3.setAge(0);
        //Exercise-6 print the Dog object to make sure the age did not change.
        System.out.println(myDog3.getAge());
        //Exercise-6 set the age of yourDog to 4 and print it again.
        myDog3.setAge(4);
        System.out.println(myDog3.getAge());
    }
}

Dog.java
// Exercise-1 creating class Dog
public class Dog
{
    //Exercise-2 create a variable at the class level that will represent the dog's name (a String). Initialize it to Lassie
    private String name = "Lassie";
    //Exercise-4 Add another field to the Dog class representing the dog's breed (a String)
    private String breed;
    //Exercise-4 add another field representing the dog's age (an int).
    private int age;
  
    /**Exercise-2
     * toString method that returns the dog's name.
     */
    public String toString()
    {
        //return this.name;
        //Exercise4 Modify the toString method to return a string
        return this.name + " is a " + this.age + "-year-old " + this.breed;
    }
  
    /**Exercise-3
     * create a constructor in the Dog class that accepts a parameters which is used to set the variables.
     */
    public Dog(String dogName, String dogBreed, int dogAge)
    {
        this.name = dogName;
        //Exercise-4 Modify the Dog constructor to set these values as well, based on parameters.
        this.breed = dogBreed;
        this.age = dogAge;
    }
  
    /**Exercise-5
     * Create getter methods for all the three variables
     */
    public String getName()
    {
        return this.name;
    }
  
    public String getBreed()
    {
        return this.breed;
    }
  
    public int getAge()
    {
        return this.age;
    }
  
    /**Exercise-6
     * Add a "setter" method for the age field
     */
    public void setAge(int dogAge)
    {
        //Exercise-6 Don't change the age if the parameter is less than 1.
        if(dogAge >= 1)
            this.age = dogAge;
    }
}

Exercise-7
DogDemo.java

//Exercise-1 creating class DogDemo
public class DogDemo
{
    /**
     * Exercise-1 Static method called makeDogs
     */
    public static void makeDogs()
    {
        /*
        //Exercise-1 create a Dog object called myDog
        Dog myDog = new Dog();
        //Exercise-2 print the Dog object you created.
        System.out.println(myDog.toString());
        //Exercise-3 create a new dog and pass in the name "Lassie" to its constructor.
        Dog myDog1 = new Dog("Lassie");
        //Exercise-3 Create a second Dog object with a different name, "Rover" for example.
        Dog myDog2 = new Dog("Rover");
        //Exercise-3 Print the two dog objects.
        System.out.println(myDog1.toString());
        System.out.println(myDog2.toString());
        */
        //Exercise-4 Define a new Dog objects in makeDogs and give them a name, breed and age.
        Dog myDog3 = new Dog("Fido", "German Sheppard", 3);
        //Exercise-4 print the dog object
        System.out.println(myDog3.toString());
        //Exercise-5 get and print just the breed of one of your created objects.
        System.out.println(myDog3.getBreed());
        //Exercise-6 try to set the age of yourDog to 0
        myDog3.setAge(0);
        //Exercise-6 print the Dog object to make sure the age did not change.
        System.out.println(myDog3.getAge());
        //Exercise-6 set the age of yourDog to 4 and print it again.
        myDog3.setAge(4);
        System.out.println(myDog3.getAge());
        //Exercise-7 print each dog's age in dog-years:Rover is 28 in dog-years.
        System.out.println(myDog3.getName() + " is " + myDog3.getDogYears() + " in dog-years." );
    }
}

Dog.java
// Exercise-1 creating class Dog
public class Dog
{
    //Exercise-2 create a variable at the class level that will represent the dog's name (a String). Initialize it to Lassie
    private String name = "Lassie";
    //Exercise-4 Add another field to the Dog class representing the dog's breed (a String)
    private String breed;
    //Exercise-4 add another field representing the dog's age (an int).
    private int age;
  
    /**Exercise-2
     * toString method that returns the dog's name.
     */
    public String toString()
    {
        //return this.name;
        //Exercise4 Modify the toString method to return a string
        return this.name + " is a " + this.age + "-year-old " + this.breed;
    }
  
    /**Exercise-3
     * create a constructor in the Dog class that accepts a parameters which is used to set the variables.
     */
    public Dog(String dogName, String dogBreed, int dogAge)
    {
        this.name = dogName;
        //Exercise-4 Modify the Dog constructor to set these values as well, based on parameters.
        this.breed = dogBreed;
        this.age = dogAge;
    }
  
    /**Exercise-5
     * Create getter methods for all the three variables
     */
    public String getName()
    {
        return this.name;
    }
  
    public String getBreed()
    {
        return this.breed;
    }
  
    public int getAge()
    {
        return this.age;
    }
  
    /**Exercise-6
     * Add a "setter" method for the age field
     */
    public void setAge(int dogAge)
    {
        //Exercise-6 Don't change the age if the parameter is less than 1.
        if(dogAge >= 1)
            this.age = dogAge;
    }
  
    /**Exercise-7
     * Add a method in Dog called getDogYears that returns the dog's age in dog-years.
     */
    public int getDogYears()
    {
        return this.getAge() * 7;
    }
}


Related Solutions

Assignment 1: JAVA Classes, Objects, and Constructors The goal of this assignment is to get you...
Assignment 1: JAVA Classes, Objects, and Constructors The goal of this assignment is to get you familiar with some of the most used JAVA syntax, as well as constructors objects, objects calling other objects, class and instance attributes and methods. You will create a small program consisting of Musician and Song classes, then you will have Musicians perform the Songs. Included is a file “Assignment1Tester.java”. Once you have done the assignment and followed all instructions, you should be able to...
9.9 LAB: Artwork label (classes/constructors) Define the Artist class with a constructor to initialize an artist's...
9.9 LAB: Artwork label (classes/constructors) Define the Artist class with a constructor to initialize an artist's information and a print_info() method. The constructor should by default initialize the artist's name to "None" and the years of birth and death to 0. print_info() should display Artist Name, born XXXX if the year of death is -1 or Artist Name (XXXX-YYYY) otherwise. Define the Artwork class with a constructor to initialize an artwork's information and a print_info() method. The constructor should by...
Write a Simple C++ Code to show that the Constructors of Composed Classes Execute before Container...
Write a Simple C++ Code to show that the Constructors of Composed Classes Execute before Container Classes.
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type float. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type float. For each class, provide its getter and setter functions, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for...
Objectives: use Scite Use recursion to solve a problem Create classes to model objects Problem :...
Objectives: use Scite Use recursion to solve a problem Create classes to model objects Problem : The Rectangle class (Filename: TestRectangle.java) Design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height....
Q.We design classes to create objects in java. Write step wise procedure for (a).How these objects...
Q.We design classes to create objects in java. Write step wise procedure for (a).How these objects created in memory? (b). Which area of memory is used for creation of the objects? (c). What is difference between stack and heap memory in java?
Using Python to play Checkers: 1) Create classes to represent the necessary objects for game play....
Using Python to play Checkers: 1) Create classes to represent the necessary objects for game play. This will include, at least, the game board, the two types of pieces and the two sides. You will need to determine the best way to represent the relationship between them. 2) Set up one side of the board. Print the status of the board.
Learning Objectives: ● review implementing the interface Comparable<T> ● programming against interfaces not objects. ● review...
Learning Objectives: ● review implementing the interface Comparable<T> ● programming against interfaces not objects. ● review reading in data from a file ● use the internet to learn about the interface Comparator<T> ● use a Comparator to provide an alternative way to order elements in a collection Description: Turn in: Turn in the assignment via Canvas Create a package called booksthat includes 3 files: Book.java, BookApp.java, and books.csv (provided). Class Book represents Pulitzer prize winning books that have a title,...
Python Please Define a class that will represent soccer players as objects. A soccer player will...
Python Please Define a class that will represent soccer players as objects. A soccer player will have as attributes, name, age, gender, team name, play position on the field, total career goals scored. The class should have the following methods: 1. initializer method that will values of data attributes arguments. Use 0 as default for career goals scored. 2. str method to return all data attributes as combined string object. 3. addToGoals that will accept an argument of int and...
Objectives: 1. Create classes to model objects Instructions: 1. Create a new folder named Lab6 and...
Objectives: 1. Create classes to model objects Instructions: 1. Create a new folder named Lab6 and save all your files for this lab into this new folder. 2. You can use any IDE (such as SciTE, NetBeans or Eclipse) or any online site (such as repl.it or onlinegdb.com) to develop your Java programs 3. All your programs must have good internal documentation. For information on internal documentation, refer to the lab guide. Problems [30 marks] Problem 1: The Account class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT