How can I get the result value in java overloading
I want to write a program such that if the input is true
, it adds a
and b
, and if the input is false
, it subtracts b
from a
. Also, when it is an ArrayList
, if the input is true
, it picks the maximum value, and if the input is false
, it picks the minimum value.
public class Source7_3 {
public static void main(String args) {
OverLoading mm = new OverLoading();
int a = new int[10];
for (int i = 0; i < a.length; i++)
a[i] = (int) (Math.random() * 100) + 1;
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + true + ") = ");
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + false + ") = ");
System.out.println("dist(arr, " + true + ") = ");
System.out.println("dist(arr, " + false + ") = ");
}
}
class OverLoading {
int a = (int) (Math.random() * 100) + 1;
int b = (int) (Math.random() * 100) + 1;
int dist(int a, int b, boolean d) {
return d == true ? a + b : a - b;
}
int dist(int a, boolean d) {
for (int j = 0; j < a.length; j++) {
int max, min;
max = min = a[0];
if (max < a[j])
max = a[j];
if (min > a[j])
min = a[j];
return true ? max : min;
}
}
}
But I can't get the result value..
How can I get it?
Thank you for your help!
java overloading
add a comment |
I want to write a program such that if the input is true
, it adds a
and b
, and if the input is false
, it subtracts b
from a
. Also, when it is an ArrayList
, if the input is true
, it picks the maximum value, and if the input is false
, it picks the minimum value.
public class Source7_3 {
public static void main(String args) {
OverLoading mm = new OverLoading();
int a = new int[10];
for (int i = 0; i < a.length; i++)
a[i] = (int) (Math.random() * 100) + 1;
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + true + ") = ");
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + false + ") = ");
System.out.println("dist(arr, " + true + ") = ");
System.out.println("dist(arr, " + false + ") = ");
}
}
class OverLoading {
int a = (int) (Math.random() * 100) + 1;
int b = (int) (Math.random() * 100) + 1;
int dist(int a, int b, boolean d) {
return d == true ? a + b : a - b;
}
int dist(int a, boolean d) {
for (int j = 0; j < a.length; j++) {
int max, min;
max = min = a[0];
if (max < a[j])
max = a[j];
if (min > a[j])
min = a[j];
return true ? max : min;
}
}
}
But I can't get the result value..
How can I get it?
Thank you for your help!
java overloading
You are not even calling thedist
function. Inside print statements you are printing a string "dist" instead of calling the method.
– Ranjan
Nov 27 '18 at 1:46
1
Also in second method maybe you are trying to doreturn b ? max : min
.
– Ranjan
Nov 27 '18 at 1:51
add a comment |
I want to write a program such that if the input is true
, it adds a
and b
, and if the input is false
, it subtracts b
from a
. Also, when it is an ArrayList
, if the input is true
, it picks the maximum value, and if the input is false
, it picks the minimum value.
public class Source7_3 {
public static void main(String args) {
OverLoading mm = new OverLoading();
int a = new int[10];
for (int i = 0; i < a.length; i++)
a[i] = (int) (Math.random() * 100) + 1;
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + true + ") = ");
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + false + ") = ");
System.out.println("dist(arr, " + true + ") = ");
System.out.println("dist(arr, " + false + ") = ");
}
}
class OverLoading {
int a = (int) (Math.random() * 100) + 1;
int b = (int) (Math.random() * 100) + 1;
int dist(int a, int b, boolean d) {
return d == true ? a + b : a - b;
}
int dist(int a, boolean d) {
for (int j = 0; j < a.length; j++) {
int max, min;
max = min = a[0];
if (max < a[j])
max = a[j];
if (min > a[j])
min = a[j];
return true ? max : min;
}
}
}
But I can't get the result value..
How can I get it?
Thank you for your help!
java overloading
I want to write a program such that if the input is true
, it adds a
and b
, and if the input is false
, it subtracts b
from a
. Also, when it is an ArrayList
, if the input is true
, it picks the maximum value, and if the input is false
, it picks the minimum value.
public class Source7_3 {
public static void main(String args) {
OverLoading mm = new OverLoading();
int a = new int[10];
for (int i = 0; i < a.length; i++)
a[i] = (int) (Math.random() * 100) + 1;
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + true + ") = ");
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + false + ") = ");
System.out.println("dist(arr, " + true + ") = ");
System.out.println("dist(arr, " + false + ") = ");
}
}
class OverLoading {
int a = (int) (Math.random() * 100) + 1;
int b = (int) (Math.random() * 100) + 1;
int dist(int a, int b, boolean d) {
return d == true ? a + b : a - b;
}
int dist(int a, boolean d) {
for (int j = 0; j < a.length; j++) {
int max, min;
max = min = a[0];
if (max < a[j])
max = a[j];
if (min > a[j])
min = a[j];
return true ? max : min;
}
}
}
But I can't get the result value..
How can I get it?
Thank you for your help!
java overloading
java overloading
edited Nov 27 '18 at 2:55
Janus Varmarken
1,69731233
1,69731233
asked Nov 27 '18 at 1:40
SIhyunSIhyun
32
32
You are not even calling thedist
function. Inside print statements you are printing a string "dist" instead of calling the method.
– Ranjan
Nov 27 '18 at 1:46
1
Also in second method maybe you are trying to doreturn b ? max : min
.
– Ranjan
Nov 27 '18 at 1:51
add a comment |
You are not even calling thedist
function. Inside print statements you are printing a string "dist" instead of calling the method.
– Ranjan
Nov 27 '18 at 1:46
1
Also in second method maybe you are trying to doreturn b ? max : min
.
– Ranjan
Nov 27 '18 at 1:51
You are not even calling the
dist
function. Inside print statements you are printing a string "dist" instead of calling the method.– Ranjan
Nov 27 '18 at 1:46
You are not even calling the
dist
function. Inside print statements you are printing a string "dist" instead of calling the method.– Ranjan
Nov 27 '18 at 1:46
1
1
Also in second method maybe you are trying to do
return b ? max : min
.– Ranjan
Nov 27 '18 at 1:51
Also in second method maybe you are trying to do
return b ? max : min
.– Ranjan
Nov 27 '18 at 1:51
add a comment |
2 Answers
2
active
oldest
votes
I think you are trying to call these methods but at the moment you are simply appending Strings
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + true + ") = ");
should maybe be
System.out.println(mm.dist(mm.a, mm.b, true);
and as the fields a
and b
are part of the class, it would not be necessary to pass them
Thank you! But how can I get the maximum and mimum value of arraylist?System.out.println(mm.dist(mm.a, true);
I wrote the same way as you did but in this case It seems wrong way.. haha
– SIhyun
Nov 27 '18 at 2:04
moveint max, min;
to be fields - after the method has finished you can get their values.
– Scary Wombat
Nov 27 '18 at 2:09
add a comment |
class OverLoading
{
int a = (int) (Math.random() * 100) + 1;
int b = (int) (Math.random() * 100) + 1;
int dist(int a, int b, boolean d) {
return d == true ? a + b : a - b;
}
int dist(int a, boolean d) {
int max, min;
max = min = a[0];
for (int j = 0; j < a.length; j++) {
if (max < a[j])
max = a[j];
if (min > a[j])
min = a[j];
}
return d == true ? max : min;
}
}
public class HelloWorld{
public static void main(String args) {
OverLoading mm = new OverLoading();
int a = new int[10];
System.out.println("n");
for (int i = 0; i < a.length; i++)
{
a[i] = (int) (Math.random() * 100) + 1;
System.out.println(a[i]);
}
System.out.println("n");
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + true + ") = "+mm.dist(mm.a,mm.b,true));
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + false + ") = "+mm.dist(mm.a,mm.b,false));
System.out.println("dist(arr, " + true + ") = "+mm.dist(a,true));
System.out.println("dist(arr, " + false + ") = "+mm.dist(a,false));
}
}
This is a working code..you can check only by copy pasting this in tutorialspoint.com/compile_java_online.php
– Dips
Nov 27 '18 at 3:06
No Problem..happy to help :-)
– Dips
Nov 27 '18 at 6:37
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%2f53491559%2fhow-can-i-get-the-result-value-in-java-overloading%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think you are trying to call these methods but at the moment you are simply appending Strings
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + true + ") = ");
should maybe be
System.out.println(mm.dist(mm.a, mm.b, true);
and as the fields a
and b
are part of the class, it would not be necessary to pass them
Thank you! But how can I get the maximum and mimum value of arraylist?System.out.println(mm.dist(mm.a, true);
I wrote the same way as you did but in this case It seems wrong way.. haha
– SIhyun
Nov 27 '18 at 2:04
moveint max, min;
to be fields - after the method has finished you can get their values.
– Scary Wombat
Nov 27 '18 at 2:09
add a comment |
I think you are trying to call these methods but at the moment you are simply appending Strings
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + true + ") = ");
should maybe be
System.out.println(mm.dist(mm.a, mm.b, true);
and as the fields a
and b
are part of the class, it would not be necessary to pass them
Thank you! But how can I get the maximum and mimum value of arraylist?System.out.println(mm.dist(mm.a, true);
I wrote the same way as you did but in this case It seems wrong way.. haha
– SIhyun
Nov 27 '18 at 2:04
moveint max, min;
to be fields - after the method has finished you can get their values.
– Scary Wombat
Nov 27 '18 at 2:09
add a comment |
I think you are trying to call these methods but at the moment you are simply appending Strings
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + true + ") = ");
should maybe be
System.out.println(mm.dist(mm.a, mm.b, true);
and as the fields a
and b
are part of the class, it would not be necessary to pass them
I think you are trying to call these methods but at the moment you are simply appending Strings
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + true + ") = ");
should maybe be
System.out.println(mm.dist(mm.a, mm.b, true);
and as the fields a
and b
are part of the class, it would not be necessary to pass them
answered Nov 27 '18 at 1:47
Scary WombatScary Wombat
35.4k32252
35.4k32252
Thank you! But how can I get the maximum and mimum value of arraylist?System.out.println(mm.dist(mm.a, true);
I wrote the same way as you did but in this case It seems wrong way.. haha
– SIhyun
Nov 27 '18 at 2:04
moveint max, min;
to be fields - after the method has finished you can get their values.
– Scary Wombat
Nov 27 '18 at 2:09
add a comment |
Thank you! But how can I get the maximum and mimum value of arraylist?System.out.println(mm.dist(mm.a, true);
I wrote the same way as you did but in this case It seems wrong way.. haha
– SIhyun
Nov 27 '18 at 2:04
moveint max, min;
to be fields - after the method has finished you can get their values.
– Scary Wombat
Nov 27 '18 at 2:09
Thank you! But how can I get the maximum and mimum value of arraylist?
System.out.println(mm.dist(mm.a, true);
I wrote the same way as you did but in this case It seems wrong way.. haha– SIhyun
Nov 27 '18 at 2:04
Thank you! But how can I get the maximum and mimum value of arraylist?
System.out.println(mm.dist(mm.a, true);
I wrote the same way as you did but in this case It seems wrong way.. haha– SIhyun
Nov 27 '18 at 2:04
move
int max, min;
to be fields - after the method has finished you can get their values.– Scary Wombat
Nov 27 '18 at 2:09
move
int max, min;
to be fields - after the method has finished you can get their values.– Scary Wombat
Nov 27 '18 at 2:09
add a comment |
class OverLoading
{
int a = (int) (Math.random() * 100) + 1;
int b = (int) (Math.random() * 100) + 1;
int dist(int a, int b, boolean d) {
return d == true ? a + b : a - b;
}
int dist(int a, boolean d) {
int max, min;
max = min = a[0];
for (int j = 0; j < a.length; j++) {
if (max < a[j])
max = a[j];
if (min > a[j])
min = a[j];
}
return d == true ? max : min;
}
}
public class HelloWorld{
public static void main(String args) {
OverLoading mm = new OverLoading();
int a = new int[10];
System.out.println("n");
for (int i = 0; i < a.length; i++)
{
a[i] = (int) (Math.random() * 100) + 1;
System.out.println(a[i]);
}
System.out.println("n");
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + true + ") = "+mm.dist(mm.a,mm.b,true));
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + false + ") = "+mm.dist(mm.a,mm.b,false));
System.out.println("dist(arr, " + true + ") = "+mm.dist(a,true));
System.out.println("dist(arr, " + false + ") = "+mm.dist(a,false));
}
}
This is a working code..you can check only by copy pasting this in tutorialspoint.com/compile_java_online.php
– Dips
Nov 27 '18 at 3:06
No Problem..happy to help :-)
– Dips
Nov 27 '18 at 6:37
add a comment |
class OverLoading
{
int a = (int) (Math.random() * 100) + 1;
int b = (int) (Math.random() * 100) + 1;
int dist(int a, int b, boolean d) {
return d == true ? a + b : a - b;
}
int dist(int a, boolean d) {
int max, min;
max = min = a[0];
for (int j = 0; j < a.length; j++) {
if (max < a[j])
max = a[j];
if (min > a[j])
min = a[j];
}
return d == true ? max : min;
}
}
public class HelloWorld{
public static void main(String args) {
OverLoading mm = new OverLoading();
int a = new int[10];
System.out.println("n");
for (int i = 0; i < a.length; i++)
{
a[i] = (int) (Math.random() * 100) + 1;
System.out.println(a[i]);
}
System.out.println("n");
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + true + ") = "+mm.dist(mm.a,mm.b,true));
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + false + ") = "+mm.dist(mm.a,mm.b,false));
System.out.println("dist(arr, " + true + ") = "+mm.dist(a,true));
System.out.println("dist(arr, " + false + ") = "+mm.dist(a,false));
}
}
This is a working code..you can check only by copy pasting this in tutorialspoint.com/compile_java_online.php
– Dips
Nov 27 '18 at 3:06
No Problem..happy to help :-)
– Dips
Nov 27 '18 at 6:37
add a comment |
class OverLoading
{
int a = (int) (Math.random() * 100) + 1;
int b = (int) (Math.random() * 100) + 1;
int dist(int a, int b, boolean d) {
return d == true ? a + b : a - b;
}
int dist(int a, boolean d) {
int max, min;
max = min = a[0];
for (int j = 0; j < a.length; j++) {
if (max < a[j])
max = a[j];
if (min > a[j])
min = a[j];
}
return d == true ? max : min;
}
}
public class HelloWorld{
public static void main(String args) {
OverLoading mm = new OverLoading();
int a = new int[10];
System.out.println("n");
for (int i = 0; i < a.length; i++)
{
a[i] = (int) (Math.random() * 100) + 1;
System.out.println(a[i]);
}
System.out.println("n");
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + true + ") = "+mm.dist(mm.a,mm.b,true));
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + false + ") = "+mm.dist(mm.a,mm.b,false));
System.out.println("dist(arr, " + true + ") = "+mm.dist(a,true));
System.out.println("dist(arr, " + false + ") = "+mm.dist(a,false));
}
}
class OverLoading
{
int a = (int) (Math.random() * 100) + 1;
int b = (int) (Math.random() * 100) + 1;
int dist(int a, int b, boolean d) {
return d == true ? a + b : a - b;
}
int dist(int a, boolean d) {
int max, min;
max = min = a[0];
for (int j = 0; j < a.length; j++) {
if (max < a[j])
max = a[j];
if (min > a[j])
min = a[j];
}
return d == true ? max : min;
}
}
public class HelloWorld{
public static void main(String args) {
OverLoading mm = new OverLoading();
int a = new int[10];
System.out.println("n");
for (int i = 0; i < a.length; i++)
{
a[i] = (int) (Math.random() * 100) + 1;
System.out.println(a[i]);
}
System.out.println("n");
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + true + ") = "+mm.dist(mm.a,mm.b,true));
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + false + ") = "+mm.dist(mm.a,mm.b,false));
System.out.println("dist(arr, " + true + ") = "+mm.dist(a,true));
System.out.println("dist(arr, " + false + ") = "+mm.dist(a,false));
}
}
answered Nov 27 '18 at 3:06
DipsDips
11
11
This is a working code..you can check only by copy pasting this in tutorialspoint.com/compile_java_online.php
– Dips
Nov 27 '18 at 3:06
No Problem..happy to help :-)
– Dips
Nov 27 '18 at 6:37
add a comment |
This is a working code..you can check only by copy pasting this in tutorialspoint.com/compile_java_online.php
– Dips
Nov 27 '18 at 3:06
No Problem..happy to help :-)
– Dips
Nov 27 '18 at 6:37
This is a working code..you can check only by copy pasting this in tutorialspoint.com/compile_java_online.php
– Dips
Nov 27 '18 at 3:06
This is a working code..you can check only by copy pasting this in tutorialspoint.com/compile_java_online.php
– Dips
Nov 27 '18 at 3:06
No Problem..happy to help :-)
– Dips
Nov 27 '18 at 6:37
No Problem..happy to help :-)
– Dips
Nov 27 '18 at 6:37
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%2f53491559%2fhow-can-i-get-the-result-value-in-java-overloading%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
You are not even calling the
dist
function. Inside print statements you are printing a string "dist" instead of calling the method.– Ranjan
Nov 27 '18 at 1:46
1
Also in second method maybe you are trying to do
return b ? max : min
.– Ranjan
Nov 27 '18 at 1:51