- Information
- AI Chat
Was this document helpful?
7. Arrays and Inheritance
Course: Java programming (CSC1016S)
174 Documents
Students shared 174 documents in this course
University: University of Cape Town
Was this document helpful?
Version 10/09/2024 14:55
Page 1 of 4
CSC1016S Assignment 7
Arrays and Inheritance in Java
Assignment Instructions
This assignment concerns (i) the use of class or ‘static’ variables and methods, (ii) the use of
arrays, and the basics of inheritance in Java question.
Some concepts from Assignment 6 are still very relevant in this assignment. This assignment is a
continuation of the previous assignment on References.
One use of class variables and methods is for defining collections of constants and routines – much
like a Python module. The class “java.lang.Math” is an excellent example. It defines constants
such as π, and provides routines for calculating square roots, powers etc. We don’t create a ‘Math’
object to make use of these facilities.
Question 1 [35 marks]
This question concerns the construction of a NumberUtils class declaration that contains a collection
of useful routines.
Write a class declaration that satisfies the following specification:
Class NumberUtils
The NumberUtils class contains a collection of routines for working with integers.
Instance variables
None
Constructors
private NumberUtils() {}
// A private, empty-bodied constructor prevents NumberUtil objects from being created.
Methods
public static int[] toArray(int number)
// Given a number that is n digits in length, maps the digits to an array length n.
// e.g. given the number 5678, the result is the array {5, 6, 7, 8}.
public static int countMatches(int numberA, int numberB)
// Given two numbers, count the quantity of matching digits – those with the same value and
// position. For example, given 39628 and 79324, there are 2 digits in common: x9xx2x.
// It is assumed that the numbers are the same length and have no repeating digits.
public static int countIntersect(int numberA, int numberB)
// Count the quantity of digits that two numbers have in common, regardless of position.
// For example, given 39628 and 97324, there are 3 digits in common: 3, 9, 2.
// It is assumed that the numbers are the same length and have no repeating digits.
You should make a simple test program (which you do not need to submit) to check your code.