Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Class Temperature, not sure about constructor/methods

  1. #1
    Junior Member
    Join Date
    May 2020
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Class Temperature, not sure about constructor/methods

    For my class I need 4 constructors, one for every object variable (set value to 0 and unit to Celsius), one with 2 parameters for the 2 objectvariables and one without arguments, whereas 0 degreeC is initialized. I think I have the constructors right.

    But I am not sure if my methods are right. I need to return Temperature (C and F); set Value, Unit or both; and return the Temperature in String. I've tried this and I think I am right but not too sure, may someone check?

    [CODE = Java]
    // object variables
    	float Value;
    	char Unit;
     
    // constructors	
    	Temp(float Value){	
    		this.Value = Value;
    		Value = 0;
    	}
     
    	Temp(char Unit){
    		this.Unit = Unit;
    		Unit = 0;
    	}
     
    	Temp (float Value, char Unit) {
    		this.Value = Value; this.Unit = Unit;
    	}
     
    	Temp () {	Value = 0; Unit = 'C'; }
     
    //now the methods:
     
            float getCelsius (float Value) {
    		float Celsius;
    		Celsius = Value;
    		return Celsius; 
    	}
     
    	float getFahrenheit (float Value) {
    		float Pre;
    		float Fahrenheit;
     
    		Pre = (9*Value/5)+32;
    		Fahrenheit = Math.round(Pre*10f)/10f;
    		return Fahrenheit;
    	}
     
    	void setValue (float V) {
    		Value = V;
    	}
     
    	void setUnit (char U) {
    		Unit = U;
    	}
     
    	void setBoth (float V, char U) {
    		Value = V;
    		Unit = U;
    	}
     
    	String tempToString (float Celsius, float Fahrenheit) {
    		String c = Float.toString(Celsius);
    		String f = Float.toString(Fahrenheit);
     
    		return c + f;
    	}
    Last edited by hiro; May 23rd, 2020 at 06:50 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Class Temperature, not sure about constructor/methods

    I think I am right but not too sure
    What concerns do you have? Is the program's output OK?

    One problem I see in the code is the naming of variables. Variable names should start with lowercase letters.

    variables can be declared and given a value in one statement on a single line. No need to have two statements and lines:
    float cel = value * 13.3;

    What is in the variable named Value? The variable name should describe what the variable contains.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2020
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Class Temperature, not sure about constructor/methods

    For example, one part task is: three methods that set value, unit (C or F) or value and unit
    My code would be:

     
    void setValue (float V) {
    		Value = V;
    	}
     
    	void setUnit (char U) {
    		Unit = U;
    	}
     
    	void setBoth (float V, char U) {
    		Value = V;
    		Unit = U;
    	}

    and I am not sure what exactly is "set" and if this is the right way. First time I use methods/constructor

    --- Update ---

    Value would be Celsius. (C or F according to task)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Class Temperature, not sure about constructor/methods

    I am not sure what exactly is "set"
    That is very important to know before trying to write any code.

    What is in the variable: Value (it should be value)? Is it the temperature in degrees? Then a better name might be numberOfDegrees.
    setValue should be named setDegrees
    The contents of unit then says whether the degrees are Fahrenheit or Celsius
    The get methods would need to look at unit to know what value was in numberOfDegrees to know whether or not it needed to be converted.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Class Temperature, not sure about constructor/methods

    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Class Temperature, not sure about constructor/methods

    'get' methods don't usually take an argument unless it's necessary to calculate or select the value to return. They certainly shouldn't take an argument with the same name as a class member variable - that's just asking for confusion, and the results may not be what you want - for example, your 'getCelsius' method will just return the same value you pass to it because the argument name hides the class variable with the same name and gets used instead.

    You should work out how you want the class to work before writing any code. For example, if someone calls setValue(...) to put a temperature value into the object, they will probably expect to get that value back when they call getValue().

Similar Threads

  1. constructor and class
    By nepperso in forum Object Oriented Programming
    Replies: 5
    Last Post: November 25th, 2013, 09:54 PM
  2. How to avoid a constructor of class A be extended to Class B?
    By chaitra1987 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 28th, 2013, 11:46 PM
  3. Question about methods, temperature conversion program
    By aivory616 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 23rd, 2012, 01:27 PM
  4. which class has a default constructor? (Req. guidance on constructor)
    By DragBall in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2012, 04:42 PM
  5. Constructor/Accessors/methods problem!!
    By gpelefty90 in forum Object Oriented Programming
    Replies: 4
    Last Post: September 26th, 2011, 12:56 AM

Tags for this Thread