Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Page 3 of 4 CSC 2 0 5 Lab 2 Part II: Array Stats Program & OOP Review Due by Monday, January 2 9 th
Page
of
CSC Lab Part II: Array Stats Program & OOP Review
Due by Monday, January th: PM along with your Clock files from Part I to your scratch area on
our Linux server cobra See end of lab for details on submitting. This lab is worth points.
Goals
After completing this lab, you should be able to:
Work with loops and arrays in Java.
Use and call multiple class methods and understand the value of structured
programming.
Make use of instance methods and understand how both instance methods and
class methods can be used within the same class.
Be able to differentiate between instance variables and class variables.
Write constructors, getters, and setters.
Lab Setup
Change into your Lab directory cd Lab Now, let us copy over a source file I have
already started for you Statsjava to use for your program along with a sample
executable file.
cp pubdighCSCLab
The Array Stats Program
Two of the measures taken on a group of data are the range of data and the mean of data.
The range of a group of data is defined to be the difference of the largest element and the
smallest element. The mean is defined to be the average of the group of data. Complete
the program Stats so that it will successfully compute the range and mean of a data set
that is input by a user.
Input and Output
The input to your program will be n positive integers one per line from the keyboard,
where n The end of all input is indicated by a line with the integer
Following the input set, your program should print out the range of the input as well as
the mean with appropriate output prompts.
Sample Run
Please Input Your Values Enter a to Stop:
The range of your items is:
The mean of your items is:
Program Design
Your main method has already been set up for you. You simply need to code the bodies
of three methods: fillUp, range, and mean.
Method fillUp is used to read all the input values into the array List and return the
number of items read in back to the main method.
Method range returns the difference between the maximum value and minimum value
in List. The calculation of the maximum and minimum can be done using a single for
loop.
Method mean returns the average of the input values. Notice that it returns a double
value so you will need to use some typecasting. There is no need to worry about a
specific double precision for this lab.
In finishing your range & mean methods, make sure your loop does NOT go all the
way to List.length. That is why we sent in numItems as a parameter. We want it
to only find the range and mean of numItems in the array List.
OOP Review
Look at class Person on the attached handout. Each person has instance variables for
an id and name associated with them. Notice also the private class variable
personCount. We will use this class variable to assign a unique id to each person in
the order they are created
Unlike instance variables, which as the name implies come with every instance of a class
ie object class variables are associated with the class instead. This means, only one
copy of this variable is shared among all objects of this class and therefore the value of
this variable is the same for all of the objects.
To declare a class variable we use the keyword static. Class methods are of course
similarly declared. Notice the static method getCount Do you see how its value is
shared among all possible variables instances of type person? This is in contrast to the
id which is different for each person.
Finally, notice how we can invoke the static method. We can call it with in instance of
the class, ie an object eg sue.getCount or and this is different, by just
referring to the name of the class, ie Person.getCount
Now, in the space below, trace through and give the output of the StaticTest
program shown.
Embellishing the Person Class
Let us overload our constructor by adding a default noargs constructor to
Person that defines an object to have the name NA and an id of Use the
this operator appropriately, and be sure to increment personCount.
Add a setter method named reset that can be used to reset the two private
instance variables of this class to two values passed in as parameters. This will
allow your Person class to now be mutable.
Add getter methods getName and getId that can be used to retrieve these two
private variables.
Finally, let us test our new methods at the end of the StaticTest program. Declare a
Person object samplePerson that uses the default constructor. Next, reset it to your
own name and the last four digits of your social security number using the reset
method. Finally, print out your name and id using your getter methods.
Lab Submission
Now, let us copy over your work for grading on scratch Remember to replace
lastfm with your cobra login name below.
cp Stats.java scratchcsclastfmLabs
cp Person.java scratchcsclastfmLabs
Next, chang
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