Object-oriented programming (OOP) refers to a type of computer programming (software design) in which programmers define the data type of a data structure, and also the types of operations (functions) that can be applied to the data structure.
Download Object-Oriented Programming: Constructors, Getters, and Setters in Java and more Study Guides, Projects, Research Functional Programming in PDF only on Docsity!
LAB REPORT:
SUBJECT : Writing classes that
represent objects, Instance data
SUBMITTED BY: SAMIA
REG. NO: 17JZBCS
DEPARTMENT: CS&IT
TITLE: Constructors, Getters and
setters
In BlueJ, create a new project called Lab
In the project create a class called DogDemo with a sta2c method called makeDogs
Create a second class in the 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 do<ed arrow in the project window to show that DogDemo depends on (uses) the Dog class.
SOLUTION:
Exercise 2:
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.
In the declara2on of the variable, ini2alize 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.
Add standard "ge<er" methods for all three fields.
In the makeDogs method, get and print just the breed of myDog.
Then get and print just the name of yourDog.
SOLUTION:
Exercise 6:
In Dog , add a "se<er" 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.
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 calcula2on used is real age 2mes 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 mul2plied by 7.
In the makeDogs method, print each dog's age in dog-years: Rover is 28 in dog-years.