Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
This is a working program, however I do not know what "return answer' accomplished here, only that something needs to be returned. Isn't there a way to use "answer" in the next method so number1-4 do not have to be retyped.
public class AverageOfGivenNumbers {
// implement
public static int sum(int number1, int number2, int number3, int number4) {
int answer = number1 + number2 + number3 +number4;
return answer;
}
public static double average(int number1, int number2, int number3, int number4) {
double average = sum(number1,number2,number3,number4) / 4.0;
return average;
}
public static void main(String[] args) {
double result = average(4, 3, 6, 1);
System.out.println("Average: " + result);
}
}
This looks like homework, so it is likely just to demonstrate passing parameters.
As I said in my comments, you could shorten everything to this:
public class AverageOfGivenNumbers {
public static int sum(int n1,int n2,int n3,int n4) {
return n1+n2+n3+n4;
}
public static double average(int n1,int n2,int n3,int n4) {
return sum(n1,n2,n3,n4)/4.0;
}
public static void main(String[] args) {
System.out.println("Average: "+average(4,3,6,1));
}
}
When we use return, we are saying what a function will evaluate to when we execute it.
This is shown in the main method where we use average(4,3,6,1).
It becomes (or, "returns") 3.5 and that's what we see when we print it.
Average: 3.5
It doesn't show Average: average(4,3,6,1) because average(4,3,6,1) evaluates to 3.5 - the value we return.
Likewise, when we say return sum(4,3,6,1)/4.0 - that becomes return 14/4.0.
You could also use this in java to pass unlimited arguments:
public static int sum(int... args) {
int sum = 0;
for (int arg : args)
sum += arg;
return sum;
}
public static double average(int... args) {
return (sum(args)/(float)args.length);
}
public static void main(String []args){
System.out.println("Average: "+average(4,3,6,1));
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Basically I have to write a program that uses data that was inputted by the user and put it in a mathematical formula so it spits out an answer. The formula has to be created as a method. This is what I have so far. When I run the code it lets me input a number, but when I do that the program does not output anything and it just finishes running.
import java.util.Scanner;
public class Namek {
static int myMethod(int radius) {
return ((radius * radius) * (22/7));
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter Radius");
String radius = keyboard.nextLine();
}
private static void myMethod() {
}
You need to call the method passing the radius as parameter of the method. You can check https://www.tutorialspoint.com/java/java_methods.htm
,this link to see how to create and call methods with parameters.
I think what you wanted to do is the following:
import java.util.*;
public class Namek {
static int myMethod(int radius) {
return ((radius * radius) * (22/7));
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter Radius");
int radius = Integer.valueOf(keyboard.nextLine());
int area = myMethod(radius);
System.out.println(area);
}
}
Below is what you were trying to do I think
public class Formula {
static int myMethod(int radius) {
return ((radius * radius) * (22/7));
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter Radius");
int radius = Integer.valueOf(keyboard.nextLine());
float output = myMethod(radius);
System.out.println(output);
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
alright so i have already made a working compound interest rate program. but now for this program i have to re write the program using more methods. using
private static double FVCALC(..)
and
private static double validate(........)
i dont quite understand how i need to do this. the current code i have only lets me input the 3 values of interest rate and it stops. is it because of the private mehtods? im not sure what to do and i have searched for 3 days now.
bottom line is. my code is not working the way i want it to.
public class interest_rate
{
static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
double i;
double n;
double FVCalc;
double PV;
System.out.print("please enter value for n (years): ");
n = input.nextInt();
System.out.print("please enter interest rate: ");
i=input.nextDouble();
System.out.print("please enter Present Value: ");
PV = input.nextDouble();
}
private static double validate (double upLimit, double lowLimit, double PV)
{
upLimit=100000.00;
lowLimit=0.00;
while(PV>upLimit|| PV<lowLimit)
{
System.out.print("please enter value between "+upLimit+" and "+lowLimit);
System.out.print("please enter PV");
PV=input.nextDouble();
}
return PV;
}
private static double FVCalc(double PV, double i, double n, double FV)
{
FV = PV*Math.pow(1+(i/100), n);
return(FV);
}
}
First, you need to call the methods in main.
Secondly, you cant pass in PV, then reassign it and expect the value to update.
For example..
private static double validate (double upLimit, double lowLimit, double PV)
You need to call this in main like so
PV = 0.0; // some double value
double validated = validate(0,100000); // return the value, don't pass it in
And remove these lines in that method because overriding parameters is typically bad.
upLimit=100000.00;
lowLimit=0.00;
Next, add a field for your values that you want to use throughout the class.
public class InterestRate
{
static double pv , fvCalc;
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
}
And remove these lines in main and use those two class variables instead
double FVCalc;
double PV;
Additionally, I don't see a reason to store fvCalc as a variable. You can just calculate it
private static double fvCalc(double pV, double i, double n)
{
return pV*Math.pow(1+(i/100), n);
}
i believe i have figured it out. and i made it pretty. excuse the notes. i wanted to keep them there to see what i did wrong. thanks all.
public class interest_rate
{
static double pV , fvCalc, i, n;
static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
double validated;
double calculated;
double i;
double n;
//double fV;
//double pV1;
System.out.print("please enter value for n (years): ");
n = input.nextInt();
System.out.print("please enter interest rate: ");
i=input.nextDouble();
validated = validate(0,100000);
System.out.println("pV is validated and equal to: "+validated);
calculated= fvCalc(validated,i,n);
System.out.println("your calulation for interest is: "+calculated);
}
private static double validate(double upLimit, double lowLimit) {
double pV;
System.out.print("please enter pV");
pV=input.nextDouble();
while(pV<upLimit|| pV>lowLimit)
{
System.out.print("please enter value between "+upLimit+" and "+lowLimit);
System.out.println("please enter pV");
pV=input.nextDouble();
}
return pV;
}
private static double fvCalc(double pV, double i, double n)
{
double fV;
fV=pV*Math.pow(1+(i/100), n);
return fV;
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am still a bit new to Java and I could use some help with this code please, so far i wrote the methods and what each methods should do but I honestly have no idea how to do the overloading effect and make it work so I would appreciate a simple explanation.
import java.util.Scanner;
public class Assignment3 {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
myMethod();
}
public static void myMethod(){
System.out.println("Welcome to Java 1 ");
}
public static void myMethod(String msg, int counter){
System.out.println("Enter your custom messege please: ");
msg = input.nextLine();
System.out.println("Please enter how many times do you wish to print the messsege: ");
counter = input.nextInt();
for (int i = 0; i <= counter; i++){
System.out.println(msg);
}
}
public static void myMethod(int lowerLimit, int upperLimit){
System.out.println("Please enter a lowerlimit: ");
lowerLimit = input.nextInt();
System.out.println("Please enter an upperlimit: ");
upperLimit = input.nextInt();
System.out.println("Press 1 for ascending order: ");
System.out.println("Press 2 for descending order: ");
System.out.println("Make your selection");
int user1 = input.nextInt();
System.out.println("How frequent do you wish the messege to be printed");
int interval = input.nextInt();
switch(user1){
case 1:
for(int counter = lowerLimit; counter <= upperLimit; counter += interval){
System.out.println(counter);
}
break;
case 2:
for(int counter = upperLimit; counter <= lowerLimit; counter -= interval){
System.out.println(counter);
}
break;
default :
System.out.println("Something went wrong !!!");
}
}
public static void myMethod(double number1, double number2){
number1 = (Math.random() * 100);
number2 = (Math.random() * 100);
double product = (number1 * number2);
System.out.println("The product of " + number1 + " and " + number2 + " is " + product);
}
]
Your myMethod method is already overloaded. An overloaded method is just a method that can accept two or more different sets of parameters. (see https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html)
For example :
public void foo(int a) {
System.out.println("Printing out the int " + a);
}
public void foo(double a) {
System.out.println("Printing out the double " + a);
}
Foo has two possible parameter sets, one that accepts an int and one that accepts a double. Now, if you do this :
int a = 10;
double b = 10.5;
foo(a);
foo(b);
It'll return :
Printing out the int 10
Printing out the double 10.5
In response to your comment:
You just need to call the two other "myMethod" in your main, with their respective signatures:
public static void main(String[] args) {
// Call without argument
myMethod();
// Call with String and integer
myMethod("test", 42);
// Call with Integer and Integer
myMethod(42, 666);
}
The right ones will be called then. Does this answer your question ?
Above post has your answer, Your myMethod method is already overloaded but Method Overloading is a feature that allows a class to have two or more methods having same name, if their argument lists are different.
You have your method which accepts different parameters with different datatypes
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm writing a program to find out if a number is even or odd and I have all of the code working, but I don't know how to write in other examples. Here is the code I have so far:
import static java.lang.System.*;
public class numberverify
{
public static boolean isOdd( int num)
{
return ((num % 2) == 1);
}
public static boolean isEven( int num)
{
return ((num % 2) == 0);
}
}
and the runner:
import static java.lang.System.*;
import java.util.Scanner;
public class numberverifyrunner
{
public static void main ( String[] args )
{
Scanner keyboard = new Scanner(in);
System.out.print("Enter an Integer :: ");
int num = keyboard.nextInt();
System.out.println( num + " is odd :: " + numberverify.isOdd(num));
System.out.println( num + " is even :: " + numberverify.isEven(num) + "\n");
//add in more test cases
}
}
Do this:
while(keyboard.hasNextInt())
{
int num = keyboard.nextInt();
System.out.println( num + " is odd :: " + numberverify.isOdd(num));
System.out.println( num + " is even :: " + numberverify.isEven(num) + "\n");
}
The program should quit if you put anything other than an integer in.
You could simplify this code somewhat (and make it easier to maintain) by only having a single method that performs the check. The second method can simply call the first one, for example:
public static boolean isOdd(int num) {
return ((num % 2) == 1);
}
public static boolean isEven(int num) {
return !isOdd(num);
}
For that matter, using a bitwise AND operation here is more efficient:
public status boolean isEven(int num) {
return (num & 1) == 0;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Say I want to combine two programs into one so that when I run the combined program, both of the outputs from the individual programs are printed. How do I write the code to accomplish this?
Program 1:
public class Number1 {
public static void main (String[] args){
double s0=1.0;
double v0=2.0;
double a=9.8;
double t=3.0;
double s;
System.out.println(s0+v0*t+0.5*a*t*t);
}
}
Program 2:
public class Number2 {
public static void main (String[] args) {
for (int i=1; i<=10; i++){
System.out.print(i*i + " ");
}
System.out.println("");
}
}
A possible solution would be to call the static methods of both classes main
public class Number3 {
public static void main (String[] args) {
Number1.main(args);
Number2.main(args);
}
}
This assumes the Number1 and Number2 are within the classpath of Number3 of course...
public class Number1 {
public static void main (String[] args){
double s0=1.0;
double v0=2.0;
double a=9.8;
double t=3.0;
double s;
System.out.println(s0+v0*t+0.5*a*t*t);
Number2.main(args);
}
}
If both the classes are in the same package then just call one main method of one of the class inside another class.Here I called main method of second class in first class
Simply paste the code of your Number2 class main() method code in Number1's class main() method.
You can write the whole code like this:
public class Combine {
public void getFirstOne() {
double s0 = 1.0;
double v0 = 2.0;
double a = 9.8;
double t = 3.0;
double s;
System.out.println(s0 + v0 * t + 0.5 * a * t * t);
}
public void getSecondOne() {
for (int i = 1; i <= 10; i++) {
System.out.print(i * i + " ");
}
System.out.println("");
}
public static void main(String[] args) {
Combine combine = new Combine();
combine.getFirstOne();
combine.getSecondOne();
}
}