
Concept explainers
1. Write a program to produce an array of integer random numbers. Your
program should find out from the user how many numbers to store. It should
then generate and store that many random integers (the random numbers must
be between 1 and 999 inclusive). The program should then determine the
smallest number, the largest number, and the average of all the numbers stored
in the array. Finally, it should print out all the numbers on the screen, five
numbers to a line with spaces in between. Once the contents of the array have
been printed to screen, display the smallest number, largest number, and
average determined previously. You should ensure that your program design in
modular.
The Random class of Java library (java.util.Random) implements a random
number generator. To generate random numbers, you construct an object of q
the class Random, and then use the method nextInt(n) which returns a number
between 0 (inclusive) and n (exclusive). Eg:
ICT167 Principles of Computer Science
2
import java.util.Random;
.....
Random generator = new Random();
.....
//generate a random number between 0 and 99 (inclusive)
int nextRand = generator.nextInt(100);
Alternatively use Math.random(), which returns a random value
in the range 0.0 to 1.0 (then adjust to the correct range)
2. Create a UML diagram to help design the class described in exercise 3 below.
Do this exercise before you attempt to code the solution. Think about what
instance variables will be required to describe a Baby class object; should they
be private or public? Determine what class methods are required; should they
be private or public?
3. Write Java code for a Baby class. A Baby has a name of type String and an age
of type integer.
Supply two constructors: one will be the default constructor, that just sets
default values for the name and age; the second constructor will take two
parameters, a string to set the name and an integer to set the age. Also, supply
methods for setting the name, setting the age, getting the name and getting the
age.
The class should not contain I/O methods; input of values to the instance
variables must be done with a set method or constructor, output of values from
the instance variables must be done with get methods. The set method for the
name instance variable should ensure that the input is not empty or contain
whitespaces (otherwise set a default value). The set method for the age
instance variable should validate the input to be between 1 and 4 inclusive
(otherwise set a default value).
Give Java code for an equals method for the Baby class. Babies count as
being the same (i.e. equal) if their names and their ages are exactly identical
(names should not be case sensitive). The method will take a Baby type
parameter and use the calling object (thus comparing these two objects via
name and age); it should return Boolean - true or false as appropriate.
Remember, if comparing Strings, you must use String comparison methods.
4. Test your Baby class by writing a client program which uses an array to store
information about 4 babies. That is, each of the four elements of the array
must store a Baby object.
If you have an array for baby names and another array for baby ages,
then you have missed the point of the exercise and therefore not met
the requirement of this exercise.
ICT167 Principles of Computer Science
3
A Baby class object stores the required information about a Baby. So
each Baby object will have its own relevant information, and thus each
object must be stored in one element of the array.
The client program should:
a. Enter details for each baby (name and age) and thus populate the
Baby array
b. Output the details of each baby from the array (name and age)
c. Calculate and display the average age of all babies in the array
d. Determine whether any two babies in the array are the same
As the required information for these tasks is stored in the Baby array, you
will need to use a loop to access each array element (and use the dot notation
to access the appropriate set and get methods to assign/retrieve the
information)

Trending nowThis is a popular solution!
Step by stepSolved in 3 steps with 1 images

- Write a program that allows a user to enter any desired amount of student grades, stores the grades in an array, then prints the results to the screen. Then, find the average of the grades.arrow_forwardDesign a program that asks the user to enter a store’s sales for each day of a 7 day week. The amounts should be stored in an array. Use a loop to calculate the total sales for the week, the average sales per day, and also display the list of values. This is for java.arrow_forwardfor beginning java, here's my assignment " Write a program that ask the user to enter a list of positive scores until the user enters a negative score to terminate the input. You need to store these scores in an array. You can assume the maximum number of scores the user may enter is 50. However, you need to keep track of the actual number of scores entered. Write 2 methods: 1. calculateAverage(): this method takes the list of scores and return the average score. 2. countPerfectScores(): this method takes the list of scores and return the number of perfect scores (100). The main program reads the input and calls these methods and print the results. Please make sure you write a comment line to document what your method does." Here is my code, " import java.util.Scanner;class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Start array for scores int[] scores = new int[50]; int size = 0; // Have user enter scores until…arrow_forward
- Write an application that prompts the user to enter nine integers and then store those nine integers in an array. Display the integers from first to last, and then display the integers from last to first. You do not have to sort them, just display the order entered and the reverse order entered. (In JAVA)arrow_forwardThe Lo Shu Magic Square is a grid with 3 rows and 3 columns shown below. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 – 9 exactly The sum of each row, each column and each diagonal all add up to the same number. This is shown below: Write a program that simulates a magic square using 3 one dimensional parallel arrays of integer type. Do not use two-dimensional array. Each one the arrays corresponds to a row of the magic square. The program asks the user to enter the values of the magic square row by row and informs the user if the grid is a magic square or not. Processing Requirements - c++ Use the following template to start your project: #include<iostream> using namespace std; // Global constants const int ROWS = 3; // The number of rows in the array const int COLS = 3; // The number of columns in the array const int MIN = 1; // The value of the smallest number const int MAX = 9; // The value of the largest number //…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





