Issue with getter and setter with one common variable
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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
add a comment |
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
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
add a comment |
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
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
java getter-setter
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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;
}
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
add a comment |
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;
}
}
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
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
}
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
add a comment |
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;
}
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
add a comment |
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;
}
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;
}
answered Nov 22 '18 at 12:31
EritreanEritrean
3,99611015
3,99611015
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
add a comment |
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
add a comment |
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;
}
}
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
add a comment |
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;
}
}
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
add a comment |
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;
}
}
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;
}
}
answered Nov 22 '18 at 12:36
sven.kwioteksven.kwiotek
1,1351018
1,1351018
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 22 '18 at 12:25
HadesZazifHadesZazif
494
494
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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