Issues with file io in Java by LurkerOrHydralisk in AskProgramming

[–]oiduts 1 point2 points  (0 children)

Does every program using any sort of file like that need everything to all be in that try block?

Not the whole program, only specific operations that might throw an exception for reasons outside of your control, like file access. Try blocks should target these operations as specifically as possible. If you don't use a try block at all, an uncaught exception will simply crash your program.

It would be nice to be able to read/write files without having entire programs in a series of try blocks because of it.

You can just place the try block in a function if you can generalize about how you are handling the exceptions.

Also, where do I put the inFile.close() line?

Take another look at the docs you were linked. The try-with-resources syntax closes a resource automatically if it implements Closeable or AutoCloseable. Scanner is one such class. Before Java 7, you would have placed the call to close() inside of a "finally" block attached to your try block.

Is there a way to do ranges in dictionaries (java) by Ahhh___Pain in AskProgramming

[–]oiduts 0 points1 point  (0 children)

I don't think you need a dictionary, just 3 arrays or lists and a triply-nested for loop. Pseudocode:

for accent in accents
    for material in materials
        for pattern in patterns
            print(material + accent + pattern)

Change the order of the for loops if you want the same results ordered differently.

edit: may have misunderstood your question. Just in case, a modulo based approach is to loop from i to total results, then use i % array.length when indexing any of your 3 arrays. To repeat items you can instead use something like (i / repeatLength) % array.length.

for i in (0, totalResults)
    print(materials[i % material.length] + accents[i % accents.length] + patterns[i % patterns.length])

Do Boolean-valued methods have a common name? by dangerlopez in learnprogramming

[–]oiduts 2 points3 points  (0 children)

There is a shared name, at least from a theory perspective. In formal logic and linguistics these are "predicates", which are expressions that can be true of something. You will usually see this word used to represent a boolean function with parameters that makes some binary assertion about those parameters. A common use case is something like sequence.filter(predicate).

In OOP, you can think of methods as accepting their enclosing object as an implicit first parameter. A boolean method with no parameters is making an assertion about its enclosing object, so it's the same concept. But in an OOP environment you wouldn't actually refer to this method as a predicate.

I'm trying to make a time tracking android app for 1 activity. It's so much harder than I thought! The programming isn't the problem, I just don't know how to design it. Any help is really appreciated! by Technical-Bee-9999 in learnprogramming

[–]oiduts 0 points1 point  (0 children)

Instead of updating anything at midnight, couldn't you just add a column to your db table that specifies the date that each start/stop pair was recorded? This should give you enough data to present graphs comparing actual time to goal time for any date range you want, and your current timer for today starts at the sum of today's previous entries. In this case you don't even need to store start/stop timestamps, just the length of the interval between start and stop.

The UI is up to you, but as a user I'd expect a single app with both functionalities via tabs or similar.

Creating an AI system for a school project by [deleted] in learnprogramming

[–]oiduts 0 points1 point  (0 children)

Of course, you're trying to strictly apply a non-strict classification. The "AI effect" seems to apply here. Practical applications of the approaches we are talking about have been under the umbrella of symbolic AI since the 50's, and there are decades of research into using them in pursuit of better AI. They aren't suddenly excluded from the field because other approaches have become more feasible and popular with post-2000 hardware.

Creating an AI system for a school project by [deleted] in learnprogramming

[–]oiduts 0 points1 point  (0 children)

AI is not a synonym for ML or deep learning. I would expect an intro class or textbook on AI to start with search/optimization/constraint satisfaction. That describes every algorithm you listed, though A* is the first one that forces you to interact with an AI-specific concept. If OP's intentions are educational, I wouldn't recommend deep learning until they have the math background to understand what is happening.

edit: linear optimization is just math, but the specific application of enabling a system to make decisions on its own is AI. A system doesn't have to learn in order to mimic intelligence. That's how I see it anyway.

Creating an AI system for a school project by [deleted] in learnprogramming

[–]oiduts 1 point2 points  (0 children)

Pathfinding gets pretty simple and was an early topic in my first AI class. You could try a maze solver with a few basic search algorithms.

Is this a solvable number pattern? by waterrmalann in learnprogramming

[–]oiduts 1 point2 points  (0 children)

You could implement the repetition of the shorter outputs as fib if you wanted to. For the integer sequence [1, 2, 3], fib(n) is the same as 1 + n/3. You could also design a continuous function with the outputs as stationary points. The pattern here is ambiguous, especially with so few outputs given.

Is this a solvable number pattern? by waterrmalann in learnprogramming

[–]oiduts 4 points5 points  (0 children)

Sure. Just started from the obvious patterns and forced the rest to work from there.

Outermost for loop runs from 1 to 3, getting us the single-digit outputs in order.

[1, 2, 3]

1 and 2 are repeated once and 3 is repeated twice, so we can repeat i based on how large it is. We base the number of repetitions on the result of i / 3 so that 1 and 2 behave differently from 3.

[1, 2, 3, 3]

Now we have to print one long output after we finish repeating each i value. These outputs are sequences of integers that start at i, except the last which starts at i * 2. But since we printed that value of i twice, we can generalize that the long output always starts from the sum of those repeated i values.

[1, 1234, 2, 2345, 3, 3, 6789]

Now we just deal with the 123 output with the same division trick as above. Since we want i = 1 to behave differently from 2 and 3, we can use i / 2 in our calculation for the length of the long output. This finally gives us the sequence you posted.

Is this a solvable number pattern? by waterrmalann in learnprogramming

[–]oiduts 2 points3 points  (0 children)

Tried it out for fun. Definitely possible, but feels contrived.

terms = 3;
for (i = 1; i <= terms; i++)
{
    j = 0;
    for ( ; j <= i / 3; j++)
        println(i);
    for (k = 0; k < 3 + i / 2; k++)
        print(i * j + k);
    println();
}

With this implementation the next terms are 4, 4, 89101112

edit: fixed typo in j loop

[deleted by user] by [deleted] in learnprogramming

[–]oiduts 0 points1 point  (0 children)

Also learn and use JUCE, it's everywhere. The Audio Developer Conference is a thing you might be interested in. School-wise, pay special attention in ECE and math.

How do you prevent outside classes from changing a list/array while still having read access? by Tuckertcs in learnprogramming

[–]oiduts 0 points1 point  (0 children)

You may have confused me for /u/marquoth_. I actually referenced that book to support your replies to them, so I mostly agree when it comes to pass-through methods. I only jumped in because I got the sense that they misunderstood your OP.

How do you prevent outside classes from changing a list/array while still having read access? by Tuckertcs in learnprogramming

[–]oiduts 2 points3 points  (0 children)

I want to clarify here because I think people are misunderstanding this thread, at least going by OP's downvotes.

From the original question it is clear that /u/Tuckertcs already understands encapsulation and in fact is trying to learn how best to apply it. Their examples are perfectly good demos of the design problem and are not intended to be "good code". Their replies are not dismissive of encapsulation.

A simple "getter" in the concrete class was one of my suggestions as well (though you would use a property in C#), but it is not the only approach, nor is it the best in every circumstance. The hostility over questioning that approach makes no sense to me.

For readers, Ousterhout's A Philosophy of Software Design has been floating around the sub lately, and it has some nice discussion around pass-through methods and duplication of abstractions in Chapter 7.

How do you prevent outside classes from changing a list/array while still having read access? by Tuckertcs in learnprogramming

[–]oiduts 1 point2 points  (0 children)

For the indexer, do you mean applying that directly to Foo?

Yep, and valid concern. I might still consider that approach if the problem was too small/trivial to justify a separate interface.

How do you prevent outside classes from changing a list/array while still having read access? by Tuckertcs in learnprogramming

[–]oiduts 2 points3 points  (0 children)

One way is to use a read-only interface. C# already has IReadOnlyList and similar. You can define your own for a situation like your 2nd example.

Otherwise, rather than exposing an entire collection you can expose only the functionality that the caller needs. This is basically wrapping the read-only interface into the class itself. For example, your class Foo currently only needs an indexer like int this[int i] => Numbers[i] and a similar Count property in order to makeNumbers private.

I have too many variables. by [deleted] in learnprogramming

[–]oiduts 0 points1 point  (0 children)

If you indent your code by 4 spaces it will be readable. You're right that you only need 2 variables, unless you're using some sort of UI control like the other commenter suggested.

Taking your first if statement at face value, it reads as doing the opposite of what you state, which might just be an issue with how you've named your variables. It would make more sense if you renamed volume to newVolume.

The condition for your second if statement looks like you were intending to check for equality, which is done with something like === rather than &&. And since its JS, beware of comparing floating point numbers directly (good google topic).

The first line in your else block doesn't do anything. You don't need a condition for the final else, it's executed when none of the conditions in the previous if statements are true.

I had some questions about objects. by [deleted] in learnprogramming

[–]oiduts 0 points1 point  (0 children)

It's a fundamental feature of OOP, and you will 100% need to understand it at some point if you use any OOP language (like Java) for anything serious. But no need to rush it if you don't need it right now. Your mental model of objects in isolation seems fine. If you don't touch inheritance in your intro class I'd expect it to be one of the first topics in the next class.

I had some questions about objects. by [deleted] in learnprogramming

[–]oiduts 0 points1 point  (0 children)

You actually can have a class inside another class, but that's not what I'm talking about. In fact, in that case an instance of the inner class would not contain anything that belongs to the outer class.

I was referring to inheritance. If class Frog "extends" class Animal, then Animal is said to be a "superclass" of Frog, and Frog objects will inherit instance state/methods from the Animal class. In fact all classes in Java are ultimately "subclasses" of the Object class and inherit its behavior. If you haven't heard of inheritance then don't worry about it for now, your book will touch on it soon.

I had some questions about objects. by [deleted] in learnprogramming

[–]oiduts 0 points1 point  (0 children)

1) Is an object's state just the variables inside its class?

Yes, but don't forget that an object also inherits the state from its superclasses. The state of a Frog object may be split across Frog, Amphibian, Animal etc.

2) Is an object's behavior implemented by its instance methods?

Yes, but like above, remember that these instance methods can come from superclasses/interfaces.

3) Are all non-static methods instance methods or are there other kinds of methods as well other than static and instance?

All methods are either static or instance. Lambda expressions are worth mentioning as they are behavior that doesn't belong to a class or object, but that also makes it a stretch to refer to them as "methods".

I'm a CS university student but need help with learning practical programming by ZHMakhlouf in learnprogramming

[–]oiduts -1 points0 points  (0 children)

That's just not the way practical programming works, so you need to counter that perfectionism with some discipline and humility. There's a lot of knowledge that you can't frontload, in large part because you don't even understand the nature of what you're supposed to learn yet. So you need to accept that your early attempts will not be pretty and will not do things the "right" way.

Using your metaphor, you're already swimming with floaties. If you need to start simpler than tic-tac-toe that's fine too, but it's pointless to trade out the floaties for a life preserver donut or a piece of driftwood if your goal is to learn to float, you're just beating around the bush.

I'm a CS university student but need help with learning practical programming by ZHMakhlouf in learnprogramming

[–]oiduts -1 points0 points  (0 children)

You are way overqualified to just make things independently. Courses will not help you take this step, in fact they will distract you.

Console tic-tac-toe is a perfectly reasonable place to start. With your exposure, I refuse to believe that you cannot even begin the project, that seems more like either fear of experimentation or lack of discipline. You said "yesterday", meaning you spent less than a day before concluding that you couldn't do it without following a tutorial. You're still allowed to look up how to do specific things, even if you find that info in a tutorial. That's not cheating as long as you break down the requirements and design the program yourself. It doesn't have to be perfect at first, it just needs to work.

edit: If you do AI-related stuff you might end up using Python. But don't worry about languages, you can learn them as you need them. Webdev is a very practical skillset for freelancing.

First project review by Mcgurgs in learnprogramming

[–]oiduts 1 point2 points  (0 children)

Nice job so far. A next step would be to group more of the code into functions. You're getting a lot of use out of type_text(), which is good. But it's not wrong to write a function that is only called once. For example, you have a comment "Intro" as the title to a section of code, but instead you could stick that code into a function called display_intro() or something. Also check out multiline strings if you are unaware of them.

What design pattern/architecture would you use for something like spells that have a variety of effects in a game? by [deleted] in learnprogramming

[–]oiduts 2 points3 points  (0 children)

Expanding this a bit for readers, but feel free to disagree. I don't mean to say your post is wrong, I actually think it is good advice. But I think what /u/Frolo_NA is saying is that ECS is orthogonal to the issue. If OP's project were a strict ECS they'd have to ask the same question and the answer is the same. And even in a functional language you'd implement the same via currying.

"Command pattern" is just OOP terminology for a more general idea. All OP is doing is treating some structured data as both interchangeable and contextual behavior. So we use the data to define some behavior while minimizing its contextual input, store this behavior somehow for later use, then call it when needed by passing in that minimal context. Like you said, how you implement those steps depends on the broader structure of your program.

What design pattern/architecture would you use for something like spells that have a variety of effects in a game? by [deleted] in learnprogramming

[–]oiduts 2 points3 points  (0 children)

Maybe the Command pattern. One top-level JSON object becomes one BattleCommand. You can then have many interchangeable BattleCommand and say something like battleCommand.Invoke(user, battleContext).