Upload to Canvas Page 1 > of 11 Computer Science The goal of this lab is...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Upload to Canvas Page 1 > of 11 Computer Science The goal of this lab is two-fold. First, you'll gain experience in writing a class, and creating several instances of an object defined in that class. And, you'll learn about Eclipse, another IDE. I. Writing a class Dog.java Recall from lecture that a class contains fields, and several methods, that manipulate and have access to the values stored in the fields. For this part of the lab, you'll write a class, Dog. java, that is meant to describe a dog. Although there are many characteristics of a dog that distinguish it from another dog, for this lab, each instance of a Dog object will contain only four fields, height In, age Years, name, and likesBarking where height In refers to the height of a dog, in inches, and age Years is the age of the dog, name is the name of the dog, likesBarking is whether the dog likes barking. To complete this portion of the lab: 1. Create a new java file, using jGRASP. 2. As you've already done with several program for your programming assignments, write a new class, using the access specifier public, the java keyword class, and the name Dog: public class Dog { } 3. Save the file as Dog.java. 4. 5. Add four fields to your class (refer to the lecture slides, for a sample). By convention, the fields are placed in the class file before the methods are written. The four fields should be: • height In, which should be private, and of type double age Years, which should be private, and of type integer name, which should be private, and of type String likesBarking, which should be private, and of type boolean Now, on to add the methods. Recall that methods in a class are usually public, and are often used to manipulate the private fields. Methods that set the values of fields are referred to as setter methods, while methods that retrieve the values of fields are referred to as getter methods. As a first step, write the setter method that will update the value of the height In field. Recall that a method declaration begins with modifiers, followed by the return type, then the name, and finally the parameter list. For example, a method that is public, returns an integer, is called calcExponent (), and receives two parameters, the first an integer and the second a double, would be written as the following: (Method Parameters) Method Modifiers Return type Method Name Tatiana Harrison, CWU, Lab 7: classes, and Eclipse Page 1 of 11 อ ZOOM + The setter method for the field height Int should be public, return nothing, be called setHeight, and receive a single argument, of type double. And, the method should update the value of the field height In, using the value that the method receives. Thus: public void setHeight (double height) { height In height; Page 2 > of 11 6. 7. 8. 9. Similarly, write the setter method for the ageYears field: public void setAge (int age) { } age Years = age; Write the setter methods for the name and likes Barking fields. (not shown) Now that you've written the setter methods, write the getter methods, which are used to retrieve the values of the fields. There are four getter methods that you should write, getHeight(), getAge (), getName() and getLikesBarking (). The first one, getHeight(), should be: public double getHeight() { } return height In; Now write the getAge (), getName() and getLikes Barking () methods, into your Dog.java class. They are very similar to the getHeight method. At this point, that's it. You've written a class, Dog.java, that is meant to store information about a dog. Compile the class and fix any syntax errors that you may have. Note that if you run the compiled class, nothing will happen, because the java code that you've written, does not contain a main method, which is where all java programs begin. II. Writing a DogKennel.java program, that uses the Dog.java class Now that you've written the Dog.java class, you can create objects of that type. This is similar to what was shown in lecture, with the Bicycle.java class example. For this part of the lab, you'll write a main method that creates three objects of type Dog. You'll use the setter methods to set the private fields of the Dog objects, and the getter methods to retrieve information about the dogs. To complete this portion of the lab: 1. Create a new java file, using jGRASP, and call it Dog Kennel.java. The skeleton of the file should be the following: public class DogKennel { public static void main(String[] args) { } Tatiana Harrison, CWU, Lab 7: classes, and Eclipse Page 2 of 11 อ ZOOM + Page 3 > of 11 2. Begin your program by issuing a System.out.println statement, so that the following text is printed to the screen, when your program is run: Welcome to the dog kennel 3. 4. 5. As was shown in lecture, now that you have a class, Dog, that you've written, you can create objects of that type. For this part of the lab, create three objects of type Dog. Remember that you need to create an object of type Dog using the java keyword new, and assign the object to a reference variable, which should be a newly created instance of an object of type Dog: Dog dog1 new Dog (); Dog dog2 = new Dog(); Dog dog3 = new Dog (); At this point, you have three reference variables, dog1, dog2, and dog3, that refer to three just- created objects of type Dog. However, the fields of those objects are empty, but you can set their values using the setter methods, that you've defined in the Dog class. To illustrate, to set the value of the height In field in the first Dog object (dog1) that you've created, you'd write: dog1.setHeight (13.5); Recall that the "dot" notation is used to invoke a method for a specific object. In the above code, you are invoking the setHeight method of the first Dog object that you've created. Similarly, to set the value of the name, age and likesBarking fields of the first Dog object, you'd write: dog1.setAge (8); dog1.setName("Igor"); dog1.setLikes Barking (true); Update the fields of the second and third Dog objects that you've created. Here are the heights, ages, and names of the Dog objects that were used to create the output shown in Figure 1 (see below). dog1 dog2 dog3 6. age height name likesBarkiing 8 13.5 Igor true 5 8.2 Lev false 6 15.1 Tatsie true 7. At this point, you have created three objects of type Dog, and you've set the fields of those objects. Next, you can use the getter methods, to retrieve the information about each Dog object. The getter methods are used the same way as the setter methods, in that you should use the dot notation. All three getter methods that you've written, getHeight, getAge, and getName, receive as input no arguments, but return values of type double, int, and String. So when you invoke those methods, you can use their return values to update a variable. For this part, compute the sum of the ages of the three Dog objects, and print the sum to the screen. You can achieve that in just two statements: int sumOfAges = dog1.getAge () + dog2.getAge () + dog3.getAge(); System.out.println("The sum of the dogs' ages is " + sumOfAges); Use the instructions in step 6 as a sample, and also sum the heights of the three dogs, and print that value Page 3 of 11 Tatiana Harrison, CWU, Lab 7: classes, and Eclipse อ ZOOM + 5. Update the fields of the second and third Dog objects that you've created. Here are the heights, ages, and names of the Dog objects that were used to create the output shown in Figure 1 (see below). dog1 dog2 dog3 age height name likesBarkiing 8 13.5 Igor true 5 8.2 Lev false 6 15.1 Tatsie true 6. 7. At this point, you have created three objects of type Dog, and you've set the fields of those objects. Next, you can use the getter methods, to retrieve the information about each Dog object. The getter methods are used the same way as the setter methods, in that you should use the dot notation. All three getter methods that you've written, getHeight, getAge, and getName, receive as input no arguments, but return values of type double, int, and String. So when you invoke those methods, you can use their return values to update a variable. For this part, compute the sum of the ages of the three Dog objects, and print the sum to the screen. You can achieve that in just two statements: int sumOfAges = dog1.getAge () + dog2.getAge () + dog3.getAge (); System.out.println("The sum of the dogs' ages is " + sumOfAges); Use the instructions in step 6 as a sample, and also sum the heights of the three dogs, and print that value Tatiana Harrison, CWU, Lab 7: classes, and Eclipse Page 3 of 11 8. 9. 10. to the screen. Use the getName() method, to retrieve the name of each Dog object, and use a System.out.println to print that information to the screen. Finally, use the getLikesBarking () method for each dog to determine how many dogs like barking. Print "n dogs like barking", where n is the number of dogs that have likesBarking to set to true. Compile your program (fix any syntax errors), and run it. If done correctly, the output should be very similar to what is shown in Figure 1. With that, you are done with that programming part of this lab, and you've written (and used three times), your own class, Dog.java. Welcome to the dog kennel The sum of the dogs' ages is 19 The sum of the dogs' heights is 36.8 The dogs are: Igor, Lev, Tatsie 2 dogs like barking Figure 1: Sample invocation of the program DogKennel.java Page 4 > of 11 อ ZOOM + 19-08- Package Explorer Lab7FirstEclipseProgram Press Esc to exit full screen MyFirstEclipselavaProject.j MyFirstEclipselavaProject.java × O public class MyFirstEclipse JavaProject (default package) JRE System Library [JavaSE-1.7] } public static void main(String[] args) { System.out.println("Hello"); System.out.println("Hello"); System.out.pr println("Hello"); Javadoc Declaratio Console Stammsited: MyFirstEclipselavaProject [Java ogram Files\Jar Hello Hello Hello Writable Smart Insert 11:2 Figure 14: The run button, the console window, and the output of a program in Eclipse 14. Once your program compiles, you are done. 15. From now on, you can use Eclipse or jGRASP to complete your labs and/or homework assignments. What to hand in Make sure to upload the following two files to Canvas zipped: Dog.java DogKennel.java Be sure that each.java file is commented and be sure to include your name at the top of each file. Code must be indented, so that it is easy to read. Finally, make sure that you have given your variables good names. V. Rubric File/task Points Dog.java compiles and all fields and methods have been included 45 DogKennel.java compiles and correctly creates three objects of type Dog. The setter and getter methods are used correctly, and output is similar to what is shown in Figure 1. All .java files are commented, and contain your name and date, code formatted properly Variable names are adequate and descriptive in all java files 45 5 5 Image of MyFirstEclipse JavaProject compiled in Eclipse uploaded 0 Total 100 Tatiana Harrison, CWU, Lab 7: classes, and Eclipse Page 11 of 11 Page 11 > of 11 ☑ ZOOM + Upload to Canvas Page 1 > of 11 Computer Science The goal of this lab is two-fold. First, you'll gain experience in writing a class, and creating several instances of an object defined in that class. And, you'll learn about Eclipse, another IDE. I. Writing a class Dog.java Recall from lecture that a class contains fields, and several methods, that manipulate and have access to the values stored in the fields. For this part of the lab, you'll write a class, Dog. java, that is meant to describe a dog. Although there are many characteristics of a dog that distinguish it from another dog, for this lab, each instance of a Dog object will contain only four fields, height In, age Years, name, and likesBarking where height In refers to the height of a dog, in inches, and age Years is the age of the dog, name is the name of the dog, likesBarking is whether the dog likes barking. To complete this portion of the lab: 1. Create a new java file, using jGRASP. 2. As you've already done with several program for your programming assignments, write a new class, using the access specifier public, the java keyword class, and the name Dog: public class Dog { } 3. Save the file as Dog.java. 4. 5. Add four fields to your class (refer to the lecture slides, for a sample). By convention, the fields are placed in the class file before the methods are written. The four fields should be: • height In, which should be private, and of type double age Years, which should be private, and of type integer name, which should be private, and of type String likesBarking, which should be private, and of type boolean Now, on to add the methods. Recall that methods in a class are usually public, and are often used to manipulate the private fields. Methods that set the values of fields are referred to as setter methods, while methods that retrieve the values of fields are referred to as getter methods. As a first step, write the setter method that will update the value of the height In field. Recall that a method declaration begins with modifiers, followed by the return type, then the name, and finally the parameter list. For example, a method that is public, returns an integer, is called calcExponent (), and receives two parameters, the first an integer and the second a double, would be written as the following: (Method Parameters) Method Modifiers Return type Method Name Tatiana Harrison, CWU, Lab 7: classes, and Eclipse Page 1 of 11 อ ZOOM + The setter method for the field height Int should be public, return nothing, be called setHeight, and receive a single argument, of type double. And, the method should update the value of the field height In, using the value that the method receives. Thus: public void setHeight (double height) { height In height; Page 2 > of 11 6. 7. 8. 9. Similarly, write the setter method for the ageYears field: public void setAge (int age) { } age Years = age; Write the setter methods for the name and likes Barking fields. (not shown) Now that you've written the setter methods, write the getter methods, which are used to retrieve the values of the fields. There are four getter methods that you should write, getHeight(), getAge (), getName() and getLikesBarking (). The first one, getHeight(), should be: public double getHeight() { } return height In; Now write the getAge (), getName() and getLikes Barking () methods, into your Dog.java class. They are very similar to the getHeight method. At this point, that's it. You've written a class, Dog.java, that is meant to store information about a dog. Compile the class and fix any syntax errors that you may have. Note that if you run the compiled class, nothing will happen, because the java code that you've written, does not contain a main method, which is where all java programs begin. II. Writing a DogKennel.java program, that uses the Dog.java class Now that you've written the Dog.java class, you can create objects of that type. This is similar to what was shown in lecture, with the Bicycle.java class example. For this part of the lab, you'll write a main method that creates three objects of type Dog. You'll use the setter methods to set the private fields of the Dog objects, and the getter methods to retrieve information about the dogs. To complete this portion of the lab: 1. Create a new java file, using jGRASP, and call it Dog Kennel.java. The skeleton of the file should be the following: public class DogKennel { public static void main(String[] args) { } Tatiana Harrison, CWU, Lab 7: classes, and Eclipse Page 2 of 11 อ ZOOM + Page 3 > of 11 2. Begin your program by issuing a System.out.println statement, so that the following text is printed to the screen, when your program is run: Welcome to the dog kennel 3. 4. 5. As was shown in lecture, now that you have a class, Dog, that you've written, you can create objects of that type. For this part of the lab, create three objects of type Dog. Remember that you need to create an object of type Dog using the java keyword new, and assign the object to a reference variable, which should be a newly created instance of an object of type Dog: Dog dog1 new Dog (); Dog dog2 = new Dog(); Dog dog3 = new Dog (); At this point, you have three reference variables, dog1, dog2, and dog3, that refer to three just- created objects of type Dog. However, the fields of those objects are empty, but you can set their values using the setter methods, that you've defined in the Dog class. To illustrate, to set the value of the height In field in the first Dog object (dog1) that you've created, you'd write: dog1.setHeight (13.5); Recall that the "dot" notation is used to invoke a method for a specific object. In the above code, you are invoking the setHeight method of the first Dog object that you've created. Similarly, to set the value of the name, age and likesBarking fields of the first Dog object, you'd write: dog1.setAge (8); dog1.setName("Igor"); dog1.setLikes Barking (true); Update the fields of the second and third Dog objects that you've created. Here are the heights, ages, and names of the Dog objects that were used to create the output shown in Figure 1 (see below). dog1 dog2 dog3 6. age height name likesBarkiing 8 13.5 Igor true 5 8.2 Lev false 6 15.1 Tatsie true 7. At this point, you have created three objects of type Dog, and you've set the fields of those objects. Next, you can use the getter methods, to retrieve the information about each Dog object. The getter methods are used the same way as the setter methods, in that you should use the dot notation. All three getter methods that you've written, getHeight, getAge, and getName, receive as input no arguments, but return values of type double, int, and String. So when you invoke those methods, you can use their return values to update a variable. For this part, compute the sum of the ages of the three Dog objects, and print the sum to the screen. You can achieve that in just two statements: int sumOfAges = dog1.getAge () + dog2.getAge () + dog3.getAge(); System.out.println("The sum of the dogs' ages is " + sumOfAges); Use the instructions in step 6 as a sample, and also sum the heights of the three dogs, and print that value Page 3 of 11 Tatiana Harrison, CWU, Lab 7: classes, and Eclipse อ ZOOM + 5. Update the fields of the second and third Dog objects that you've created. Here are the heights, ages, and names of the Dog objects that were used to create the output shown in Figure 1 (see below). dog1 dog2 dog3 age height name likesBarkiing 8 13.5 Igor true 5 8.2 Lev false 6 15.1 Tatsie true 6. 7. At this point, you have created three objects of type Dog, and you've set the fields of those objects. Next, you can use the getter methods, to retrieve the information about each Dog object. The getter methods are used the same way as the setter methods, in that you should use the dot notation. All three getter methods that you've written, getHeight, getAge, and getName, receive as input no arguments, but return values of type double, int, and String. So when you invoke those methods, you can use their return values to update a variable. For this part, compute the sum of the ages of the three Dog objects, and print the sum to the screen. You can achieve that in just two statements: int sumOfAges = dog1.getAge () + dog2.getAge () + dog3.getAge (); System.out.println("The sum of the dogs' ages is " + sumOfAges); Use the instructions in step 6 as a sample, and also sum the heights of the three dogs, and print that value Tatiana Harrison, CWU, Lab 7: classes, and Eclipse Page 3 of 11 8. 9. 10. to the screen. Use the getName() method, to retrieve the name of each Dog object, and use a System.out.println to print that information to the screen. Finally, use the getLikesBarking () method for each dog to determine how many dogs like barking. Print "n dogs like barking", where n is the number of dogs that have likesBarking to set to true. Compile your program (fix any syntax errors), and run it. If done correctly, the output should be very similar to what is shown in Figure 1. With that, you are done with that programming part of this lab, and you've written (and used three times), your own class, Dog.java. Welcome to the dog kennel The sum of the dogs' ages is 19 The sum of the dogs' heights is 36.8 The dogs are: Igor, Lev, Tatsie 2 dogs like barking Figure 1: Sample invocation of the program DogKennel.java Page 4 > of 11 อ ZOOM + 19-08- Package Explorer Lab7FirstEclipseProgram Press Esc to exit full screen MyFirstEclipselavaProject.j MyFirstEclipselavaProject.java × O public class MyFirstEclipse JavaProject (default package) JRE System Library [JavaSE-1.7] } public static void main(String[] args) { System.out.println("Hello"); System.out.println("Hello"); System.out.pr println("Hello"); Javadoc Declaratio Console Stammsited: MyFirstEclipselavaProject [Java ogram Files\Jar Hello Hello Hello Writable Smart Insert 11:2 Figure 14: The run button, the console window, and the output of a program in Eclipse 14. Once your program compiles, you are done. 15. From now on, you can use Eclipse or jGRASP to complete your labs and/or homework assignments. What to hand in Make sure to upload the following two files to Canvas zipped: Dog.java DogKennel.java Be sure that each.java file is commented and be sure to include your name at the top of each file. Code must be indented, so that it is easy to read. Finally, make sure that you have given your variables good names. V. Rubric File/task Points Dog.java compiles and all fields and methods have been included 45 DogKennel.java compiles and correctly creates three objects of type Dog. The setter and getter methods are used correctly, and output is similar to what is shown in Figure 1. All .java files are commented, and contain your name and date, code formatted properly Variable names are adequate and descriptive in all java files 45 5 5 Image of MyFirstEclipse JavaProject compiled in Eclipse uploaded 0 Total 100 Tatiana Harrison, CWU, Lab 7: classes, and Eclipse Page 11 of 11 Page 11 > of 11 ☑ ZOOM +
Expert Answer:
Answer rating: 100% (QA)
class Dog public class Dog class variables double heightIn int a... View the full answer
Related Book For
Posted Date:
Students also viewed these programming questions
-
Data set Theory Assume an informational record with one association parent including matches (a, b) where a can't try not to be a parent of b. (a) Write a Datalog demand which gives the graph of...
-
A company has 10,000 employees. Each employee is entitled to twenty days of paid holiday per calendar year. Up to five days of this entitlement may be carried forward and taken in the following year...
-
Jyoti Inc. had outstanding $10 million of 8% bonds (interest payable March 31 and October 31) due in 12 years. Jyoti was able to reduce its risk rating through investing in more real estate. As a...
-
List the point coordinates for all atoms that are associated with the FCC unit cell(figure). (b) (a) (c)
-
Distinguish between the term 'change of state', 'path', and 'process'.
-
Kokomochi is considering the launch of an advertising campaign for its latest dessert product, the Mini Mochi Munch. Kokomochi plans to spend $5 million on TV, radio, and print advertising this year...
-
write a assignment as a business report to your manager using the Accounting Information System's Guidelines and Identifying Risks, Risk Statement, Classifications of Internal Controls, and the...
-
Refer to Exercise 13-4. Using the same information, assume that the Brazilian real is identified as the functional currency of the subsidiary. Required: A. Remeasure the account balances that are...
-
tutorial/theory for chemical shifts and descriptions of peak splitting. NMR There are 4 NMR problems. For each problem, you have been given the chemical formula as well as the IR and NMR spectra....
-
A loan was made in 40 months at 6% simple interest. The principal of the loan has just been repaid along with 800 of interest. Compute the principal amount of the loan
-
An economy has a population of 400 million with a labor force of 240 million. The number of unemployed is 20 million. What is the unemployment rate?
-
Answering Required: Introduction of process of budgetary control project Create the plan of project QUESTION Below are the steps in the Process of Budgetary Control. Choose a project you had from...
-
A glass rod rubbed against silk gains a charge of -7.4 C. What is the electric force between the rod and the silk when the two are separated by a distance of 11.6 cm? (Assume that the charges are...
-
At the beginning of each year in Guelph's arboretum, biologists add 1,000 frogs to the population and at the end of the year, biologists harvest 5% of the population. Let the difference equation that...
-
3 lim 1 (a) e4 (b) e-2 (c) 0 (d) 1 (e) none of these
-
Would you use the adjacency matrix structure or the adjacency list structure in each of the following cases? Justify your choice. a. The graph has 10,000 vertices and 20,000 edges, and it is...
-
Select an industry in which you are interested and select two companies within that industry. Obtain their annual reports by going to each companys website and downloading the report for the most...
-
Bretts Restaurant Supply is preparing its cash budgets for the first two months of the upcoming year. Here is the information about the companys upcoming cash receipts and cash disbursements: a....
-
Shark Rides manufactures snowboards. Roy Bettano, the CEO, is trying to decide whether to adopt a lean thinking model. He expects that adopting lean production would save $ 67,000 in warehousing...
-
Select an information system with which you are familiar, and which you feel needs to be improved, based upon your experiences as an employee, customer, other system user, or system owner, Switch...
-
You have learned the importance of making sure that requirements are correctly identified. Hut how do you know when you have a correct requirement-that is, what criteria must each requirement meet in...
-
Prepare a full feasibility analysis, including Economic, Operational, Schedule, Legal, and Technical analyses for the system you are suggesting in problem 1. Your analysis should be no more than 30...
Study smarter with the SolutionInn App