Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

need help on the questions with asterisks (*) but help on anything else is appreciated too. 1 Submission Instructions Create a document using your favorite

need help on the questions with asterisks (*) but help on anything else is appreciated too.image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

1 Submission Instructions Create a document using your favorite word processor and type your exercise solutions. For exercises which require you to draw an image, insert the image into your word processing document. For exercises which require you to write a complete Java program, you do not need to insert the code into your word processing document, but you must submit the Java source code file named as requested. For exercises which ask you to only write a small bit of Java code or a method or two, insert the Java code into your word processing document. 3 ArrayLists 3.1 Write a method named Arraylist listlnit) that creates an Arraxist Integer> object named list and fills list with these numbers (using one or a pair of for or while loops) 912 3 45678 9 10 0 12345 678910 3.2 Consider the ArrayList object named list containing these Integers: list -1, 2, 3, 4, 5, 4, 3, 2,1, 0 What are the contents of list after this loop completes? for (int 1-1; object named list are negative. Print the count after the loop terminates. 34, write a void method void listinsertName(ArrayListstring t string pName) which has two input parameters: (1) BWEt i a list of first names; and (2) pNeme is a person's first name. We wish for the first names in pList to always be sorted into ascending order. The method shall insert pNeme into plist such that the sort order is maintained. Note that pName may be inserted at the beginning of pList, at the end of plist, or in the middle There are at least two methods one could use to solve this problem. The method you shall not use is to append pName to the end ofBbGX and then sort Bb (this method is too slow to be a viable solution). The method you shall use is to write a for or while loop that iterates over the Strings in BU (starting at index 0) until the index is reached where RNeremust be inserted so pList remains sorted 4 Text File n/utput 4.1 Explain what happens if you try to open a file for reading or writing that does not exist. 4.2 Name your source code file EX42.java. Write a program that prompts the user for the name of a Java source code file. The program shall read the source code file and output the contents to a new file named the same as the input file, but with a .tnt file name extension (e.g., if the input file is foo.java then the output file shall be named feo.iave.fnt). Each line of the input file shall be numbered with a line number (formatted as shown below) in the output file. For example, if the user enters EX42.java as the name of the input file and EX42.java contains: I/ // CLASS: EX42 (EX42.java) EX42 public static void main(StringlI pAres) public EX420 U then the contents of the output file EX42.java.fmt would be [001]/* ** [002]// CLASS: EX42 (EX42.java) [003]/** [004] public class EX42 [005 public static void pain(StringlI oAres) [006] [007] public EX420 [008] oo9] If the specified file cannot be opened for reading, then the program shall display an error message informing the user that the file the user entered could not be opened for reading and then the program shall terminate. If the output file cannot be opened for writing, then the program shall display a message informing the user that the output file could not be opened for writing and and then the program shall terminate Hint: to print an integer as shown above in a field of width 3 with leading O's, read about the PrintWriter.printf) method in the Text File I/O lecture notes. 5 Exceptions and Exception Handling 5.1. Explain the difference between throwing an exception and catching an exception, ie., explain what happens when an exception is thrown and when one is caught? 5.2 Explain what a checked exception is. Give one example. Explain what an unchecked exception is. Give one example 5.3 Why don't you need to declare that your method might throw an JndexOutofBoundsException? 5.4 Must the type of the exception object that is thrown be the same as the exception type declared in the catch clause that catches the exception? If not, why not? 5.5' What is the purpose of the finally clause? Give an example of how it can be used. 6 Objects and Classes 6.1 Explain how an instance method differs from a class method (static method) 6.2 Explain what happens if we write a class named C but do not implement any constructors in C. 6.3* (a) In a static method, it is easy to differentiate between calls to instance methods and calls to static methods. How do you tell the method calls apart? (b) Why is it not as easy to distinguish between calls to instance and static methods which are called from an instance method 6.4 Write the declaration for a public class named C that declares: (1) a private int instance variable named mX (2) a private int class variable named mY initialized to 0; (3) a private int class constant named A which is equivalent to 100; (4) a public int class constant named B which is equivalent to 200; (5) public accessor and mutator methods for X named getx) and setX: (6) public accessor and mutator methods for Y named getxl) and sety0: (7) a public constructor that has one int input parameter named RX which calls seen to initialize RXto BX (8) a public default constructor that calls qnt) to initialize HXto-1 6.5 Continuing with the previous exercise, write the declaration for a class named Main that implements the main) method. Within moin0, declare and instantiate a C object named cObji, calling the default constructor. Next, declare and instantiate another C object named cObj2, calling the second constructor to initialize cObj2.mX to -1. 6.6. Continuing, within Heino, are the following statements legal, i.e., do they compile? If so, explain what happens when the statement is executed. If not, explain why the statement is illegal. (a n a1 Sx (f) int a5 cObj1.getx0 (k) int a7 cObj1gety0 (b) int a2 m (g) cObj1.setx(20); (I) cObj1.setY(20) (c) int a3 SA h) cObj2.setX(cObj1.getxO): (m) int a8 Sget (e) cObj1.C(20) ) S.setx(20); 6.7 Continuing, suppose we add these two methods to the declaration of class C. For each assignment statement, if it is legal (compiles) explain what happens when the statement is executed. For each assignment statement that is illegal (syntax error) explain why the code is illegal public void { public static void g() { "x-0; Obiect Oriented Design and UML Class Diagrams 7.1 Below is the code for a Java class named C. Using a UML class diagram drawing tool, draw the UML class diagram that corresponds to this code (hand-drawn images will result in a score of 0). There are several free and open-source tools for drawing UML diagrams. Two reasonably simple-to-use and cross-platform (Windows, Mac, Linux) tools are: http://www.umlet.com Violet http://alexdp.free.fr/violetumleditor The instructor has been using Umlet for all of the class diagrams he has drawn for the lecture notes and he highly recommends it as it is very easy to learn how to quickly draw a nice-looking class diagram. Using Umlet you can export your diagram as an image file by selecting File | Export As... on the main menu. Since this exercise is not graded, you do not need to include your class diagram in your zip archive public class C Lpublic static final int CONST1 100; private int m private double mY private String z public C() this(0, 0,); public C(int pX, double gY String oZsetXieX): setYloY): setzleZ): public int get) return "x; } public int getu) { return} private String getZO { return H public void sex(int AX)( HX X public void setYlint gy eprivate void setzjint pZ) imZ-e1 7.2 Shown here is a UML class diagram which shows several classes that are associated in various ways. Using ymlet or some other UML class diagram tool, draw the class diagram and then modify the image as requested in the problem statement. For grading, export the image from your drawing tool as a PNG image and insert the PNG image into your word processing document. Make sure to size the image so the text is clearly readable. Also, include the PNG image in your zip archive. (a) Note that in Course there is an instance variable m Roster which is an instance of Roster. When a Course object is deleted, the Roster instance mRester will also be deleted Given that, what type of class relationship, if any, exists between Course and Roster? Ifthere is a relationship, modify the diagram to indicate the relationship type. If multiplicity applies, then label one or both ends of the relationship with the appropriate multiplicity. (b) Note that in Roster there is an instance variable mStudeats which is an Arravlist of Student objects. When a Roster object is deleted, mStudents is also deleted but each Student object within mStudents is Course Roster - mRoster: Roster - mStudents: ArrayList Student mName: String mld: int UndergradStudent GradStudent not deleted. Given that, what type of class relationship, if any, exists between Roster and Student. If there is a relationship, modify the diagram to indicate the relationship type. If multiplicity applies, then label one or both ends of the relationship with the appropriate multiplicity. (c) What type of class relationship, if any, exists between Student and ndergradStudent? If there is a relationship, modify the diagram to indicate the relationship type. If multiplicity applies, then label one or both ends of the relationship with the appropriate multiplicity. (d) Repeat (c) for Student and Sradstudent 8 Inheritance 8.1 Is it required to provide an accessor and/or mutator method for every instance variable of a class? If yes, explain why this is required, and if no, explain why not. 8.2 Suppose the class Sub extends Sandwich. Which of the following statements (a)-(d) are legal? Sandwich x - new Sandwichl Sub ynew Sub); (a) xy; (b) y x; (c) Sub y new Sandwich); (d) Sandwich x- new Subo; 8.3 True or False? A subclass declaration will generally contain declarations for instance variables that are specific to objects of that subclass, i.e., those instance variables represent attributes that are not part of superclass objects, whether you choose True or False, explain your answer, i.e., why you believe the statement is True or False 8.4' True or False? A superclass declaration will generally contain declarations for instance variables that are specific to objects of that superclass, i.e., those instance variables represent attributes that are not part of subclass objects, whether you choose True or False, explain your answer, i.e., why you believe the statement is True or False 8.5 Consider classes C1, C2, and C3. Answer the following questions. Regarding a class's instance variables, by "directly accessible", we mean that if we are executing, say, c2Method1), is it syntactically legal to write a statement that accesses, say, instance variable x3 of class C1, e.g., in c2Method1(), is -x3; a legal statement? Similarly, regarding a class's instance methods, by "callable", we mean that if we are executing, say, c3Method2(), is it syntactically legal to write a statement c1Method2(1; to call that method which is declared in C1? class C1 class C2 extends C1 protected int x2; class C3 extends C2 public intx1 public int y1 public int z1; protected int y2; protected int z2; private int y3 private int x3, public void c1Method10public void c2Method1 private int z3; public void c3Method10 protected void c1Method20 0 protected void c2Method20 c3Method30 0 protected void c3Method20 t private void c1Method3) private void c2Method30 0 private void 1. Which instance variables x1, x2, x3 declared in C1 are directly accessible in c1Method10? 2. Same variables, in c1Method20? 3. Same variables, in c1Method3)? 4. Which instance variables x1, x2, x3 declared in C1 are directly accessible in c2Method1)? 5. Same variables, in c2Method20? 6. Same variables, in c2Method3)? Which instance variables x1, x2, x3 declared in C1 are directly accessible in c3Method1)? 8. Same variables, in c3Method20? 9. Same variables, in c3Method3)? 10. Which instance variables y1, y2, y3 declared in C2 are directly accessible in c1Method1)? 11. In c1Method20)? 12. In c1Method30? 13. Which instance variables z1, z2, Z3 declared in C3 are directly accessible in c1Method1)? 14. Same variables, in c1Method2()? 15. Same variables, in c1Method3()? 16. Which instance methods c1Method10, c1Method2(), c1Method3() are callable from c2Method1()? 17. Which C1 methods are callable from c2Method2()? 18 Which C1 methods are callable from c2Method3()? 19. Which instance methods c1Method1(0, c1Method2(), c1Method3() are callable from c3Method1)? 20. Which C1 methods are callable from c3Method20)? 21 Which C1 methods are callable from c3Method3()? 22. Which instance methods c2Method1(), c2Method2(), c2Method30 are callable from c1Method1(0? 23. Which C2 methods are callable from c1Method2()? 24. Which C2 methods are callable from c1Method3()? 25. A C1 object is created as a block of memory. Within that block of memory, how many instance variables exist? 26. A C2 object is created as a block of memory. Within that block of memory, how many instance variables exist? 27. A C3 object is created as a block of memory. Within that block of memory, how many instance variables exist? 8.6 Explain what an overloaded method is and give an example by writing some Java code 8.7" Explain what an overridden method is and give an example by writing some Java code 8.8 Explain what accidental overloading is and the preferred Java method for preventing it. 8.9* If an overridden method in a subclass needs to call the overridden superclass method, how is this accomplished? 8.10 True or False? It is legal to write a method in a class which overloads another method declared in the same class. Explain your answer 8.11 True or False? It is legal to write a method in a class which overrides another method declared in the same class. Explain your answer 8.12 True or False? It is legal in a subclass to write a method which overloads a method declared in the superclass. Explain your answer 8.13 True or False? It is legal to write a method in a subclass which overrides a method declared in the super-class. Explain your answer 8.14 In a subclass constructor, the superclass default constructor is automatically called before the statements of the subclass constructor begin executing. Suppose we wish to call a different superclass constructor (i.e., not the default constructor) from the subclass constructor. Explain how this is accomplished and give a Java example 1 Submission Instructions Create a document using your favorite word processor and type your exercise solutions. For exercises which require you to draw an image, insert the image into your word processing document. For exercises which require you to write a complete Java program, you do not need to insert the code into your word processing document, but you must submit the Java source code file named as requested. For exercises which ask you to only write a small bit of Java code or a method or two, insert the Java code into your word processing document. 3 ArrayLists 3.1 Write a method named Arraylist listlnit) that creates an Arraxist Integer> object named list and fills list with these numbers (using one or a pair of for or while loops) 912 3 45678 9 10 0 12345 678910 3.2 Consider the ArrayList object named list containing these Integers: list -1, 2, 3, 4, 5, 4, 3, 2,1, 0 What are the contents of list after this loop completes? for (int 1-1; object named list are negative. Print the count after the loop terminates. 34, write a void method void listinsertName(ArrayListstring t string pName) which has two input parameters: (1) BWEt i a list of first names; and (2) pNeme is a person's first name. We wish for the first names in pList to always be sorted into ascending order. The method shall insert pNeme into plist such that the sort order is maintained. Note that pName may be inserted at the beginning of pList, at the end of plist, or in the middle There are at least two methods one could use to solve this problem. The method you shall not use is to append pName to the end ofBbGX and then sort Bb (this method is too slow to be a viable solution). The method you shall use is to write a for or while loop that iterates over the Strings in BU (starting at index 0) until the index is reached where RNeremust be inserted so pList remains sorted 4 Text File n/utput 4.1 Explain what happens if you try to open a file for reading or writing that does not exist. 4.2 Name your source code file EX42.java. Write a program that prompts the user for the name of a Java source code file. The program shall read the source code file and output the contents to a new file named the same as the input file, but with a .tnt file name extension (e.g., if the input file is foo.java then the output file shall be named feo.iave.fnt). Each line of the input file shall be numbered with a line number (formatted as shown below) in the output file. For example, if the user enters EX42.java as the name of the input file and EX42.java contains: I/ // CLASS: EX42 (EX42.java) EX42 public static void main(StringlI pAres) public EX420 U then the contents of the output file EX42.java.fmt would be [001]/* ** [002]// CLASS: EX42 (EX42.java) [003]/** [004] public class EX42 [005 public static void pain(StringlI oAres) [006] [007] public EX420 [008] oo9] If the specified file cannot be opened for reading, then the program shall display an error message informing the user that the file the user entered could not be opened for reading and then the program shall terminate. If the output file cannot be opened for writing, then the program shall display a message informing the user that the output file could not be opened for writing and and then the program shall terminate Hint: to print an integer as shown above in a field of width 3 with leading O's, read about the PrintWriter.printf) method in the Text File I/O lecture notes. 5 Exceptions and Exception Handling 5.1. Explain the difference between throwing an exception and catching an exception, ie., explain what happens when an exception is thrown and when one is caught? 5.2 Explain what a checked exception is. Give one example. Explain what an unchecked exception is. Give one example 5.3 Why don't you need to declare that your method might throw an JndexOutofBoundsException? 5.4 Must the type of the exception object that is thrown be the same as the exception type declared in the catch clause that catches the exception? If not, why not? 5.5' What is the purpose of the finally clause? Give an example of how it can be used. 6 Objects and Classes 6.1 Explain how an instance method differs from a class method (static method) 6.2 Explain what happens if we write a class named C but do not implement any constructors in C. 6.3* (a) In a static method, it is easy to differentiate between calls to instance methods and calls to static methods. How do you tell the method calls apart? (b) Why is it not as easy to distinguish between calls to instance and static methods which are called from an instance method 6.4 Write the declaration for a public class named C that declares: (1) a private int instance variable named mX (2) a private int class variable named mY initialized to 0; (3) a private int class constant named A which is equivalent to 100; (4) a public int class constant named B which is equivalent to 200; (5) public accessor and mutator methods for X named getx) and setX: (6) public accessor and mutator methods for Y named getxl) and sety0: (7) a public constructor that has one int input parameter named RX which calls seen to initialize RXto BX (8) a public default constructor that calls qnt) to initialize HXto-1 6.5 Continuing with the previous exercise, write the declaration for a class named Main that implements the main) method. Within moin0, declare and instantiate a C object named cObji, calling the default constructor. Next, declare and instantiate another C object named cObj2, calling the second constructor to initialize cObj2.mX to -1. 6.6. Continuing, within Heino, are the following statements legal, i.e., do they compile? If so, explain what happens when the statement is executed. If not, explain why the statement is illegal. (a n a1 Sx (f) int a5 cObj1.getx0 (k) int a7 cObj1gety0 (b) int a2 m (g) cObj1.setx(20); (I) cObj1.setY(20) (c) int a3 SA h) cObj2.setX(cObj1.getxO): (m) int a8 Sget (e) cObj1.C(20) ) S.setx(20); 6.7 Continuing, suppose we add these two methods to the declaration of class C. For each assignment statement, if it is legal (compiles) explain what happens when the statement is executed. For each assignment statement that is illegal (syntax error) explain why the code is illegal public void { public static void g() { "x-0; Obiect Oriented Design and UML Class Diagrams 7.1 Below is the code for a Java class named C. Using a UML class diagram drawing tool, draw the UML class diagram that corresponds to this code (hand-drawn images will result in a score of 0). There are several free and open-source tools for drawing UML diagrams. Two reasonably simple-to-use and cross-platform (Windows, Mac, Linux) tools are: http://www.umlet.com Violet http://alexdp.free.fr/violetumleditor The instructor has been using Umlet for all of the class diagrams he has drawn for the lecture notes and he highly recommends it as it is very easy to learn how to quickly draw a nice-looking class diagram. Using Umlet you can export your diagram as an image file by selecting File | Export As... on the main menu. Since this exercise is not graded, you do not need to include your class diagram in your zip archive public class C Lpublic static final int CONST1 100; private int m private double mY private String z public C() this(0, 0,); public C(int pX, double gY String oZsetXieX): setYloY): setzleZ): public int get) return "x; } public int getu) { return} private String getZO { return H public void sex(int AX)( HX X public void setYlint gy eprivate void setzjint pZ) imZ-e1 7.2 Shown here is a UML class diagram which shows several classes that are associated in various ways. Using ymlet or some other UML class diagram tool, draw the class diagram and then modify the image as requested in the problem statement. For grading, export the image from your drawing tool as a PNG image and insert the PNG image into your word processing document. Make sure to size the image so the text is clearly readable. Also, include the PNG image in your zip archive. (a) Note that in Course there is an instance variable m Roster which is an instance of Roster. When a Course object is deleted, the Roster instance mRester will also be deleted Given that, what type of class relationship, if any, exists between Course and Roster? Ifthere is a relationship, modify the diagram to indicate the relationship type. If multiplicity applies, then label one or both ends of the relationship with the appropriate multiplicity. (b) Note that in Roster there is an instance variable mStudeats which is an Arravlist of Student objects. When a Roster object is deleted, mStudents is also deleted but each Student object within mStudents is Course Roster - mRoster: Roster - mStudents: ArrayList Student mName: String mld: int UndergradStudent GradStudent not deleted. Given that, what type of class relationship, if any, exists between Roster and Student. If there is a relationship, modify the diagram to indicate the relationship type. If multiplicity applies, then label one or both ends of the relationship with the appropriate multiplicity. (c) What type of class relationship, if any, exists between Student and ndergradStudent? If there is a relationship, modify the diagram to indicate the relationship type. If multiplicity applies, then label one or both ends of the relationship with the appropriate multiplicity. (d) Repeat (c) for Student and Sradstudent 8 Inheritance 8.1 Is it required to provide an accessor and/or mutator method for every instance variable of a class? If yes, explain why this is required, and if no, explain why not. 8.2 Suppose the class Sub extends Sandwich. Which of the following statements (a)-(d) are legal? Sandwich x - new Sandwichl Sub ynew Sub); (a) xy; (b) y x; (c) Sub y new Sandwich); (d) Sandwich x- new Subo; 8.3 True or False? A subclass declaration will generally contain declarations for instance variables that are specific to objects of that subclass, i.e., those instance variables represent attributes that are not part of superclass objects, whether you choose True or False, explain your answer, i.e., why you believe the statement is True or False 8.4' True or False? A superclass declaration will generally contain declarations for instance variables that are specific to objects of that superclass, i.e., those instance variables represent attributes that are not part of subclass objects, whether you choose True or False, explain your answer, i.e., why you believe the statement is True or False 8.5 Consider classes C1, C2, and C3. Answer the following questions. Regarding a class's instance variables, by "directly accessible", we mean that if we are executing, say, c2Method1), is it syntactically legal to write a statement that accesses, say, instance variable x3 of class C1, e.g., in c2Method1(), is -x3; a legal statement? Similarly, regarding a class's instance methods, by "callable", we mean that if we are executing, say, c3Method2(), is it syntactically legal to write a statement c1Method2(1; to call that method which is declared in C1? class C1 class C2 extends C1 protected int x2; class C3 extends C2 public intx1 public int y1 public int z1; protected int y2; protected int z2; private int y3 private int x3, public void c1Method10public void c2Method1 private int z3; public void c3Method10 protected void c1Method20 0 protected void c2Method20 c3Method30 0 protected void c3Method20 t private void c1Method3) private void c2Method30 0 private void 1. Which instance variables x1, x2, x3 declared in C1 are directly accessible in c1Method10? 2. Same variables, in c1Method20? 3. Same variables, in c1Method3)? 4. Which instance variables x1, x2, x3 declared in C1 are directly accessible in c2Method1)? 5. Same variables, in c2Method20? 6. Same variables, in c2Method3)? Which instance variables x1, x2, x3 declared in C1 are directly accessible in c3Method1)? 8. Same variables, in c3Method20? 9. Same variables, in c3Method3)? 10. Which instance variables y1, y2, y3 declared in C2 are directly accessible in c1Method1)? 11. In c1Method20)? 12. In c1Method30? 13. Which instance variables z1, z2, Z3 declared in C3 are directly accessible in c1Method1)? 14. Same variables, in c1Method2()? 15. Same variables, in c1Method3()? 16. Which instance methods c1Method10, c1Method2(), c1Method3() are callable from c2Method1()? 17. Which C1 methods are callable from c2Method2()? 18 Which C1 methods are callable from c2Method3()? 19. Which instance methods c1Method1(0, c1Method2(), c1Method3() are callable from c3Method1)? 20. Which C1 methods are callable from c3Method20)? 21 Which C1 methods are callable from c3Method3()? 22. Which instance methods c2Method1(), c2Method2(), c2Method30 are callable from c1Method1(0? 23. Which C2 methods are callable from c1Method2()? 24. Which C2 methods are callable from c1Method3()? 25. A C1 object is created as a block of memory. Within that block of memory, how many instance variables exist? 26. A C2 object is created as a block of memory. Within that block of memory, how many instance variables exist? 27. A C3 object is created as a block of memory. Within that block of memory, how many instance variables exist? 8.6 Explain what an overloaded method is and give an example by writing some Java code 8.7" Explain what an overridden method is and give an example by writing some Java code 8.8 Explain what accidental overloading is and the preferred Java method for preventing it. 8.9* If an overridden method in a subclass needs to call the overridden superclass method, how is this accomplished? 8.10 True or False? It is legal to write a method in a class which overloads another method declared in the same class. Explain your answer 8.11 True or False? It is legal to write a method in a class which overrides another method declared in the same class. Explain your answer 8.12 True or False? It is legal in a subclass to write a method which overloads a method declared in the superclass. Explain your answer 8.13 True or False? It is legal to write a method in a subclass which overrides a method declared in the super-class. Explain your answer 8.14 In a subclass constructor, the superclass default constructor is automatically called before the statements of the subclass constructor begin executing. Suppose we wish to call a different superclass constructor (i.e., not the default constructor) from the subclass constructor. Explain how this is accomplished and give a Java example

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions