You are writing a number of Java Object Classes that will be used for inheritance. Some of the Java Class files will be part of a Java Package called Finance . Some other Java Class files will be part...

You are writing a number of Java Object Classes that will be used for inheritance. Some of the Java Class files will be part of a Java Package called
Finance. Some other Java Class files will be part of a Java Package called
Banking. What
Access Type Modifier
would you assign to the constructors and the instance methods of all the Class files written in the Finance Package to insure that
Classes
in the Banking Package have access to the constructors and the instance methods only?A. privateB. publicC. protectedD. default (no modifier)Answer: pg. 2Section 1: Classes and Objects (continued)
Problem 3.
Analyze the following code and select the correct answer below:public class Test1 { public static void main(String[] args) { int n = 2; xMethod(n); System.out.println("n is " + n); } public void xMethod(int n) { n++; } } A. The code has a compile error because xMethod is declared static. B. The code has a compile error because xMethod does not return a value. C. The code prints n is 1. D. The code prints n is 2. E. The code prints n is 3. Answer:


Name: Section 1: Problem 1. Analyze the following code and select the correct answer below: public class Test { public static void main(String[] args) { A a = new A( ); a.print(); } } class A { String s; public A( ) { } public A(String s) { this.s = s; } public void print() { if (this.s == null) System.out.println("String is Null"); else System.out.println(this.s); } } A. The program has a compile error because class A is not a public class. B. The Test program has a compile error. C. The program would compile and run if you change A a = new A( ) to A a = new A("5"). D. The program compiles and runs fine and prints nothing. Answer: Problem 2. You are writing a number of Java Object Classes that will be used for inheritance. Some of the Java Class files will be part of a Java Package called Finance. Some other Java Class files will be part of a Java Package called Banking. What Access Type Modifier would you assign to the constructors and the instance methods of all the Class files written in the Finance Package to insure that Classes in the Banking Package have access to the constructors and the instance methods only? A. private B. public C. protected D. default (no modifier) Answer: pg. 2 Section 1: Classes and Objects (continued) Problem 3. Analyze the following code and select the correct answer below: public class Test1 { public static void main(String[] args) { int n = 2; xMethod(n); System.out.println("n is " + n); } public void xMethod(int n) { n++; } } A. The code has a compile error because xMethod is declared static. B. The code has a compile error because xMethod does not return a value. C. The code prints n is 1. D. The code prints n is 2. E. The code prints n is 3. Answer: pg. 3 Name: Section 1: Classes and Objects (continued) Problem 4. Using the provided Book Class, write a method in Book Class called toString that returns a String that represents a book object. The output must be in the following format: *BookID: N1845 *Book Title: Introduction to Java *Book Type: NONFICTION *Book Cost: $31.50 public String toString( ) { Problem 5. The header for the 3-Arg constructor method in Book Class has been provided for you below. All instance variables must be set to a value. You must use the following methods from the Book Class in the constructor: validBookType (already provided in the class) setBookID (already provided in the class) setCost (already provided in the class) Additional requirements: When the book type instance variable is set it must be all uppercase. The valid book types are either “fiction” or “nonfiction”. The validBookType method determines if the book type is valid (“fiction” or “nonfiction”). If the book type is not a valid book type, the variable must be set to “UNKNOWN”. public Book(String booktitle, String booktype, double bookcost) { pg. 4 Name: Section 2: Composition Problem 1. Review the code below and then write the class method called dropStudent that drops a student out of the String array called students. The students String Array contains strings that represent student’s names. If the student is not found print out a message indicating that the student was not found. Import java.util.ArrayList; public class Course { private String courseName; private ArrayList students = new ArrayList(); public Course(String cn) { this.courseName = cn; } public void addStudent(String student) { students.add(student); } public void dropStudent(String student) { pg. 5 Section 2: Composition (continued) Problem 2. Relationship Matching (Match the relationship term with its relationship diagram). A. Composition: B. Inheritance: C. No Relationship: Relationship Diagram 1 ( public class Book { private String BookID; private String Title; . . . } ) ( public class Author { private String Name; private char Gender; . . . } ) Relationship Diagram 2 ( public class Book { private String BookID; private String Title; . . . } ) ( public class Author extends Book { private String Name; private char Gender; . . . } ) Relationship Diagram 3 ( public class Book { private String BookID; private String Title; . . . } ) ( public class Author Book { private Book [ ] bArray; private String Name; private char Gender; . . . } ) pg. 6 Name: Section 2: Composition (continued) Review the Job Class code and Person Class code. Problem 3. Write the 1-Arg Constructor for the Person Class. Write the input parameter and code block statements. public Person( ) { pg. 7 Name: Section 2: Composition (continued) Problem 4. Write the toString Method for the Person Class that would output the Person and Job information in the following output format: Person Name: John M. Smith Job History =========== *Company Name: Amazon Corp. *Job Role: Software Engineer *Salary: $90,500.00 *Company Name: Oracle Corp. *Job Role: Technical Trainer *Salary: $110,600.00 public String toString( ){ pg. 8 Name: Section 3: Inheritance The section 3 questions have to do with Class Inheritance. Review and use the four classes provided (DessertItem, Candy, Cookie, and IceCream) to answer questions in this section. (DO NOT USE ECLIPSE OR ANY OTHER IDE TO ANSWER THE QUESTIONS IN THIS SECTION!!) Problem 1. ssAssume all of the following code segments below appear in the main method of a class that is in the same package as the DessertItem, Candy, Cookie, and IceCream Classes. For each of these questions A – H below provide the correct answer or code statement for each. Each is worth 5 points. The code that is provided, or that you have to write would be executed in the main method and would be executed in sequential order. ArrayList dessert = new ArrayList(); DessertItem d1 = new DessertItem("Sugar Cookies"); dessert.add(new Candy("Gummy Bears", 1.0, 3.99)); dessert.add(new Cookie("Italian Sugar Cookies", 9, 4.99)); Candy c1 = new Candy("Fudge", 2.0, 2.99); dessert.add(new DessertItem("Caramal Apples")); dessert.add(new IceCream("Neapolitan Ice Cream", 3.50)); IceCream c2 = new IceCream("Chocolate Ice Cream", 1.10); Cookie c3 = new Cookie("Chocolate Chip Cookies", 12, 2.75); dessert.add(new Cookie("Peanut Butter Cookies", 5, 2.50)); A. Write a code statement that prints out the Sugar Cookies DessertItem object. What is printed? System.out.println(d1) B. Write a code statement that prints out only the Fudge DessertItem object weight and cost instance variables. What is printed? System.out.println(c1.getWeight()+ ”” + c1.getCost()); C. What method from what class is called when this code statement is executed: System.out.println(c3); D. What is the object removed by this command: dessert.remove(2); and what was the object’s Class. E. Write a code statement that prints out the Neapolitan Ice Cream DessertItem object cost instance variable. pg. 9 Name: F. Write a code statement that prints out how many DessertItem objects are in the dessert array. G. Write code statements that prints out a boolean value of true for the Gummy Bears DessertItem object. H. Write a code statement that prints out the Object Class for the Peanut Butter Cookies object. pg. 10
Nov 20, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here