Two Plus Two Publishing LLC
Two Plus Two Publishing LLC
 

Go Back   Two Plus Two Poker Forums > >

Notices

Programming Discussions about computer programming

Reply
 
Thread Tools Display Modes
Old 04-24-2015, 08:44 PM   #451
TheDuker
old hand
 
TheDuker's Avatar
 
Join Date: Oct 2013
Posts: 1,388
Re: Programming homework and newbie help thread

Quote:
Originally Posted by javahelp View Post
Hi all,

I'm in need of assistance with a java application.

I'm trying to build an application with a gui that takes text that've been input in a textfield (txtInput) and sends it to a web service where it gets a reply and then presents that reply in a textarea (txtDisplay). And a button to send it.

As it is it's not working, and from the below, "send" and "txt.Display" gets marked red.

final String display = control.send(input);
txtDisplay.setText(display);


Here's what I've got so far:

gui.java:

controller.java

I don't really know what to do here :S

Thanks
In general, classes should be capitalized, and your source files should share the class name. So your Window class should be in Window.java, and Controller should be in Controller.java.

For your main question, "control.send(input)" is marked red because your Controller does not have a send() method. It has a getFileContents() method and it looks like that's what you're intending to use.

Obviously nothing will work until your service variable is assigned to a real service object that actually has a getFileContents() method.
TheDuker is offline   Reply With Quote
Old 04-25-2015, 03:49 AM   #452
daveT
S.A.G.E. Master
 
daveT's Avatar
 
Join Date: Jun 2005
Location: La La Land
Posts: 23,160
Re: Programming homework and newbie help thread

Quote:
Originally Posted by just_grindin View Post
Newish to programming so not sure how to compare algorithm speed but the following equation is the closed form of the Fibonnaci sequence:

Fsub(n) = (((1+sqrt(5))/2)^n- ((1-sqrt(5))/2)^n/sqrt(5)

So you could loop through only even powers of N and compute that result. Like I said not sure if it's faster or better in any way.
This won't work because you end up with floating point limits after the fractions are converted to floats.
daveT is offline   Reply With Quote
Old 04-26-2015, 12:16 AM   #453
just_grindin
Pooh-Bah
 
Join Date: Dec 2007
Posts: 4,879
Re: Programming homework and newbie help thread

Quote:
Originally Posted by daveT View Post
This won't work because you end up with floating point limits after the fractions are converted to floats.
I wondered about that after I posted it. Thank you for clarity.
just_grindin is offline   Reply With Quote
Old 04-26-2015, 07:00 PM   #454
daveT
S.A.G.E. Master
 
daveT's Avatar
 
Join Date: Jun 2005
Location: La La Land
Posts: 23,160
Re: Programming homework and newbie help thread

That issue is constrained to languages that don't support fractions. Some languages do, so it would probably work in those languages, though I'm not sure how well sqrt(5) is handled. It may bug at some very large n.
daveT is offline   Reply With Quote
Old 04-28-2015, 09:09 AM   #455
Greeksquared
adept
 
Join Date: Jun 2004
Location: part-time carnivore
Posts: 1,093
Re: Programming homework and newbie help thread

Gotta db question:

I have now created multiple projects that read/write to a mysql db hosted online (bluehost). The connections happen through a basic php script that begin like this:

Code:
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname"
$conn = mysqli_connect($servername, $username, $password, $dbname);
I see multiple problems here:
1. My password is visible in plain text
2. If you know the file name you can retrieve data or possibly inject data into my database (though you'd have to know the exact query parameters)

What is a better play here?
Greeksquared is offline   Reply With Quote
Old 04-28-2015, 08:10 PM   #456
adios
Carpal \'Tunnel
 
Join Date: Sep 2002
Location: Russian Troll
Posts: 21,499
Re: Programming homework and newbie help thread

Quote:
Originally Posted by daveT View Post
That issue is constrained to languages that don't support fractions. Some languages do, so it would probably work in those languages, though I'm not sure how well sqrt(5) is handled. It may bug at some very large n.
Use double instead of float.
adios is offline   Reply With Quote
Old 05-01-2015, 01:57 PM   #457
Anais
Carpal \'Tunnel
 
Anais's Avatar
 
Join Date: Mar 2006
Posts: 6,666
Re: Programming homework and newbie help thread

c++ casting question:

static_cast<baseClass const&>(subClassObject)

so I'm using casting to access parent friend overloaded operator functions. The above works, the problem is that - for the example output our instructor gave, his used a copy constructor and mine did not. (There's a short output to let you know when one constructor or destructor is called)

Curious what I might be doing wrong, if anything.
Anais is offline   Reply With Quote
Old 05-01-2015, 03:50 PM   #458
adios
Carpal \'Tunnel
 
Join Date: Sep 2002
Location: Russian Troll
Posts: 21,499
Re: Programming homework and newbie help thread

Quote:
Originally Posted by Anais View Post
c++ casting question:

static_cast<baseClass const&>(subClassObject)

so I'm using casting to access parent friend overloaded operator functions. The above works, the problem is that - for the example output our instructor gave, his used a copy constructor and mine did not. (There's a short output to let you know when one constructor or destructor is called)

Curious what I might be doing wrong, if anything.
I need a little more context like the code hopefully and specifics about the error IE what isn't getting called, the constructor or the destructor?

One thing to be aware of, if your class does not have an explicit copy constructor, the compiler will create a default one and it will be used implicitly. This is true for constructors, destructors, and the = sign operator.
adios is offline   Reply With Quote
Old 05-01-2015, 04:00 PM   #459
Anais
Carpal \'Tunnel
 
Anais's Avatar
 
Join Date: Mar 2006
Posts: 6,666
Re: Programming homework and newbie help thread

baseClass is a 2d object (i.e. 2 variables), subClass is a 3d object. Global ostream 'outs' set up.

Code:
outs << "casting subClass as baseClass using subClassObject" 
  << static_cast<baseClass const&>(subClassObject) << endl
  << "casting subClass as baseClass using subClassObjectPtr" 
  << static_cast<baseClass const&>(*subClassObjectPtr) << endl
sCO & its pointer already declared/defined. Both classes have constructors & destructors, plus overloaded <<.

In the given example, when we get to that part, the base copy constructor is called, then after the casting bit, the destructor is called. So:

Quote:
baseClass Copy Constructor called
casting subClass as baseClass using subClassObject
data1 data2
casting subClass as baseClass using subClassObjectPtr
data1 data2
baseClass Destructor called
hopefully that makes sense. Data3 is left out by casting the object as its base class.
Anais is offline   Reply With Quote
Old 05-01-2015, 04:03 PM   #460
adios
Carpal \'Tunnel
 
Join Date: Sep 2002
Location: Russian Troll
Posts: 21,499
Re: Programming homework and newbie help thread

Quote:
Originally Posted by Anais View Post
baseClass is a 2d object (i.e. 2 variables), subClass is a 3d object. Global ostream 'outs' set up.

Code:
outs << "casting subClass as baseClass using subClassObject" 
  << static_cast<baseClass const&>(subClassObject) << endl
  << "casting subClass as baseClass using subClassObjectPtr" 
  << static_cast<baseClass const&>(*subClassObjectPtr) << endl
sCO & its pointer already declared/defined. Both classes have constructors & destructors, plus overloaded <<.

In the given example, when we get to that part, the base copy constructor is called, then after the casting bit, the destructor is called. So:



hopefully that makes sense. Data3 is left out by casting the object as its base class.
What is the error exactly?
adios is offline   Reply With Quote
Old 05-01-2015, 05:57 PM   #461
Anais
Carpal \'Tunnel
 
Anais's Avatar
 
Join Date: Mar 2006
Posts: 6,666
Re: Programming homework and newbie help thread

no error. as i say, the constructors and destructors print out messages when they're called. and the code i have doesn't print out any message for the base class con/destructor being called even though it's apparently supposed to
Anais is offline   Reply With Quote
Old 05-01-2015, 05:58 PM   #462
kazana
Pooh-Bah
 
kazana's Avatar
 
Join Date: May 2006
Location: que, said che
Posts: 5,659
Re: Programming homework and newbie help thread

Anais, just a hunch here, but what happens if you use this instead (note the removed reference &):

Code:
static_cast<baseClass const>(subClassObject)
What you do have in your code looks like an implicit conversion to me, so no copying happening.
kazana is offline   Reply With Quote
Old 05-01-2015, 09:14 PM   #463
adios
Carpal \'Tunnel
 
Join Date: Sep 2002
Location: Russian Troll
Posts: 21,499
Re: Programming homework and newbie help thread

Quote:
Originally Posted by Anais View Post
c++ casting question:

static_cast<baseClass const&>(subClassObject)

so I'm using casting to access parent friend overloaded operator functions. The above works, the problem is that - for the example output our instructor gave, his used a copy constructor and mine did not. (There's a short output to let you know when one constructor or destructor is called)

Curious what I might be doing wrong, if anything.
Ok so you are using this cast while your prof is using a copy constructor right? If so looks good to me and the C++ concept you are employing is called RTTI.
adios is offline   Reply With Quote
Old 05-02-2015, 03:45 PM   #464
joel2006
old hand
 
joel2006's Avatar
 
Join Date: Jan 2004
Location: Under the Boardwalk.
Posts: 1,626
Re: Programming homework and newbie help thread

What does it mean to create an ordered tree from a list (of unordered numbers)?
joel2006 is offline   Reply With Quote
Old 05-03-2015, 08:48 PM   #465
Somnius
grinder
 
Somnius's Avatar
 
Join Date: Oct 2007
Posts: 556
Re: Programming homework and newbie help thread

Hackerrank doesn't allow you to copy so have to post a pic:

Problem 'Service Lane' using JavaScript



It is passing 2 of the 11 tests - it is most likely a result of my array definitions for var i and var j.

My ability to manipulate the problem inputs and create proper arrays etc with the info is the issue and have not been able to find the proper methods / it is starting to get convoluted and some intervention in efficiency would be great about now.

The first 2 tests have single digit integers - after that there are multiple integers and line 17
Code:
...inputArray[n][2]);
becomes a problem.

There is probably a much simpler way of organizing the input data to avoid these pitfalls. Any input appreciated.
Somnius is offline   Reply With Quote
Old 05-04-2015, 10:43 AM   #466
Somnius
grinder
 
Somnius's Avatar
 
Join Date: Oct 2007
Posts: 556
Re: Programming homework and newbie help thread

split the input differently and all good. nm
Somnius is offline   Reply With Quote
Old 05-04-2015, 02:51 PM   #467
NiSash1337
Carpal \'Tunnel
 
NiSash1337's Avatar
 
Join Date: Dec 2010
Location: Germany
Posts: 9,186
Re: Programming homework and newbie help thread

Hey guys new semester and we need to learn some c, we have to write a simple program that uses a number via scanf and then prints out the result of the calculation with printf

#include <stdio.h>
#include <string.h>

int main ()
{

double zahl;
double mwst;
double bruttopreis;
double skonto;
double endpreis;

printf("Bitte geben Sie den Nettopreis ein ein: ");
scanf("%f",&zahl);;

mwst = zahl * 0.2;
bruttopreis = zahl + mwst;
skonto = bruttopreis * 0.02;
endpreis = bruttopreis - skonto;
printf("Nettopreis \t Euro %.2f ",endpreis);

return 0;

Now everytime I do this with 1000 it gives me 0.00

now this:


int main ()
{

double zahl;
double mwst;
double bruttopreis;
double skonto;
double endpreis;

printf("Bitte geben Sie den Nettopreis ein ein: ");
scanf("%d",&zahl);;

mwst = zahl * 0.2;
bruttopreis = zahl + mwst;
skonto = bruttopreis * 0.02;
endpreis = bruttopreis - skonto;
printf("Nettopreis \t Euro %d ",endpreis);

return 0;
}

gives me 1176
NiSash1337 is offline   Reply With Quote
Old 05-04-2015, 05:05 PM   #468
Benholio
Carpal \'Tunnel
 
Benholio's Avatar
 
Join Date: Jun 2004
Location: Atlanta, GA
Posts: 8,292
Re: Programming homework and newbie help thread

Quote:
Originally Posted by NiSash1337 View Post
Hey guys new semester and we need to learn some c, we have to write a simple program that uses a number via scanf and then prints out the result of the calculation with printf
Your problem is with the usage of scanf. Your input variable is a double, but you are scanning for a float in the first example ("%f") and an integer in the second example ("%d"). To read in a double value, you need to use the format specifier "%lf". More details here: Scanf format string specifications
Benholio is offline   Reply With Quote
Old 05-04-2015, 05:21 PM   #469
NiSash1337
Carpal \'Tunnel
 
NiSash1337's Avatar
 
Join Date: Dec 2010
Location: Germany
Posts: 9,186
Re: Programming homework and newbie help thread

Thx works, I figured that it is the same for printf and scanf(%f) but well apparently it isnt.

Last edited by NiSash1337; 05-04-2015 at 05:32 PM.
NiSash1337 is offline   Reply With Quote
Old 05-05-2015, 12:26 PM   #470
plx
journeyman
 
Join Date: Aug 2011
Posts: 219
Re: Programming homework and newbie help thread

I'am learning C but this is a major pain sometimes, i'am stuck with stuck big chucks of time with little things.

Can someone help me with this? Its just supposed to create a file with the name user wants.

Code:
char FileName[50];
        printf("What is the name of the file you want to create? ");
        scanf("%49s", FileName);
        char AuxFileName[100];
        sprintf(AuxFileName, "C:\\%s.txt", FileName);
        FILE *f;
        f=fopen("AuxFileName", "w");
        if(f==NULL)
        {
            printf("Error\n");
            return 0;
I don't have erro anymore but it doesn't create file in C:
What's wrong?

Last edited by plx; 05-05-2015 at 12:43 PM.
plx is offline   Reply With Quote
Old 05-05-2015, 06:11 PM   #471
kazana
Pooh-Bah
 
kazana's Avatar
 
Join Date: May 2006
Location: que, said che
Posts: 5,659
Re: Programming homework and newbie help thread

Your call in fopen puts the variable name in parentheses.
Check you execution folder, it will probably contain a file called AuxFileName
kazana is offline   Reply With Quote
Old 05-05-2015, 06:26 PM   #472
adios
Carpal \'Tunnel
 
Join Date: Sep 2002
Location: Russian Troll
Posts: 21,499
Re: Programming homework and newbie help thread

Quote:
Originally Posted by plx View Post
I'am learning C but this is a major pain sometimes, i'am stuck with stuck big chucks of time with little things.

Can someone help me with this? Its just supposed to create a file with the name user wants.

Code:
char FileName[50];
        printf("What is the name of the file you want to create? ");
        scanf("%49s", FileName);
        char AuxFileName[100];
        sprintf(AuxFileName, "C:\\%s.txt", FileName);
        FILE *f;
        f=fopen("AuxFileName", "w");
        if(f==NULL)
        {
            printf("Error\n");
            return 0;
I don't have erro anymore but it doesn't create file in C:
What's wrong?
Try "w+" instead of "w"
adios is offline   Reply With Quote
Old 05-05-2015, 06:33 PM   #473
kazana
Pooh-Bah
 
kazana's Avatar
 
Join Date: May 2006
Location: que, said che
Posts: 5,659
Re: Programming homework and newbie help thread

Ah yes, and that.
kazana is offline   Reply With Quote
Old 05-05-2015, 06:46 PM   #474
Somnius
grinder
 
Somnius's Avatar
 
Join Date: Oct 2007
Posts: 556
Re: Programming homework and newbie help thread

Why is this recursive calls true determination changing to 'undefined' before returning to the parent comparison?

Code:
function deepEqual(a, b) {
  // value comparison
  if (a === b) { return true };
  
  var propaCount = 0;
  var propbCount = 0;
  
  // object comparison
  if (typeof(a) == 'object' && typeof(b) == 'object' && a != null && b != null) {
    // count the properties in object a
    for (var prop in a) {
      propaCount += 1;
    }
    // starting comparing properties of b in a and recursively comparing the values
    for (var prop in b) {
      propbCount += 1;    
      if (!(prop in a) || !deepEqual(a[prop], b[prop])) {
        return false;  
      }
    }
  // if the object test fails
  } else {
    return false;
  }
  // to get it to work I have to include return true; here however don't understand why the recursive determination of true using the comparison at top of function changes to 'undefined' when returning back to the comparison initiating the call (using pythontutor)
}
any help appreciated please.
Somnius is offline   Reply With Quote
Old 05-05-2015, 07:27 PM   #475
plx
journeyman
 
Join Date: Aug 2011
Posts: 219
Re: Programming homework and newbie help thread

Thanks guys. I have another question. How do i make a BinarySearch function to search min number in array? I have the code below. I know its buggy with iMin but i've been messing around and i can't find an efficient way to solve it. How would you guys do it?

Code:
int MinBinarySearch(int Elem, int v[], int start, int end)
{
    int middle, iMin=-1;
    if((start>end) && iMin==-1)
        return -1;
    middle=(start+end)/2;
    if(Elem==v[meio])
    {
        iMin=middle;
        return MinBinarySearch(Elem,v,start,middle-1);
    }
    if(Elem<v[middle])
        return MinBinarySearch(Elem,v, start, middle-1);
    else
        return MinBinarySearch(Elem,v,middle+1,end);
    return iMin;
}
plx is offline   Reply With Quote

Reply
      

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Forum Jump


All times are GMT -4. The time now is 04:13 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
Copyright © 2008-2017, Two Plus Two Interactive
 
 
Poker Players - Streaming Live Online