Java project easy1
Computer Science 110 Introduction to Algorithms and Programming: Java
Programming Project #5 – Classes and Objects
Due Date: April 17, 2014
You will create 3 new classes for this project. The first class, named Book will
represent a printed book, like the textbook for this class. The second class, named
Student will represent a generic CSUN student. The third class will be one that you
invent on your own.
Design Your Classes First
Create three (3) UML diagrams for your three classes. Two of the classes are
assigned by the instructor (see the paragraph above) and one you must invent. Choose
as the third class, a singular noun that identifies something you know very well or
love doing. If soccer is your favorite sport, create a class named Soccer. If you surf,
create a class named Surfboard. Don’t overthink it, just come up with the 3 most
important data members (i.e. instance variables) that describe the class/object you
want to build and then write headers for 2 constructors, 3 get methods, 3 set
methods and a toString() method for each diagram.
Next to each UML diagram, decide on a default value, minimum value and a
maximum value for each NUMERIC variable. For example, a Shoe class might have
a variable called size. This size variable might have a default value of 0, a minimum
size of 1 and a maximum size of 15. A String variable might have a default value of
null and a minimum and maximum length for that String. For example, a Computer
class might have a variable called brand. This brand variable might have a default
value of null, a minimum string length of 3 and a maximum length of 20. Please DO
NOT use the same 3 data types for any classes’ global variables. That is, do not
make them all “int” or all “String”. It is acceptable to define two variables with the
same data type, so long as the third one has a different data type.
Implement (Code) Next
Once you have designed your classes on paper, create a jGrasp project named
Project5. Within Project5, create a Java file for EACH of the three classes. For
example, add to your project a new file called Book.java and then create the new class
from scratch in that file. Use your UML diagrams as the guideline for writing the
code. The variables and methods from the diagrams will be part of each of your new
classes. Make sure ALL your variables are declared to be private and do not make
them static. Also, make sure you follow the Java naming conventions listed in your
textbook by capitalizing the first letter of each word in the class name. It is important
to follow standardized naming conventions to make your programs easy to read.
Protect Your Data!
Objects store data or information! All class-level variables should be declared as
private, to protect data from access by code outside of the class. Think back to the
BankAccount example that was demonstrated in lecture—the public variables in that
class permitted a main() method in another class to “reach in” and take $2 million out
of myAccount. Do not make the same mistake; ALWAYS protect the data in your
objects by making them private.
Also, never allow bad data to be stored in your objects! In each “set” method,
make sure the value passed to the method is within a range, greater than or equal to a
minimum and less than or equal to a maximum. For Strings, you may check the
length of the String. Each “set” method should have some sort of “if-else” statement
which assigns the data when it is good and print an informative message when an
incorrect value is passed. A Shoe class setSize() method would assign the value “10”
to the size global variable when it is passed to the method, but it would print a
message stating “Shoe size must be between 1 and 15” and NOT change the global
variable when values like “437” are passed to the method. The private variable
declarations build a wall around your data, and the “set” methods are the
“gatekeepers” that allow only “good” information in. Your constructor that takes
arguments and assigns values to global variables should use the “set” methods so you
DO NOT have to repeat the same checks in the constructor. The NO-ARGS
constructor can go ahead and directly set the default values into the global variables.
Test Last
For each class, create a main method that will declare, build and use an object of that
class. So the main() method in Book.java will declare, build and use a Book object,
and the other two classes will do the same. Use the command-line interface to ask
the user to input a value for EACH global variable. Call a constructor or the set
methods and insert that information into the object. Once the data is inserted, use the
object to call the toString() method (using the dot operator) and print the object to
the console. You will be writing THREE main methods, one for each class. When
you test, make sure your set methods DO NOT allow bad data into the object. Try
to make it fail, see if you can sneak bad values into the variables.
To insure you complete each class, use this checklist:
_____ Three global variables (not the same type)
_____ Two constructor methods
_____ Three “get” methods
_____ Three “set” methods
_____ One 'toString' method
_____ One main method that creates an object, assigns values, and prints the
object
Extra Credit (3 points) - See if you can get jGrasp to build your project into one
compressed 'Project5.jar' file. Build a NEW project/program that has
NOTHING to do with Project5 and is a separate directory/folder. Add your jar
file to the CLASSPATH for this project. Create a test program within this new
project. Build and use objects from all three classes in this program.