Issue with getter and setter with one common variable












1















I have a task with a temperature converter program. I have to use one veriable in a class what is used by two setter and two getter method.



This is the main class:



public class Main {

public static void main(String args) {
Homero ho = new Homero();
ho.setCelsius(0);
System.out.println(ho.getFahrenheit());
System.out.println(ho.getCelsius());
ho.setFahrenheit(212);
System.out.println(ho.getFahrenheit());
System.out.println(ho.getCelsius());
}
}


How can I say to the getters that when the main function set the first setter, the getter return with this... and in the second time return with that...



This is the Homero class:



public class Homero {
double celsius;

public void setCelsius(double celsius){
this.celsius = celsius;
}

public void setFahrenheit(double fahr){
this.celsius = fahr;
}

public double getFahrenheit(){
return (this.celsius*9)/5+32;
}


public double getCelsius(){
return (this.celsius-32)*5/9;
}
}


And this would be the proper output:




  • 32.0

  • 0.0

  • 212.0

  • 100.0


And the wrong output what I've got.




  • 32.0

  • -17.77777777777778

  • 413.6

  • 100.0










share|improve this question























  • of course this goes wrong. setCelsius(x) and if you then do a getCelcius(), you should get that x, but you calculate it into something else.

    – Stultuske
    Nov 22 '18 at 12:10






  • 2





    Choose the unit you store. Then convert in the setter and getter of the other unit.

    – ernest_k
    Nov 22 '18 at 12:11






  • 2





    this.celsius = fahr; does not look good. There needs to be a conversion here.

    – Thilo
    Nov 22 '18 at 12:12











  • Yes @Thilo you're right. I didn't understand properly the task. That was the solution.

    – Sirsemy
    Nov 22 '18 at 12:51
















1















I have a task with a temperature converter program. I have to use one veriable in a class what is used by two setter and two getter method.



This is the main class:



public class Main {

public static void main(String args) {
Homero ho = new Homero();
ho.setCelsius(0);
System.out.println(ho.getFahrenheit());
System.out.println(ho.getCelsius());
ho.setFahrenheit(212);
System.out.println(ho.getFahrenheit());
System.out.println(ho.getCelsius());
}
}


How can I say to the getters that when the main function set the first setter, the getter return with this... and in the second time return with that...



This is the Homero class:



public class Homero {
double celsius;

public void setCelsius(double celsius){
this.celsius = celsius;
}

public void setFahrenheit(double fahr){
this.celsius = fahr;
}

public double getFahrenheit(){
return (this.celsius*9)/5+32;
}


public double getCelsius(){
return (this.celsius-32)*5/9;
}
}


And this would be the proper output:




  • 32.0

  • 0.0

  • 212.0

  • 100.0


And the wrong output what I've got.




  • 32.0

  • -17.77777777777778

  • 413.6

  • 100.0










share|improve this question























  • of course this goes wrong. setCelsius(x) and if you then do a getCelcius(), you should get that x, but you calculate it into something else.

    – Stultuske
    Nov 22 '18 at 12:10






  • 2





    Choose the unit you store. Then convert in the setter and getter of the other unit.

    – ernest_k
    Nov 22 '18 at 12:11






  • 2





    this.celsius = fahr; does not look good. There needs to be a conversion here.

    – Thilo
    Nov 22 '18 at 12:12











  • Yes @Thilo you're right. I didn't understand properly the task. That was the solution.

    – Sirsemy
    Nov 22 '18 at 12:51














1












1








1








I have a task with a temperature converter program. I have to use one veriable in a class what is used by two setter and two getter method.



This is the main class:



public class Main {

public static void main(String args) {
Homero ho = new Homero();
ho.setCelsius(0);
System.out.println(ho.getFahrenheit());
System.out.println(ho.getCelsius());
ho.setFahrenheit(212);
System.out.println(ho.getFahrenheit());
System.out.println(ho.getCelsius());
}
}


How can I say to the getters that when the main function set the first setter, the getter return with this... and in the second time return with that...



This is the Homero class:



public class Homero {
double celsius;

public void setCelsius(double celsius){
this.celsius = celsius;
}

public void setFahrenheit(double fahr){
this.celsius = fahr;
}

public double getFahrenheit(){
return (this.celsius*9)/5+32;
}


public double getCelsius(){
return (this.celsius-32)*5/9;
}
}


And this would be the proper output:




  • 32.0

  • 0.0

  • 212.0

  • 100.0


And the wrong output what I've got.




  • 32.0

  • -17.77777777777778

  • 413.6

  • 100.0










share|improve this question














I have a task with a temperature converter program. I have to use one veriable in a class what is used by two setter and two getter method.



This is the main class:



public class Main {

public static void main(String args) {
Homero ho = new Homero();
ho.setCelsius(0);
System.out.println(ho.getFahrenheit());
System.out.println(ho.getCelsius());
ho.setFahrenheit(212);
System.out.println(ho.getFahrenheit());
System.out.println(ho.getCelsius());
}
}


How can I say to the getters that when the main function set the first setter, the getter return with this... and in the second time return with that...



This is the Homero class:



public class Homero {
double celsius;

public void setCelsius(double celsius){
this.celsius = celsius;
}

public void setFahrenheit(double fahr){
this.celsius = fahr;
}

public double getFahrenheit(){
return (this.celsius*9)/5+32;
}


public double getCelsius(){
return (this.celsius-32)*5/9;
}
}


And this would be the proper output:




  • 32.0

  • 0.0

  • 212.0

  • 100.0


And the wrong output what I've got.




  • 32.0

  • -17.77777777777778

  • 413.6

  • 100.0







java getter-setter






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 12:09









SirsemySirsemy

528




528













  • of course this goes wrong. setCelsius(x) and if you then do a getCelcius(), you should get that x, but you calculate it into something else.

    – Stultuske
    Nov 22 '18 at 12:10






  • 2





    Choose the unit you store. Then convert in the setter and getter of the other unit.

    – ernest_k
    Nov 22 '18 at 12:11






  • 2





    this.celsius = fahr; does not look good. There needs to be a conversion here.

    – Thilo
    Nov 22 '18 at 12:12











  • Yes @Thilo you're right. I didn't understand properly the task. That was the solution.

    – Sirsemy
    Nov 22 '18 at 12:51



















  • of course this goes wrong. setCelsius(x) and if you then do a getCelcius(), you should get that x, but you calculate it into something else.

    – Stultuske
    Nov 22 '18 at 12:10






  • 2





    Choose the unit you store. Then convert in the setter and getter of the other unit.

    – ernest_k
    Nov 22 '18 at 12:11






  • 2





    this.celsius = fahr; does not look good. There needs to be a conversion here.

    – Thilo
    Nov 22 '18 at 12:12











  • Yes @Thilo you're right. I didn't understand properly the task. That was the solution.

    – Sirsemy
    Nov 22 '18 at 12:51

















of course this goes wrong. setCelsius(x) and if you then do a getCelcius(), you should get that x, but you calculate it into something else.

– Stultuske
Nov 22 '18 at 12:10





of course this goes wrong. setCelsius(x) and if you then do a getCelcius(), you should get that x, but you calculate it into something else.

– Stultuske
Nov 22 '18 at 12:10




2




2





Choose the unit you store. Then convert in the setter and getter of the other unit.

– ernest_k
Nov 22 '18 at 12:11





Choose the unit you store. Then convert in the setter and getter of the other unit.

– ernest_k
Nov 22 '18 at 12:11




2




2





this.celsius = fahr; does not look good. There needs to be a conversion here.

– Thilo
Nov 22 '18 at 12:12





this.celsius = fahr; does not look good. There needs to be a conversion here.

– Thilo
Nov 22 '18 at 12:12













Yes @Thilo you're right. I didn't understand properly the task. That was the solution.

– Sirsemy
Nov 22 '18 at 12:51





Yes @Thilo you're right. I didn't understand properly the task. That was the solution.

– Sirsemy
Nov 22 '18 at 12:51












3 Answers
3






active

oldest

votes


















2














The getter and setter for celsius does not need a conversion. Do your calculations in the getter and setter for fahrenheit:



    public void setCelsius(double celsius){
this.celsius = celsius;
}

public void setFahrenheit(double fahr){
this.celsius = (fahr -32)*5/9;
}

public double getFahrenheit(){
return this.celsius*9/5+32;
}


public double getCelsius(){
return this.celsius;
}





share|improve this answer
























  • Thank you. It works. And with your code I finally understand the original task. That was the perfekt solution.

    – Sirsemy
    Nov 22 '18 at 12:44



















2














I would suggest to reset the other value by the setters, so every time you set a value the other is also new calculated:



public static class Homero {
double celsius;
double fahrenheit;
public void setCelsius(double celsius) {
this.celsius = celsius;
this.fahrenheit = this.celsius * 1.8 + 32;
}

public void setFahrenheit(double fahr) {
this.fahrenheit = fahr;
this.celsius = (this.fahrenheit - 32) * 5 / 9;
}

public double getFahrenheit() {
return this.fahrenheit;
}


public double getCelsius() {
return this.celsius;
}
}





share|improve this answer
























  • Thank you. I've tried to solve with an extra variable but the solution couldn't come to my mind. This is also good.

    – Sirsemy
    Nov 22 '18 at 12:48



















0














public void setFahrenheit(double fahr){
this.celsius = fahr;
}


this is worng you should convert fahr to cel before assignement.



here :



public double getCelsius(){
return (this.celsius-32)*5/9;
}


you should return this.celcius






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430702%2fissue-with-getter-and-setter-with-one-common-variable%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    The getter and setter for celsius does not need a conversion. Do your calculations in the getter and setter for fahrenheit:



        public void setCelsius(double celsius){
    this.celsius = celsius;
    }

    public void setFahrenheit(double fahr){
    this.celsius = (fahr -32)*5/9;
    }

    public double getFahrenheit(){
    return this.celsius*9/5+32;
    }


    public double getCelsius(){
    return this.celsius;
    }





    share|improve this answer
























    • Thank you. It works. And with your code I finally understand the original task. That was the perfekt solution.

      – Sirsemy
      Nov 22 '18 at 12:44
















    2














    The getter and setter for celsius does not need a conversion. Do your calculations in the getter and setter for fahrenheit:



        public void setCelsius(double celsius){
    this.celsius = celsius;
    }

    public void setFahrenheit(double fahr){
    this.celsius = (fahr -32)*5/9;
    }

    public double getFahrenheit(){
    return this.celsius*9/5+32;
    }


    public double getCelsius(){
    return this.celsius;
    }





    share|improve this answer
























    • Thank you. It works. And with your code I finally understand the original task. That was the perfekt solution.

      – Sirsemy
      Nov 22 '18 at 12:44














    2












    2








    2







    The getter and setter for celsius does not need a conversion. Do your calculations in the getter and setter for fahrenheit:



        public void setCelsius(double celsius){
    this.celsius = celsius;
    }

    public void setFahrenheit(double fahr){
    this.celsius = (fahr -32)*5/9;
    }

    public double getFahrenheit(){
    return this.celsius*9/5+32;
    }


    public double getCelsius(){
    return this.celsius;
    }





    share|improve this answer













    The getter and setter for celsius does not need a conversion. Do your calculations in the getter and setter for fahrenheit:



        public void setCelsius(double celsius){
    this.celsius = celsius;
    }

    public void setFahrenheit(double fahr){
    this.celsius = (fahr -32)*5/9;
    }

    public double getFahrenheit(){
    return this.celsius*9/5+32;
    }


    public double getCelsius(){
    return this.celsius;
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 22 '18 at 12:31









    EritreanEritrean

    3,5881914




    3,5881914













    • Thank you. It works. And with your code I finally understand the original task. That was the perfekt solution.

      – Sirsemy
      Nov 22 '18 at 12:44



















    • Thank you. It works. And with your code I finally understand the original task. That was the perfekt solution.

      – Sirsemy
      Nov 22 '18 at 12:44

















    Thank you. It works. And with your code I finally understand the original task. That was the perfekt solution.

    – Sirsemy
    Nov 22 '18 at 12:44





    Thank you. It works. And with your code I finally understand the original task. That was the perfekt solution.

    – Sirsemy
    Nov 22 '18 at 12:44













    2














    I would suggest to reset the other value by the setters, so every time you set a value the other is also new calculated:



    public static class Homero {
    double celsius;
    double fahrenheit;
    public void setCelsius(double celsius) {
    this.celsius = celsius;
    this.fahrenheit = this.celsius * 1.8 + 32;
    }

    public void setFahrenheit(double fahr) {
    this.fahrenheit = fahr;
    this.celsius = (this.fahrenheit - 32) * 5 / 9;
    }

    public double getFahrenheit() {
    return this.fahrenheit;
    }


    public double getCelsius() {
    return this.celsius;
    }
    }





    share|improve this answer
























    • Thank you. I've tried to solve with an extra variable but the solution couldn't come to my mind. This is also good.

      – Sirsemy
      Nov 22 '18 at 12:48
















    2














    I would suggest to reset the other value by the setters, so every time you set a value the other is also new calculated:



    public static class Homero {
    double celsius;
    double fahrenheit;
    public void setCelsius(double celsius) {
    this.celsius = celsius;
    this.fahrenheit = this.celsius * 1.8 + 32;
    }

    public void setFahrenheit(double fahr) {
    this.fahrenheit = fahr;
    this.celsius = (this.fahrenheit - 32) * 5 / 9;
    }

    public double getFahrenheit() {
    return this.fahrenheit;
    }


    public double getCelsius() {
    return this.celsius;
    }
    }





    share|improve this answer
























    • Thank you. I've tried to solve with an extra variable but the solution couldn't come to my mind. This is also good.

      – Sirsemy
      Nov 22 '18 at 12:48














    2












    2








    2







    I would suggest to reset the other value by the setters, so every time you set a value the other is also new calculated:



    public static class Homero {
    double celsius;
    double fahrenheit;
    public void setCelsius(double celsius) {
    this.celsius = celsius;
    this.fahrenheit = this.celsius * 1.8 + 32;
    }

    public void setFahrenheit(double fahr) {
    this.fahrenheit = fahr;
    this.celsius = (this.fahrenheit - 32) * 5 / 9;
    }

    public double getFahrenheit() {
    return this.fahrenheit;
    }


    public double getCelsius() {
    return this.celsius;
    }
    }





    share|improve this answer













    I would suggest to reset the other value by the setters, so every time you set a value the other is also new calculated:



    public static class Homero {
    double celsius;
    double fahrenheit;
    public void setCelsius(double celsius) {
    this.celsius = celsius;
    this.fahrenheit = this.celsius * 1.8 + 32;
    }

    public void setFahrenheit(double fahr) {
    this.fahrenheit = fahr;
    this.celsius = (this.fahrenheit - 32) * 5 / 9;
    }

    public double getFahrenheit() {
    return this.fahrenheit;
    }


    public double getCelsius() {
    return this.celsius;
    }
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 22 '18 at 12:36









    sven.kwioteksven.kwiotek

    1,125917




    1,125917













    • Thank you. I've tried to solve with an extra variable but the solution couldn't come to my mind. This is also good.

      – Sirsemy
      Nov 22 '18 at 12:48



















    • Thank you. I've tried to solve with an extra variable but the solution couldn't come to my mind. This is also good.

      – Sirsemy
      Nov 22 '18 at 12:48

















    Thank you. I've tried to solve with an extra variable but the solution couldn't come to my mind. This is also good.

    – Sirsemy
    Nov 22 '18 at 12:48





    Thank you. I've tried to solve with an extra variable but the solution couldn't come to my mind. This is also good.

    – Sirsemy
    Nov 22 '18 at 12:48











    0














    public void setFahrenheit(double fahr){
    this.celsius = fahr;
    }


    this is worng you should convert fahr to cel before assignement.



    here :



    public double getCelsius(){
    return (this.celsius-32)*5/9;
    }


    you should return this.celcius






    share|improve this answer




























      0














      public void setFahrenheit(double fahr){
      this.celsius = fahr;
      }


      this is worng you should convert fahr to cel before assignement.



      here :



      public double getCelsius(){
      return (this.celsius-32)*5/9;
      }


      you should return this.celcius






      share|improve this answer


























        0












        0








        0







        public void setFahrenheit(double fahr){
        this.celsius = fahr;
        }


        this is worng you should convert fahr to cel before assignement.



        here :



        public double getCelsius(){
        return (this.celsius-32)*5/9;
        }


        you should return this.celcius






        share|improve this answer













        public void setFahrenheit(double fahr){
        this.celsius = fahr;
        }


        this is worng you should convert fahr to cel before assignement.



        here :



        public double getCelsius(){
        return (this.celsius-32)*5/9;
        }


        you should return this.celcius







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 12:25









        HadesZazifHadesZazif

        414




        414






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430702%2fissue-with-getter-and-setter-with-one-common-variable%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            If I really need a card on my start hand, how many mulligans make sense? [duplicate]

            Alcedinidae

            Can an atomic nucleus contain both particles and antiparticles? [duplicate]