In: Computer Science
Learning Objectives
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)
Exercise 2 (20 Points)
Exercise 3 (15 Points)
Exercise 4 (15 Points)
Fido is a 3-year-old German Sheppard
Exercise 5 (10 Points)
Exercise 6 (10 Points)
Exercise 7 (15 Points)
Rover is 28 in dog-years.
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;
}
}