Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Error: Should be declared in a file named

Tags:

java

I'm fairly new to Java and trying to figure out how to solve the following error:

Error reads

CalculatorWithMemory.java:1: class Calculator is public, should be declared 
  in a file named Calculator.java
public class Calculator

So my thought was that this means that I have to save 2 different .java files. However, this being for a class, I only have a provided text block to type my solution into so I cannot save these as .java files. Any thoughts on a solution would be great.

Thanks in advance!

To Provide all the information. I'm trying to solve for the following.

Question

The superclass Calculator contains:

  • a (protected) double instance variable, accumulator, that contains the current value of the calculator.

write a subclass, CalculatorWithMemory, that contains:

  • a double instance variable, memory, initialized to 0
  • a method, save, that assigns the value of accumulator to memory
  • a method, recall, that assigns the value of memory to accumulator
  • a method, clearMemory, that assigns zero to memory
  • a method, getMemory, that returns the value stored in memory
like image 659
Ren Avatar asked Apr 29 '12 23:04

Ren


1 Answers

As the error message implies, if you declare a class as public, it needs its own .java file. If you don't want to do that, then don't define it as public.

like image 177
Oliver Charlesworth Avatar answered Sep 22 '22 02:09

Oliver Charlesworth