• Post Reply Bookmark Topic Watch Topic
  • New Topic

Correcting an implementation of an interface  RSS feed

 
Oscar E Rodriguez
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was given the following interface:

and asked to:
"Fix the following class B to make it compile without errors.
Note you cannot change that fact that it implements
Interface A."

So I made the following changes to class B:

and resolved all the errors in compiling referring to lines in class B. However it still gives me the following error in line one:

class A is public, should be declared in a file named A.java
public interface A {

I am able to resolve this compiling error by simply removing the "public" from the



Is there any other way to resolve this last compiling error without removing public from the first line of code or altering the first 5 lines of the code that define the interface, for that matter?

Thank you, in advance for your help...and thank you to everyone at JavaRanch Big Moose Saloon. I have never tried to reach out to a Java forum, or any forum for that matter, but everyone here made it not only easy but one of the best learning experiences I've had. I just wish I had known about you guys when I first started trying to learn Java. Thank you.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Public types need to be defined in their own .java file, with the file name the same as the type name. So if you wanted an interface defined as:

public interface A

Then you will need to put it into a file named

A.java

You would not be able to define it in the same .java file you are using for Class B, which would be

B.java

since there can be only one public top level type per .java file.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think that was what you were supposed to do. Put the two classes in their own files, then correct the class A so it compiles.
 
Oscar E Rodriguez
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for the feedback, Steve and Campbell.

I considered what you both recommended, but given that it explicitely asks to fix class B, I wasn't sure if the assignement allowed me to create the two files (A.java and B.java) or alter the code in class A.

I will send a e-mail to my professor for clarifcation and get back to you both.

Thanks, again.
 
Oscar E Rodriguez
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Oscar E Rodriguez:
thank you for the feedback, Steve and Campbell.

I considered what you both recommended, but given that it explicitely asks to fix class B, I wasn't sure if the assignement allowed me to create the two files (A.java and B.java) or alter the code in class A.

I will send a e-mail to my professor for clarifcation and get back to you both.

Thanks, again.


I spoke to my professor and he told me that I should keep in mind that the question comes from an older written version of the test and that I should find a way to correct it as if I was sitting in a class room with the question in front of me and with a pencil make corrections to the code as instructed. That is why I made the changes I did, but now it won't compile giving me the "class A is public...." error I described before.
Here is the original question again:

Given the following interface

Fix the following class B to make it compile without errors.

Note you cannot change that fact that it implements
Interface A.

Any ideas would be greatly appreciated.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And your first suggestion looks perfectly all right. The problem is not that you have written anything wrong, but you are mistranslating writing to code. You can write public class A and public class B on the same sheet of paper; just don't put them in the same file.
 
Oscar E Rodriguez
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
And your first suggestion looks perfectly all right. The problem is not that you have written anything wrong, but you are mistranslating writing to code. You can write public class A and public class B on the same sheet of paper; just don't put them in the same file.


Thank you for all your help this past 2 weeks, Campbell.

I see what you are saying....I think.

When my professor was asking in the question to:
"Fix the following class B to make it compile without errors."

He was saying to make it so that just class B compiles without errors, which is what I did...right?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write out two Java files called A.java and B.java and put the classes or interfaces of those names in the files of those names, and get them to compile. Then you will see that you have got it right.

Has your professor done something to scare you? Or are you simply worrying unnecessarily?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!