Java Programming: Developing ProcessScores and Light Class in Java

Verified

Added on  2024/05/29

|5
|648
|338
Practical Assignment
AI Summary
This assignment solution demonstrates Java programming concepts through the implementation of two classes: ProcessScores and Light. The ProcessScores class reads scores from a file, calculates statistics such as lowest, highest, range, average, median, and mode, and displays the results. The Light class models a conference hall light with properties like brightness, on/off state, and color, including methods to set and retrieve these properties, as well as a toString method to describe the light's state. The TestLight class creates objects of the Light class, sets their properties, and uses the toString method to output their descriptions. UML diagrams are provided to visualize the structure of both the Light class and the complete program.
Document Page
PROGRAMMING IN JAVA
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Contents
Task 1:.........................................................................................................................................................3
Task 2..........................................................................................................................................................4
1
Document Page
Task 1:
In task 1, it was required to write a class named ProcessScores. The class is used to read the
scores from the given file afl.text and store the read scores in the array. After storing the scores,
we need to calculate and the display the statistics relating the scores.
The input to this class is the afl.txt file which contains all the scores of the season. The output is
the statistics calculated – lowest, highest, range, average, median and mode of the score in the
season.
Firstly the input is taken from the afl.txt file using the FileReader class. Each score in the class
is stored in an ArrayList and then the arraylist is converted to an array. The final array is then
passed as an argument to the static methods which returns the statistic calculated value. The
static methods defined are:
1) lowest(): finds the lowest score in the season.
2) highest(): finds the highest score in the season.
3) range():finds the difference between the highest and lowest score in the season.
4) average():finds the average of all scores in the season.
5) median():finds the median score in the season.
6) mode():finds the mode of scores in the season.
2
Document Page
Task 2
Output:
In task 2, a class Light is written which will represent the light of the conference hall. In this,
three constants named LOW, MEDIUM, and HIGHare declared with their constant values 1, 2
and 3 respectively which represent the brightness of the light. Two private variables is defined,
one is ‘brightness’ and another is ‘on’ which is a Boolean variable and contains the on-off state
of the light. A string variable ‘color’ is defined to represent the color of the light.
The accessor and the mutator methods that is the getter and setter methods of the variable
defined are initiated. The getter method is used to get the value and the setter method is used to
set the value of the variable. A constructor of the class is defined to initiate default values of the
variables ‘brightness’, ‘on’ and ‘color’ as LOW, false and red respectively.A toString() method
returns the description of the light in the string. If variable ‘on’ contains ‘true’ value, then the
method returns brightness and color of light in a string otherwise the method returns the color
combined with string “light is off”.A TestLight class is defined to create two objects of the
Light class.
First, the constructor Light sets the default value of the ‘brightness’, ‘on’ and ‘color’ as LOW,
false and red respectively using the setter methods of the variable. As the object of the Light
class is created in the TestLight class, the values of the three variables are set by calling the
setter methods using the object. As the values are set, the toString() function is invoked using
the Light class object. The method checks whether light is on or off by using getter getOn() and
then the string is returned which contains the description of the light according to the condition
given.
UML Class Diagram of Light class:
3
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Output:
UML Class Diagram of the complete program:
4
chevron_up_icon
1 out of 5
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]