CIS 340 E13 Exercise PDF

Title CIS 340 E13 Exercise
Course Business Information System Development II
Institution Arizona State University
Pages 6
File Size 448.1 KB
File Type PDF
Total Downloads 33
Total Views 166

Summary

Exercise 13 homework template for CIS340
Does not contain answers...


Description

CIS 340 – Business Information Systems Development I Exercise 13 (E13)

Purpose Learn how to …    

Implement a class with instance variables Implement encapsulation with getters and setters Create object instances from classes Use instance methods, getters, and setters

The chart below provides an overview of where this exercise fits in the sequence of topics in class (see redcolored items).

Topic Chart

© Copyright Arizona Board of Regents | K. Roumina

1

Description You will be using the Eclipse IDE for your Java programming. Overview In this exercise, you will create a “model” (or a representation) of a college class named Course, using ObjectOriented Design. The model will be in the form of a Java Class. As previously discussed, a Class is a template or a blueprint for the type of information that needs to be modeled/maintained for a real-world object. Classes become available as a new datatype! The Course Class will maintain (keep track of) several pieces of data: the prefix of the course, the number of the course, the college name, etc. Blueprints or models are just plans. To use them in reality, you need to manufacture items based on those plans/designs. In the programming environment, the equivalent of “manufacturing items” is “instantiating objects” based on classes. Here, you will create a CourseApp Class in which you will instantiate (i.e. create) two objects, courseOne and courseTwo based on the Course Class, and utilize these objects to achieve the desired objective. This brief exercise demonstrates the reusability of Object-Oriented Design – once you create the Course Class, you will be able to seamlessly create many objects of type Course without any additional work. Sample Output

Note: The line before last should say “A new Course has been created for SCM 300 at ASU.”

© Copyright Arizona Board of Regents | K. Roumina

2

Instructions Create a Java class named CourseApp containing a main()method. Add another Java class to the project and name it Course. Do NOT add a main()method to this second class. There can be only one main() method in the entire project. In the Course.java class, you will develop Java code for the following UML Class Diagram, which models a Course. For this exercise, the instructions that follow are an annotated version of the UML diagram. However, you should be able to look at the UML Diagram and develop the entire Java code without further instructions. Take a few minutes and study this diagram.

Figure 1. Course Class UML Diagram

Create the following “getter” and “setter” (i.e. mutator) methods to manage the “state” of the object. Creating fields is usually a combination of two tasks: 1) declare the field, and 2) define its “getter” and “setter” methods. 1. Prepare the CoursePrefix field: a. Declare a private instance variable of type String called coursePrefix. It will be non-static. b. Create a getter method for the field. The getter returns the value of the coursePrefix. However, return the coursePrefix value in all uppercase! c. Create a setter method for the field. The setter assigns the value of the parameter variable to the private instance variable. Make sure you use the “this” keyword.

© Copyright Arizona Board of Regents | K. Roumina

3

2. Prepare the CourseNumber field: a. Declare a private instance variable of type integer called courseNumber. It will be non-static. b. Create a getter method for the field. The getter returns courseNumber. c. Create a setter method for the field. The setter assigns the parameter variable to the private instance variable. Make sure you use the “this” keyword. 3. Prepare the CollegeName field: a. Declare a private static variable of type String called collegeName. b. Create the getter for the field. The getter returns collegeName. c. Create a setter for the field. The setter assigns the parameter variable to the private instance variable. Since collegeName is static, you cannot use “this”. Instead, use the class name to access the static field. Using class name with the field name is known as using the fully qualified name of the variable. 4. Prepare the ClassAverage field: a. Declare a private instance variable of type double called classAverage (non-static). Initialize it to 0.0. b. Create the getter for the field. The getter returns classAverage. Having no setter will make this field read-only to outside client classes. Methods in client classes will not be able to modify the value of classAverage. We won’t be implementing logic for the average, but want to show a static field example that could be implemented later.

Create the following Methods to manage the “behavior” of the object. 

Create a public method, named displayMessage. o The method should print out a one-line message to the Console, e.g., “Welcome to CIS 340”. The course name should not be hardcoded. The method should use a placeholder and use the coursePrefix and courseNumber getters to write the course name. This way it will use the values of the fields in the object, resulting in accurate course names being generated for all objects of type Course.

Within the main()method of the CourseApp class: 1. Declare two object references, courseOne and courseTwo of type Course. These should be done in a manner similar to how it is demonstrated in class. e.g., Classname objectReference; // data type followed by name of object Do the following separately for each of your two Course object references.

© Copyright Arizona Board of Regents | K. Roumina

4

2. Instantiate a new Course object. Store the resulting reference in your Course object reference. e.g., objectReference = new ClassName(); // instantiate a new “ClassName” and assign it to the “objectReference” 3. Set the coursePrefix and courseNumber using the setter. This means invoke the setter method and supply the values as arguments. Ask user for input using a nextLine() and store the course prefix and course number in the course object. 4. Set the college name using the setter. Do not ask the user. Hardcode “ASU”. Look up whether the college name is “static” or “non-static”. How you access the setter for college name depends on whether it is static or non-static. 5. Similarly, try setting the class average to 85. Does it let you? Why / Why not? 6. Print the course prefix, number and the college name to the Console in the format. Use the getter to get access to the fields. e.g., “A new Course was created for CIS 425 at ASU” The way to use the getter is shown below. Type in the name of the object reference and access the getter after placing a dot. Include parentheses because getters are methods! Keep in mind that getters are not printing anything to the Console. They will merely return a value. You will have to process the returned value and print it to the Console here. e.g. objectReference.getFieldName(); 7. Call the displayMessage() method for the objects, e.g., objectReference.methodName();

Once you are done and out of curiosity…   

Set either courseNumber or coursePrefix to static and run the application again. What happens? Set one of the getters/setters to private. What happens when you run the program? There is nothing to turn in for these “curiosity” points!

© Copyright Arizona Board of Regents | K. Roumina

5

Deliverables All CIS 340 submissions must adhere to standards detailed in the following documents (available on the course site) for full credit. • • •

CIS 340 Assignment Submission Instructions CIS 340 Programming Conventions CIS 340 Commenting Guide

Note: Assignments will be scored on source code AND output, with the emphasis on the source code.

© Copyright Arizona Board of Regents | K. Roumina

6...


Similar Free PDFs