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

04-24-2015 , 08:44 PM
Quote:
Originally Posted by javahelp
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.
Programming homework and newbie help thread Quote
04-25-2015 , 03:49 AM
Quote:
Originally Posted by just_grindin
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.
Programming homework and newbie help thread Quote
04-26-2015 , 12:16 AM
Quote:
Originally Posted by daveT
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.
Programming homework and newbie help thread Quote
04-26-2015 , 07:00 PM
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.
Programming homework and newbie help thread Quote
04-28-2015 , 09:09 AM
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?
Programming homework and newbie help thread Quote
04-28-2015 , 08:10 PM
Quote:
Originally Posted by daveT
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.
Programming homework and newbie help thread Quote
05-01-2015 , 01:57 PM
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.
Programming homework and newbie help thread Quote
05-01-2015 , 03:50 PM
Quote:
Originally Posted by Anais
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.
Programming homework and newbie help thread Quote
05-01-2015 , 04:00 PM
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.
Programming homework and newbie help thread Quote
05-01-2015 , 04:03 PM
Quote:
Originally Posted by Anais
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?
Programming homework and newbie help thread Quote
05-01-2015 , 05:57 PM
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
Programming homework and newbie help thread Quote
05-01-2015 , 05:58 PM
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.
Programming homework and newbie help thread Quote
05-01-2015 , 09:14 PM
Quote:
Originally Posted by Anais
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.
Programming homework and newbie help thread Quote
05-02-2015 , 03:45 PM
What does it mean to create an ordered tree from a list (of unordered numbers)?
Programming homework and newbie help thread Quote
05-03-2015 , 08:48 PM
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.
Programming homework and newbie help thread Quote
05-04-2015 , 10:43 AM
split the input differently and all good. nm
Programming homework and newbie help thread Quote
05-04-2015 , 02:51 PM
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
Programming homework and newbie help thread Quote
05-04-2015 , 05:05 PM
Quote:
Originally Posted by NiSash1337
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
Programming homework and newbie help thread Quote
05-04-2015 , 05:21 PM
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.
Programming homework and newbie help thread Quote
05-05-2015 , 12:26 PM
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.
Programming homework and newbie help thread Quote
05-05-2015 , 06:11 PM
Your call in fopen puts the variable name in parentheses.
Check you execution folder, it will probably contain a file called AuxFileName
Programming homework and newbie help thread Quote
05-05-2015 , 06:26 PM
Quote:
Originally Posted by plx
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"
Programming homework and newbie help thread Quote
05-05-2015 , 06:33 PM
Ah yes, and that.
Programming homework and newbie help thread Quote
05-05-2015 , 06:46 PM
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.
Programming homework and newbie help thread Quote
05-05-2015 , 07:27 PM
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;
}
Programming homework and newbie help thread Quote

      
m