Open Side Menu Go to the Top
Register
Programming homework and newbie help thread Programming homework and newbie help thread

09-07-2018 , 01:17 PM
Quote:
Originally Posted by d7o1d1s0
- Verify it's returning all my posts (102 for the first thread, know there's a way to check this on the forum but haven't found it yet)
In the forum index click the number of posts on the thread of interest, it gives you a list of all thread contributors and their post count.
Programming homework and newbie help thread Quote
09-14-2018 , 07:55 AM
I am learning java and I am practising making classes and methods.

I've created a method "question" method which I want person1 to call. I have no problem calling the method but it skips my first two if statements and goes to the default "Speak up".

Any advice is appreciated.
__________________________________________________ ____________________
package Tutorials;

import java.util.*;

class Person {
String name;
int age;
Scanner input = new Scanner(System.in);
String answer;

void question() {
System.out.println("Please say hello to our new robot friend");
answer = input.nextLine();
if (answer == "Hello") {
System.out.println("Hello yourself");

} else if (answer == "Hi") {
System.out.println("Queen's English please!");
}
}

void speak() {
System.out.println("Hello");
}

}
__________________________________________________ ____________

public class thirteen {
public static void main(String[] args) {

Person person1 = new Person();
person1.question();

}

}
__________________________________________________ ____________________
Programming homework and newbie help thread Quote
09-14-2018 , 08:45 AM
Log what answer is after you collect it to make sure it’s what you think it is. In particular there might be newline characters or something there.

Also, Strings are objects in Java so you want to use ‘.equals’ instead of == so that you’re comparing the content of the string and not the object reference.
Programming homework and newbie help thread Quote
09-14-2018 , 10:33 AM
Quote:
Originally Posted by jjshabado
Also, Strings are objects in Java so you want to use ‘.equals’ instead of == so that you’re comparing the content of the string and not the object reference.
Yeah this is going to be the issue. Usually you do it like

Quote:
if("Hello".equals(answer)) { ...
that's safer, because if you do if(answer.equals("Hello")) then it will throw a NullPointerException when answer is null (which it won't be here, but it's a good habit).

To expand on equality a bit more, in Java == does value comparison for primitive types. So:

Quote:
int x = 1;
int y = 1;
boolean areEqual = (x == y) //true
But for objects, == is a reference comparison. Object variables are actually pointers to a location in memory. == tests if they point to the same location. So:

Quote:
String x = "hi";
String y = "hi";
boolean areEqual = (x == y); //false
areEqual = (x.equals(y)); //true
y = x;
areEqual = (x == y); //true, because it's not just that they're both "hi", they're both the SAME "hi", at the same place in memory. y = x, for objects, says "point y to the same place in memory as x".
Other similar languages (like C#) make an exception for strings and do a value comparison for ==, but Java doesn't, basically because it's the mission of Java to be as pedantic as possible and make your life difficult.

Also, if you write your code in IntelliJ (which is free, for the basic edition), it will warn you about your dubious use of == and you can set breakpoints and debug to see the execution path your code is taking.
Programming homework and newbie help thread Quote
09-17-2018 , 02:07 PM
IPC question

What is the best way to store an array in shared memory with threading? Is it better to try to store the whole 2d array in shared memory as a 2d array, or try to cram it into 16 bits, and do the calculation in the threads. I am thinking about storing it in a 32 bit integer, and changing my look-up tables. Before I was doing base 3 with 16 bits. Thoughts?

Also, what about more complicated data such as structs? Thank you.
Programming homework and newbie help thread Quote
09-17-2018 , 02:19 PM
I don't really follow what you mean by "cram it into 16 bits", but in general I would say it's not the size of the data that's an issue for shared memory, it's going to be how you synchronize access if multiple threads need to write, or if you need to ensure a particular order of events between threads. (i.e. let one thread finish writing before a second thread reads).

I haven't really done anything remotely like this in years, so I don't have much of a recommendation, but it might be useful to implement some existing library for managing this. There seem to be a few options, although which you prefer might depend on what platform you are targeting.
Programming homework and newbie help thread Quote
09-18-2018 , 12:02 PM
Thanks for the answers
Programming homework and newbie help thread Quote
09-19-2018 , 05:29 PM
I worked on it some more. There's nothing special you have to do to convert struct pointers to void pointers other than ordinary casting. Though you can shave bits with number theory and unsigned integers, it's premature optimization to do so at the beginning. It is better practice to use structs, and only after everything else is done, go back and improve it. Structs naturally use the compiler to limit the size of your data structure.

raw bits != friendly

Last edited by leavesofliberty; 09-19-2018 at 05:34 PM.
Programming homework and newbie help thread Quote
10-18-2018 , 01:20 PM
My simple issue does not deserve a thread on its own so asking here. Im trying to write hot keys in python for playing(middle mouse button for fold etc). My script need to determine button was clicked on the table and act only then on that table. So when using:

hwnd=win32gui.WindowFromPoint(win32api.GetCursorPo s())

i get HWND of item under cursor, which can be a button handle or chatbox handle etc. I could manage with a makeshift solution here, but im looking for best one. All this windows terminology of properties of windows is really confusing(owner, child, parent etc)

So what im trying to achieve here is determining whether user clicked on a poker table, then determining its size in pixels. Does whole table frame has a HWND? How to get it from object HWND within a table?
Programming homework and newbie help thread Quote
10-18-2018 , 03:43 PM
I had a little library that did something much like this, but I can no longer find the code for it.

Have you tried following the "parent" upwards until you get to a window that has a "title" that looks like what a poker window should look like?
Programming homework and newbie help thread Quote
10-18-2018 , 03:56 PM
For now solved by using GetForegroundWindow(), but that works on mouse button release, as only then window jumps to focus, not on mouse press. Not optimal solution, but will do for now.

So lets say chatbox would be considered a 'child' and table Window would be considered a parent? I might experiment with that in future.
Programming homework and newbie help thread Quote
10-18-2018 , 04:44 PM
Quote:
Originally Posted by fastriffs
For now solved by using GetForegroundWindow(), but that works on mouse button release, as only then window jumps to focus, not on mouse press. Not optimal solution, but will do for now.

So lets say chatbox would be considered a 'child' and table Window would be considered a parent? I might experiment with that in future.
In general you can think of a Windows window as a collection of rectangles, with each rectangle optionally containing a collection of rectangles, all the way down. So each element has a single parent and may have multiple children. It's basically a tree. From any HWND you can examine your parent or see what your children are. You can usually get properties of a window from it's HWND - I don't know the python win32 implementation, just the C/C++ library so I don't know what all you can do

Not all windows have a parent - there's a special code that indicates that a window is toplevel but owned by another window - this is common for popup windows for example, they are a top level window, but they're considered part of another toplevel window. Windows that don't have a parent are usually toplevel windows.

Something else I used to do was use "AutoIt" which is a program that lets you script a lot of this stuff in a simple way. You can make autoit scripts and bind them to keys or mouse clicks, and back when I was doing this, at least, they had a C library that you could do so that you could embed their actions into your C programs. There are python bindings for autoit but I've never used them
Programming homework and newbie help thread Quote
10-18-2018 , 06:09 PM
Thanks for clarification! Im doing this project mostly for my own education in python, so will stick to python instead of ready solutions. So i was doing pretty well untill i've hit a problem: My mouse events hook is not reporting any mouse events on tables while PT4 auto import/HUD is running. Im using this lib: 'https://github.com/boppreh/mouse'. There does not seem to be many alternatives to this one for python. Im pretty stuck for now.
Programming homework and newbie help thread Quote
10-18-2018 , 06:54 PM
That's probably a "run as administrator" thing.
Programming homework and newbie help thread Quote
10-18-2018 , 07:12 PM
Ideed! Your valuable tip saved me alot of time. I would never have guessed this might be the issue.
Programming homework and newbie help thread Quote
10-18-2018 , 07:36 PM
Huh, I wouldn't have considered that either. My theory was this:

The way a HUD usually works is that it positions a window directly on top of the poker table, covering it entirely. It has settings that makes it's background transparent, but the foreground not-transparent, so the foreground elements appear to float over the table. The HUD intercepts all the clicks, same as if you put like a browser window over the poker client, then you couldn't click on it there either.

Hm maybe I'm wrong and clicks on the transparent parts go through to the underlying windows. I don't remember. I wrote a HUD but it was, uh, like 12 years ago.
Programming homework and newbie help thread Quote
11-07-2018 , 01:11 PM
Help with upcasting in Java please.

Code:
class Cycle {
	public int wheels = 0;
	public int wheels() {
		return wheels;
	}
} 

class Unicycle extends Cycle {
	public int wheels = 1;
	public int wheels() {
		return wheels;
	}
}

public class Vehicles {
	public static void ride(Cycle cycle) {
		System.out.println("Number of wheels: " + cycle.wheels);
	}
	public static void main(String[] args) {
		Unicycle unicycle1 = new Unicycle();
		ride(unicycle1);
		System.out.println(unicycle1.wheels());
	}
}
ride(unicycle1) prints out 0, and println(unicycle1.wheels()) prints out 1. Why is ride() not printing out 1 as well?
Programming homework and newbie help thread Quote
11-07-2018 , 01:16 PM
As is tradition for me, as soon as I ask for help on something I figure it out.

System.out.println("Number of wheels: " + cycle.wheels); should be System.out.println("Number of wheels: " + cycle.wheels());
Programming homework and newbie help thread Quote
11-21-2018 , 06:50 PM
I'm stuck early on in an assignment. I am given the following code (with unnecessary bodies of classes deleted):

-- removed at poster's request

Last edited by _dave_; 11-23-2018 at 08:50 PM.
Programming homework and newbie help thread Quote
11-21-2018 , 07:27 PM
All of your products have your class Product as an ancestor so they are products also. So something like arraylist < product > is what you need.

I'm reading on my phone so apologies if I missed something.
Programming homework and newbie help thread Quote
11-21-2018 , 07:34 PM
Right, that's what I have now I think? The problem is that it has to exclusively take those 3 subclasses, and not all the other subclasses that also inherit from Product.
Programming homework and newbie help thread Quote
11-21-2018 , 07:57 PM
Wouldn't it just be something like arraylist<computerPart peripheral service> ?
Programming homework and newbie help thread Quote
11-21-2018 , 08:06 PM
I see. How about if you filter operations which add products. Make an add method for each type you accept. Then treat everything as a product after that.
Programming homework and newbie help thread Quote
11-21-2018 , 08:12 PM
mmmmm I've tried stuff like arraylist<computerPart peripheral service>, arraylist<computerPart, peripheral, service>, arraylist<computerPart><peripheral> <service> and none of it works.

I get

new 1.java:16: error: > expected
PartyTrayOrder(ArrayList<ComputerPart Peripheral Service> name) {


new 1.java:16: error: wrong number of type arguments; required 1
PartyTrayOrder(ArrayList<ComputerPart, Peripheral, Service> name) {
Programming homework and newbie help thread Quote

      
m