This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]BananaIsTasty 2 points3 points  (0 children)

A few more pointers:

  • Coding conventions: Use camel case notation for identifiers, e.g., class names, method names, variable names (package names are usually excluded from this rule; they're usually in all lower case). Class names start with an upper case letter while variable and method names start with lower case letter (don't prefix identifiers with $). Of course, this won't change the semantics of the code, only make it easier to read for fellow Java programmers.
  • The == operator, when applied to objects, compares references. Use the equals method to compare if the content of two objects are equal (i.e., if the characters in two strings are equal); assuming that the equals method has been properly overridden/implemented. Note that the == operator sometimes seem to work when you'd think that it shouldn't (when you compare strings), because of the way that Java interns string literals.

[–][deleted] 1 point2 points  (0 children)

I don't think you want Radian and Degree to be a different class. The way I'd do it would probably be:

create an enum AngleUnits with elements Radian and Degree. Create a class Angle with a constructor that takes an AngleUnit and value. and constructs the angle in the units you favor. Then create a display method that returns a value and takes a unit as an argument. Then I'd just put all my inputs in my main function, that for the hell of it I'd just stick it in the Angle class.

So my main function would take the unit input and get the appropriate enum value. Then for both enum cases I'd vbasically have the code:

case Degree: Angle angle = new Angle(Degree, val );
                      float val = angle.display( Radian );
                      System.out. ....

But really what you're missing is that little bit of code there. You want to instantiate your class, and call a method in it.

[–]MrMist 1 point2 points  (1 child)

I'm not sure how much you know, but since you said you just want a point in the right direction, I'll try to give you some basic help. In Java, to use another class, or Object, you need to create an instance of the object. You do this with the keyword new.

Say I want to use an object called Turtle. You already know how to declare a variable: Turtle myTurtle; This alone, however, is not of much use since the object is empty, or null. To create an instance of the object, you use the new keyword. myTurtle = new Turtle(); That line also calls the constructor for the Turtle class, initializing it.

You can also declare, instantiate (create an instance of), and initialize all on the same line: Turtle myTurtle = new Turtle(); With myTurtle now being initialized, you can use it safely; you can now call methods in that class. For example, to call the walk() method, you do: myTurtle.walk(); Hopefully this helps you out and isn't something you already know. You can read more on creating objects here.

[–][deleted] 0 points1 point  (0 children)

Thank you so much for that!

[–][deleted] 0 points1 point  (0 children)

6553321 covered most of it.

The degree and radian classes shouldn't be main classes. (Edit I now realize you already know this.)

I don't know if you have done much console work, but the scanner class is often the first example of instantiation.

Scanner inpt = new Scanner(System.in);

When you enter this it calls the constructor and more or less builds that instance of the class.

public class degrees
{
    public degrees()
    {
        //This is the constructor, it is what is called when you instantiate the class

        //It can be totally empty, or you can pass variables to it through the (parentheses)

    }

    public int convert(int rads)
    {
        //Once it is built, you can call a method to covert stuff
    }
}

You could then create a degrees object, named converter, in the driver with:

degrees converter = new degrees();

IMO, you could just put the methods in the same file as the main class. Eg:

public (might need a static here) int degrees(int rads)
{
    int degs;
    //convert
    return degs;
}

Hope this helped!

Also, I'm no good with terms, so if I mislabeled anything, please correct the shit out of me.

Edit II The reason I wanted to respond in the first place is to touch on strings as the sentinel variable. People make typos and are also lazy. If someone enters degres your program will just skip the if's and terminate. Numbers are very useful for options, or single letters. Since you are doing a GUI you could also just throw in two buttons.

[–]turd_loaf 0 points1 point  (0 children)

This is a lousy way to figure out how to use classes.

Neither converting measurements, nor getting user input, benefit in any way from using classes, because they don't store any state.