Thread: Can Someone Translate this text

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    12

    Can Someone Translate this text

    develop a program that outputs the user's weight on different planets. However, write the program using a class to represent the planet and its gravity. The class should include a constructor that allows a planet to be specified with a string, using any capitalization (if the string is not a planet name, then Earth should be assumed). The default construtor for the class will create an object representing Earth. The class has an observer operator that takes a weight on Earth as an argument and returns the weight on the planet. It should have a seond observer that returns the name of the planet as a string with proper capitalization.

    I believe I can do this program, if only I understood what it is asking me to do specifically. Thank you in advance to whoever contribute their time into reading and replying to this message.

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    91
    homework, i'll do it for a few quid..

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    plan it on paper then use your editor and compile it bit by bit. if you get any errors when u compile, rethink it on paper.

    I know what the answer is, but I know more C than C++ so the final program would do you no good lol!

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Which of these requirements don't you understand?

    develop a program that outputs the user's weight on different planets.

    However, write the program using a class to represent the planet and its gravity.

    The class should include a constructor that allows a planet to be specified with a string, using any capitalization (if the string is not a planet name, then Earth should be assumed).

    The default construtor for the class will create an object representing Earth.

    The class has an observer operator that takes a weight on Earth as an argument and returns the weight on the planet.

    It should have a seond observer that returns the name of the planet as a string with proper capitalization.
    Do you know how to make a class?

    Do you know how to make a constructor? Multiple (overloaded) constructors? A default constructor?

    Do you know how to test a string to see if it's a planet name? Do you know how to re-capitalize a string?

    Have you been given the relative gravational-pulls of each planet? (...Probably not too hard to find.)

    I don't know what "observer operator" means, but it sounds like a type of member function. Do you know how to write member functions?
    Last edited by DougDbug; 01-16-2006 at 04:16 PM.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I claim - and Google agrees with me - that there is no such thing as an observer operator in C++ programming.

    BTW, do you have to do it in C++? The introduction to typesafe enums in the Java5 documentation contains most of the code such a program would need in Java.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    12
    here is my header file. I believe I am missing something or doing something wrong here. no matter how I try to change it. I cannot compile it.
    Code:
    class planet
    {
    public:
    void getweight();
    void print() const;
    
    planet(string myplanet);
    planet();
    
    private:
    string plan;
    int weight;
    float newweight;
    };
    I just like to know why I cannot declare declare plan a string. Also, what is wrong with declaring the function name planet(string myplanet)?
    Also I would like to know what "Multiple (overloaded) constructors" are, how to use it, and why is it relavent to this program. I have never heard of this type of constructor. I do know the gravitational pull of each planet and I know how to output the user's weight on each planet given the planet name and the user's weight. But I do not know how to re-capitalize a string. Oh yeah, and I am doing this in C++
    Last edited by lanh215; 01-17-2006 at 02:43 AM.

  7. #7
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    I just like to know why I cannot declare declare plan a string.
    You did. Who told you it's wrong ? Post his comments.
    ( Or more likely, your compilers error message. )
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    12
    I do not understand why my compiler tells me that planet has not been declared when it has in my header file.
    Code:
    planet::planet()
    {
    weight = myweight;
    plan = myplanet;
    }

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    12
    Here's how I define my class, can someone point out what is wrong with it. Maybe my logic is messed up, or maybe something is wrong with the way I define my function. Well, here it is
    Code:
    #include "planet.h"
    #include <iostream>
    
    using namespace std;
    
    void planet::getweight()
    {
    if(plan == "Mercury") {
    newweight = weight * 0.4155;
    }
    if(plan == "Venus") {
    newweight == weight * 0.8975;
    }
    if(plan == "Earth") {
    newweight == weight * 1.0;
    }
    if(plan == "Moon") {
    newweight == weight * 0.166;
    }
    if(plan == "Mars") {
    newweight == weight * 0.3507;
    }
    if(plan == "Jupiter") {
    newweight == weight * 2.5374;
    }
    if(plan == "Saturn") {
    newweight == weight * 1.0677;
    }
    if(plan == "Uranus") {
    newweight == weight * 0.8997;
    }
    if(plan == "Neptune") {
    newweight == weight * 1.1794;
    }
    if(plan == "Pluto") {
    newweight == weight * 0.0899;
    }
    }
    void planet::Print() const
    {
    cout << newweight << endl;
    }
    planet::planet(string myplanet)
    {
    if(myplanet == "Mercury" || myplanet == "mercury") {
    return "Mercury";
    }
    if(myplanet == "Venus" || myplanet == "venus") {
    return "Venus";
    }
    if(myplanet == "Earth" || myplanet == "earth") {
    return "Earth";
    }
    if(myplanet == "Moon" || myplanet == "moon") {
    return "Moon";
    }
    if(myplanet == "Mars" || myplanet == "mars") {
    return "Mars";
    }
    if(myplanet == "Jupiter" || myplanet == "jupiter") {
    return "Jupiter";
    }
    if(myplanet == "Saturn" || myplanet == "saturn") {
    return "Saturn";
    }
    if(myplanet == "Uranus" || myplanet == "uranus") {
    return "Uranus";
    }
    if(myplanet == "Neptune" || myplanet == "neptune") {
    return "Neptune";
    }
    if(myplanet == "Pluto" || myplanet == "pluto") {
    return "Pluto";
    }
    if(myplanet == "")
    return "you need to type a planet name";
    else
    return "Earth";
    }
    planet::planet()
    {
    weight = myweight;
    plan = myplanet;
    }
    Hey all, I like to thank all of you for taking the time to reply to my thread. I really need the help, I wish I joined this community sooner. I been stuck on this program for a while now. I just do not understand what I am doing wrong. I created a class before call the time class. It worked out great, but that program only focused on intergers. I guess my biggest problem is dealing with strings and associating a string with a float number.

  10. #10
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    I just do not understand what I am doing wrong.
    But someone else does, or you would not be here

    Your compiler already parsed your files and found out what is wrong. I don't feel like doing work in my sparetime that a machine already did way faster and way more accurate than I ever could. So what did your compiler say ?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  11. #11
    Registered User
    Join Date
    Jan 2006
    Posts
    12
    it's saying all these, but...it doesn't make any sense.

    /Users/orbitgalaxy/Gravity/gravity.h:7: error: expected `)' before "myplanet"
    /Users/orbitgalaxy/Gravity/main.cpp:46: error: return type specification for constructor invalid
    /Users/orbitgalaxy/Gravity/main.cpp:46: error: prototype for `planet:lanet(std::string)' does not match any in class `planet'
    /Users/orbitgalaxy/Gravity/gravity.h:2: error: candidates are: planet:lanet(const planet&)
    /Users/orbitgalaxy/Gravity/gravity.h:8: error: planet:lanet()
    /Users/orbitgalaxy/Gravity/main.cpp:49: error: returning a value from a constructor
    /Users/orbitgalaxy/Gravity/main.cpp:52: error: returning a value from a constructor
    /Users/orbitgalaxy/Gravity/main.cpp:55: error: returning a value from a constructor
    /Users/orbitgalaxy/Gravity/main.cpp:58: error: returning a value from a constructor
    /Users/orbitgalaxy/Gravity/main.cpp:61: error: returning a value from a constructor
    /Users/orbitgalaxy/Gravity/main.cpp:64: error: returning a value from a constructor
    /Users/orbitgalaxy/Gravity/main.cpp:67: error: returning a value from a constructor
    /Users/orbitgalaxy/Gravity/main.cpp:70: error: returning a value from a constructor
    /Users/orbitgalaxy/Gravity/main.cpp:73: error: returning a value from a constructor
    /Users/orbitgalaxy/Gravity/main.cpp:76: error: returning a value from a constructor
    /Users/orbitgalaxy/Gravity/main.cpp:79: error: returning a value from a constructor
    /Users/orbitgalaxy/Gravity/main.cpp:81: error: returning a value from a constructor

  12. #12
    Registered User
    Join Date
    Jan 2006
    Posts
    12
    This assignment seem so simple. How is it that I am having a hard time with it.

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The first thing you probably need to do is #include <string> in planet.h. Declare the member plan in class planet to be of type std::string rather than string (I won't go into reasons now, but it's rarely a good idea to have "using namespace std;" in a header file).

    That won't fix all your problems, but will probably fix some of them.

  14. #14
    Registered User
    Join Date
    Sep 2005
    Posts
    10
    Here's how I define my class, can someone point out what is wrong with it.
    Blimey O'Reilly, where to start?

    Code:
    planet::planet()
    {
    weight = myweight;
    plan = myplanet;
    }
    This is a compile time error. I suspect you are attempting to assign default values to weight & plan (but strangely not to newweight?), but have gone somewhat astray. How can you expect the data members to be initialised correctly in this manner? You are trying to assign values to them using undeclared, uninitialised variables. Are you aware of what an initialiser list is?

    Code:
    planet::planet(string myplanet)
    {
    if(myplanet == "Mercury" || myplanet == "mercury") {
    return "Mercury";
    }
    Another compile time error. Constructors cannot return values in the same manner as functions (as your compiler points out).

    I could go on to detail other flaws (such as using the comparison operator instead of assigning to newweight in the getweight() function), but I can't be arsed to be honest. I think a better suggestion would be for you to go over the fundamentals of classes until you have a better understanding of what their purpose is and how to implement them. With a bit of perserverance, you'll look back on this effort and laugh / cringe. I know I did at my first efforts.

  15. #15
    Registered User
    Join Date
    Jan 2006
    Posts
    12
    I'm sorry, but what is an initialiser list?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble drawing text xlib.
    By Qui in forum Linux Programming
    Replies: 1
    Last Post: 05-10-2006, 12:07 PM
  2. Appending text to an edit control
    By dit6a9 in forum Windows Programming
    Replies: 3
    Last Post: 08-13-2004, 09:52 PM
  3. Text positioning and Text scrolling
    By RealityFusion in forum C++ Programming
    Replies: 3
    Last Post: 08-13-2004, 12:35 AM
  4. Scrolling The Text
    By GaPe in forum C Programming
    Replies: 3
    Last Post: 07-14-2002, 04:33 PM
  5. Replies: 1
    Last Post: 07-13-2002, 05:45 PM