Lab assignment: Dating application.

The topic of the lab today is java classes, how to create them, how to create the constructors, variables, getters and setters. The program that you must write deals with data about different people, such as name, attractiveness, etc. The program must also be able to compute a value of character similarity between two people (see Stage 3.).

Preliminary Information:
You must use the following two java classes:

  1. Class SpecialFriend (YOU MUST CREATE THIS CLASS) with the following variables:
    • String name;
    • int attractiveness; // values from 0 (not at all attractive) to 5 (really hot).
    • int spontaneous; // values from 0 (very careful person) to 5 (very spontaneous person).
    • int personality; // values from 0 (none) to 5 (lots).
  2. Class Lab5 (PROVIDED BELOW), which contains the main and the code to create two instances of SpecialFriend and implements the three stages listed below.

Code to get started: Lab5.java

Stage 1:
Create a new class named SpecialFriend, write the Constructor for class SpecialFriend, and set and get methods for the class SpecialFriend. Don't forget to create a new Eclipse java project before creating the classes SpecialFriend and Lab5.
The constructor should set all the values of the variables for the new object to 3, with default name of "Pat".
Remember the constructor is called when you create a new object or instance of the class, that is when you
use the operator new. The constructor is basically just an initializer for the new object. The constructor of
the class SpecialFriend should set all the values of the variables for the new object to 3, with default name of "Pat".
The get and set methods must also be written for this stage. The get methods just return the values of the corresponding
variable when they are called, while the set methods set the variable to some value. For instance, getAttractiveness() should be written to get the value of the variable attractiveness, while setAttractiveness(int someNumberBetween0And5) can be written
to set the value of the variable attractiveness to someNumberBetween0And5.
Error Control: The set methods for the int variables MUST check that the new value is between 0 and 5, before setting the value of the variable. If the new value is not between 0 and 5, an error message should be displayed.

Hint: After you write the declaration of the variables of the class SpecialFriend, to create automatically the get and set methods,
on Eclipse go to Source -> Generate Getters and Setters...


Stage 2:
The driver code, works for the code below (and other similar code). That is, the following code should work inside the main in the class Lab5:
   SpecialFriend friend1 = new SpecialFriend(); //constructor, should set name to "Pat" and all the variables of the object friend1 to 3.
   friend1.setName("Chris"); //the instance friend1 was created, now must change the value of its variables. name is changed here.
   friend1.setAttractiveness( 4); //attractiveness changed here
   friend1.setSpontaneous( 5); //spontaneity changed here
   friend1.setPersonality( 2); //personality changed here
   //error control testing
   friend1.setPersonality(1023); //1023 is not between 0 and 5, so the set method should not set the value, but print an error message
   friend1.setAttractiveness(-100);//-100 is not between 0 and 5, so the set method should not set the value, but print an error message
  //note that 1023 and -100 are just examples, the error control code should work with any number greater than 5 and smaller than 0.

   SpecialFriend friend2 = new SpecialFriend(); //a new instance of SpecialFriend is created

  // display the two friends
  // we supply the code for displayValues()
   displayValues( friend1);     
   displayValues( friend2);

Note that the code of the method displayValues is already provided in the class Lab5, but it uses get methods, which are not implemented yet.

Stage 3 (Advanced):
Implement the methods for the following driver code to work:

   int difference = friend1.similarityTo( friend2);  //sum of absolute value of differences across 3 criteria
   System.out.println(" friend1 and friend 2 have a difference rating of: " + difference);

In other words the code above should work correctly in the main method of the class Lab5.

Solution:

Lab5.java
SpecialFriend.java