Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I need help with part F for drawing a UML class diagram for the Student class. I don't know how to create that diagram. Make
I need help with part F for drawing a UML class diagram for the Student class. I don't know how to create that diagram.
Make sure that you list all of the fields and methods in the diagram. Also, make sure that you use the correct notations.
Thanks for helping.
OVERVIEW In this lab, you are going to create a Student class. Think about (1) things that a university needs to know about students (these are its data attributes or characteristics that in object oriented programming are stored in what are called instance variables [We'll discuss why they're called "instance" variables later.]) and (2) things that a student needs to be able to do these are called methods). Instance variables and methods are the two main characteristics of every class like the Student class you are getting ready to create. 1) Part A: Create a Student class and add instance variables 2) Part B: Add constructors to Student class 3) Part C: Add getters and setters to Student class 4) Part D: Add more fields and methods to Student class 5) Part E: Add Javadoc comments to Student class 6) Part F: Draw UML diagram for Student class Part A: Create a Student class and add instance variables 1) Each instance variable is defined by its datatype and a unique name. Because instance variables are most often "things", we generally use nouns as instance variable names. Note that in Java the convention for variable names is what's known as camel case. That is, the first word of the variable identifier is all lower-case letters. If multiple words are used in the identifier, the first character of every word after the first is upper case like the hump on a camel). For example, a variable to hold your first name might have an identifier like firstName. A variable for the street of your address might just be street because it's only one word. A variable to hold your favorite movie might be named myavoriteMovie. The table below shows the instance variable tables in the Student class. Instance Variable Description First name Middle Initial Last Name Student ID Age Lives on campus Student Class Instance Variables Java Datatype Java Identifier Sering firstName char middle Inicial Sering lastName Sering studentid age boolean 1. veOnCampus Page 1 of 8 3) NetBeans will create a university Driver package and open a UniversityDriver.java source code file. 4) Expand the University Driver project, right-click the Source Packages icon, click New, then click Java Class. In the popup, name your class Student (in the Class Name box). Click the Package dropdown arrow and select universityDriver. Click Finish. Java creates this class, adds it to the universityDriver package, and opens the Student.java source code file. 5) The contents of the Student class are everything from the right of the opening curly brace that follows the word Student to the left of the closing curly brace two lines below. Those two curly braces define what's known as a block. Everything inside this block defines the Student class. One thing to get used to is that many Java constructs come in opening and closing pairs like parentheses (and curly braces { }. Having a starting curly brace without a corresponding closing one or a closing one without the corresponding starting one will cause an error. It's easy to do when writing large amounts of code. To avoid this, if I type an opening parenthesis or curly brace IIMMEDIATELY type the corresponding closing one (NetBeans does this for you automatically). Then I set my cursor back between the two and carry on. Mark my words, you're going to hate yourself when you get one of these errors after you've written a couple of hundred lines of code and have to find the single parenthesis or curly brace that's not matched properly. 6) Now start adding instance variable definitions to your student class. Click the blank line below public class Student {. Type one line of code for each of the Instance variables in your instance variables table above. In Java, variables defined at the class level are called fields. To enter a field definition, type the datatype first then the identifier then a semicolon. For example, the definition for the firstName field would look like this: e / 9 10 Cauthor Bruce 11 12 public class Student 13 String firstName; 14 15 7) Continue this until all your fields have been defined. 8) To create an object of the student class, type the following inside the UniversityDriver main method: Student hakim = new Student (); 9) Student () is a special method called a constructor. Constructors are a required part of every class. Their purpose is to initialize the instance variables of an object as soon as it's created. There are a couple of important things to know about constructors: They always have exactly the same name as the class. That's how you know they're a constructor. They don't have a return type. Constructors are required but you'll notice there's no method in our student class named Student. That's because Java includes what's known as a default constructor. You can write your own constructors (you will do so in Part B) but if you don't Java pretends you did and uses its own default constructor. Note that default constructor does not take any arguments. 10) Print out the instance variables of hakim by typing the following inside the main method: System.out.println(hakim. firstName); System.out.printin (hakim.middleInitial); System.out.println (hakim.lastName); System.out.println(hakim. StudentId); System.out.println (hakim.age); System.out.println (hakim.livesonCampus); 11) Check the output window, and you will see that all of these instance variables are printed with default value. This is because the default constructor initializes each field to default value: String type is null, char type is '\0', numeric type is 0, and boolean type is false. In the next step, you are going to add a non-default constructor to the Student class. Part B: Constructors In Part A, you created a Student object using the default constructor, which initializes all of the fields to default values. When you create a Student object, you may want to set the instance variables of that object - the fields - to values associated with the actual student this object represents. Since every student is different, we need a way to specify different values for each student. To do this, you need to have a constructor that takes parameters, which is non-default constructor. 1) In the student.java, below the fields, let's add a constructor with the following parameters in the parenthesis: String Name, char minitial, String IName, String ID, int studentAge, boolean islaveOnCampus Remember that the constructor has the same name as the class, and it does not have a return type. Inside the constructor, assign the fields to these parameters. 2) After adding this constructor, you will notice that your driver class has errors. Why? Because Java only provides a default constructor if no constructor is given. However, since you defined your own constructor now, the student class does not have a default constructor any more. To fix this, you can add a default constructor to the Student class. Remember that the default constructor does not take any argument. Furthermore, you may leave the body of the default constructor empty (still include the opening and closing curly brackets), and it initializes all of the fields to default values. After this, compile your project, the error message went away. You may run your program again, and it will give you the same output as before. Now there are two constructors in your student class, and they are overloaded. 3) Let's try to use the constructor with parameters to create another object: Student maria = new Student ("Maria", 'I', "Garces", "800555555", 19, true); 4) Add some print statements at the end of the main method to print out all of the fields of object john. Verify your constructor works properly. Part C: Getters and setters Encapsulation, sometimes referred to as data hiding, is an important concept of object-oriented programming. An object hides its internal data from code that is outside the class that the object is an instance of. Only the class's methods may directly access and make changes to the object's internal data. As a programmer, you can create a class definition, hide the fields and methods inside that definition, and set rules so that programmers who want to use your class have to follow the rules or be denied access. You can see how important something like this would be to prevent accidental access to information. 1) In Student.java, add keyword private in front of all of the fields. Compile your project, you will see errors in the main method. This is because the fields are private now, and one cannot access Part C: Getters and setters Encapsulation, sometimes referred to as data hiding, is an important concept of object-oriented programming. An object hides its internal data from code that is outside the class that the object is an instance of. Only the class's methods may directly access and make changes to the object's internal data. As a programmer, you can create a class definition, hide the fields and methods inside that definition, and set rules so that programmers who want to use your class have to follow the rules or be denied access. You can see how important something like this would be to prevent accidental access to information. 1) In Student.java, add keyword private in front of all of the fields. Compile your project, you will see errors in the main method. This is because the fields are private now, and one cannot access them directly from a different class. How to fix this? This leads us to special methods called getters and setters. Page 4 of 8 2) More precisely, getters are technically known as accessors because they access data, and setters are known as mutators because they mutate or change data. We're going to use the getter/setter terminology. First, create some simple getters and setters for your student class. 3) The convention for naming getters is the word "get" followed by the name of the field we're getting. The name for the firstName field getter would then be getFirstName. (Note how the method naming convention of camel case makes the getter method name just a tiny bit different from the name of the field you're getting.) Getters by their very nature must return some information to the calling program. The return datatype will be the same as the field's datatype. So the complete method definition for the getFirstName method would be: public String getrirstName() { return firstName; Typically, getters and setters are placed after the constructors although it's legal to place them in any valid location within the class definition. For consistency, let's follow that convention. 4) Now add a getter for each field in the Student class. 5) Fix the error in the main method by accessing each field for hakim and maria through the getter. 6) Setters are a little different because they change the value of the field, not finding out what's currently stored in it. This means that you need to provide a new value for the field. Normally this is done by passing in the new value as an argument. That means you have to define a parameter list for each setter method. Normally setters don't return a value so their return type is void. You do have to pass in some data and usually it's the same datatype as the field you're setting a new value of. Also, the naming convention for setter methods is the word "set" followed by the name of the field. Here's the setter for the firstName field, just copy it into your student class: public void setFirstName (String Name) { firstName = Name; 7) Now add a setter for each field in the Student class. 8) To ensure that your getters and setters work you should modify your driver class to test them. Back in your UniversityDriver class, comment out all the statements and add the following line: Student torrance = new Student ("Torrance", "U", "Deleon", "300234567", 19, false); Add the following lines after the one above: System.out.println("Object torrance first name torrance.getFirstName()); torrance. setFirstName ("Michel"); System.out.println("Object torrance first name torrance.getFirstName()); 9) What's going on here? By entering torrance.getFirstName(), you're asking Java to find the copy of the Student class file that belongs to the torrance object and call the getFirstName method that's stored there. If you'll look in the Student class definition you'll see that the get FirstName method returns whatever the value of that object's firstName field is. When you created the torrance object and ran the constructor you entered a value of "Torrance" for the first parameter, the one associated with the firstName field. So until you change it, the torrance object's firstName field will remain set to the string "Torrance". 10) Next, call the torrance. setFirstName ("Michel") method and pass it a value of "Michel". Inside the Student class definition, you see that the argument value for that method is stored in the firstName field thus replacing whatever was there before. So the torrance object's firstName field value becomes Michel". When you execute your third statement you retrieve the current value of the firstName field and print it out. If you run your program now you should see this (or something very like it): Object torrance first name is Torrance Object torrance first name is Michel 11) Add enough test statements to show that each getter/setter in the Student class works. Part D: Add more fields and methods to student dass 1) To keep track of each student's balance on the 49er card, you are going to add a double type field balance to the Student class. Initialize this field to o in both constructors. 2) Add a getter method for the balance field. 3) You are not going to include a setter method for the balance field. Instead, you are going to provide the following two methods: transferToCard payFromCard 4) Start by creating the transfertocard method. This method allows a Student object to transfer funds to their 49er card account. The bullet points below tell you what the method must do. public void transferrocard (double amount) If the amount is positive, add the amount to the balance and print out the new balance. o Otherwise, print out an error message "The transfer amount must be greater than 0" 5) Now create the payromCard method. This method allows Student object to pay with their 49er card account. The bullet points below tell you what the method must do. public void payFromCard (double amount) If the amount is less than or equal to balance, subtract the amount from the balance and print out the new balance. Otherwise, print out an error message "Sorry, your balance is too low!" 6) In the main method, add statements to test the newly added methods. You must test for all possible cases, i.e., valid input to cause the balance to be updated, and invalid input to cause error messages to be printed out. Part E: Add Javadoc comments to Student class Add a dar comment at the tan afvalace Thau chaula ineluda 4) When you are done, generate the Javadoc. Check the class summary, the method summary and the method details to ensure your comments were put into the Java Documentation correctly. Part F: Draw a UML diagram for Student class 1) Draw a UML class diagram for the Student class. Make sure that you list all of the fields and methods in the diagram. Also, make sure that you use the correct notations. Part G: Turn in your activity 1. Submit the following files: a. .java files. b. File with UML diagram You're done! If you are using the lab computer, remember to logout. So what did you learn in this lab? 1) How to create new classes in NetBeans 2) What instance variables are and how to add them to a class 3) What "public" means 4) How constructors are named 5) How the default constructor is created and used 6) How to write your own constructors 7) How getters and setters implement the concept of encapsulation 8) Naming conventions for getters and setters 9) Returning values from getter methods 10) Passing in values to setter methods 11) Testing getters and setters 12) How to write Javadoc for a class 13) How to draw UML class diagram Page 8 of 8
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started