Question: Java program Please Help Your class should have the following fields: myArray: a generic array numElements: an int which specifies how many items have been
Java program
Please Help
Your class should have the following fields:
- myArray: a generic array
- numElements: an int which specifies how many items have been added to the MyArray
- defCap: an int initialized to 10, which is the default size of a MyArray
- origCap: an int which specifies the original size of the MyArray
Your class should have the following methods:
- default constructor: Create an array of size 10, initialize origCap to 10 and numElements to 0.
- constructor with one int parameter: Create an array of the size given by the parameter, initialize origCap to the size of the array, and initialize numElements to 0.
- int size: Has no parms; returns numElements.
- boolean add(T item): append item to MyArray so that it is in the location right after what was the last element. Call expand if there is no room for another item, then add the item.
- boolean add(int loc, T item): add item to MyArray in location loc. Items from location loc to the end of the MyArray are shifted up to make room for item. For example, if the MyArray contains "yes" "no" "red" "green", add(2, "blue") will return "yes" "no" "blue" "red" "green". add can be used to add in any existing MyArray location or to the end of the MyArray. There cannot be any null items within the array, so if the array contains three elements you can add an element in loc three (the fourth element) but you can't add in any loc higher than that. Throw IndexOutOfBoundsException if loc is invalid. Call expand if there is no room for another item, then add the item.
- void expand(): expand is a private method, called by add to allocate a new larger array. The expand method creates a new array, copies the array values into the new array, and saves this new array in the myArray field. The size of the new array is the size of the existing array plus origCap.
- T remove(int loc): remove the element in position loc and return the element that was removed. Later items are moved down to fill in position loc, and the last position is set to null. For example, if the array contains "yes" "no" "blue" "red" "green", remove(2) will change the array to "yes" "no" "red" "green" null and return "blue". Decrement numElements. Throw IndexOutOfBoundsException if the subscript is invalid.
- T remove(T value): remove the first occurrence of value and return true. If value is not in the array it throws NoSuchElementException. Later items are moved down to fill in position loc, and null is put in the last position. Subtract one from numElements. For example, if the array contains "yes" "no" "blue" "red" "green", remove("blue") will change the array to "yes" "no" "red" "green" null and return "blue".
- T get(int): The parm is a subscript; get returns the array element at that subscript. For example, get(7) will return the 8th element in the array. Throws IndexOutOfBoundsException if the subscript is invalid. Use numElements as the upper bound, not the size of the array.
- void set(int loc, T item): set stores item in the array at location loc. For example, set(7, 44) will store 44 as the 8th element in the array. If loc does not contain an array element, throw ArrayIndexOutOfBoundsException.
- int indexOf(T item): search the array for item and return the subscript where item is found in the array. If item is not in the array return -1. If item is in the array more than once, return the subscript of the first occurrence.
- String toString(): return a String which contains the array elements separated by newlines.
After you complete your MyArray class use MyArrayTest.java to make sure that your MyArray class works correctly. Check the output from MyArrayTest to verify that your MyArray class is working correctly before you proceed to Part 3 of the lab.
In the client/demo code you will read a transcript from a file, print the transcript, and calculate and print the number of credits and GPA. Then you will add and drop current semester courses from the keyboard, and print the transcript again.
- Create a MyArray of Course objects.
- Read the student name, student number, and completed courses from the file transcript.txt. The file has student name on the first line, student number on the second line, and then the courses, with each field on a separate line, in the following order: course number, course name, section, department, semester (Fall, Spring, Winter, or Summer and year), grade, credits. For each course read the fields, create a Course object, and add it to the MyArray of Courses.
- Print the student name and student number, then print all completed courses.
- Calculate and print the total credits earned.
- Calculate and print the GPA. This table shows the points earned for each grade.

- Calculate the grade points for a course by multiplying the points earned by the number of credits. Then calculate the GPA, by adding the points earned for all courses and dividing by the total number of credits completed.
- Prompt the user to enter four courses being taken during the current semester. Use an empty String for the grade.
- Prompt the user to drop one of the courses. Prompt for the course number, department, and semester. Find the course in the MyArray and remove it.
grade points | 4.0 A- 3.7 B+ 3.5 B 3 2.7 B- C+ C 2.5 2.0 1.0 0 D F
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
