I realizw everything is spelled out for me here but im have to leave for a few h
ID: 3832780 • Letter: I
Question
I realizw everything is spelled out for me here but im have to leave for a few hours then come back. then its due like an hour after I get back. this way I could copy paste yours into mine. Java 1
This lab will help in your understanding of Inheritance. This is a long lab, with there being a total of 4 classes. Two classes will inherit from a third class. The fourth class will test this inheritance. The Horse class This class will contain all the basic attributes for all horses. 1. Open Eclipse and create a new Java project named Chapter10Lab1. 2. With the project folder selected create a new class named Horse. Generate comments but do not include the main() method. 3. Alter the generated, block comments to include your name, date, and the purpose of the program. In this case the purpose is to develop a class that contains basic attributes for all show horses. 4. First we'll declare our variables. After the opening brace for the class, type: // Declarations private int idNo; private String coatColor; private int age; private double height; Notice that there are variables representing the id number of the horse, its coat color, age and height. All variables are private to prevent other classes from altering the attribute values. 2 5. Next we'll create a default constructor method and overload it. Type: //constructor methods public Horse() { } //overloaded constructor public Horse(int anIdNo, String aCoatColor, int anAge, double aHeight) { setIdNo(anIdNo); setCoatColor(aCoatColor); setAge(anAge); setHeight(aHeight); } 3 6. For every variables we will have both a mutator method and accessor method. This is so that we can alter values outside the class but not directly alter a variables value. First we create the mutator methods. Type: // set accessor methods with simple validation public void setIdNo(int anIdNo) { idNo = anIdNo; } public void setCoatColor(String aCoatColor) { coatColor=aCoatColor; } public void setAge(int anAge) { age = anAge; } public void setHeight(double aHeight) { height = aHeight; } Unless you do not want an outside source to alter variables values, you should have a mutator method for each value in which an outside class can pass a new value without directly altering a variable value. The value passed will be for a particular object and not affect the individual variable value. Notice the code in each is very simple. It simply sets a variable declared in the class to the value of a variable passed to the method. 4 7. Our accessor method will access the current value of a variable from method and return the value to where the method is called. Type: // get accessor methods public int getIdNo() { return idNo; } public String getCoatColor() { return coatColor; } public int getAge() { return age; } public double getHeight() { return height; } Notice that there is an accessor method for each variable. The return type is the same as the data type for the corresponding variable. This must always be the case. 5 8. Our last method will display the information on the horse when called. Type: // custom method public String tellAboutHorse() { // returns values of attributes as one string String horseDetails; horseDetails = String.format("%n%n%s%n%d%n%n%s%n%s%n%n%s%n%d%n%n%s%n%.1f %s", "ID number: ", idNo, "Coat Color: ", coatColor, "Age: ", age, "Height: ", height, " hands "); return horseDetails; } 9. Save your changes. Fix any errors if necessary. The DraftHorse class This DraftHorse class will inherit the attributes/methods from the Horse class. It will add attributes specific to Draft horses. 1. With the project folder selected create a new class named DraftHorse. Change its superclass to Horse and generate comments. We will not include the main() method in this class, but do generate comments. Your class should current look like: Notice the "extends" Horse. The keyword "extends" means to inherit from. 2. Alter the generated comments to include your name, date, and purpose of the class. 6 3. Scroll down to after the opening brace for the class. We will declare several variables. Type: // additional attributes beyond those inherited from Horse private String classification; private String draftBreed; Two new variables are created. One will be for the classification of the horse, what category of draft horse does each horse fall intol. The second variable represents the breed. Yes, we could have also included this in the Horse class. 4. Next are our constructor methods. The first is the default, which has no parameters. Type: public DraftHorse() { } public DraftHorse(int anIdNo, String aCoatColor,int anAge,double aHeight,String aClassification,String aDraftBreed) { // invoke super class constructor super(anIdNo, aCoatColor, anAge, aHeight); // set subclass attribute values setClassification(aClassification); setDraftBreed(aDraftBreed); } Look at the overloaded method. All the parameters include the attributes from the Horse class in addition to the two new attributes for just draft horses. Within the method we call the constructor method in the Horse class, passing to it the first four variable values. The Horse class will handles these attributes with its methods. We pass the two new variable values to methods within this class. 7 5. Like the Horse class we have mutator and accessor methods for the variables declared in this class. We also have a tellAboutHorse() method like the one in the Horse class. Type: // set accessor methods public void setClassification(String aClassification) { classification=aClassification; } public void setDraftBreed(String aDraftBreed) { draftBreed = aDraftBreed; } // get accessor methods public String getClassification() { return classification; } public String getDraftBreed() { return draftBreed; } public String tellAboutHorse() { // returns values of attributes as one string String horseDetails; horseDetails = String.format("%s%n%n%s%n%s%n%n%s%n%s", super.tellAboutHorse(), "Classification: ", classification, "Draft Breed: ", draftBreed); return horseDetails; } Look at the last method. For the variable horseDetails we use the String class' format method to format the output. We call the Horse class' tellAboutHorse() method and add output for the two variables created in this class. The super refers to the superclass. 6. Save your changes and fix any errors if necessary. Do not run the program. 8 The SaddleHorse class The SaddleHorse class is very similar to the DraftHorse class, but with different variables. Since it is so similar I will not go into detail about the code. 1. With the project folder selected create a new class named SaddleHorse. Set the superclass to Horse and generate comments but do not include the main() method. 2. Alter the generated comments to include your name, date, and purpose of the class. 3. Scroll down to after the opening brace for the class and type: private String saddleType; private String saddleBreed; public SaddleHorse() { } // constructor public SaddleHorse(int anIdNo, String aCoatColor,int anAge,double aHeight,String aSaddleType,String aSaddleBreed) { super(anIdNo,aCoatColor,anAge,aHeight); setSaddleType(aSaddleType); setSaddleBreed(aSaddleBreed); } // set accessor methods public void setSaddleType(String aSaddleType) { saddleType=aSaddleType; } public void setSaddleBreed(String aSaddleBreed) { saddleBreed = aSaddleBreed; } 9 // get accessor methods public String getSaddleType() { return saddleType; } public String getSaddleBreed() { return saddleBreed; } public String tellAboutHorse() { // returns values of attributes as one string String horseDetails; horseDetails = String.format("%s%n%n%s%n%s%n%n%s%n%s", super.tellAboutHorse(), "Saddle Type: ", saddleType, "Saddle Breed: ", saddleBreed); return horseDetails; } 4. Save your changes and fix any errors if necessary. Do not run the program. Testing the classes In our final class we'll test the SaddleHorse and DraftHorse classes and in turn test the Horse class. 1. With the project folder selected create a new class named Ch10Lab1. Include the main() method and generate comments. 2. Within the comments include your name, date, and purpose of the program. 3. After the comments type the import statement that will import the Scanner class. We have done this often enough that you should be able to do it on your own. 4. Scroll down to after the opening brace for the main() method. Change the line comment to: //Declarations 10 5. We'll declare several variables, a Scanner object, and a SaddleHorse and DraftHorse object. Type: Scanner keyboard = new Scanner(System.in); SaddleHorse horse1 = new SaddleHorse(); DraftHorse horse2 = new DraftHorse(); int idNo, age, height; String color, saddleType, breed, classification; Here we are constructing the two objects using the classes' default constructor method. There are no arguments within the (). 6. For the SaddleHorse we'll request information from the user. Type: System.out.printf("%100s%n%n", "Let's enter a Saddle Horse"); System.out.println("Enter the horse's id number (whole numbers only)"); idNo = keyboard.nextInt(); System.out.println("Enter the horse's coat color e.g. Roan"); color = keyboard.next(); System.out.println("Enter the horse's age (whole numbers only)"); age = keyboard.nextInt(); System.out.println("Enter height of horse in hands (whole numbers only)"); height = keyboard.nextInt(); System.out.println("Enter the saddle type for this horse (English/Western)"); saddleType = keyboard.next(); keyboard.nextLine(); System.out.println("Enter the breed of this horse e.g. Thoroughbred"); breed = keyboard.nextLine(); The purpose of the line keyboard.nextLine() is to clear line. This is not always necessary but for the breed you can use spaces when typing the breed of the horse. 11 This can some cause problems with inputting different data types, a string with no spaces, and a string that can include spaces. 7. Next we'll pass the values of the variables to the SaddleHorse class' methods. Type: horse1.setIdNo(idNo); horse1.setCoatColor(color); horse1.setAge(age); horse1.setHeight(height); horse1.setSaddleType(saddleType); horse1.setSaddleBreed(breed); 8. We repeat this for the DraftHorse object. Type: System.out.printf("%100s%n%n", "Let's enter a Draft Horse"); System.out.println("Enter the horse's id number (whole numbers only)"); idNo = keyboard.nextInt(); System.out.println("Enter the horse's coat color e.g. Roan"); color = keyboard.next(); System.out.println("Enter the horse's age (whole numbers only)"); age = keyboard.nextInt(); System.out.println("Enter height of horse in hands (whole numbers only)"); height = keyboard.nextInt(); System.out.println("Enter the classification for this horse (Harness/Saddle)"); classification = keyboard.next(); keyboard.nextLine(); System.out.println("Enter the breed of this horse e.g. Percheron"); breed = keyboard.nextLine(); horse2.setIdNo(idNo); horse2.setCoatColor(color); horse2.setAge(age); horse2.setHeight(height); 12 horse2.setClassification(classification); horse2.setDraftBreed(breed); 9. We end the program by displaying the result for both horses. Type: System.out.println(" Let's display the information on the Saddle Horse"); System.out.println(horse1.tellAboutHorse()); System.out.println(" Let's display the information on the Draft Horse"); System.out.println(horse2.tellAboutHorse()); Notice that we invoked the tellAboutHorse() method for both objects. This will call the method from either the SaddleHorse or DraftHorse class depending on the object type. 10. Save your changes and fix any errors if necessary. 11. Run and test your program. 12. Compress the entire project folder into a single zip or rar file and submit
Explanation / Answer
I have coded according to your details. I cannot attach a zip file so pasted the code below. Please add these classes to eclipse. Change the comment before classes according to need. Run the code as per your need and submit. I have also included an output from sample run
Below are your classes: -
Horse.java
/**
* Name: - Date: - Purpose: - develop a class that contains basic attributes for
* all show horses
*/
public class Horse {
// Declarations
private int idNo;
private String coatColor;
private int age;
private double height;
// constructor methods
public Horse() {
}
// overloaded constructor
public Horse(int anIdNo, String aCoatColor, int anAge, double aHeight) {
setIdNo(anIdNo);
setCoatColor(aCoatColor);
setAge(anAge);
setHeight(aHeight);
}
public int getIdNo() {
return idNo;
}
public void setIdNo(int idNo) {
this.idNo = idNo;
}
public String getCoatColor() {
return coatColor;
}
public void setCoatColor(String coatColor) {
this.coatColor = coatColor;
}
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;
}
// custom method
public String tellAboutHorse() {
// returns values of attributes as one string
String horseDetails;
horseDetails = String.format("%n%n%s%n%d%n%n%s%n%s%n%n%s%n%d%n%n%s%n%.1f %s", "ID number: ", idNo,
"Coat Color: ", coatColor, "Age: ", age, "Height: ", height, " hands ");
return horseDetails;
}
}
DraftHorse.java
/**
* Name: - Date: - Purpose: - It will add attributes specific to Draft horses
*/
public class DraftHorse extends Horse {
// additional attributes beyond those inherited from Horse
private String classification;
private String draftBreed;
public DraftHorse() {
}
public DraftHorse(int anIdNo, String aCoatColor, int anAge, double aHeight, String aClassification,
String aDraftBreed) {
// invoke super class constructor
super(anIdNo, aCoatColor, anAge, aHeight);
// set subclass attribute values
setClassification(aClassification);
setDraftBreed(aDraftBreed);
}
public String getClassification() {
return classification;
}
public void setClassification(String classification) {
this.classification = classification;
}
public String getDraftBreed() {
return draftBreed;
}
public void setDraftBreed(String draftBreed) {
this.draftBreed = draftBreed;
}
public String tellAboutHorse() {
// returns values of attributes as one string
String horseDetails;
horseDetails = String.format("%s%n%n%s%n%s%n%n%s%n%s", super.tellAboutHorse(), "Classification: ",
classification, "Draft Breed: ", draftBreed);
return horseDetails;
}
}
SaddleHorse.java
/**
* Name: - Date: - Purpose: - It will add attributes specific to Saddle horses
*/
public class SaddleHorse extends Horse {
private String saddleType;
private String saddleBreed;
public SaddleHorse() {
}
// constructor
public SaddleHorse(int anIdNo, String aCoatColor, int anAge, double aHeight, String aSaddleType,
String aSaddleBreed) {
super(anIdNo, aCoatColor, anAge, aHeight);
setSaddleType(aSaddleType);
setSaddleBreed(aSaddleBreed);
}
public String getSaddleType() {
return saddleType;
}
public void setSaddleType(String saddleType) {
this.saddleType = saddleType;
}
public String getSaddleBreed() {
return saddleBreed;
}
public void setSaddleBreed(String saddleBreed) {
this.saddleBreed = saddleBreed;
}
public String tellAboutHorse() {
// returns values of attributes as one string
String horseDetails;
horseDetails = String.format("%s%n%n%s%n%s%n%n%s%n%s", super.tellAboutHorse(), "Saddle Type: ", saddleType,
"Saddle Breed: ", saddleBreed);
return horseDetails;
}
}
Ch10Lab1.java
import java.util.Scanner;
/**
* Name: - Date: - Purpose: - To test our classes.
*/
public class Ch10Lab1 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
SaddleHorse horse1 = new SaddleHorse();
DraftHorse horse2 = new DraftHorse();
int idNo, age, height;
String color, saddleType, breed, classification;
System.out.printf("%100s%n%n", "Let's enter a Saddle Horse");
System.out.println("Enter the horse's id number (whole numbers only)");
idNo = keyboard.nextInt();
System.out.println("Enter the horse's coat color e.g. Roan");
color = keyboard.next();
System.out.println("Enter the horse's age (whole numbers only)");
age = keyboard.nextInt();
System.out.println("Enter height of horse in hands (whole numbers only)");
height = keyboard.nextInt();
System.out.println("Enter the saddle type for this horse (English/Western)");
saddleType = keyboard.next();
keyboard.nextLine();
System.out.println("Enter the breed of this horse e.g. Thoroughbred");
breed = keyboard.nextLine();
horse1.setIdNo(idNo);
horse1.setCoatColor(color);
horse1.setAge(age);
horse1.setHeight(height);
horse1.setSaddleType(saddleType);
horse1.setSaddleBreed(breed);
System.out.printf("%100s%n%n", "Let's enter a Draft Horse");
System.out.println("Enter the horse's id number (whole numbers only)");
idNo = keyboard.nextInt();
System.out.println("Enter the horse's coat color e.g. Roan");
color = keyboard.next();
System.out.println("Enter the horse's age (whole numbers only)");
age = keyboard.nextInt();
System.out.println("Enter height of horse in hands (whole numbers only)");
height = keyboard.nextInt();
System.out.println("Enter the classification for this horse (Harness/Saddle)");
classification = keyboard.next();
keyboard.nextLine();
System.out.println("Enter the breed of this horse e.g. Percheron");
breed = keyboard.nextLine();
horse2.setIdNo(idNo);
horse2.setCoatColor(color);
horse2.setAge(age);
horse2.setHeight(height);
horse2.setClassification(classification);
horse2.setDraftBreed(breed);
System.out.println(" Let's display the information on the Saddle Horse");
System.out.println(horse1.tellAboutHorse());
System.out.println(" Let's display the information on the Draft Horse");
System.out.println(horse2.tellAboutHorse());
}
}
Sample Run : -
Let's enter a Saddle Horse
Enter the horse's id number (whole numbers only)
203
Enter the horse's coat color e.g. Roan
Red
Enter the horse's age (whole numbers only)
12
Enter height of horse in hands (whole numbers only)
13
Enter the saddle type for this horse (English/Western)
English
Enter the breed of this horse e.g. Thoroughbred
Through
Let's enter a Draft Horse
Enter the horse's id number (whole numbers only)
204
Enter the horse's coat color e.g. Roan
Brown
Enter the horse's age (whole numbers only)
26
Enter height of horse in hands (whole numbers only)
23
Enter the classification for this horse (Harness/Saddle)
Saddle
Enter the breed of this horse e.g. Percheron
Percheron
Let's display the information on the Saddle Horse
ID number:
203
Coat Color:
Red
Age:
12
Height:
13.0 hands
Saddle Type:
English
Saddle Breed:
Through
Let's display the information on the Draft Horse
ID number:
204
Coat Color:
Brown
Age:
26
Height:
23.0 hands
Classification:
Saddle
Draft Breed:
Percheron