Can you help me find my error?
I'm trying to use these two methods here but my output is not working.
class Nine {
public static void Nine(String[] args) {
int x,y,z;
y = 3;
x = 7;
z = addEm(a, b);
System.out.println("answer= " +x);
}
public static addEm (double a, double b){
int c;
c = a+b;
}
}
Actually there are a lot of error in your code:
z=addEm(a, b);
here a and b are meaningless, you should use z=addEm(y,x); (if your intent is to sum three with seven)
System.out.println("answer= " +x);
I guess that you want to show the the results of the sum, therefore you should print z (and not x), so you should substitute with System.out.println("answer= " +z);
public static addEm (double a, double b) {
Here you missed the return type, and you need to consider also the type of parameters a and b. Since y,x and z are int, it is better if also a and b are int, and therefore specify also the return type as int:
public static int addEm (int a, int b) {
Or you can declare everything (y,x,z,a,b and return type) as a double: the important here is that they should be all of the same type. Moreover you miss also the return statement of the function addEm, that summarizing becomes:
public static int addEm (int a, int b)
{
int c;
c=a+b;
return c;
}
And finally also the function
public static void Nine(String[] args)
it is not right named for an entry point: its names should be main.
So in conclusion, if you apply all the fix (by modifying as less as possible your original code) a code that compile, run and works following some 'logic' is:
class Nine {
public static void main(String[] args) {
int x, y, z;
y = 3;
x = 7;
z = addEm(y, x);
System.out.println("answer= " + z);
}
public static int addEm(int a, int b) {
int c;
c = a + b;
return (c);
}
}
Man, this is a very basic java lesson:
every prog need an entry point, which is in java:
public static void main(String args[]){}
And then your code will execute.
You're passing arguments a and b to addEm, but those variables aren't initialized. I'm expecting you wanted to pass x and y instead.
class Nine
{
public static void Nine(String[] args)
{
int x,y,z;
y=3;
x=7;
z=addEm(x, y);
System.out.println("answer= " +x);
}
public static addEm (double a, double b)
{
int c;
c=a+b;
}
}
Your code will not work because your addEm method does not have any return type. In addition, the method you wrote takes Double params but while using you are trying to pass int to it. You also do not have any main method. I am assuming you misspelled or misunderstood the main method so below is the code which should work
class Nine
{
public static void Main(String[] args)
{
int x,y,z;
y=3;
x=7;
z=addEm(x, y);
System.out.println("answer= " + x);
}
public static int addEm (int a, int b)
{
int c;
c=a+b;
return c;
}
}
Related
I am writing code in Java which has multiple methods and these methods have multiple variables. I want the other methods to access the variables of another method using actual and formal parameters. How can I do it?
I am pasting an example of the problem I'm facing.
Error : variable is not defined.
Code
public class example {
public void addition() {
int a = 0;
int b = 10;
int c = a + b;
}
public void result() {
System.out.println("The result for the above addition is" + c);
}
}
IM GETTING AN ERROR SAYING VARIABLE IS NOT DEFINED
You should declare c as global variable
public class Example {
int c;
public void addition() {
int a = 0;
int b = 10;
c = a + b;
}
public void result() {
System.out.println("The result for the above addition is " + c);
}
public static void main(String[] args) {
Example e = new Example();
e.addition();
e.result();
}
}
well, your java syntax is quite wrong... if you need to do an addition, you can do as follows:
public class Addition {
public static int addition(int a, int b)
{
int c= a + b;
return c;
}
public static void main(String[] args) {
int a = 1;
int b = 10;
int c = addition(a,b);
System.out.println("The result for the above addition is " + c);
}
}
where addition function does add a + b and return the result to your main method.
This is one of the practice questions of a test:
Write a method which accepts two integer values as input parameters and returns the boolean result true if the sum of the inputs is greater than or equal to 10 (and falseotherwise)
My answer is below but I don't think it looks correct. Can anyone give me a pointer?
public class Bruh{
public static void main (String [] arg){
int a;
int b;
boolean sum = true;
if ( a+b > 10)
System.out.println ("yo");
else{
sum = false;
}
}
}
You only wrote some code in the main method but you did not create one.
In order to do that you need to actually create a method in your Bruh class like:
public static boolean isSumGreaterThan9(int a, int b){
return (a + b) > 9;
}
Than call it from the main method:
public static void main (String [] arg){
int a = 4; // or whatever
int b = 7; // or whatever
System.out.println(isSumGreaterThan9(a, b));
}
You need to put your logic into a method and change your comparison to >= as per the requirement:
public static boolean isSumGreaterThanOrEqualToTen(int a, int b) {
return (a + b) >= 10;
}
this is my first coding class ever and i'm very new to all this please. I'm trying to create a code that will accept two numbers and return the larger of the two numbers. For e.g if the function is given da integers 7 and 12 the function will return da integer 12.
This is the code i've written so far but it's far from being correct.
public class Return
{
public static void main(String[] args)
{
public static int max("int num1, int num2,");
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
}
Java do not have nested methods. You write method inside method. Move your method outside and there are some syntax errors
public class Return
{
public static void main(String[] args)
{
int result = max (3,4);
System.out.println(result);
}
public static int max(int num1, int num2){
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
}
I suggest you should read some basic concepts of programming and language you are using.
But let me try to help you. Your code should look something like:
public static int max(int num1, int num2) {
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
public static void main(String[] args) {
System.out.println(max(1, 2));
}
Mistakes in your code were:
max method declared inside main.
arguments are passed inside quotes "int num1, int num2" which is wrong.
No definition of max
not calling max from main
I hope it helped to understand issues with the code.
This short code will return a larger one from two integers.
public static int larger(int a, int b)
{
return a >= b ? a : b;
}
Copy paste this method to your desired class and call this method
larger(12, 7);
Given your class:
public class Return
{
public static int larger(int a, int b)
{
return a >= b ? a : b;
}
public static void main(String[] args)
{
int larger127 = larger(12,7);
System.out.println("The larger int from 12 and 7 is: " + larger127);
}
}
You cannot have a method inside another method. Do it like this:
public static void main(String[] args) {
int result = max (3,4);
System.out.println(result);
}
private static int max(int i, int j) {
return i > j ? i : j;
}
max uses ternary operator to find the largest of the two numbers.
I need one static method that simply adds two different numbers together and returns the result. However, it needs to be able to accept different types of numbers such as Integer and Doubles which is where I am getting stuck. Here is the main method that I have which, cannot be changed.
public static void main(String[] args)
{
Double answer1 = add(2, 7);
Number answer2 = add(new Integer(4), new Double(5.2));
double answer3 = add(8, 1.3);
System.out.println(answer1 + " " + answer2 + " " + answer3);
}
public static Double add(Double num1, Integer num2)
{
return num1 + num2;
}
The method above has the correct body I just don't know what should be where Double is after static. Is there some type that accommodates for both doubles and integers?
New situation:
public static double add(Number num1, Number num2)
{
return num1 + num2; //error is here
}
You can use the common baseclass Number.
public class Test {
public static void main(String[] args) {
int x = 5;
double y = 10;
System.out.println(getDoubleValue(x, y));
}
private static double getDoubleValue(Number x, Number y){
return x.doubleValue() + y.doubleValue();
}
}
Output:
15.0
public void draw(String s) {
...
}
public void draw(int i) {
...
}
public void draw(double f) {
...
}
public void draw(int i, double f) {
...
}
Look at the following link
http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
Means you have to use method overloading
You should use method overloading.
Double Foo() // Version with no arguments
{
}
Double Foo(int arg) // Version with a single int
{
}
Double add(Double num1, Integer num2) // Version with integer and double parameters
{
}
You could use method overloading for that. Creating an identical method with different datatype :)
I was wondering how I could call getJD() within getToD and keep the parameters intact,(or temporarily set the parameters as variables in main and call the variables to the method).
The parameters are going to be input using the scanner class later in the main method.
import static java.lang.Math.*;
public class jdMethods
{
public static double getJD(double y, double m, double d){
if (m<=2.0){
y--;
m += 12.0;
}
double a=floor(y/100.0);
return (365.25*(y+4716.0))+(30.6001*(m+1))+d+(2.0-a+floor(a/4.0))-1524.5;
}
public static double getToD(int h, int m, int s)
{
double a = getJD(a, a, a) + ((h-12)/24) + (m/1440) + (s/86400);
return a;
}
}
Edited for clarity.
It's not perfectly clear on what you are trying to do, but I assumed that you just want to save the result of your first getJD() and to use the result within your getToD(), so I made a private _jd and created a setter and getter for it.
import static java.lang.Math.*;
public class jdMethods
{
private double _jd;
public double getJD(){
return _jd;
}
public void setJD(double y, double m, double d){
if (m<=2.0){
y--;
m += 12.0;
}
double a=floor(y/100.0);
_jd = (365.25*(y+4716.0))+(30.6001*(m+1))+d+(2.0-a+floor(a/4.0))-1524.5;
}
public double getToD(int h, int m, int s)
{
double a = getJD() + ((h-12)/24) + (m/1440) + (s/86400);
return a;
}
}
So here is how you call it:
jdMethods testRun = new jdMethods();
testRun.setJD(1,2,3);
System.out.println(testRun.getToD(3, 2, 1));
All those parameters will be intact since you are using double and int, those are not Object s so it's value is copied when passed to a function, unlike Object s that a reference to it is passed to the function.
About your code, undefined variable a won't let it compile:
double a = getJD( a, a, a ) + ((h-12)/24) + (m/1440) + (s/86400);
I don't get what you are trying to do there, remember that a from getJD method is not the same a into getToD.