Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 601 602 [603] 604 605 ... 795

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 815521 times)

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9030 on: February 15, 2016, 08:52:33 pm »

This discussion of compiler optimization is something I've definitely had my opinions on challenged recently.  At this point I mostly just trust compilers to generate good machine code for what they believe is the best way to implement my high level code.  That is, I don't really expect them to generate optimal machine code for a problem, but I do expect that the machine code they generate is probably pretty good from an architectural standpoint.  Minimal stalls due to instruction ordering, good memory alignment and so on.

As I mentioned a long time back, I discovered not that long ago that g++ isn't able to determine that a vector's size is invariant over a fairly trivial for loop that doesn't manipulate the vector, or if it is able to determine that it doesn't bother to cache the value before the loop.  Changing a call from vector::size() in a for loop condition to checking a variable caching that value produced a measurable speedup (something like 15%) for one of my programs.  I should probably go and optimize the rest of my code to do the same, come to think of it.
Logged
Through pain, I find wisdom.

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9031 on: February 15, 2016, 10:09:40 pm »

I'm trying to grab a name by using a Scanner java and then sending that name for to a class to be later made into a object.

Something like this:

Code: [Select]
String nameOfAccount = namingAccount.nextLine();
An then to send whatever the input is (Lets say the name they put in was 'Bob') and send that to another class to be made into a object. Is there a way to do this?


Spoiler (click to show/hide)
[/code]
Logged

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9032 on: February 15, 2016, 10:49:28 pm »

Er, yeah, you just pass it the variable as per usual?  Like, if you had a String str="Bob"; you pass the constructor str either way.
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9033 on: February 15, 2016, 11:10:54 pm »

So like

BankAccount(str);

Would that be more or less the code?
Logged

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9034 on: February 15, 2016, 11:15:28 pm »

Yes, that is EXACTLY the code.

Well,
BankAccount varname = new BankAccount(str);
But yeah.
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9035 on: February 15, 2016, 11:57:34 pm »

Okay I'm asking the wrong question then..

What i'm trying to do is make an object in BankAccounts which I showed earlier. The idea is if I put my name 'Bob' or w.e then my class will take that name and then give me an ID as well as store that object in a arraystring that I have.

Problem is...i'm just lost tbh lol. I've tried to send the string to setname on that class and it didn't work (I later slapped myself for being silly). An now i'm trying to figure out if I should make the object first by calling (but then how would I set the name?) or if theirs just something blatantly obvious i'm missing.
Logged

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9036 on: February 16, 2016, 12:03:17 am »

I think you're missing the difference between an object and a class.

...I have to go do something right now, but that's a good start. I'll come back and edit with further explanation if nobody else does.

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #9037 on: February 16, 2016, 12:16:56 am »

I think you're missing the difference between an object and a class.
I use Python and what is this
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9038 on: February 16, 2016, 12:25:36 am »

I think you're missing the difference between an object and a class.
I use Python and what is this

Is this a trolling attempts or are you for real?
Logged

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #9039 on: February 16, 2016, 12:28:09 am »

It's a joke. In Python there's no distinction between classes, objects, or structs. They're just classes.
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9040 on: February 16, 2016, 12:30:01 am »

wait no i think i might be wrong

anyway, you would set the name probably in the constructor function for that particular instantiation of the class

It's a joke. In Python there's no distinction between classes, objects, or structs. They're just classes.

...no? not at all, python's where i learned the distinction

an object is an instantiation of a class

Code: [Select]
Class foo:
    def __init__(self):
        pass

bar=foo()

bar is an object, foo is a class

In fact, classes are explicitly a different type than objects in Python--see this description of classmethod
« Last Edit: February 16, 2016, 12:58:34 am by Putnam »
Logged

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9041 on: February 16, 2016, 12:39:11 am »

Well that's the thing..

I have two constructors one that has no arg that just gives an ID. An one that has an Arg but gives back the same ID. (Note: Their all randomish but yeah).

My proffesor has said that these args along with the rest of the class BankAccount should not be modified by any means. So my idea is to call the no arg constructor to make the object and then perhaps find some way to give it a name. But I'm still clueless on how to do that.

Out of curiosity what is it that you were thinking?
Logged

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9042 on: February 16, 2016, 01:07:33 am »

Just make a new object and use the method setName(name).

A new object within the class I have would be breaking the rules my prof. told us though (at least that's what the paper says).

The idea is that a customer can make one of three accounts.

[X] Regular
[X] Savings
[X] Current/checking

Account. I need to be able to make multiple or at least one of each and store them in a array. All of these accounts are being accessed by another named Bank (I know it gets a bit confusing but theirs a BankAccount and Bank class. Their different) so would you mean to make a new object within Bank or one of the others? If your saying to mod the constructors then no go.

I basically have only these two to work with:

Code: [Select]
public BankAccount() {
//NO ARGUMENT CONSTRUCTOR
accountId = (int) (Math.random() * 9000) + 1000;

}

public BankAccount(double accountBalance) { // ONE ARGUMENT CONSTRUCTOR

accountId = (int) (Math.random() * 9000) + 1000;

//setAccountBalance(accountBalance);
}

Then I need to be able to close them using the arraylist by finding the name (of person) and the account ID. *huff*

Wow that is hard to type. As of now still working on getting the object to appear with the name.



EDIT: Specifically this:

if (userChoice == 1){
            //Make a regular acct
            System.out.println ("Okay lets make a regular account. Please Give me your name.");
            String nameOfAccount = namingAccount.nextLine();
            //HOW TO NAME THE ACCOUNT AND SEND IT TO THE bankAccounts
            
            //BankAccount.setName(nameOfAccount);
            
            BankAccount  = new BankAccount();
            
         }

Do I make the account with a no arg constructor and then just name it? But then I run into the problem of 'what if' I need to make another of these types? Won't that confuse the array/make it harder to find when looking for it?
« Last Edit: February 16, 2016, 01:10:09 am by 3man75 »
Logged

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9043 on: February 16, 2016, 01:09:35 am »

You don't need to make a new object within the class you have to use setName on an existing object as long as the class already has setName.

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9044 on: February 16, 2016, 01:12:19 am »

You don't need to make a new object within the class you have to use setName on an existing object as long as the class already has setName.

So like BankAccount variableName = new BankAccount();

then

variableName.setname(string w.e);

This is a little pseudo but am I close?
Logged
Pages: 1 ... 601 602 [603] 604 605 ... 795