JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You create the new balance in newBalance, but then you ignore that.
Either just update Balance directly, or copy newBalance to Balance at the end of the transaction.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The "standard" solution is to parse the expression into RPN form using a stack, eg see http://faculty.cs.niu.edu/~hutchins/csci241/eval.htm

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Looks like its caused by an extra > on or near line 22374

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The purpose of if(flag) is a bit puzzling!

AFAICS it toggles on each pass of the loop so that the code on line 26 etc is executed on every other string. And yes, there are better ways to do that!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The web has many many tutorials that cover writing simple Android apps, so it's unrealistic to expect anyone here to write another one.
Google for tutorials, pick one you like, and try it.
If you get stuck then come back here, show what you have done so far, and explain what's blocking you. Someone will help.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

People have taken the time and effort to reply to you, and I can't delete your post(s) without deleting theirs, so the topic will remain as-is.
Hopefully others will read it and avoid making the same mistake.
JC

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If this is in the context of a Senior Java Developer then maybe they are looking for some more genrelaised approach such as
define a functional interface Rule that takes an int and returns a String or null
define a collection of Rules
populate it with the three Rules as simple lambda expressions
add a final rule that always returns the int as a String
try all the rules in turn against each int until a rule returns a non-null string.
Now its easy to add new rules that perform any kind of test on the int. Eg

        List<IntFunction<String>> rules = new ArrayList<>();
        rules.add(i -> i % 15 == 0 ? "BAZ" : null);
        rules.add(i -> i % 5 == 0 ? "BAR" : null);
        rules.add(i -> i % 3 == 0 ? "FOO" : null);
        rules.add(i -> String.valueOf(i));

        for (int i = 1; i <= 100; i++) {
            for (IntFunction<String> rule : rules) {
                String res = rule.apply(i);
                if (res != null) {
                    System.out.println(res);
                    continue;
                }
            }
        }
rproffitt commented: Meets spec. Ship it. +15
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It's hard to express fully what scum these people are.
They prey on worried students and take their money in exchange for which:

The student fails to learn whatever the course was teaching
The student gets thrown out when the cheating is discovered
Honest hard-working students have to compete with lying cheats
The teacher's time has been wasted

but then there are always some bottom feeders for whom no level is too low provided they can get a bit more cash, regardless of who gets hurt.

I was going to simply delete this rubbish spam, but maybe its better to leave it here, expose it for what it it, and let the scum see what we think of them.

JC

rproffitt commented: Worse than Rebel Scum. +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

At line 10 your variable "a" tells you how many asterisks to print. SO you can use a second loop there. Execute the new loop "a" times and print a single asterisk each time.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I'm not sure why this is a programming question - it's really a combination of aesthetics and ergonomics.
But in terms of programming the important thing is not to hard-code any colours. Make them some kind of global shared resource (details depend on programming language) and set or load the values at start-up time. This will also allow you to support multiple color themes, eg day and night. And it allows you to complete coding without any colour decisions, then try different colour pallets in the actual game to see which work best.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

An object is a combination of some data and some methods that together represent some kind of entity eg an employee, an invoice, an IP address etc etc.
Constructors are special methods that are part of the object's definiton and are responsible for initialising the state of any new object when it is created.

You define an object when you want to represent something (see above) in your program. That definition includes the definition of one or more constructors if you need to initialise new objects to some particular value or state.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK, that's good.
In my experience, licencing per-machine for small companies is full of problems. The client has a hardware crash and needs to run the app on another machine immediately; the app is on a couple of machines and an employee copies it to another machine not realising he's making an unlicenced copy; they only need to run one copy of the app at any one time, but it could be on either of 2 machines; the list goes on.
Given that you have, and want to keep, good relations then maybe you should look at licensing the client company based on their expected number of simultaneous users, with annual reviews. Burn the company name into the code you ship to them, and relax.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Manoj: don't hijack old topics. Start you own.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

FYI (if anyone was wondering) I found a solution and this is it:

I replace both routers with a single Asus running Merlin (enhanced Asus-wrt) that supports OpenVPN with routing rules. Now I just need that one router (so no LAN problems) and use outbound VPN rules so that only the TV devices use the VPN

rproffitt commented: That's the ticket. Fix this at the router level. +15
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

(item_count > 5) should work in most of the languages you have tagged this topic with, given a suitable variable declaration.

What you have posted is roughly equivalent to:

"change gear - tags: Ford, Chrysler, Harley Davidson, Tesla"

ie meaningless, not a question, conflicting tags, nowhere near enough info...

Try again, but this time ask a clear specific question.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Regardless of any language or syntax problems, your class hierarchy is wrong.

Inheritance is a "is-a-kind-of" relationship, as in:
Mammal is a kind of Animal
Cat is a kind of Mammal
so Cat is a subclass of Mammal, Mammal is a subclass of Animal.

Is a Course a kind of Student? Definitely not. Same for fee details.

Those classes are related by a "has-a" relationship - composition, not inheritance
a Student has (one or more) courses.
You implement that by Student having a variable that can contain one or more Courses, not by inheritance.

(Strictly speaking that is association rather than composition, but the distinction may be a bit subtle when just starting to learn OO)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Hi Guys. I knw what I want to achieve but not how to achieve it! Any hints would be gratefuly received, or, if it's not possible, tell me so I can stop trying.

I have an internet connection -> router with NAT, DHCP etc -> LAN with all kinds of switches and WiFi access points all on the same subnet.
I have a couple of TV devices that need to access the LAN and also access the internet via a VPN for access to the right countries.
So I connected a VPN router to the LAN and the TV devices to that router. Now all the TV traffic goes via the VPN - so far so good. But the VPN router also VPN's access to the local LAN addresses, so they are inaccessible.
I've fudged it for the moment with a physical 2-1 ethernet switch so I can switch the TVs direct to LAN or via the VPN router, which works, but obviously isn't a good solution.
What I think I need is a router to hang off my my LAN that will simple pass all local traffic but route all other addresses via the VPN.
Is there such a thing? Or is the another way to do it?
cheers
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

There's nothing "on the right"
Show what you have done so far.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK, I have written it. Have you? You can buy my version for only 99USD and pretend its yours if you want. Then you will pass your course and everyone will know how clever you are.

Now go back and read DaniWeb's rules , especially "Do provide evidence of having done some work yourself if posting questions from school or work assignments"

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Hi Crystal

I clicked your link feeling very suspicious, but when I read the page I was really impressed. Absolutely excellent essential advice. It's terrifying how many people start to develop something by diving into an IDE and bashing out random code. I'm adding that page to my "essential advice for devlopers" list. Thank you.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

err, no, no way, absolutely not
"This tutorial only works for Xcode 4.6 or lower." The current version is 9.2
and it uses Objective C rather than Swift, which was OK when that tutorial was written back in 2012, but not in 2018.

Why not go straight to the source and use Apple's own IOS developer training?

rproffitt commented: So many tutorials now. Need to check expiry dates from now on! Thanks. +15
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Mansoor.
It's incomprehensible how you could read the previous posts then add yours on the end.
Read them now.
Then read the thread that the previous post links to.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

@Elifas.
Did you bother to spend even a few moments reading the "Read this before posting a question" at the top of the page?

We do not waste time helping people who are so lazy that all they can do is post their homework task verbatim and wait for someone else to do something.

We help a lot pf people here, and we will help you, but only of you show some effort first.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You know the rules: we only help people who are showing some effort.
Show what you have done so far, explain what's stopping you going further, and someone wil help.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

No need to shout!

Let me explain:
at DaniWeb we help people who are learning or having a problem. We try to help by teaching them what they need to know to solve their own problem, and others like it in the future.
There's no way we will ever just do someone's work for them because they are to lazy or cannot be bothered to make some effort themselves.

So get on to Google, find some learning resources, read/study, then try to do this yourself. If/when you get stuck come back here, explain what you have done and what's stopping you, and someone will help.

ddanbe commented: Well said +15
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

What is it about that code that you don't understand?

ps: although it's legal Java, it's not very good code - obviously written by a beginner. Don't use it as an example to follow.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

@james - that's "Mad" world.

Oh yes. Now don't I look foolish. But Lambert really was amazing anyway.

Reverend Jim commented: Well, yeah. +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It would take you about one minute to try each of those modifiers in the java compiler to get an absolutely definitive answer.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Then fire up your text editor and start writing!
When you get stuck then come back here, show what you have done, and explain what’s stopping you.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Ok, just think of it like this.
You have a method that gives you a random int in a range starting at 0
You want to start at 1
That’s just 1+random int 0-9
You know how to get a random int in the range 0-9, and I’m quite certain you know how to add 1
:)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Break this down into easy steps.
Your destination is to have the database query running in a different thread from the Swing thread, because the whole gui will freeze during any processing that you do on its thread. That’s why you can use SwingWorker, for example.
But first, just ignore the threading and get the database query and GUI display working.
Then look at moving the query to separate thread. SwingWorker can be hard to understand, so maybe just use a simple Runnable in a Thread that you create, and hand the results back to a Gui update method via SwingUtilities exécuteLater

Ps please excuse typos, I’m on an iPad

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Your File object does not refer to any actual file (the file name is a zero length string) so it has not got a parent.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

He referred to setting a null layout manager, which, as you say, is generally a bad idea in almost all circumstances. (Eg any time there is any text being displayed!)
But he was specifically discussing some of the special cases where a null LM could be appropriate.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

EIQ Studio's

In English there is no apostophe in the plural "Studios". Not a good start.

Is this
a) a scam
b) wishful thinking by some teenager in his/her parent's bedroom
or
c) a real thing...
... in which case maybe you should concentrate less on using unskilled labour and get some people who actually know what they are doing, starting with the copy and visual design.

jwenting commented: you missed "amature" :) +15
rproffitt commented: My French friend says there's no apostrophe in French as well. +14
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, very interesting. Did you have a question?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

According to the output it seems finally was not executed despite the try block getting completed.

? line 3 of the output appears to be the message from line 23 in the finally block so it looks to me like the finally is being executed.

Be careful when reading a single output that combines System.out and System.err. They are separtely buffered, and may not appear in the same order that they were generated.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Hold fire... I have a better idea..

Imagine the rect. Now draw an extended shape around it that's distance r from the rect (where r is the radius of the circle). That shape has 4 straight segments and 4 ninety degree arc segments.
At the point of contact the center of the circle is on exactly one of those 4+4 segments.

Now draw the lines segment joining the position of the center of the circle at the previous time step with its position now.

If there was a collision then that line will intersect one of the 4+4 segments, and which one tells you which side/corner it collided with.

LIne2D has an intersects method that tells if two straight line segments intersect.
Line2D has a ptSegDist method that gives you the closest distance between a line segment and a point. If the point is the center of a circle and the distance is less than the radius then the segment intersects the circle. Voila! The Java API has al the geometric methods we need!

Just one possible complication: depending on the speeds and sizes and the size of the time step, the circle's center could enter and leave the extended shape in one tick. That case gives you two intersections, and you have to use the velocity to see which was the entry point. A little tedious set of if tests, but not in any way hard.

Summary: This algorithm should be exactly correct for all possible values, …

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The great thing about Shape is the Ellipses, Rectangles etc are all Shapes, so the only draw method you need is draw(Shape). So leverage the classes that Java gives you and define all your movable things ("Sprites" is the jargon) as being or having a Shape and they can all share (inherit) the same simple drawing method.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

What would this method be used for?

It tests for two Shapes overlapping. They can be rectanges, circles, polygons or completely arbitrary Shapes. If you have a Circle then that's the shape you should be using, not its bounding rectangle.

in the program I took that code from the moving objects ("Sprites") are actually transparent GIF Images. The Shape is a compound thing I hand-craft to fit reasonably round the visible part of the image so I can detect collisions between the visible parts of two Images.

I agree with AN's advice to get something simple working first before moving on to harder stuff. BUT you should accept that the simple stuff is just a learning exercise and not the base for a final implementation. Before you ge too far you have to take what you have learned, set aside the learning code, and think through a proper design for the real thing. Just adding complexity to an arbitrary simple case is a formula for chaos!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Have a look at java.awt.geom.Area - it implements Shape but represents a closed shape at a particular position. It has all kinds of useful methods that Shape doesn't have, including area.intersects(other area)
Here's code I used a while back to do an exact check on collision between two arbitrary Shapes. It gets the Area from each shape, translates them to the correct x.y position, and intersects them. If the intersection is empty then they do not overlap.

    private boolean exactCollision(Sprite s1, Sprite s2) {
        Area a1 = new Area(s1.getShape());
        a1.transform(AffineTransform.getTranslateInstance(s1.getX(), s1.getY()));

        Area a2 = new Area(s2.getShape());
        a2.transform(AffineTransform.getTranslateInstance(s2.getX(), s2.getY()));

        a1.intersect(a2);
        return !a1.isEmpty();
    }
AssertNull commented: Good link +8
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can check for the two Shapes (ellipse and rectangle) intersecting. Which side is then just a tedious testing of the x and y coordinates.
Bouncing off a side of a rectangle (assuming it;s horiontal/vertical) is trivial (1), but bouncing off a corner is a different question. If you get a decent solution then please share it.

(1) if circle.centre.y < top of rectangle && > bottom of rectangle and dx > 0 then it was the left side. Etc

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It looks like you have been experimenting with a variety of layout mangers!
Firstly - have you seen the Oracle tutorials - they are excellent. They start here: https://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

In my experience they are all too simpistic for anything but the simplest of requirements except GridBagLayout and SpringLayout. SpringLayout was designed specifically to support drag'n'drop GUI builders and is a pain to work with at the code level.
GridBagLayout is the one I always go to when I have an interesting design, especially if I want it to behave in a particular way when porting across OSs or when the window is resized.

Of course you can nest JPanels with a box layout here and a flow or grid layout there, but controlling that on a resize is a nightmare. I suggest you jump straight to GridBag. Yes, there's a bit of a learning curve, but learn it once and you're set up for life!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

No time for a proper answer now, but here's some code I use that I modified from something on the web that bounces balls a and b that have position x,y and velocity dx,dy. I gives bounces that look very realistic to me. It's very similar to your code,

        double ra = a.width / 2; // radius
        double rb = b.width / 2; // radius
        double radiiSquared = (ra + rb) * (ra + rb);  // square sum of radii
        double xDist = (a.x + ra) - (b.x + rb);
        double yDist = (a.y + ra) - (b.y + rb);
        double distSquared = xDist * xDist + yDist * yDist;
        if (distSquared > radiiSquared) return false; // balls don't touch

        // circles touch (or overlap) so they bounce off each other...
        double xVelocity = b.dx - a.dx;
        double yVelocity = b.dy - a.dy;
        double dotProduct = xDist * xVelocity + yDist * yVelocity;
        if (dotProduct > 0) { // moving towards each other
            double collisionScale = dotProduct / distSquared;
            double xCollision = xDist * collisionScale;
            double yCollision = yDist * collisionScale;
            //The Collision vector is the speed difference projected on the Dist vector,
            //thus it is the component of the speed difference needed for the collision.
            double combinedMass = a.width * a.width + b.width * b.width; // mass porp. to area
            double collisionWeightA = 2* b.width * b.width / combinedMass;
            double collisionWeightB = 2* a.width * a.width / combinedMass;
            a.dx += collisionWeightA * xCollision;
            a.dy += collisionWeightA * yCollision; …
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The "first version" is from your "I've changed this..." post - where you have one class for the game JPanel and another class (extends JFrame) for the main window, For me that's good design when each class represents one thing fully.

super. in constructors:
Netbeans is designed to support huge mission-critical projects and so warns of all kinds of code that, in theory, could cause bugs. Calling a method in a constructor, when the method could have been overridden and the override method assumed construction is complete can cause bugs. Will this happen in a one-person small project? Highly unlikely.
For now you can safely ignore that message - just keep your constructors small and simple, and be aware of what order things are being done in.

As for your other 3 issues - without seeiing the code there's nothing I can do.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Leaking in constructor...

The language spec only guarantees that the new object will be in a complete and consistent state after the constructor has finished.
When you add the key listener you pass a reference to you current object, so it's immediately possible for another class to call methods in your object, even though its constructor has not finished.
It's one of those theoretical warnings that rarely cause problems in practice, but the simplest way to keep safe in a case like this one is to finish creating the window and make it visible before adding any listeners.

ps I greatly preferred the first version. It shouldn't be the responsibility of a JPanel to create its own parent frame like in v2.
pps you don’t need all those supers when calling methods inherited from a superclass

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

first time the renderer gets called i get a null

You create a BallGame before you create a GameThread, but BallGame creates the window and makes it visible, which causes a call to Renderer's paintComponent, which tries to call the GameThread, which doesn't exist yet.
Don't try to fix this before you fix the structure, it;s just an artifact of the two JPanels mistake.

You may find this code useful - it's taken from a sample program I wrote to help teach sprite-based animation - but the following excerpt shows one way for the model view and controller to fit together and split up the responsibilities between them (ignore the Sprite references for the moment).

public class Animation2 {
    // Outline class structure demo for multi-sprite animation

    public static void main(String[] args) {
        SwingUtilities.invokeLater(Animation2::new);
    }

    // shared constants
    final AnimationModel model;
    final AnimationView view;
    final int TIMER_MSEC = 16; // mSec per clock tick

    Animation2() {
        model = new AnimationModel(600, 400);

        view = new AnimationView(model);
        view.setPreferredSize(new Dimension(model.width, model.height));

        new MainWindow(this);

        new ScheduledThreadPoolExecutor(1).
                scheduleAtFixedRate(this::updateAnimation, 0, TIMER_MSEC, MILLISECONDS);
    }

    void updateAnimation() {
        model.updateSimulation();
        view.repaint();
    }

    void close() { // tidies up and quits application
        // do any cleanup etc here
        System.exit(0);
    }

}

class AnimationModel {

    final List<Sprite> sprites = new ArrayList<>();
    final int width, height;

    AnimationModel(int width, int height) {
        this.width = width;
        this.height = height;
        sprites.add(new Sprite(this).velocity(2, 1));
        sprites.add(new BouncingSprite(this).
                color(green).size(80, 80).at(0, 250).velocity(7, -10));
        sprites.add(new BouncingHeavySprite(this).
                color(RED).size(60, 60).at(0, 0).velocity(4, 0));
    }

    void updateSimulation() {
        for (Sprite …
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

ps: I don't think ypur class structure is helping here... your GameThread class
1) Isn't a thread
2) Extends JPanel, but is never visible
3) Has lots of painting code that belongs in Renderer

It looks like maybe you have created classes to answer "where can I put this code?" rather than "what are the objects and their responsibilities needed for this application?"

For a Java Swing game the answer is almost always some kind of MVC as in (just an example):

class Model - represents the state of the game, update method moves it on one time unit.
class Renderer - extends JPanel, paintComponent displays the current state of the Model
class WIndow - extends JFrame, contains a Renderer plus buttons & controls as required, responds to keyboard.
class Controller - ties it all together, creates Window, Model and Renderer, starts a timer to update Model and repaint Renderer.

If you do that kind of design first then the resulting code will be much cleaner.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Interesting link - one man's opinion even if it's not so mainstream.
What he says about java,util.Timer and javax.swing.Timer is basically right, which is why the ScheduledThreadPoolExecutor is the recomended timing mechanism. It's just as easy as the others to use but gives you as much control over the threading as you want.

eg to run a method updateSimulation() immediately then every 30 mSec on a single thread:

new ScheduledThreadPoolExecutor(1).
                scheduleAtFixedRate(this::updateSimulation, 
                0, 30, MILLISECONDS);

If you really try with your own loop and thread management then maybe eventually you could make it as solid, safe, hardware/OS independent, and resiliant, but why bother?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

No, you call run directly in the constructor and that calls repaint, which results in a call to paintComponent - which may be before the constructor is finished, depending on how the threads are scheduled. If you want run to execute only after it then you need to call it from somewhere else or do some thread scheduling of your own.

I'm going to try this just one more time.
You should not use a loop with "something added" to get a steady fps in a Swing application. Use a java.util.Timer or ThreadPoolScheduler that will call your model update at a regular intervals (regardless of hardware) and do so without creating thread blocking problems. This is one of those absolutely basic concepts about Swing animation. Why are you opposed to it?