代写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]





热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图