Win a copy of OCP Oracle Certified Professional Java SE 11 Developer Practice Tests this week in the OCP forum!

salvin francis

Bartender
+ Follow
since Jan 12, 2009
salvin likes ...
Google Web Toolkit Eclipse IDE Java
Forum Moderator
salvin francis currently moderates these forums:
Mumbai
Cows and Likes
Cows
Total received
149
In last 30 days
14
Total given
169
Likes
Total received
433
Received in last 30 days
8
Total given
230
Given in last 30 days
11
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt Green check

Recent posts by salvin francis

This is a heuristic based algorithm. The solutions may range from simply looking at past data to advanced techniques such as machine learning algorithms that look at data and try to predict the next.
7 hours ago
Even if you don't have access to the source, the following command will work for you: where "foo.bar" is the package name. This would list out all the package/protected/public classes and members for the given class. Look specifically all the methods having "get" in the name and you might find a suitable candidate.
8 hours ago

Carey Brown wrote:It looks like logs is an array...

The increment operator wont work since it's a String array. Maybe OP wants messages in it.
1 day ago

Rob Spoor wrote:That second example is just evil...

I agree, I dislike it too.

I think that the original code is good enough. Since the else is implied, the condition is no longer needed. Here's a minor modification:
Having said that, there's a trick that you can do using Optional's map() method. The map method is evaluated only if an optional is present. This allows you to do something like this:In the above code,
  • if employee is null, we get a blank string
  • if employee is not null but address is null, we still get a blank string
  • if employee is not null and address is not null, but street is null, we still get a blank string

  • However, this does not match OP's requirement exactly and hence I would not suggest using Optional here.
    1 day ago
    First of all, the move statement looks odd:

    Is this a typo ? I see a few issues with the code above, mainly why does move method take the object's own x and y coordinates rather than a move offset ? and why does it need a reference to itself as a third parameter ?

    Next, can you give an example of how the logs array should look like at the end of the program?
    1 day ago
    Hi Tomas,
    I moved your post to the right forum and edited the title for a better response. I suggest modifying your post to include country or timezone for better response. You may include the hourly rate or similar if you wish.
    5 days ago
    Here's my suggestion:
    Explanation:
    Assumption: the dates and times are in 2 digit formats strictly.
    Let us know if it works
    6 days ago
    I have a slightly different thought process here...

    Does the 1 say that the board cell is occupied ?
    What's the use of having a large array of 0s and having only some of them as 1 ?

    I would imagine a simple array like this:


    where BoardPiece can be:


    If there is more than one "state" .. you may want to think about an enum here.
    1 week ago

    Campbell Ritchie wrote:Should we use Actions instead of action listeners?

    Yes, that's a good idea. It exactly matches our purpose in this case.
    2 weeks ago
    Let me explain a few sections of my code for better understanding. The code starts with a lambda expression:
    This can be written as:
    Effectively, the main program only creates an object of ShapeDrawingApp and then calls it's initUI() method. This follows the paradigm that https://coderanch.com/wiki/660020/Main-Pain
    Now, you might wonder why dont we directly call the method instead of using invokeLater. The answer is well explained in the java tutorials here: https://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
    It simply states:

    java tutorial wrote:all code that creates or interacts with Swing components must run on the event dispatch thread

    I suggest reading about lambdas a bit if you find that statement confusing: https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
    To put it simply, it's just a compact way of writing anonymous classes with a single method.

    Now, let's go to the variables... As you can see, I have only used 3 variables: The JFrame, the ButtonGroup and the ShapeDrawingPanel. This way, we only have field variables that are required to be accessed across methods.
    The ButtonGroup has a method called getSelection. This method returns the selected button from the "circle", "square", "rectangle" ones. Hence we don't a reference to each of them (your original circle, square, triangle, rectangle, sphere, cube, cone, cylinder, torus variables). We are only interested in the selected one. Let me know if that makes sense

    Moving to the initUI method, I created all panels in methods to keep the code neater and concise.
    Looking at the code for JRadioButton, the code you wrote was repeating the same steps for each of them, so this is where a loop comes into picture.

    The code reads as:
    We can discuss this further if it's a bit confusing. Streams may seem a bit scary at first ;)
    Once we have a list of all created JRadio buttons, we simply loop through them and add them to the button group as well as the panel. As you probably discovered already, adding them to the group creates this bond that only allows one of them to be selected. I have also taken the first item from the list using stream's findFirst() method and set it to selected by default. This is in line with your original code.

    Now that the GUI is done, we add the listeners to the buttons. Since shapeSelectionGroup is a field variable, we can access it and find out which one is selected every time the user clicks the button. Additionally, since drawShapePanel is a field variable as well, we can ask it to draw our selected shape. As Campbell rightly stated, the if-else condition is still not a good way to draw the shape especially given that we know that they can be different objects with behaviors.

    I'll let you work that one out, let us know if you are still unable to get that to work. Hint, Campbell has shared a nice little interface that can draw itself !!
    2 weeks ago
    I may have gone overboard in helping out OP. Not sure, but I don't mind if the post gets deleted.
    I can re-post using smaller code bits. e.g. Using actionCommand as a standalone example, etc...
    2 weeks ago
    Although we have a rule against doing this (https://coderanch.com/wiki/659991/Code-Mill), I am making an exception here since you have shared your complete code. I have modified your program and removed all the unnecessary code:

    Since your initial problem is based on listeners, notice how I have used actionCommand in the code above. You can also note that the total number of field variables are bare minimum. The shapes in my program are also responsive to resizing the frame. The code is a bit long since it's swing UI code. There are some TODOs in the code above that I suggest you can research on.
    Hope this helps.
    2 weeks ago
    paintComponent which is called during every layout calls getDim1/getDim2 which calls a JOptionPane.showInputDialog !!!
    This means that the user is prompted with a dialog every time the screen is repainted or layout is done. That's quite bad.
    2 weeks ago
    From a user experience standpoint, I don't think "Enter tuition" sounds right. Maybe you want to prompt the user to enter the total fee ? I am also curious about what is the expected output when user selects 2 or 3 from your displayed menu.
    2 weeks ago