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

07-07-2016 , 06:58 PM
Thought this was a decent read about flow control in all software:
http://www.drdobbs.com/jvm/programmi...8200966?pgno=1


Sent from my SM-G900R4 using Tapatalk
Programming homework and newbie help thread Quote
07-10-2016 , 12:33 AM
Question about Java:

I have a homework assignment that requires me to write a program that draws lines at random locations of random colors and random thickness (haven't done the thickness part). What I have so far works and satisfies the color and location requirements, but I am very puzzled about one aspect of it. Here is the code for the method that does the drawing:
Code:
  public void paint(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
        
        for(int z = 0; z < 10; z++)
        {
            double x1 = Math.random() + ((int)(Math.random() * 1000));
            double x2 = Math.random() + ((int)(Math.random() * 1000));
            double y1 = Math.random() + ((int)(Math.random() * 1000));
            double y2 = Math.random() + ((int)(Math.random() * 1000));
            
            g2.setColor(new Color((float)Math.random(), (float)Math.random(),
                                 (float)Math.random()));
            
            g2.draw(new Line2D.Double(x1, y1, x2, y2));
            System.out.println(z);
        }
    }
So I expect this to draw 10 lines, but it draws 20. The for loop goes from 0-9 twice and I have absolutely no idea why.

EDIT
I just ran the program with Debug instead of Run Project in NetBeans, and the for loop only went from 0-9 once, just adding to my confusion.

Last edited by LBloom; 07-10-2016 at 12:44 AM.
Programming homework and newbie help thread Quote
07-10-2016 , 12:46 AM
so it runs fine in debug? are you using breakpoints?

When it wasn't running fine, it was printing 0,1,2... or 0,0,1,1,2,2..?
Programming homework and newbie help thread Quote
07-10-2016 , 12:53 AM
It prints 0-9 and then 0-9 again, while drawing 20 lines. When I ran it with debug it only printed 0-9 once and drew 10 lines. I didn't use a breakpoint when I did that.
Programming homework and newbie help thread Quote
07-10-2016 , 12:58 AM
Just ran it with a breakpoint on "System.out.println(z)" and as far as I can see everything works as expected. Each iteration of the loop creates new random numbers, z increments by 1, 10 lines get drawn, and then the program ends.
Programming homework and newbie help thread Quote
07-10-2016 , 01:56 AM
Your method is getting invoked twice somehow, where's the code that invokes it?
Programming homework and newbie help thread Quote
07-10-2016 , 02:17 AM
Wondering if paint is auto-called by some portion of Java defaults os
Programming homework and newbie help thread Quote
07-10-2016 , 12:55 PM
Yeah paint gets called whenever an update is needed. Do you need to clear the screen or buffer at the start of paint?
Programming homework and newbie help thread Quote
07-10-2016 , 12:57 PM
Quote:
Originally Posted by ChrisV
Your method is getting invoked twice somehow, where's the code that invokes it?
Here's the entirety of the program. I'm having similar issues with other assignments.
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Line2D;

public class RandomLines extends JFrame
{
    public RandomLines()
    {
        super("Drawing Lines");
        setSize(1200, 1200);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    
    @Override
    public void paint(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
        
        for (int z = 0; z < 10; z++)
        {
            double x1 = Math.random() + ((int)(Math.random() * 1000));
            double x2 = Math.random() + ((int)(Math.random() * 1000));
            double y1 = Math.random() + ((int)(Math.random() * 1000));
            double y2 = Math.random() + ((int)(Math.random() * 1000));
            
            g2.setColor(new Color((float)Math.random(), (float)Math.random(),
                                 (float)Math.random()));
            g2.setStroke(new BasicStroke((int)(Math.random() * 50)));
            
            g2.draw(new Line2D.Double(x1, y1, x2, y2));
            System.out.println(z);
        }
    

package randomlines;

public class Test13_8 
{
    public static void main(String[] args)
    {
        new RandomLines().setVisible(true);
    }
}
Programming homework and newbie help thread Quote
07-10-2016 , 02:35 PM
Try adding
super.paint(g)
as the first line in your paint function.

The problem is, paint() is called whenever the program thinks it needs to be called, and it is meant to re-draw the screen from scratch. So your program needs to clear it.

It's been a long damn time but I think if you call your parent's paint it'll clear it for you.

Also if you look here:
http://stackoverflow.com/questions/3...-from-a-jpanel
it seems to indicate that you should be overriding paintComponent() anyway, instead of paint()
Programming homework and newbie help thread Quote
07-15-2016 , 11:53 AM
hello!

i'm another poker player trying to get my feet wet programming in python. Things have been going reasonably well, but I'm stuck on the following thing for almost 2 days and making very little progress.

right now i'm trying to build a simple Android app. I want to send "Hello World!" to my app and have it appear as a push notification. To do this i want to use Firebase / Firebase Cloud Messaging (correct me if i'm wrong here) and I'm totally stuck trying to do this.

From what I understand, when a user opens the app the first time, it needs to register with Firebase and receive a unique ID, which I can then target to send messages. Now this seems pretty easy to do if I was using Android Studio, but i'm an idiot and am using just python IDLE with kivy for the GUI and buildozer to turn it into an apk file. should I just learn to make a basic app with say Android Studio and then register it with Firebase as described HERE or is there a reasonable way to do it all in Python?
Programming homework and newbie help thread Quote
07-16-2016 , 05:39 AM
Quote:
Originally Posted by skillgambler
hello!

i'm another poker player trying to get my feet wet programming in python. Things have been going reasonably well, but I'm stuck on the following thing for almost 2 days and making very little progress.

right now i'm trying to build a simple Android app. I want to send "Hello World!" to my app and have it appear as a push notification. To do this i want to use Firebase / Firebase Cloud Messaging (correct me if i'm wrong here) and I'm totally stuck trying to do this.

From what I understand, when a user opens the app the first time, it needs to register with Firebase and receive a unique ID, which I can then target to send messages. Now this seems pretty easy to do if I was using Android Studio, but i'm an idiot and am using just python IDLE with kivy for the GUI and buildozer to turn it into an apk file. should I just learn to make a basic app with say Android Studio and then register it with Firebase as described HERE or is there a reasonable way to do it all in Python?
Post your code if you can. Use the code tags.
Programming homework and newbie help thread Quote
07-16-2016 , 06:17 PM
I'm looking for a little direction as to where I can find help for a program I'm working on for class. My semester actually ends tonight for an online class (a fact I only learned two days ago, I had thought it ended tomorrow night). Unfortunately I've had some personal issues in my life the past week that have prevented me from putting in the amount of work I usually do every week (bad timing).

Anyway, I need to write a program / gui that allows a user to sign their signature to a line, by holding and dragging the mouse, in a frame and provide some buttons. I pretty much know how to design the interface, but I really don't know what I'm doing when it comes to drawing with the mouse. I've been googling around but not finding a whole ton of relevant information. Does anyone know anywhere I can look to get some help with this? Thanks, I hate being up against a deadline like this.
Programming homework and newbie help thread Quote
07-16-2016 , 06:22 PM
Uh, what language is this even?
Programming homework and newbie help thread Quote
07-16-2016 , 06:30 PM
Sorry, pretty frazzled as I'm working on it. It is Java. It is supposed to be basically a box with a line to sign in the middle, and an accept and cancel button at the bottom.
Programming homework and newbie help thread Quote
07-16-2016 , 07:22 PM
So, like...
https://docs.oracle.com/javase/tutor...elistener.html

I mean I don't really do java but there's a ton of **** about mouse events out there
Programming homework and newbie help thread Quote
07-21-2016 , 08:39 AM
Hi guys. Looking for some brains.

Part of my job for one week every 6 weeks is working from home...

Our work email addy receives a mail every 15 minutes to say that the system is running ok. This means I have to stay awake all night and watch that no emails are late.

It would better to receive an email only when things were not okay instead of when it is ok. Then I could set up a rule in outlook to forward a mail to an android gmail and have it play a loud ass alarm.

Any ideas of what I could maybe do to receive a mail (phone call would be ideal) when a mail is late. Some rule in outlook to run a script?
I haven't written scripts in outlook before but would be a good excercise.
Programming homework and newbie help thread Quote
07-22-2016 , 09:29 AM
wait, you need to stay awake every night for a full week to check these ?
Programming homework and newbie help thread Quote
07-22-2016 , 10:16 AM
Jesus christ what is wrong with the place you work? There are dozens or maybe hundreds of systems/software out there for alerting. The most recent one I've used was PagerDuty.
Programming homework and newbie help thread Quote
07-22-2016 , 07:08 PM
Quote:
Originally Posted by Victor
wait, you need to stay awake every night for a full week to check these ?
I need to change my sleep pattern for a week.

Quote:
Jesus christ what is wrong with the place you work? There are dozens or maybe hundreds of systems/software out there for alerting. The most recent one I've used was PagerDuty.
They've made up a few more things for us to do since I've made that post so please ignore it.
Programming homework and newbie help thread Quote
07-25-2016 , 08:47 PM
I sat down today to create an AHK script to use on Carbon Poker. The Carbon client is not responding to commands like WinMove, WinMinimize, and some other similar "window commands". Is there a way to get around this? Thanks.
Programming homework and newbie help thread Quote
08-08-2016 , 06:28 PM
I am doing a takehome assignment for a job interview. The idea is pretty basic, make a command line program that does the follwing.

1. prompts a user for input.
2. With this input call the companies product api which returns an array of products.
3. Take the first product and call a second api that returns a list of suggested products.
4. Output the first 10 products sorted by rating

I built this using node and am using mocha for the tests.

In the instructions they say my solution and tests should build and execute via the command line. Any idea what it means to "build and execute"?

My thinking is in the readme I should tell them to
1. type npm install to install the dependencies.
2. type npm test to run the tests
3. type npm start to run the program.

Anything I am missing here?
Programming homework and newbie help thread Quote
08-08-2016 , 07:14 PM
Make a shell script that does all that
Programming homework and newbie help thread Quote
08-08-2016 , 08:55 PM
Ah cool thanks. So apparently npm scripts can call other scripts. Now it works as follows:
npm start first installs, then runs the tests, then starts the program
Programming homework and newbie help thread Quote
08-08-2016 , 09:21 PM
I'm confused by the type Comparator<? super T> for the sort method on a collection,

sort(List<T> list, Comparator<? super T> c)

Code:
class OrderByPercentage implements Comparator<Student>
{
    @Override
    public int compare(Student s1, Student s2)
    {
        return s1.percentage - s2.percentage;
    }
}
 
...
...
 
Collections.sort(listOfStudents, new OrderByPercentage());
code from 2nd to last example here, http://javaconceptoftheday.com/how-t...ylist-in-java/

sort(List<T> list, Comparator<? super T> c)

Does this not mean that the 2nd argument has to implement the Comparator interface with generic type of some superclass of T? Here we're clearly using a Comparator with generic type of Student, not the superclass of Student.
Programming homework and newbie help thread Quote

      
m