How would you prevent a method from being overridden

Assignment Help JAVA Programming
Reference no: EM131737035

Assignment

1 What is the printout of running the class C (via the command "java C") in the code below (saved in C.java)? (And, is that what you expected?)

1: class A {

2: public A () {

3: System.out.println( "A's no-arg constructor is invoked" );

4: }

5: }

6:

7: class B extends A {

8: }

9:

10: public class C {

11: public static void main ( String [] args ) {

12: B b = new B();

13: }

14: }

2 What problem do you expect to see if compiling the program below? What was the error(s), if any, actually produced?

1: class A {

2: public A ( int x ) {

3: }

4: }

5:

6: class B extends A {

7: public B () {

8: }

9: }

10:

11: public class C {

12: public static void main ( String [] args ) {

13: B b = new B();

14: }

15: }

3 Which of the follow statements are true? Which are false? Explain why.

A subclass is a subset of a superclass.

B When invoking a constructor from a subcass, its superclass's no-arg constructor is always invoked.

C You can override a private method defined in a superclass.

D You can override a static method defined in a superclass.

4 What is the benefit of using the @Override annotation?

5 a Show the output of the following program:

1: public class Test {

2: public static void main ( String [] args ) {

3: A a = new A( 3 );

4: }

5: }

6:

7: class A extends B {

8: public A ( int t ) {

9: System.out.println( "A's constructor is invoked" );

10: }

11: }

12:

13: class B {

14: public B () {

15: System.out.println( "B's constructor is invoked" );

16: }

17: }

B Is the no-arg constructor of Object invoked when new A(3) is invoked?

6 Indicate true or false for the follow statements:

A You can always successfully cast an instance of a subclass to a superclass.

B You can always successfully cast an instance of a superclass to a subclass.

7 What's wrong with the following code?

1: public class Test {

2: public static void main ( String [] args ) {

3: Object fruit = new Fruit();

4: Object apple = (Apple) fruit;

5: }

6: }

7:

8: class Apple extends Fruit {

9: }

10:

11: class Fruit {

12: }

8 When overriding the equals method, a common mistake is mistyping its signature in the subclass. For example, the equals method is incorrectly written as equals(Circle circle) as shown in (a) below. It should be written as equals(Object circle), as shown in (b) below. Show the output of running class Test using the Circle class first from (a), and then from (b). Explain the output.

1: public class Test {

2: public static void main ( String [] args ) {

3: Object circle1 = new Circle();

4: Object circle2 = new Circle();

5: System.out.println( circle1.equals( circle2 ) );

6: }

7: }

1: class Circle {

2: double radius;

3: public boolean equals ( Circle circle ) {

4: return this.radius == circle.radius;

5: }

6: }

1: class Circle {

2: double radius;

3: public boolean equals ( Object circle ) {

4: return this.radius == ((Circle) circle).radius;

5: }

6: }

Next, try adding the "@Override" annotation to the equals method in (a), and then try compiling. Repeat with (b). What are the results, and are they as you expected?

9 How would you prevent a class from being extended? How would you prevent a method from being overridden?

10 Which of the following classes define legal abstract classes?

A 1: class A {

2: abstract void unfinished ( ) {

3: }

4: }

B 1: public class abstract A {

2: abstract void unfinished ( ) {

3: }

4: }

C 1: class A {

2: abstract void unfinished ( ) ;

3: }

D 1: abstract class A {

2: protected void unfinished ( ) ;

3: }

E 1: abstract class A {

2: abstract void unfinished ( ) ;

3: }

F 1: class A {

2: abstract int unfinished ( ) ;

3: }

11 Suppose A is an interface. Can you create an instance using "new A()"?

12 Which of the following (if any) is a correct interface declaration? (Assume I1 and I2 are correctly defined elsewhere.)

A 1: interface A {

2: void print () {

3: };

4: }

B 1: abstract interface A extends I1, I2 {

2: abstract void print () {

3: };

4: }

C 1: abstract interface A {

2: print () ;

3: }

D 1: interface A {

2: void print () ;

3: }

1 How do you create a Date for the current time? How do you display the current time?

2 How do you do the following tasks?

A Create an ArrayList for storing double values?

B Append an object to a List?

C Insert an object at the beginning of a List?

D Find the number of objects in a List?

E Remove a given object from a List?

F Remove the last object from a List?

G Check whether a given object is in a List?

H Retrieve an object at a specified index from a List?

3 Identify the errors in the following code fragment:

1: ArrayList<String> list = new ArrayList<String>();

2: list.add( "Denver" );

3: list.add( "Austin" );

4: list.add( new java.util.Date() );

5: String city = list.get( 0 );

6: list.set( 3, "Dallas" );

7: System.out.println( list.get(3) );

4 Suppose the ArrayList list contains duplicate elements. Does the following code correctly remove all elements from list? If not, correct the code.

1: for ( int i = 0; i < list.size(); i++ )

2: list.remove( element );

5 Explain why the following code fragment displays [1, 3] rather than [2, 3].

1: ArrayList<Integer> list = new ArrayList<Integer>();

2: list.add(1);

3: list.add(2);

4: list.add(3);

5: list.remove(1);

6: System.out.println( list );

6 How do you create an object of the java.util.Calendar class, that represents the current moment in time?

7 For a Calendar object c, how do you get its year, month, date, hour, minute, and second?

8 Using the java.time API, how can you add one month to a LocalDate object? How can you create a LocalDate object that represents the last Friday of the current month?

Reference no: EM131737035

Questions Cloud

Perform an independent analysis : Be specific about how such policies contribute to the firm's competitive advantage. Include at least one reference. Be sure to perform an independent analysis.
Npv of development with a lease-up phase : Consider a speculative development project with the following characteristics: The current (time 0) market value of the land is $2,000,000.
Determine the maximum flexural stress and its location : An 8 x 8 x 1 -in. angle is used for a beam that supports a positive bending moment of 7500 ft-lb. Determine the maximum flexural stress and its location
Road trip through the whitest towns in america : According to the speaker, what factors led to the development of the whitopias he describes? What "pushed" and what "pulled" this development?
How would you prevent a method from being overridden : How would you prevent a class from being extended? How would you prevent a method from being overridden? Which classes define legal abstract abstract classes?
Define self-analysis of personality : This exercise is meant to encourage you to think about your own personality in terms of several of the approaches we've considered during the summer
Describe the role that networking can play : Then develop a well-written paper that includes the following: Describe the role that networking can play in your professional life.
Thinking about expansion internationally : Thinking about expansion internationally, are you aware of a company who has expanded internationally?
What is the projected balance due on a construction loan : The following table shows the projected draws to pay the construction costs of a project that is expected to take four months to complete.

Reviews

Write a Review

JAVA Programming Questions & Answers

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd