CS201

LAB 7

The lab is due July 27

GRADE POINTS: 4 points towards final grade.

.

NOTE 2: This lab involves strictly programming and there are no questions to be answered. There is no special answer sheet for that in this assignment.

TASK 1 (1 point)

OBJECTIVE:

The primary new concepts in Task 1 and Task 2 of this lab has to do with searching of arrays and implementing the selection sort algorithm.

  1. Usage of enum class.
  2. Equality of objects.
  3. Using a reference of a class as an attribute in another class.
  4. Arrays of objects.
  5. Vectors.
  6. Static and non static members of a class.
  7. Using multiple service classes.
  8. Sequential search of arrays.
  9. Selection Sort algorithm.
  10. Binary Search algorithm.
  11. Developing a simple algorithm.

Keep in mind that this document is like a specification, you must abide to it regardless if there is another way of doing the problem. In areas where the specification is silent then you can use your discretion.

Use Notepad++ or equivalent editor to create source code. Do not use eclipse.

TASK 1 DESCRIPTION:

In tasks 1 and 2 of the Lab we are going to have 3 normal classes (one being the client class and the other two being service classes) and one enumeration class. The client class would have to use both of the service classes.

Task 1 (1 point)

  1. There is an enumeration of the material categories: The categories are: AluminumBased,Metal,Cellulose,Glass.

This enum resides in package firstname.lastname.

  1. Use the class Rod.javawhose code is given below. THIS SERVICE CLASS NEEDS TO BE MODIFIED ACCORDING TO THE INSTRUCTIOS BELOW.

import java.text.DecimalFormat;

public class Rod

{

String material=" ";

double matlength=0.0;

double area=0.0;

public Rod()

{

material="any";

matlength=-0.1;

area=-0.1;

}

public Rod(String mn, double ar, double matl, int t)

{

material=mn;

area=ar;

matlength=matl;

}

public String getMaterial()

{

return material;

}

public void setMaterial(String mat)

{

material=mat;

}

public double getMatLegth()

{

return matlength;

}

public void setMatLength(double ml)

{

matlength=ml;

}

public double getArea()

{

return area;

}

public void setArea(double are)

{

area=are;

}

public String toString()

{

String output=" ";

output="The category is:"+" "+mc+" "+"The name of the material is"+" "+material+" "+"The length is"+" "+matlength+" "+

"The cross area is"+" "+area+" ;

return output;

}

public double calculateExpansion(double cofthermal)

{

DecimalFormat nf=new DecimalFormat("0.0000");

double changelength=cofthermal*matlength*temp;

double changelengthf=Double.parseDouble(nf.format(changelength));

return changelengthf;

}

public double calculateForce(double y, double dl)

{

DecimalFormat nf=new DecimalFormat("0.00");

if(matlength<=0)

return -1.0;

else

{

double force=y*area*dl/matlength;

double forceformatted=Double.parseDouble(nf.format(force));

return forceformatted;

}

}

}

  • The service class Rod .java should be packaged to path firstname.lastname.
  • Add the enum as an attribute with the corresponding accessor / mutator methods and withits value appearing also in the toString method.
  • Amend the class Rod to include one more attribute of type int which represents temperature (call it temp, as this identifier also appears in the help method calculateExpansion as you can see in the above provided code). Add accessor and mutator methods for this attribute and also added to the toString method “The temperature is:”+… Th default temperature is zero, the last argument t in the non default constructor sets the temp attribute.
  • Provide additional attributes (one is static) to be able to advance the id of an object via either of the constructors. Add the proper code in the constructors so that the object is advanced by 1 every time a constructor is called. Provide accessor/mutator methods and add these two new attributes in the toString method also.
  • Default temperature is 0.
  • Include the temperature attribute as the first argument in the non default constructor

Note: The method calculateExpansion has only one argument of double type representing the coefficient of thermal expansion.

  1. CREATE ADDITIONALNEW SERVICE CLASS.
  • Create a second service class called ReadData which also resides in the package firstname .lastname when compiled (same package as Rod and MaterialCategories.
  • It has two instance variables which are arrays of type double. They would be used to store the coefficient and young’s modulus numbers when the text file is read. Make sure that they are declared public.
  • This class has a method called vectorData. It takes as an argumengt a String that represents the name of the text filecoefficients.txt(data for that file is shown below). It reads the data line by line off the text file, tokenizes it and creates objects of Rod which it stores into a vector data structure. It saves the coefficients of thermal expansion into an array which is an instance variable of this class, and the Young’s modulus numbers into another array which is also an instance variable of this class (keep in mind that these two arrays need to be instantiated based on the size of the data!). It returns the vector.

public Vector vectorData(String filename).

  • There is also a method called sortVector. The method accepts as argument a Vector of non sorted Rod objects. It sorts the Rod objects according to the tempearature from largest to smallest. It returns the sorted vector.

public Vector sortVector(Vector v).

  • There is also a method called searchVector. It takes as argument a sorted Vector according to Temperature value of eache Rod object, and a int value that serves as a search key for a temperture value. It returns the index of the Vector at which the Rod object with the key temperature is stored or -1 if the temperature value is not found. It uses Binary Search algorithm.

public int searchVector(Vector sv, int key)

  • Compile this class.

NOTE: When you compile disregard the following message, your file is compiled fine:

C:\Lab3Solution>javac -d . ReadData.java

Note: ReadData.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

Note: Disregard the above warnings given by the compiler when you compile!!!!

Task 2 (1.0 Points)

Copy all the classes from Task1 into this new folder Task2, including the compiled classes in their corresponding packages.

Client class RodClient:

  1. Create the class RodClient to be placed in package firstname.

The main method requires the NAME value of the text file with the data captured command line in order to continue with the program.

  • It invokes the method vectorData from the ReadData class and captures the data structure.
  • It captures the two arrays instance variables from ReadData (the coefficients array and the young’s modulus array).
  • It invokes the method sortVector from the ReadData class and passes it the unsorted vector. It captures the returned vector.
  • It displays the values of the attributes of each Rod object in the sorted vector including the expansion for each rod object in the sorted vector and the required force for each rod object in the sorted vector. Notice that you should much the same output values as in lab 2! You can double check that by comparing objects according to their id values, since in lab 2 the data is outputted sequentially from id=1 ,2,3,4,5,6 etc. (but in lab 3 they are sorted by temperature).
  • It searches the sorted vector for the temperature value 89. It uses the returned value to display the attributes of the Rod object if found or a statement that it was not found.
  • It then searches the sorted vector for the temperature value of 156. It uses the returned value to display the attributes of the Rod object if found or a statement that it was not found.
  1. A text file with the data needed for this program should be created using Notepad (or download the text file from the course’s web site):

Notice that the temperature is the last number after Young’s modulus number!

The sequence of data is : category, name of material, cross sectional area, length of material , coefficient of thermal expansion, Young’s modulus, temperature. The file is also available on the course’s web site.

TEXT FILE DATA

AluminumBased,Aluminum,1.25,2.5,0.000013,10000000,23

Metal,Brass,2.25,25.0, 0.00001,13000000,40

Metal,Copper,2.25,25.0,0.0000094,16000000,37

Cellulose,CelluloseAcetate,2.5,5.0,0.000130,20000000,89

Metal,Lead,2.25,25.0, 0.000017,2300000,100

Glass,Hard,0.4,3.0,0.0000059,90000000,150

Metal,Steel,2.25, 25.0,0.0000067,29000000,10

Metal,Iron,2.25,25.0, 0.0000067,13000000,26

AluminumBased,AlumAlloy,1.25,2.5,0.0000023,10200000,56

Cellulose,CelluloseNitrate,2.5,5.0,0.000100,50000000,90

Glass,Pyrex,0.4,3.0,0.000004,17000000,36

AluminumBased,Aluminum,0.25,3.5,0.000013,10000000,22

Metal,Lead,1.25,5.0, 0.000017,2300000,35

Metal,Iron,0.25,25.0, 0.0000067,13000000,56

Glass,Pyrex,1.4,3.0,0.000004,17000000,200

Cellulose,CelluloseAcetate,6.5,25.0,0.000130,20000000,250

  • See the sample output for details on the output format.

Compile all classes in this task

Interpret the program!

Sample Output based on current text file data

C:\CS116\SPRING2014\Labs\Lab3\Lab3Solution>java firstname.RodClient coefficients.txt

The category is: Cellulose The name of the material is CelluloseAcetate The length is 25.0 The cross area is 6.5 The temperature is: 250 the object idis 16

The expansion is: 0.8125

The force required is: 4225000.0

The category is: Glass The name of the material is Pyrex The length is 3.0 The cross area is 1.4 The temperature is: 200 the object id is 15

The expansion is: 0.0024

The force required is: 19040.0

The category is: Glass The name of the material is Hard The length is 3.0 The cross area is 0.4 The temperature is: 150 the object id is 6

The expansion is: 0.0027

The force required is: 32400.0

The category is: Metal The name of the material is Lead The length is 25.0 The cross area is 2.25 The temperature is: 100 the object id is 5

The expansion is: 0.0425

The force required is: 8797.5

The category is: Cellulose The name of the material is CelluloseNitrate The length is 5.0 The cross area is 2.5 The temperature is: 90 the object id is 10

The expansion is: 0.045

The force required is: 1125000.0

The category is: Cellulose The name of the material is CelluloseAcetate The length is 5.0 The cross area is 2.5 The temperature is: 89 the object id is 4

The expansion is: 0.0578

The force required is: 578000.0

The category is: AluminumBased The name of the material is AlumAlloy The length is 2.5 The cross area is 1.25 The temperature is: 56 the object id is9

The expansion is: 3.0E-4

The force required is: 1530.0

The category is: Metal The name of the material is Iron The length is 25.0 The cross area is 0.25 The temperature is: 56 the object id is 14

The expansion is: 0.0094

The force required is: 1222.0

The category is: Metal The name of the material is Brass The length is 25.0 The cross area is 2.25 The temperature is: 40 the object id is 2

The expansion is: 0.01

The force required is: 11700.0

The category is: Metal The name of the material is Copper The length is 25.0 The cross area is 2.25 The temperature is: 37 the object id is 3

The expansion is: 0.0087

The force required is: 12528.0

The category is: Glass The name of the material is Pyrex The length is 3.0 The cross area is 0.4 The temperature is: 36 the object id is 11

The expansion is: 4.0E-4

The force required is: 906.67

The category is: Metal The name of the material is Lead The length is 5.0 The cross area is 1.25 The temperature is: 35 the object id is 13

The expansion is: 0.003

The force required is: 1725.0

The category is: Metal The name of the material is Iron The length is 25.0 The cross area is 2.25 The temperature is: 26 the object id is 8

The expansion is: 0.0044

The force required is: 5148.0

The category is: AluminumBased The name of the material is Aluminum The length is 2.5 The cross area is 1.25 The temperature is: 23 the object id is 1

The expansion is: 7.0E-4

The force required is: 3500.0

The category is: AluminumBased The name of the material is Aluminum The length is 3.5 The cross area is 0.25 The temperature is: 22 the object id is 12

The expansion is: 0.001

The force required is: 714.29

The category is: Metal The name of the material is Steel The length is 25.0 The cross area is 2.25 The temperature is: 10 the object id is 7

The expansion is: 0.0017

The force required is: 4437.0

//FIND TEMERATURE OUTPUT

The object was found and it is: The category is: Cellulose The name of the material is CelluloseAcetate The length is 5.0 The cross area is 2.5 The temperature is: 89 the object id is 4

Temperature 156 value was not found

C

// END OF FIRST PROGRAM

NEW PROGRAMs

Objectives for Tasks 3, 4:

  1. Designing an algorithm for data formatting (working with algorithms).
  2. Multidimentional Arrays and formatting displays.
  3. Working with ArrayList.
  4. Multiple service and helper classes.
  5. Compiling classes that have cross referencing.
  6. Inheritance.
  7. Abstract classes.
  8. Polymorphism.

Task 3 ( 1 point)

In this task we are going to use classes provided to you on which you will need to add your own code. You can download those classes from the course’s web site (link: HelpClassesForLab7Task3.zip). These class files provided are:

Enumeration classes: Dealer.java and VehicleEngine.java

Service classes: Vehicle.java and CarDealer.java

Client class: VehicleClient.java (incomplete class)

Study the code provided and make the changes requested in this Task. Make sure that the work done on this task is on its own folder named Task 3 . The text data file is also provided to you.

Leave the packages as they are in the provided code.

  1. You must compile the files provided to you before you make any of the changes requested in this lab. There are two service classes: Vehicle.java and CarDealer.java. A technique called object cross referencing is used in those two classes. This means that there is an attribute of type CarDealer in the Vehicle class and an attribute Vehicle in the CarDealer class. Therefore in trying to compile we have an issue- which comes first. Therefore to compile:

First compile the two enum classes

To compile the two service classes : first comment out any references to CarDealer (attribute, accessor/mutator and toString ) in the Vehicle class.Then compile the Vehicle class.

Now go ahead and compile the CarDealer class.

Go back to the Vehicle class and get rid of the commenting out (now CarDealer referencing is O.K.). Recompile the Vehicle class. Not recompiling the classes will make the rest of your code changes useless and you will cause you to lose 1 point.

  • The new task is to create a display of sales of vehicles sold, in the form of a 3 dimensional table. Sold vehicles will be tracked by dealership, week number and day of the week. There are 6 days in a week that dealerships are open Monday, Tuesday, Wednesday, Thursday, Friday and Saturday. A dealer can sell another dealer’s vehicle, therefore who has possession of the car (as per the data of the text file) does not make any difference as far as the sale of the car is concerned.
  • A three dimensional array will hold the sale price of each vehicle sold.
  • Vehicles are sold as follows:

The 5 dealers have sold the below listed vehicles by id number. The id numbers correspond to the object id numbers created by the Vehicle class when Vehicle objects are created based on the text file’s data. The pattern of the sales by dealer (dealer’s name below is the name of the dealer who sold the car and not the dealer who had possession of the car) is as follows:

Week #1
ID1:
Monday : vehicle id: #1 is sold (its price is the data)
Wednesday: vehicle id: #2 is sold (its price is the data)
Friday: vehicle id: # 3 is sold (its price is the data)
ID2:
Tuesday: vehicle id: # 4 is sold (its price is the data)
Thursday: vehicle id:# 5 is sold (its price is the data)
Saturday: vehicle id: # 6 is sold (its price is the data)
ID3:
Monday: vehicle id: #7 is sold (its price is the data)
Wednesday: vehicle id: #8 is sold (its price is the data)
Friday: vehicle id: #9 is sold (its price is the data)
ID4:
Tuesday: vehicle id: # 10 is sold (its price is the data)
Thursday: vehicle id: #11 is sold (its price is the data)
Saturday: vehicle id: #12 is sold (its price is the data)
ID5:
Monday: vehicle id: #13 is sold (its price is the data)
Wednesday: vehicle id: #14 is sold (its price is the data)
Friday: vehicle id: #15 is sold (its price is the data) / Week #2
ID1:
Monday : vehicle id: #16 is sold (its price is the data)
Wednesday: vehicle id: #17 is sold (its price is the data)
Friday: vehicle id: # 18 is sold (its price is the data)
ID2:
Tuesday: vehicle id: # 19 is sold (its price is the data)
Thursday: vehicle id: # 20 is sold (its price is the data)
Saturday: vehicle id:# 21 is sold (its price is the data)
ID3:
Monday: vehicle id: #22 is sold (its price is the data)
Wednesday: vehicle id: #23 is sold (its price is the data)
Friday: vehicle id: #24 is sold (its price is the data)
ID4:
Tuesday: vehicle id: # 25 is sold (its price is the data)
Thursday: vehicle id: #26 is sold (its price is the data)
Saturday: vehicle id: #27 is sold (its price is the data)
ID5:
Monday: vehicle id: #28 is sold (its price is the data)
Wednesday: vehicle id: #29 is sold (its price is the data)
Friday: vehicle id: #30 is sold (its price is the data)

Based on the above table you will have to come up with an algorithm to store data in a 3 -dimensional array. The data of course is made out of the sale price of Vehicle objects. The dimensions of the array are: 1st dimension: Dealer, 2nd dimension: week , 3rd dimension :day

Study the pattern of sales in order to develop your algorithm.

  • Since we need data for 30 vehicles, a text file with data is provided:.

1.5,2,owner1,Type2,ID1,17900.20

2.0,3,owner2,Type2,ID3,10000.0

3.3,4,owner3,Type1,ID2,20000.0

4.0,5,owner4,Type4,ID5,45000.30

5.3,6,owner5,Type4,ID1,23050.0

2.5,4,owner6,Type4,ID2,10000.40

5.3,3,owner7,Type3,ID2,23040.0

3.0,3,owner8,Type3,ID5,34000.30

2.4,5,owner9,Type1,ID3,45000.0

4.0,2,owner10,TYpe4,ID4,22000.0

2.0,8,owner11,Type4,ID1,35000.20

1.0,3,owner12,Type1,ID5,16000.0

2.9,1,owner13,Type3,ID3,24500.00

4.0,2,owner14,Type3,ID1,12750.25

8.0,4,owner15,Type2,ID1,19600.00

1.0,1,owner16,Type2,ID2,25000.00

2.0,2,owner17,Type2,ID5,20000.50

6.0,1,owner18,Type1,ID2,34000.00

9.0,1,owner19,Type2,ID1,56000.00

9.0,2,owner20,Type3,ID1,19000.40

3.0,8,owner21,Type4,ID4,37000.00

4.0,4,owner22,Type4,ID5,28000.00

5.0,1,owner23,Type1,ID3,50000.00

4.0,3,owner24,Type2,ID2,44000.00

3.5,4,owner25,Type3,ID2,45600.00

1.8,1,owner26,Type1,ID5,15000.00

2.0,3,owner27,Type4,ID1,14000.03

3.0,4,owner28,Type3,ID3,18000.0

2.7,3,owner29,Type1,ID4,24000.00

4.0,3,owner30,Type2,ID5,10000.00

Note: Make sure that there is no space between characters in the text file.

  • When a vehicle is sold it needs to be marked as sold. Therefore add an instance variable in the Vehicle class which is responsible for marking a vehicle “sold”. You need to figure the data type of this variable. Initialize the variable in both constructors (initialization value should show the vehicle as not sold). The arguments of the non default constructor do not change. Add accessor/mutator methods for the new instance variable and also add the variable in the toString method with a short descriptive String ahead of it i.e “Sold status=”+..
  • In the client class called VehicleClient make the following changes.
  • Create a NON static method called tripleArraywhich takes as argument the ArrayList of Vehicle objects and returns a triple array of double values that represent the prices of the cars sold. The double values represent the sales for each day of the week for each dealer. The first dimension is the dealer

Index 0: ID1

Index 1: ID2

Index 2: ID3

Index 3: ID4

Index 4: ID5

The second dimension is the weeks.