代写COMPSCI 4039 PROGRAMMING IT 2023代写Java程序

2024-12-20 代写COMPSCI 4039 PROGRAMMING IT 2023代写Java程序

PROGRAMMING IT

(COMPSCI 4039)

December 2023

Important note: throughout this exam, whenever you are asked to write Java code, do not worry about whether your code compiles. You should attempt to produce ‘correct’ code but the markers will never test any submitted code from this exam.

1. (a) Define a class named Book with the following attributes: title, author, and publicationYear. The publicationYear is stored as a number and the other two attributes are textual. Choose appropriate data types to store the attributes and include a parameterised constructor that initialises these attributes. [5]

(b) Override the constructor for the Book class to allow for default values of the attributes. The default value of title should be ”Unknown Title”, the default value of author should be ”Unknown Author” and the default value for publicationYear should be 0. You should define four additional constructors, one each for the cases where individual attributes are missing as well as a version where no attributes are known. [4]

(c) Create a method within the Book class that will allow the title, author and publicationYear for each book to be printed out using System.out.println(b), where b is a Book object. The book title, author and publication year should be printed out in that order, in the fixed width order given below (note that the periods should be printed as spaces – periods are just being used in this diagram so you can count them easily):

The Hobbit..........;....JRR Tolkien;.1937

The width of these 3 elements is respectively 20, 15 and 5 characters. [4]

(d) The method you created for (c) prints the attributes to specific lengths: what would be the result of applying this method to a (Book) object that has a very long title, for instance “Harry Potter and the Prisoner of Azkaban”? [2]

2. (a) Consider a class hierarchy for representing different shapes. Define the base class called Shape, which has an attribute numSides, for storing the number of sides of the shape, a single size attribute, size, and a method calculateArea, which returns the area of the shape. You should assume that there are no restrictions on the value that size can take, and therefore that calculateArea can return. When designining your base class you should use appropriate data types for all attributes and methods but assume that all values, including the return value of calculateArea, are zero. Include a basic constructor method as well as a calculateArea method in your base Shape class; [5]

(b) Create subclasses Circle and Rectangle, which inherit from Shape. For each, you should include any extra attributes needed to describe the shape as well as an appropriate calculateArea method. You should assume that a circle is a one-sided shape and note that the area of a circle is A = πr 2 where π = 3.14159 and r is the radius of the circle. [6]

(c) Override the constructor of Rectangle to allow for the situation where the rectangle being declared is in fact a square, i.e. a rectangle where all sides are the same length. [2]

(d) An oval can be considered to be like a circle with two different radiuses, a squashed one and a stretched one and an area that can be calculated as A = πr1r2. Describe the changes required to your Circle class that would allow it to also describe ovals. [2]

3. (a) The Book class you defined in 1a) is to be used by the university library to store the details of the books in their collection. Define an array javaTextbooks of Book objects that will store the details of the 47 Java textbooks in the collection. [3]

(b) You are given a comma separated file holding the details of these 47 textbooks. In this file the books are stored as

Title, Author, Year

with each line of the file representing a single book.

Write a method, readBooksFromFile, that will open the file and read the contents into the array declared in 3a). This method should be passed the name of the file and the empty array to be populated. Your method should be passed the filename, as a String named filename, and the empty Book array and return the array once it has been populated.

Your method should be reusable for other sections of the collection so you should not assume the array is of any particular size but you can assume the array is always the correct size to store all the books in the file.

Your method should also handle any exception raised if filename does not contain the name of a valid file. [10]

(c) Since the first version of Java was released in 1996 we know that the publicationYear of any of the Java textbooks cannot be before that year. Declare an exception, JavaBookYearException, to be thrown if a book appears to be unreasonably old and explain how it would be thrown if such a book is found. [2]

4. (a) Below is some Java code that generates an array of the Book objects we have been working with, having asked the user to enter the number of books that will be stored. It then prompts the user to enter the title, author and publication year, while performing checks on the values entered. It checks if the author entered is a specific name and if it is, prints a message to screen. No books published earlier than 1700 should be entered and the year is checked to ensure that it is valid. This code contains a number of errors. Find up to 6 errors and describe in detail how they could be repaired: there may be more than 6 errors but you will only be given credit for 6. The errors are a mix of logical errors (‘bugs’ that mean the code will not do what is expected) and syntax errors (such as spelling and punctuation errors or incorrect labels). Line numbers are given to help you report where the errors are.

(1) import java.util.Scanner;

(2) public class BookInputWithValidation {

(3) public static main(String[] args) {

(4) Scanner scanner = new Scanner(System.in);

(5) System.out.print("Enter the number of books: ");

(6) int numBooks = scanner.nextInt();

(7) Book bookArray = new bookArray[numBooks];

(8) for (int i = 0; i < numBooks; i++) {

(9) System.out.println("Enter details for Book " +

(i + 1));

(10) String title = scanner.next();

(11) System.out.print(Enter author: );

(12) String author = scanner.next();

(13) if (author == "JRR Tolkien") {

(14) System.out.println("Here be Hobbits!");

(15) }

(16) int publicationYear = scanner.nextInt();

(17) int publicationYear;

(18) do {

(19) System.out.print("Enter publication year " +

"between 1700 and 2023):");

(20) publicationYear = scanner.nextInt();

(21) if (publicationYear < 1700 ||

publicationYear > 2023) {

(22) System.out.println("Invalid year!

Please enter a year between " +

"1700 and 2023.");

(23) } while (publicationYear < 1700 &&

publicationYear > 2023);

(24) bookArray[i] = Book(author,

title, publicationYear);

(25) }

(26) System.out.println("Entered Book Details:");

(27) for (i=0;i<numBooks;i++)

(28) System.out.println(book[i].toString());

(29) scanner.close();

(30) }

(31) }                                                                                                         [12]

(b) The code in 4a) includes a do loop (lines 18 to 23). Explain why a do loop is the correct form. of iteration for this case and give the code for the case where a while loop was used instead. Include any extra code before the loop that would be needed to result in a while loop that has the same effect as the do loop in the code above.            [3]