Results 1 to 11 of 11
  1. #1
    Professor is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default Need help with GUI

    So I am super new to java, haven't even finished my first e-book. I want to see if I can make my first program from scratch. It is basically a program that will let a user type in how much weed he has and it will tell him how much it is worth and what type of bag he has. The code I have so far is this
    Java Code:
    package Storage;
    
    
    public class Storage {
         static double[] weight = new double[12];
            static String[] weightNames = { "Blunt", "Half Eigth", "Dime", "Eighth",
        "Dub", "Quarter", "Half Ounce", "Ounce", "Quarter Pound", "Half Pound", "Pound", "Kilo" };
    
    
    
    public int getWeight() {
    
        int currentWeight = 0;
        return currentWeight;
    }
    }
    And then
    Java Code:
    class Convert extends Storage {
        public static void main(String[] args) {
        //set values
            weight[0] = 1.4;
            weight[1] = 1.7;
            weight[2] = 2.8;
            weight[3] = 3.5;
            weight[4] = 5.6;
            weight[5] = 7;
            weight[6] = 14;
            weight[7] = 28;
            weight[8] = 112;
            weight[9] = 224;
            weight[10] = 448;
            weight[11] = 985.6;
            //get users weight
            String myWeight = "";
            int userAmount = Integer.parseInt(myWeight);
            
            
            
                    
        }
    
    }
    It is not close to being finished however I need to add some type of GUI that lets the user type in his weight and will give a display etc... I have never messed with GUI so any help is better than none!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,988
    Rep Power
    9

    Default

    Hahahaha. Wow.

    Here's your starting point: Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Professor is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default

    Thanks bro I will start reading it now. I will probably have more questions with this program, one being, in the
    Java Code:
    if(userAmount > 0) {
    How will I make it to where it then fetches what the user put in and rounds it to the closest "weight" array, also how do I relate the weight and weightNames arrays?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,988
    Rep Power
    9

    Default

    You're asking a few different questions at once.

    One thing you might want to do is get this working on the command prompt first- that way you're not dealing with juggling a GUI with your program logic at the same time.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Professor is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default

    Your right, the logic should be worked out first. Care to help me with it?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,988
    Rep Power
    9

    Default

    I'm sure there are plenty of people here who can help with it. But you have to make an attempt first- what have you tried? Where did you get stuck? Your best bet is to ask a specific question and post an SSCCE that demonstrates what you're doing.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    santeron is offline Member
    Join Date
    Apr 2011
    Location
    Athens, Greece
    Posts
    52
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    ...you're not dealing...
    Dude, don't say stuff like dealing in this post... :D

    Yeah I agree with the command line idea. Get it working and then you can build a simple GUI on it.

    1) I would suggest you initialize the weight array right from the start.
    Java Code:
    private double[] weight = {1.4,1.7,...,985.6}
    2)
    Java Code:
    public int getWeight() {
    
        int currentWeight = 0;
        return currentWeight;
    }
    This doesn't make sense. You always get a 0 value from this because you initialize a new variable named "currentWeight" to zero and then return it. You should make currentWeight a class variable and then just return it's value from your getter method.

    3) In the convert class you don't get a user input. You just trying to make an int number out of an empty String. You need to look into the BufferedReader class a bit and get an actual user input to convert to a number.

    4) You don't need to extend Storage in Convert class. You extend a class if those two have something in common and you want to add functionality to the second one like having an Animal class and you say "Dog class extends Animal". A rule of thumb is using a IS-A keyword as in Dog IS-A(n) Animal. Convert IS-A Storage doesn't make sense.

    5)Let's say you managed to get the user input and it's 9. The easiest way I can think of is a series of if-ifelse clauses and you want to catch in which situation it applies. An example:
    Java Code:
    if (a >100) {
    //do something
    } else if (a >60) {
    //do something else
    } else if (a >40) {
    //do something else
    } else {
    //do something else
    }
    Another way is looping your strings and making a dynamic if clause from the values of your arrays but I'm not sure you could follow that with your current knowledge. If you want I could try to explain.

    Hope I helped and keep it clean while coding ;)

  8. #8
    Professor is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default

    First off I would like to say ty for the help.

    Sateron I don't understand what you mean by number 2. I didn't think setting the variable to 0 would do anything, but wasn't sure what I should do instead.

    Also the extends is just because when I was setting the weight array it didn't recognize weight[0] for some reason and when I extended Storage it did.

    I would like it if you would try and explain the loop also, the more I learn the better. :D because as you can see I am obviously a java noob.

  9. #9
    santeron is offline Member
    Join Date
    Apr 2011
    Location
    Athens, Greece
    Posts
    52
    Rep Power
    0

    Default

    I'm a noob too dw about it :P

    2) You declare a method doing some work. When you call that method from somewhere inside your code imagine what it would do by following the code line by line. So let's "decrypt" your code:
    Java Code:
    public int getWeight() {
        int currentWeight = 0;
        return currentWeight;
    }
    • public means that everyone can access it.
    • int means that this method returns an int value
    • getWeight is the name of the method
    • () means it gets no arguments when it's called
    • int currentWeight = 0; - You create a new variable of int type named currentWeight and you set it to 0.
    • return currentWeight; - You return the value of currentWeight... Which is 0 from the previous line. It never changes.
    What you should do is:
    Java Code:
    static String[] weightNames = {blah blah};
    int currentWeight; //By default it's initialized to 0 but you can write "= 0 " just to state that for sure
    
    public int getWeight() {
        return currentWeight;
    }
    So now you return the value of the class variable currentWeight which could be anything.
    --------------
    The next part is the whole point of Object Oriented programming. When you write to your main you should make a new Storage object. The class is like a blueprint of an object. You should write: Storage variableName = new Storage(); What you got now is a now storage object named variableName (use your own) and you can access that object's variables and methods by simply writing int myWeight = variableName.getWeight(). You should add a set method too, to replace the value of the weight variable of your object.
    --------------
    Seriously the loop will confuse you...

  10. #10
    Professor is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default

    I don't really understand that last part :S lol. Think you could dumb it down just a little bit more please?

  11. #11
    santeron is offline Member
    Join Date
    Apr 2011
    Location
    Athens, Greece
    Posts
    52
    Rep Power
    0

    Default

    You should have two classes, Storage and Convert.

    Storage will be used to create a new Storage object. Every class has this form:
    Java Code:
    class className {
    // Class variables.
    int a,b;    
    String c;  // You could use any type of data
    
    //Methods
    public int getA() {
        return a;
    }
    
    public void setA(int a) {
        this.a = a;
    }
    // some other methods that adds functionality to this class
    } //end of class
    So you build your class named Storage in this form (which you have)

    Now you have another class named Convert where you main is and you want to work with the Storage object. You create a new Storage object just like you do to a String object:
    Java Code:
    Storage myStorage = new Storage();
    Now that you have a Storage object named myStorage you can access all it's methods and variables (if they are not private). So by writing double tempStorage = myStorage.getWeight(); you get the value stored in the weight variable of object myStorage. You should add a setWeight(double) method too so you can pass the value the user has input in your object.

    Did I make it clearer?? :P

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •