ray
ray

Reputation: 21

Java object, interface

using UI-Model i have a problem in blue J the problem code is " class Operation is public , should be declared in a file named Operation.jave" model class

import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;

public class CalcModel extends Observable
{

    private JTextArea JDisplay;
 private JTextField JScreen;
     private OperandEntry eState;
    private String sScreen, sDisplay;
    private Operation operation; 
    private double dOperand1,dOperand2;


        private void ClearData(boolean bAll)
    {
        sScreen = "";
        int a= 1;
        if (bAll == true)
        {
            sDisplay = ("");
            a= 3;
            eState = OperandEntry.First;
            operation = null;            
        }
        SetUpdate(iChange);
        JScreen.setText("");

        }
    private void ProcessOpr()
    {
       dOperand2 = Double.parseDouble(sScreen);
       dOperand1 = objOpr.execute(dOperand1,dOperand2);
        sDisplay(dOperand2+ " = " + dOperand1+ " " );
        SetUpdate(x);
    }


      private Operation add = new Operation()
    {   public double execute(double d1,double d2)
        { return d1 + d2;
          SetUpdate(x);
        }
    };
     private Operation Div = new Operation()
        {                     

        public double execute(double d1,double d2)
        {  

            return d1 /d2;
           SetUpdate(x);
        }

    };
    private Operation sub = new Operation()
    {   
        public double execute(double d1,double d2)
        { return d1 -d2;
         SetUpdate(x);
        }
    };
    private Operation Multi = new Operation()
    {  
        public double execute(double d1,double d2)
        {
            return d1 * d2;
            SetUpdate(x);
        }

    };
    private Operation Mag = new Operation()
    {  
        public double execute(double d1,double d2)
         {
             if((int)d1 != d2 || (int)d1 != d1)
            {
                d1=(int)d1;
                d2=(int)d2;
                MsgInfo("Assuming Integers;" + d1 + " %" +d2);
            }

            return d1 % d2;
            SetUpdate(x);
        }

    };

    public void SetUpdate(ObservableState o)
    {
        setChanged();
        notifyObservers(o);
    }


  }
     public  interface Oparation 
    {
        public double execute(double d1,double d2);
    }

     public  enum OperandEntry{ 
         First, Second,ResultFirst
        };
     public  enum ObservableState{ Screen,Display,Both};

Upvotes: 0

Views: 324

Answers (5)

rsp
rsp

Reputation: 23373

If they are not needed at top level, you can place Operation, OperandEntry and ObservableState as children of your class CalcModel. If you do need them at top level, you must put them in a .java file of their own.

Upvotes: 0

Robert
Robert

Reputation: 6540

First of all, you've misspelled Operation in the interface definition. I don't know if that's a typo here or in your source

Second of all, a public interface or class needs to be in its own file. There's no reason that you can't have a file-local class:

interface Operation { ... }

or even a public class inside of another:

public class CalcModel extends Observable {
    ...
    public class Operation { ... }
}

You can the access the class as CalcModel.Operation

The error tells you all this. The class can't be public, or it needs to be in its own file.

Upvotes: 1

OscarRyz
OscarRyz

Reputation: 199215

In the line 100 you're declaring:

public interface Oparation {
...

Each top level element ( class or interface in this case ) should be saved in its own file ( just like the error message indicates )

Save that portion into "Oparation.java" file and try again.

Upvotes: 0

Eric Asberry
Eric Asberry

Reputation: 612

You can't have multiple public classes/interfaces in a single Java file. Remove the public keyword and nest them inside the CalcModel class if you want to keep them all in the same file, otherwise it needs to be in its own file.

Also, you misspelled the interface declaration "Oparation" (which you refer to as Operation elsewhere).

The code seems to have quite a few other issues as well ...

Upvotes: 0

Falmarri
Falmarri

Reputation: 48567

The error message tells you EXACTLY what you need to do.

class Operation is public , should be declared in a file named Operation.java

Upvotes: 2

Related Questions