Description
2d arrays homework
Submission Checklist
?
?
?
RaggedArray.java containing all methods for Task 1
School.java containing all methods for Task 2
Student.java containing all methods for Task 2
Task 1
For Task 1, you will be working with 2-dimensional integer arrays alongside constructor
overloading and file parsing. However, the caveat is that these arrays will not always be
rectangular – they could be jagged. An example of a jagged array is shown below:
array
[0]
array
[1]
array
[2]
1
3
6
3
4
1
Please download the starter code from here. Ensure that there are two files:
RaggedArray.java and RaggedArrayTester.java. You will be implementing several
methods within RaggedArray.java, and you will be testing those methods in
RaggedArrayTester.java. For Task 1, you ONLY need to submit RaggedArray.java.
RaggedArrayTester.java
Within RaggedArrayTester.java are a few test cases for the RaggedArray class. As you
are implementing RaggedArray.java, you should test thoroughly to make sure that
your program is functioning as expected. We will not be collecting
RaggedArrayTester.java.
RaggedArray.java
Within RaggedArray.java, there is 1 private member variable given to you:
? private int[][] myArray – a 2-dimensional integer array.
NOTE: You can assume that myArray (and its inner rows) will never be null, and that, if there
is space in myArray (i.e. if myArray has a length greater than 0), then myArray will only
contain integers between 0 and 1000 (inclusive).
Take note of the declared type of myArray. Do NOT change the type.
We also provide you with a single getter method. Please read and understand the method.
Finally, we provide you with a method called findMaxWrong. You can use findMaxWrong as
inspiration for one of the methods in Task 1, but (more importantly) you will also be using
findMaxWrong in Task 3 (which will be discussed later). Do NOT modify findMaxWrong.
For the rest of RaggedArray.java, you will need to implement the 3 following methods.
DO NOT MODIFY ANY METHOD DECLARATIONS:
1. public RaggedArray(int[][] arrayIn)
Within this constructor, you need to perform a deep copy of arrayIn into the member
variable myArray. Use what you have learned in this course to perform a deep copy.
NOTE: For this constructor (and all other constructors that involve deep copies), we will
be testing for shallow copies.
2. public RaggedArray(String inputPath) throws IOException
Within this constructor, you will be reading in the text from the file inputPath to
initialize myArray. We provide two input files for you (input1 and input2), but
you should create more for further testing.
The input file will have the following format:
1. The first line of the file will always contain two numbers that are separated by a
single space.
a. The first number will represent the number of rows in the array, while the
second number will represent the number of columns in the array. These
numbers will not necessarily be equal (i.e. the array could be a rectangle).
However, because of the rectangular constraints, you can assume
that with this constructor, the array will not be jagged. You can also
assume that these numbers will always be positive and will always exist.
2. The rest of the lines of the file is the array:
a. Each non-space character will always be a valid integer. No other
characters will be in the file.
b. Between each column, there will be a single space. Every row is
separated with a ‘n’ (a new-line character).
c. There are no leftover blank lines in the input file (i.e. no trailing
whitespace). There are no extra spaces between rows or between
columns. There are no extra spaces before nor after each line.
Using what you have learned in this course, read the input file, and convert the input file
into a 2-dimensional integer array to initialize myArray. If you use a Scanner object,
then do NOT forget to close the Scanner for the input file after you finish reading the file.
NOTE: You can assume that the file always exists, and that the file is properly formatted
(i.e. no corrupt/bad inputs).
HINT: This process of file processing is similar to a previous programming assignment.
3. public int findMax()
This method should return the maximum element in myArray. For example, the
maximum from the array below would be 6.
array
[0]
array
[1]
array
[2]
1
3
6
3
4
1
HINT: You can use findMaxWrong as inspiration (although there is an error with that
code).
Task 2
For Task 2, you will be working on two classes that represent a school of students.
Please download the starter code from here. Ensure that there are three files: Student.java,
School.java, and SchoolTester.java. You will be implementing several methods
within Student.java and School.java, and you will be testing those methods in
SchoolTester.java. For Task 2, you need to submit Student.java and School.java.
SchoolTester.java
Within SchoolTester.java are a few test cases for the School and Student class. As you
are implementing Student.java and School.java, you should test thoroughly to make
sure that your program is functioning as expected. We will not be collecting
SchoolTester.java.
Student.java
Within Student.java, there are 2 private member variables given to you:
? private String name – the name of a student
? private int[] scores – an array of exam score(s) of a student (between 0 – 100,
inclusive)
NOTE: You can assume that name will always be a valid String, but scores could be an empty
array.
Take note of their declared types. Do NOT change any of their types.
We also provide you with two getter methods. Please read and understand the methods.
Finally, we provide you with a default constructor called public Student(). This default
constructor takes in NO arguments, and simply initializes this.name to “N/A” and
this.scores to an empty integer array.
For the rest of Student.java, you will need to implement the 2 following methods. DO
NOT MODIFY ANY METHOD DECLARATIONS:
1. public Student(String name, int[] scores)
Within this constructor, you need to perform a deep copy of the parameters name and
scores into their respective member variables. Use what you have learned in this
course to perform a deep copy.
2. public Student(Student student)
Within this copy constructor, you need to perform a deep copy of the member variables
from the parameter student into this (current instance’s) respective member
variables. Use what you have learned in this course to perform a deep copy.
School.java
Within School.java, there are 2 private member variables given to you:
? private String name – the name of the school
? private Student[] students – an array of Students that attend the school
NOTE: You can assume that name will always be a valid String.
Take note of their declared types. Do NOT change any of their types.
We also provide you with two getter methods. Please read and understand the methods.
For the rest of School.java, you will need to implement the 3 following methods. DO
NOT MODIFY ANY METHOD DECLARATIONS:
1. public School(String name, Student[] students)
Within this constructor, you need to perform a deep copy of the parameter?s name and
students into their respective member variables. Use what you have learned in this
course to perform a deep copy.
2. public double[] meanScore()
This method should calculate the average (mean) score per student as a double,
returning an array of doubles where each element corresponds to a student’s mean
score. If the student name is “N/A”, or if the student has no scores (i.e. their scores
array has a length of 0), then their corresponding mean score should be -1.0. For
example, consider the following code:
Student stu1 = new Student(“John Doe”, new int[] {98, 94, 84});
Student stu2 = new Student(“Jane Doe”, new int[] {100, 95, 90});
Student stu3 = new Student();
Student[] stuList = new Student[] {stu1, stu2, stu3};
School sch1 = new School(“UCSD”, stuList);
If I call sch1.meanScore(), then I expect the return value to be a double array with the
values [92.0, 95.0, -1.0].
NOTE 1: When performing the average (mean) calculation, you do NOT have to round
the double to any decimal point – just leave the calculation as is.
NOTE 2: If students is empty, then this method should just return an empty double
array.
3. public double meanScore(int idx)
This method should calculate the average (mean) score of the student at index idx as a
double, returning a single double that represents a student’s mean score. If the student
name is “N/A”, or if the student has no scores (i.e. their scores array has a length of
0), then this method should return -1.0. For example, with the same code block as
above, calling sh1.meanScore(1) would return 95.0.
NOTE 1: When performing the average (mean) calculation, you do NOT have to round
the double to any decimal point – just leave the calculation as is.
NOTE 2: idx is NOT guaranteed to be within bounds. If idx is out of bounds, then
return -1.0.
HINT: If you already implemented meanScore(), then this method should be rather
simple to implement.
Task 3
For Task 3, answer the following three questions.
1. Back in Task 1, we talked about how findMaxWrong() has an error. For this
question, we want you to write, show, and run a test case that will replicate an
error in regards to findMaxWrong().
a. To be specific, we are looking for the following:
i.
Within RaggedArrayTester.java, show and describe your test case
that will cause an error, then run your tester file.
ii.
Show the error that is shown in your command prompt or terminal.
iii.
Show where and discuss why this error occurred (i.e. we want you to
show where and discuss why in findMaxWrong() did the error occur?).
2. Consider the following block of code inside SchoolTester.java:
Student stu1 = new Student(“Jane Doe”, new int[] {71, 84});
Student stu2 = new Student(“John Doe”, new int[] {46, 84, 98});
Student stu3 = new Student();
Student[] stuList = new Student[] {stu1, stu2, stu3};
School sch1 = new School(“UCSD”, stuList);
For this question, we want you to show AND describe what happens if you were to
call sch1.meanScore(3).
a. To be specific, we are looking for the following:
i.
Within SchoolTester.java, show the lines of code that are given to
you above, alongside sch1.meanScore(3) (for a total of 6 lines of
code).
ii.
Inside School.java, show and discuss what happens inside
meanScore(int idx), particularly with the input of 3.
1. If you do not have a properly implemented method, then you can
describe what would happen to the best of your ability. However,
we want to see your code no matter what.
iii.
Show the returned value of sch1.meanScore(3).
3. Consider the following 3 lines of code inside SchoolTester.java:
Student stu1 = new Student(“Jane Doe”, new int[] {71, 84});
Student stu2 = new Student(“John Doe”, new int[] {46, 84, 98});
Student stu3 = new Student();
For this question, we want you to show a memory model diagram that depicts
everything in memory AFTER the 3 lines of code have finished running. We do not
need to see any constructor stack frame(s), but we expect those 3 lines of code to exist
within the main method stack frame.
a. To be specific, we are looking for the following:
i.
Where is the memory allocated in the stack and in the heap for stu1?
ii.
Where is the memory allocated in the stack and in the heap for stu2?
iii.
Where is the memory allocated in the stack and in the heap for stu3?
?
?
?
RaggedArray.java containing all methods for Task 1
School.java containing all methods for Task 2
Student.java containing all methods for Task 2
Purchase answer to see full
attachment