Software Development and Programming Careers (Official Discussion Thread)

KingBowser

Rookie
Joined
Jul 14, 2012
Messages
47
Reputation
10
Daps
152
Reppin
NULL
This is a python solution (first time using python), so adapted a template to fit the problem
Code:
# calculator program

# NO CODE IS REALLY RUN HERE, IT IS ONLY TELLING US WHAT WE WILL DO LATER
# Here we will define our functions
# this prints the main menu, and prompts for a choice
def menu():
    #print what options you have
    return input ("1. Addition\n2. Multiplication\n3. Division\n4. Subtraction\n5. Exit\n")

# this adds two numbers given
def add(a,b):
    print "The output is: ", a + b
 
# this subtracts two numbers given
def sub(a,b):
    print "The output is: ", b - a
 
# this multiplies two numbers given
def mul(a,b):
    print "The output is: ", a * b
 
# this divides two numbers given
def div(a,b):
    print "The output is: ", a / b
 
# NOW THE PROGRAM REALLY STARTS, AS CODE IS RUN
loop = 1
choice = 0
another = ""

while loop == 1:
    choice = menu()
    if choice == 1:
        print "You have chosen Addition"
        add(input("Enter number A: "),input("Enter number B: "))
    elif choice == 2:
        print "You have chosen Multiplication"
        mul(input("Enter number A: "),input("Enter number B: "))
    elif choice == 3:
        print "You have chosen Division"
        div(input("Enter number A: "),input("Enter number B: "))
    elif choice == 4:
        print "You have chosen Subtraction"
        sub(input("Enter number A: "),input("Enter number B: "))
    elif choice == 5:
        loop = 0
     
    if choice != 5:
        another = raw_input("Would you like to perform another operation?Y/N ")
        if another == "Y":
            loop = 1
        elif another == "N":
            loop = 0

# NOW THE PROGRAM REALLY FINISHES

A C++ solution
Code:
#include<iostream>
#include<string>

void calculate(const int &option);
void getValidInt(int &num);
void runAgain(int &option);

int main()
{
    int option = 0;

    while (option != 5)
    {
        std::cout << "Welcome to Calculator, which operation would you like to perform?" << std::endl;
        std::cout << "1. Addition\n2. Multiplication\n3. Division\n4. Subtraction\n5. Exit\n";

        std::cin >> option;
        if (std::cin.fail() || option < 1 || option > 5)
        {
            std::cin.clear();
            std::cin.ignore(INT_MAX, '\n');
            std::cout << "Please enter valid option (1 - 5).\n";
        }
        else
        {
            if (option != 5)
            {
                switch (option){
                case 1: std::cout << "You have chosen Addition.\n";
                    break;
                case 2: std::cout << "You have chosen Multiplication.\n";
                    break;
                case 3: std::cout << "You have chosen Division.\n";
                    break;
                case 4: std::cout << "You have chosen Subtraction.\n";
                    break;
                }
                calculate(option);
                runAgain(option);
            }
        }
     
    }
}

void runAgain(int &option)
{
    char runOption;
    bool valid = false;
    while (!valid)
    {
        std::cout << "Would you like to perform another operation? (Y for Yes, N for No) \n";
        std::cin >> runOption;

        if (runOption == 'Y' || runOption == 'y')
        {
            valid = true;
        }
        else if (runOption == 'N' || runOption == 'n')
        {
            option = 5;
            valid = true;
        }
        else
        {
            std::cout << "Invalid entry - Please enter valid option\n";
            valid = false;
        }
    }
}

void getValidInt(int &num)
{
    std::cin >> num;
    while (std::cin.fail())
    {
        std::cin.clear();
        std::cin.ignore(INT_MAX, '\n');
        std::cout << "Please enter a valid number:\n";

        std::cin >> num;
    }
}

void calculate(const int &option)
{
    int num1, num2;
    std::cout << "Enter number A:\n";
    getValidInt(num1);

    std::cout << "Enter number B:\n";
    getValidInt(num2);

    std::cout << "The output is:\n";
    switch (option){
    case 1:
        std::cout << std::to_string(num1 + num2);
        break;
    case 2:
        std::cout << std::to_string(num1 * num2);
        break;
    case 3:
        std::cout << std::to_string(num1 / num2);
        break;
    case 4:
        std::cout <<  std::to_string(num1 - num2);
        break;
    }
    std::cout << std::endl;
}
 

semtex

:)
Joined
May 1, 2012
Messages
20,310
Reputation
3,396
Daps
46,188
teaching myself some web API programming. :ahh: Need to brush up on my android dev too it's been a while.
 

Richard Wright

Living Legend
Joined
Jan 16, 2013
Messages
3,401
Reputation
690
Daps
6,376
How do I do spoilers on here? Im gonna be that guy

public static boolean isEven(int input){

int ret = input & 1;
return ret == 1 ? false : true;


}
 

kevm3

follower of Jesus
Supporter
Joined
May 2, 2012
Messages
16,300
Reputation
5,571
Daps
83,590
How do I do spoilers on here? Im gonna be that guy

public static boolean isEven(int input){

int ret = input & 1;
return ret == 1 ? false : true;


}

just put the brackets around the word spoiler and it should work, and to use code tags, put the brackets around the word code
 

kevm3

follower of Jesus
Supporter
Joined
May 2, 2012
Messages
16,300
Reputation
5,571
Daps
83,590
Here's something interesting in regards to why you may want to type it out explicitly instead of using a compound operator. Compound operators are operators such as *=, /=, +=, -=, etc.

Take this example:
Code:
#include "stdafx.h"
#include <stdio.h>

int main()
{
   int total1 = 5;
   int total2 = 5;

   total1 *=  2 + 5;
   total2 = total2 * 2 + 5;

   printf("Total 1: %d \n", total1);
   printf("Total 2: %d \n", total2);
}

You would think both would have the same answer and both statement would be evaluated as total1 = total1 * 2 + 5 and total2 = total2 * 2 + 5. In this C example, they both have different answers. In the one using the compound operator, the compound operator has a lower precedence than the others, so in reality it would be evaluated as total1 * (2+7). Total 1 would be 35 while total 2 would be 15. It may be different in other languages, but this is something to be very careful about, as it can lead to quite unexpected results if you're not careful.
 
Last edited:

kevm3

follower of Jesus
Supporter
Joined
May 2, 2012
Messages
16,300
Reputation
5,571
Daps
83,590
Something you will have to accept as a programmer? Uncertainty and knowing that you simply do not know A LOT. You also have to be willing to accept the feeling that comes with you being at the limit of your knowledge and having to deal with a completely new body of knowledge. You will feel a bit of discomfort initially, but after you've gone through the cycle enough, you'll realize that ignorance is a way of life and once you accept that, you won't allow fear to dissuade you from exploring a subject you have little current knowledge about because you've already dealt with numerous subjects that you were initially ignorant about and eventually learned them thoroughly.
 

kevm3

follower of Jesus
Supporter
Joined
May 2, 2012
Messages
16,300
Reputation
5,571
Daps
83,590
I've been jumping between learning languages, those languages being mostly C++ and Java, with a bit of C there. I'm getting fairly fluid at Java, but with C++, I still have to get more comfortable with pointers and it's take on OO. The initialization list thing for constructors is strange to me, but given enough time I'll get it down. I'll have to get back on my Javascript soon as well. I think after I get a hang of Java and C++, I'll take it easy on the new languages and really focus on the APIs and libraries to build things. Then, a year or two down the line I'll pick up something like Ruby, Scala or something of the sort.
 

semtex

:)
Joined
May 1, 2012
Messages
20,310
Reputation
3,396
Daps
46,188
I've been jumping between learning languages, those languages being mostly C++ and Java, with a bit of C there. I'm getting fairly fluid at Java, but with C++, I still have to get more comfortable with pointers and it's take on OO. The initialization list thing for constructors is strange to me, but given enough time I'll get it down. I'll have to get back on my Javascript soon as well. I think after I get a hang of Java and C++, I'll take it easy on the new languages and really focus on the APIs and libraries to build things. Then, a year or two down the line I'll pick up something like Ruby, Scala or something of the sort.
Constructors basically control how an instance of the class (aka an object) can be created. Usually you give the class's attributes properties inside the constructor for that class.

Public class Sandwich{

IList<Topping> toppings;

Public Sandwich(){
//parameterless constructor
//For creating a sandwich with no toppings
}
Public Sandwich(IList<Topping> t)
{
//for creating sandwich with toppings
this.toppings= t;
}



Sorry for the formatting I'm on my iPhone
 

kevm3

follower of Jesus
Supporter
Joined
May 2, 2012
Messages
16,300
Reputation
5,571
Daps
83,590
Constructors basically control how an instance of the class (aka an object) can be created. Usually you give the class's attributes properties inside the constructor for that class.

Public class Sandwich{

IList<Topping> toppings;

Public Sandwich(){
//parameterless constructor
//For creating a sandwich with no toppings
}
Public Sandwich(IList<Topping> t)
{
//for creating sandwich with toppings
this.toppings= t;
}



Sorry for the formatting I'm on my iPhone

Thanks for the reply. Yeah constructors are quite simple in Java and Javascript, but what I've looked at of them from C++, I still have some figuring out to do.


On another note, there's like 3 different ways to access members in C++ obj::method, obj->method, and the typical way in obj.method

I'm having a hard time figuring out which goes to which. I think obj-> is for pointers, obj::method is for accessing static members and obj.method is for accessing normal methods and properties of objects. I'll have to post the answer here once I figure it out as it'll probably be helpful to someone.

On a side note, how long did it take you to learn C# after learning Java? I heard they are quite similar, so I might take some time to delve off into that so I can mess around with Unity. Also, in the Java world, what do you think about JavaFX? How about Spring on the web side of things?
 

semtex

:)
Joined
May 1, 2012
Messages
20,310
Reputation
3,396
Daps
46,188
Thanks for the reply. Yeah constructors are quite simple in Java and Javascript, but what I've looked at of them from C++, I still have some figuring out to do.


On another note, there's like 3 different ways to access members in C++ obj::method, obj->method, and the typical way in obj.method

I'm having a hard time figuring out which goes to which. I think obj-> is for pointers, obj::method is for accessing static members and obj.method is for accessing normal methods and properties of objects. I'll have to post the answer here once I figure it out as it'll probably be helpful to someone.

On a side note, how long did it take you to learn C# after learning Java? I heard they are quite similar, so I might take some time to delve off into that so I can mess around with Unity. Also, in the Java world, what do you think about JavaFX? How about Spring on the web side of things?
not long, my job is c#. The little Java spring I've seen strongly reminds me of asp.net mvc which is a damn good thing
 

kevm3

follower of Jesus
Supporter
Joined
May 2, 2012
Messages
16,300
Reputation
5,571
Daps
83,590
not long, my job is c#. The little Java spring I've seen strongly reminds me of asp.net mvc which is a damn good thing

Yeah I remember you were initially heavy into Java but ended up getting a job with C#. You doing web programming with it?
 

semtex

:)
Joined
May 1, 2012
Messages
20,310
Reputation
3,396
Daps
46,188
Yeah I remember you were initially heavy into Java but ended up getting a job with C#. You doing web programming with it?
Yep I like it. Wasn't really into web stuff before but I'm very interested now which I think will benefit me. I'm learning to write web APIs now at home.
 

kevm3

follower of Jesus
Supporter
Joined
May 2, 2012
Messages
16,300
Reputation
5,571
Daps
83,590
Yep I like it. Wasn't really into web stuff before but I'm very interested now which I think will benefit me. I'm learning to write web APIs now at home.

I'm glad I'm not the only guy into web programming. Have you jumped into Javascript yet?

I had to take a detour from Javascript and learn Java because JS is missing things like access modifiers and it doesn't have a class system, so a lot of devs started emulating those features. It was hard knowing what they were doing until I studied a language with the features they were trying to emulate.
 

semtex

:)
Joined
May 1, 2012
Messages
20,310
Reputation
3,396
Daps
46,188
I'm glad I'm not the only guy into web programming. Have you jumped into Javascript yet?

I had to take a detour from Javascript and learn Java because JS is missing things like access modifiers and it doesn't have a class system, so a lot of devs started emulating those features. It was hard knowing what they were doing until I studied a language with the features they were trying to emulate.
Haven't done much with JavaScript. I'm not a fan of it but it's not going anywhere so might as well add it to the toolbox
 
Top